Answer:
Following are the code to this question:
public class Pig //Defining class Pig
{
private String name; //Defining string variable name
private int age; // Defining integer variable age
private double weight; // Defining double variable weight
Pig (String name, int age, double weight) //Defining parameterized constructor
{
super(); //using super key
this.name = name; //holding value in name variable
this.age = age; // holding value in age variable
this.weight = weight; // holding value in weight variable
}
String getName() //Defining method getName
{
return name; //return name value
}
void setName(String name) // Defining method setName
{
this.name = name; //hold name value
}
int getAge() // Defining method getAge
{
return age; //return value
}
void setAge(int age) // Defining method setAge
{
this.age = age; // hold age value
}
double getWeight() //Defining method getWeight
{
return weight; //return weight value
}
void setWeight(double weight) //Defining method setWeight
{
this.weight = weight; //hold weight value
}
void display() //Defining method display
{
System.out.println("Name:" + name + " Age:" + age + " Weight:" + weight); //print value
}
public static void main(String[] ar) //Defining main method
{
Pig onc = new Pig("Jig",5,14.5); //creating class object and called parameterized constructor
onc.display();//calling display method
}
}
Output:
please find the attachment.
Step-by-step explanation:
In the given java program, a class "Pig" is declared, in which three name, age, and weight is defined which differs in datatypes, in the next step, parameterized constructor, get and set method, and display method declared, which can be described as follows:
- In the parameterized constructor, uses super and this keyword to call and holds parameter value.
- In the get method three methods "getName, getAge, and getWeight" are defined, that return method values, and the set method "setName, setAge, and setWeight" uses this keyword to hold value in its variables.
- The display method is used to print all method store values by its variables name.
- Inside the main method, class object "onc" is created, which stores the value in it and calls the display method that print value with a message.