Question: Design and Implement a class called personType the following propertie
ID: 3677521 • Letter: Q
Question
Question:
Design and Implement a class called personType the following properties:
a. The class personType has four member variables: name (string), age (int), weight (double), height (double)
b. The class personType has the following member functions. (Make each accessor function constant. Can you figure out which functions areaccessor functions?)
print— outputs the data stored in the member variables with the appropriate titles
setName— function to set the name
setAge— function to set the age
setWeight— function to set the weight
setHeight— function to set the height
getName— value- returning function to return the name
getAge— value- returning function to return the age
getWeight— value- returning function to return the weight
getHeight— value- returning function to return the height
constructor— with default parameters: The default value of name is the empty string "", and the default values of age, weight, and height are 0.
Explanation / Answer
Setter Methods: Generally This methods are used only to store the data into the instance variables.Thats why these are also called as Mutator methods.
Getter Methods: Generally This methods are used only to access the data from the instance variables.Thats why these are also called as Accessor methods.
We can make a variable (or) a method (or) a class as constant by declaring it with a keyword final.
Then, final methods cannot be overridden in it subclasses.final classes cannot be extended .
PersonType.java
package org.students;
public class PersonType
{
private String name;
private int age;
private double weight;
private double height;
public PersonType()
{
name="";
age=0;
weight=0;
height=0;
}
//Accessor Method
//This Final methods cannot be Extended in its Sub-classes.
public final String getName() {
System.out.println(" This getName() accessor Method and it has been called ");
return name;
}
public void setName(String name) {
this.name = name;
}
//Accessor Method
//This Final methods cannot be Extended in its Sub-classes.
public final int getAge() {
System.out.println(" This getAge() accessor Method and it has been called");
return age;
}
public void setAge(int age) {
this.age = age;
}
//Accessor Method
//This Final methods cannot be Extended in its Sub-classes
public final double getWeight() {
System.out.println("This getWeight() accessor method and it has been called");
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
//Accessor method
//This Final methods cannot be Extended in its Sub-classes
public final double getHeight() {
System.out.println(" This getHeight() accessor method and it has been called ");
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------Test.java
package org.students;
public class Test {
public static void main(String[] args) {
PersonType pt = new PersonType();
pt.setAge(20);
pt.setHeight(6);
pt.setName("Williams");
pt.setWeight(65);
String name = pt.getName();
System.out.println("Name of the person :" + name);
System.out.println(" ");
int age = pt.getAge();
System.out.println("Age of the person :" + age);
System.out.println(" ");
double height = pt.getHeight();
System.out.println("Height of the person :" + height);
System.out.println(" ");
double weight = pt.getWeight();
System.out.println("Weight of the person :" + weight);
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.