Think of your favorite (generic) object in the whole wide world. You are going t
ID: 3886603 • 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 (10 point)
You must use at least 3 different data types for your fields (10 point)
3. Create getter (accessor) and setter (mutator) methods
Create a getter (accessor) method for each of your instance fields (10 point)
Create a setter (mutator) method for each of your instance fields (10 point)
4. Create a Method to display your data (10 point)
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 (10 point)
Create a parameterized constructor that takes all instance variables as parameters, and sets the instance variables to the values provided by the parameters (10 point)
6. Testing your program
Create a separate class called Demo.java. This class will contain your main method (5 point)
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 (5 point)
Call the objects display method, to print out it's values (5 point)
Create another instance of your class (another object) by using the parameterized constructor (10 point)
Call the objects display method, to print out it's values
7. Write Javadoc style comments for each of your methods (5 point)
Explanation / Answer
Shoes.java
public class Shoes {
// Declaring instance variables
private int size;
private String brand;
private double price;
// Zero argumented constructor
public Shoes() {
this.size = 7;
this.brand = "Red Tape";
this.price = 50;
}
// Parameterized constructor
public Shoes(int size, String brand, double price) {
super();
this.size = size;
this.brand = brand;
this.price = price;
}
// getters and setters
/*
* This method will get the price of instance variable
*
* @params void
*
* @return double
*/
public int getSize() {
return size;
}
/*
* This method will set the size of instance variable
*
* @params int
*
* @return void
*/
public void setSize(int size) {
this.size = size;
}
/*
* This method will get the brand name of instance variable
*
* @params void
*
* @return String
*/
public String getBrand() {
return brand;
}
/*
* This method will set the brand of instance variable
*
* @params String
*
* @return void
*/
public void setBrand(String brand) {
this.brand = brand;
}
/*
* This method will return the price of instance variable
*
* @params void
*
* @return double
*/
public double getPrice() {
return price;
}
/*
* This method will set the price of instance variable
*
* @params double
*
* @return void
*/
public void setPrice(double price) {
this.price = price;
}
/*
* This method will display the values of instance variables
*
* @params void
*
* @return void
*/
void display() {
System.out.println("Size :" + size);
System.out.println("Brand :" + brand);
System.out.println("Price :$" + price);
}
}
_______________________________________
Demo.java
public class Demo {
public static void main(String[] args) {
//Creating an instance of Shoes class object using Default constructor
Shoes s1 = new Shoes();
//Calling the setter methods by putting the values in the arguments
s1.setSize(9);
s1.setBrand("Reebok");
s1.setPrice(75);
//calling the display() method on the Shoes class
s1.display();
/* Creating an instance of Shoes class object using Parameterized
* constructor by putting the values as arguments
*/
Shoes s2 = new Shoes(6, "Puma", 60);
//calling the display() method on the Shoes class
s2.display();
}
}
____________________
Output:
Size :9
Brand :Reebok
Price :$75.0
Size :6
Brand :Puma
Price :$60.0
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.