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
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
// Shervin Shabanpour
public class Car {
private String brand;
private String modle;
private int numofdoors;
private int speed;
public Car()
{
}
public Car(String carBrand, String carModle, int doors)
{
brand = carBrand;
modle = carModle;
numofdoors = doors;
}
public int getSpeed()
{
return speed;
}
public void setSpeed(int setSpeed)
{
speed = setSpeed;
}
public void Accelerate()
{
speed += 5;
}
public String toString ()
{
return
"Brand; "+brand+" "+"Modle; "+modle+" "+"Doors; "+numofdoors+" "+"Speed; "+speed;
}
}
// Shervin Shabanpour
public class Demo {
public static void main(String[] args) {
Car myCar = new Car("Honda", "Accord", 2);
myCar.setSpeed(65);
myCar.Accelerate();
myCar.Accelerate();
System.out.println(myCar.toString());
}
}
I was able to write a program for it but I'm not sure if I got everything that the question wanted. Also can u help me with the this one using the program that I wrote above?
Create a sub class that inherits everything from your base class. (For example, if Car is the base class then SportsCar could be your sub class)
Provide at least one additional attribute to your subclass
Create gettter/setter methods for it.
Create a default constructor for the subclass, that uses super to call the base class default constructor.
It should set all attributes in the subclass as well as the super class to default values
Create a parameterized constructor for the subclass, that uses keyword super to pass the inherited parameters to the base class.
It should set all attributes in the subclass as well as the super class to the values that are passed in to the constructor.
Override the display() method to print out all the instance variable values from the base class, and also from the sub class.
In your main method, create 2 new object using your subclass, set the data, and call the display method. Create one object using the no-arg (default) constructor of your sub-class.
Call the set methods to set all attribute data associated to that object.
Call the display method for that object
Create one object using the parameterized constructor of your sub-class.
Call the display() method for this object too
Create an interface called Printable that has the following methods declared: display(), printToFile() and toString. Then make your subclass implement the interface. Also , create an array of objects from both your base class and subclass and put them in an array, and loop through it to call the display method. And finally override the toString and equals() method and demonstrate how to use them in the main method.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started