..il Sprint 6:49 PM Back Assignment 2 Detail Submission Grade Think of your favo
ID: 3593991 • Letter: #
Question
..il Sprint 6:49 PM Back Assignment 2 Detail Submission Grade Think of your favorite 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 1. Create a java class named after your object. (This class will not have a main method) 2. Create Instance fields (attributes) (1 point) . Create at least 2 private fields (attributes) for your class . You must use a different data type for each one 3. Create getter (accessor) and setter (mutator) methods (1 point) Previous Next Courses Calendar To Do Notifications MessagesExplanation / Answer
As I’m allowed to create my favorite Object, I have created a class called Car. It has 3 attributes :-
name: Name of the car ; of String type
top_speed: Top speed of the car; of float type
seats: The number of seats in the car ; of int type
Then I created getter and setter methods for these three attributes; along with a display() method to print out the details. A default constructor and a parameterized constructor are defined. I created another class called Demo.java which contains the main() function. I have created two objects and displayed their details on the screen.
//Car.java
public class Car {
private String name; /*Name of the car*/
private float top_speed; /*Top speed of the car*/
private int seats; /*Number of seats in the car*/
public String getName() { /*getter method to retrieve car name*/
return name;
}
public void setName(String name) { /*setter method to set the car name*/
this.name = name;
}
public float getTop_speed() { /*getter method to retrieve car top speed*/
return top_speed;
}
public void setTop_speed(float top_speed) { /*setter method to set the top speed*/
this.top_speed = top_speed;
}
public int getSeats() { /*getter method to retrieve number of seats*/
return seats;
}
public void setSeats(int seats) { /*setter method to set the number of seats*/
this.seats = seats;
}
public void display(){ /*a function to display all the info about a car*/
System.out.println("Car name: "+name);
System.out.println("Top speed: "+top_speed);
System.out.println("Number of seats: "+seats);
}
Car(){ /*default constructor*/
name=" ";
top_speed=0.0f;
seats=0;
}
Car(String name,float top_speed,int seats){ /*parameterized constructor*/
this.name=name;
this.top_speed=top_speed;
this.seats=seats;
}
}
//Demo.java
public class Demo {
public static void main(String[] args) { /*main method*/
Car ferrari=new Car(); /*creating a Car object; invoking default constructor*/
ferrari.setName("Ferrari"); /*setting the name using setter method setName()*/
ferrari.setSeats(4); /*setting the number of seats using setter method setSeats()*/
ferrari.setTop_speed(400); /*setting the top speed using setter method setTop_speed()*/
ferrari.display(); /* displaying the details of the car */
Car lambo=new Car("Gallardo",450,2); /*creating Car object and invoking parameterized constructor*/
lambo.display();/* displaying the details of the car */
}
}
//Output
Car name: Ferrari
Top speed: 400.0
Number of seats: 4
Car name: Gallardo
Top speed: 450.0
Number of seats: 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.