Write the definition of a class that has the following properties: a. The name o
ID: 3767072 • Letter: W
Question
Write the definition of a class that has the following properties:
a. The name of the class is secretType.
b. The class secretType has four member variables: name of type string, age and weight of type int, and height of type double.
c. The class secretType has the following member functions. (Make each accessor function constant.)
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.
d. Write the definition of the member functions of the class secretType, as described in Part c.
will rate and provide feedback, please label answers seperately, thank you
Explanation / Answer
// All utility classes reside in this package
import java.util.*;
// Create class secretType
//Question A - The name of the class is secretType.
public class secretType
{
// data declarations, variables with their own data types
//Question B - The class secretType has four member variables: name of type string, age and weight of type int, and height of type double.
String name = "";
int age = 0;
weight = 0;
double height = 0.0;
// constructor method, the name is same as the class without any return type
public Secret(String coname, int coage, int coweight, double coheight)
{
// Print all the default values.
print();
System.out.println("Above are the Default values printed from Constructor");
//print an Emplty line
System.out.println();
System.out.println("Input the values to print them.");
//Call methos takeInput() to prompt for input values
takeInput(coname, coage, coweight, coheight);
}
//---------------------------------------------------------
// The following methods will be used when the instantiated
// object call them from the main()
public void takeInput(String ip_name, int ip_age, int ip_weight, double ip_height)
{
//Question C - The class secretType has the following member functions.
//using a pre-defined class, instantiating a new Scanner object
Scanner readdata = new Scanner(System.in);
// invoking the print() method of System.out object
System.out.print("Enter your name: ");
// invoking the nextLine() method of Scanner class (System.in object)
ip_name = readdata.nextLine();
setName(ip_name);
System.out.print("Enter your age: ");
ip_age = readdata.nextInt();
setAge(ip_age);
System.out.print("Enter your weight: ");
ip_weight = readdata.nextInt();
setWeight(ip_weight);
System.out.print("Enter your height: ");
ip_height = readdata.nextDouble();
setHeight(ip_height);
print();
}
// setName method() - set name to a new value
//Question D - Write the definition of the member functions of the class secretType
public void setName(String yourname)
{
name = yourname;
}
// setAge method() - set the age to a new value
public void setAge(int yourage)
{
age = yourage;
}
// setWeight method() - set the weight to a new value
public void setWeight(int yourweight)
{
weight = yourweight;
}
// setHeight method() - set the height to a new value
public void setHeight(double yourheight)
{
height = yourheight;
}
// getName method() - return the name
public String getName()
{
return name;
}
// getAge method() - return the age
public int getAge()
{
return age;
}
// getWeight method() - return the weight
public int getWeight()
{
return weight;
}
// getHeight method() - return the height
public double getHeight()
{
return height;
}
// print method() - output all data to standard output
public void print()
{
System.out.println();
System.out.println("Your name is: " + getName() + ".");
System.out.println("Your age is: " + getAge() + " years old.");
System.out.println("Your weight is: " + getWeight() + " kgs.");
System.out.println("Your height is: " + getHeight() + " cms.");
}
// main() method - the start of this program execution point
public static void main(String[] args)
{
// instantiate an object of type secretType class
Secret mysecret = new Secret(" ",0,0,0.0);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.