Computer Science, asked by MrTSR, 2 months ago

J A V A Programing

1st Que:-
Write a Program in which create two packages P1 and P2. Define a class under P1 and then access it inside the package P2.

2nd Que:
Program to implement the above structure in inheritance and access the eat() method.
-- Structure has been attached above!​

Attachments:

Answers

Answered by Oreki
22

\textsf{\textbf{Question - 1}}

     \texttt{package P1;}\\\texttt{public class Number \{}\\\texttt{\hspace{1.5em} public static boolean isEvil(int number) \{}\\\texttt{\hspace{3em} return Integer.bitCount(number) \% 2 == 0;}\\\texttt{\hspace{1.5em} \}}\\\texttt{\}}}

     \texttt{package P2;}\\\\\texttt{import P1.Number;}\\\\\texttt{public class TestNumberClass \{}\\\texttt{\hspace{1.5em} public static void main(String[] args) \{}\\\texttt{\hspace{3em} boolean isEvil = Number.isEvil(9);}\\\texttt{\hspace{3em} System.out.println((isEvil ? "Is" : "Not")}\\\texttt{\hspace{9.5em} .concat(" Evil number."));}\\\texttt{\hspace{1.5em} \}}\\\texttt{\}}

     {\sf Outpu t : }\texttt{\: Is Evil number.}

\textsf{\textbf{Question - 2}}

     \texttt{class Animal \{}\\\texttt{\hspace{1.5em} void eat() \{}\\\texttt{\hspace{3em} System.out.println("*munching sounds*");}\\\texttt{\hspace{1.5em} \}}\\\texttt{\}}

     \texttt{public class SocialAnimal \{}\\\texttt{\hspace{1.5em}public static void main(String[] args) \{}\\\texttt{\hspace{3em} SocialAnimal socialAnimal = new SocialAnimal();}\\\texttt{\hspace{3em} socialAnimal.eat();}\\\texttt{\hspace{1.5em}\}}\\\texttt{\}}

     {\sf Outpu t : }\texttt{\: *munching sounds*}

Attachments:
Similar questions