Think of your favorite (generic) object in the whole wide world. You are going t
ID: 3886478 • Letter: T
Question
Think of your favorite (generic) object in the whole wide world. You are going to class a class in Java to create that object. For example, if your favorite thing is your teddy bear, your class can be TeddyBear. If you love a pair of shoes, then you can create a class called Shoes.java. Whatever you choose, just make sure it is a generic class name like Coffee, and not StarbucksCoffee. In a future assignment, when we learn about Inheritance, we will make a more specialized version of your class)
1. Create a java class named after your object. (This class will not have a main method)
2. Create Instance fields (attributes)
Create at least 3 private instance fields (attributes) for your class
You must use at least 3 different data types for your fields
3. Create getter (accessor) and setter (mutator) methods
Create a getter (accessor) method for each of your instance fields
Create a setter (mutator) method for each of your instance fields
4. Create a Method to display your data
Create a method called display, that simply prints out the values of all instance variables of your object
This method will have no parameters and not return a value
5. Create 2 Constructors
Create a default constructor (no parameters) that assigns all your instance variables to default values
Create a parameterized constructor that takes all instance variables as parameters, and sets the instance variables to the values provided by the parameters
6. Testing your program
Create a separate class called Demo.java. This class will contain your main method
Create an instance of your class (an object) by using the default constructor.
Call all your objects setter (mutator) methods to assign values to your object
Call the objects display method, to print out it's values
Create another instance of your class (another object) by using the parameterized constructor
Call the objects display method, to print out it's values
7. Write Javadoc style comments for each of your methods
Submission
Write your name at the top of both .java files as a block style comment
Follow all java standard conventions
Attach both .java files when submitting your assignment
Explanation / Answer
/**** Created a Generic Class Human ********/
/***
*
*
*
* @author Nitin
*
*/
class Human {
private String name;
private double weight;
private int salary;
private String sex;
public Human(){
}
public Human(String name, double weight, int salary, String sex){
this.name = name;
this.weight = weight;
this.salary = salary;
this.sex = sex;
}
/**
* This method is used to get Name of Human
* @param no param
* @return name
*/
public String getName() {
return name;
}
/**
* This method is used to set Name of Human
* @param name
* @return nothing
*/
public void setName(String name) {
this.name = name;
}
/**
* This method is used to get Weight of Human
* @param no param
* @return weight
*/
public double getWeight() {
return weight;
}
/**
* This method is used to set WEIGHT of Human
* @param weight
* @return nothing
*/
public void setWeight(double weight) {
this.weight = weight;
}
/**
* This method is used to get salary of Human
* @param no param
* @return salary
*/
public int getSalary() {
return salary;
}
/**
* This method is used to set salary of Human
* @param salary
* @return nothing
*/
public void setSalary(int salary) {
this.salary = salary;
}
/**
* This method is used to get Sex of Human
* @param no param
* @return Sex
*/
public String getSex() {
return sex;
}
/**
* This method is used to set Sex of Human
* @param Sex
* @return nothing
*/
public void setSex(String sex) {
this.sex = sex;
}
/**
* This method is used to display the human object
* @param no param
* @return doesn't return anything
*/
public void display(){
System.out.println(" name : " + this.name + " weight : " + this.weight + " salary: " + this.salary + " sex: " + this.sex) ;
}
}
public class Demo {
/**
* This is the main method which constructs human object and calls display method.
* @param args Unused.
* @return Nothing.
*/
public static void main(String args[]){
Human human1 = new Human();
human1.setName("Nitin");
human1.setWeight(72.20);
human1.setSalary(40000);
human1.setSex("Male");
human1.display();
Human human2 = new Human("Maryada", 63.00, 45000, "Female");
human2.display();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.