Example questions(10 marks)• Explain the following concepts and illustratehow you can use them with examples.i. Encapsulationii. Information hidingiii. Private helperiv. Polymorphismv. Default constructorExample questions(15 marks)Suppose that a Mammal class written in Java is already available which youcan use (i.e., you are not asked to write this class). This class has instancevariables, constructors and methods as described below:1. instance variables that represent a mammal’s name as a String and themammal’s age as an integer2. a default constructor3. a constructor which is given a mammal’s name as a String and themammal’s age as an integer4. a method set (…) for setting the name and age of a mammal5. a method named printDetails() that prints out the name and the age ofthe mammal6. an equals(…) method which compares the parameter of mammal class tothe current object and returns true if two mammals have the same nameand same ageExample questions• Give Java code for a client program which uses the Mammalclass to do the following. It will create a Mammal object usingthe default constructor. It will then get the name and age of amammal from the user and set the Mammal object to theinput values. The program will then get the name and age of asecond mammal from the user and create the secondmammal object using the non-default constructor. It will thendetermine whether or not the two mammals are the sameand display this information. Finally, the program will print outthe details of the two mammals.
Question
Example questions(10 marks)• Explain the following concepts and illustratehow you can use them with examples.i. Encapsulationii. Information hidingiii. Private helperiv. Polymorphismv. Default constructorExample questions(15 marks)Suppose that a Mammal class written in Java is already available which youcan use (i.e., you are not asked to write this class). This class has instancevariables, constructors and methods as described below:1. instance variables that represent a mammal’s name as a String and themammal’s age as an integer2. a default constructor3. a constructor which is given a mammal’s name as a String and themammal’s age as an integer4. a method set (…) for setting the name and age of a mammal5. a method named printDetails() that prints out the name and the age ofthe mammal6. an equals(…) method which compares the parameter of mammal class tothe current object and returns true if two mammals have the same nameand same ageExample questions• Give Java code for a client program which uses the Mammalclass to do the following. It will create a Mammal object usingthe default constructor. It will then get the name and age of amammal from the user and set the Mammal object to theinput values. The program will then get the name and age of asecond mammal from the user and create the secondmammal object using the non-default constructor. It will thendetermine whether or not the two mammals are the sameand display this information. Finally, the program will print outthe details of the two mammals.
Solution
Sure, let's break down the question and answer it step by step.
First, let's explain the concepts:
i. Encapsulation: Encapsulation is one of the four fundamental OOP concepts. It is the technique of making the fields in a class private and providing access to the fields via public methods. For example, in a class Employee, we can make the fields like name, age, and salary as private and provide public methods getName(), getAge(), getSalary() to access these fields.
ii. Information hiding: Information hiding is a principle that reduces the visibility of the software components (like classes, methods etc) to the minimum. It's a way to reduce complexity. For example, when you use a class (like Scanner or System in Java), you don't need to know how these classes are implemented. You just need to know how to use them.
iii. Private helper: A private helper method is a private method that performs a common task for several public methods in the same class. This method cannot be accessed outside of the class. For example, in a class that performs mathematical operations, you might have a private helper method to check if a number is zero.
iv. Polymorphism: Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. For example, if a parent class named Animal has a method sound(), the subclasses like Dog, Cat can override this method to provide their own implementation.
v. Default constructor: A default constructor is a constructor that doesn't have any parameters. When no constructors are defined in a class, the Java compiler automatically creates a default constructor. For example, in a class Employee, if no constructors are defined, the compiler will create a default constructor like Employee().
Now, let's write the Java code for the client program:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Create a Mammal object using the default constructor
Mammal mammal1 = new Mammal();
// Get the name and age of a mammal from the user and set the Mammal object to the input values
System.out.println("Enter the name and age of the first mammal:");
mammal1.set(scanner.nextLine(), scanner.nextInt());
// Get the name and age of a second mammal from the user
System.out.println("Enter the name and age of the second mammal:");
scanner.nextLine(); // Consume newline left-over
String name = scanner.nextLine();
int age = scanner.nextInt();
// Create the second mammal object using the non-default constructor
Mammal mammal2 = new Mammal(name, age);
// Determine whether or not the two mammals are the same and display this information
if (mammal1.equals(mammal2)) {
System.out.println("The two mammals are the same.");
} else {
System.out.println("The two mammals are not the same.");
}
// Print out the details of the two mammals
mammal1.printDetails();
mammal2.printDetails();
}
}
Please note that this code assumes that the Mammal class and its methods are correctly implemented.
Similar Questions
What is the output of the following code? 1: class Mammal { 2: public Mammal(int age) { 3: System.out.print("Mammal"); 4: } 5: } 6: public class Platypus extends Mammal { 7: public Platypus() {8: System.out.print("Platypus"); 9: } 10: public static void main(String[] args) { 11: new Mammal(5); 12: } 13: } *1 pointA. PlatypusB. MammalC. PlatypusMammalD. MammalPlatypusE. The code will not compile because of line 8.F. The code will not compile because of line 11
e correct answerUsing which of the following, multiple inheritance in Java can be implemented?
he correct answerEncapsulation helps in writing ___________ classes in java
What is the output of the following code? 1: abstract class Reptile { 2: public final void layEggs() { System.out.println("Reptile laying eggs"); } 3: public static void main(String[] args) { 4: Reptile reptile = new Lizard(); 5: reptile.layEggs(); 6: } 7: } 8: public class Lizard extends Reptile { 9: public void layEggs() { System.out.println("Lizard laying eggs"); } 10: } *1 pointA. Reptile laying eggsB. Lizard laying eggsC. The code will not compile because of line 4.D. The code will not compile because of line 5.E. The code will not compile because of line 9.
Illustrate the concept of constructor overloading through a java program which when given an input 1 will output ‘This is the parrot’ and when given an input 2 will output ‘this is the main’ and when given an input 3 will output ‘This is the 20 years sparrow’.Input:Input 1Output :This is the parrotInput:Input 2Output :this is the mainInput:Input 3Output :This is the 20 years sparrow
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.