Question

In: Computer Science

Java Write a class called Car that contains instance data that represents the make, model, and...

Java

Write a class called Car that contains instance data that represents the make, model, and year of the car.

Define the Car constructor to initialize these values

Include getter and setter methods for all instance data and a toString method that returns a one-line description of the car.

Add a method called isAntique that returns a boolean indicating if the car is an antique (if it is more than 45 years old).

Create a driver class called CarTest, whose main method instantiates and updates several Car objects

Must have two constructors, one accepts nothing and another overloaded constructor that accepts parameters.

Must have accessor and mutator methods for each instance variable, and the toString method

Must test ALL (the two constructors, the accessors, and mutators for each variable and the other methods if there are any) methods explicitly in the driver class’s main method with at least two objects.

Addon:

Create an array of five objects of the class you have created. Use a for loop to display the five objects first. Then test all methods using the objects in the array and finally print out each object using a for loop again.

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________


// Car.java

import java.time.Year;

public class Car {
   //Declaring instance variables
   private String make;
   private String model;
   private int year;

   //Zero argumented constructor
   public Car() {
   }
   //Parameterized constructor
   public Car(String make, String model, int year) {
       this.make = make;
       this.model = model;
       this.year = year;
   }

   // getters and setters
   public String getMake() {
       return make;
   }

   public void setMake(String make) {
       this.make = make;
   }

   public String getModel() {
       return model;
   }

   public void setModel(String model) {
       this.model = model;
   }

   public int getYear() {
       return year;
   }

   public void setYear(int year) {
       this.year = year;
   }

   //toString method is used to display the contents of an object inside it
   @Override
   public String toString() {
       return "Car : Make=" + make + ", Model:" + model + ", Year:" + year;
   }
   public boolean isAntique()
   {
       if(Year.now().getValue()-year>45)
       {
           return true;
       }
       else
           return false;
   }

}
___________________

// CarTest.java

public class CarTest {

   public static void main(String[] args) {
       //Creating an instance of Car class
Car car1=new Car();
Car car2=new Car("Honda","Civic",2018);
car1.setMake("Ford");
car1.setModel("T-Model");
car1.setYear(1927);
//Displaying the Car class object Info
System.out.println(car1);
boolean b=car1.isAntique();
if(b)
{
   System.out.println(car1.getModel()+" is Antique.");
}
else
{
   System.out.println(car1.getModel()+" is not Antique.");
}
System.out.println(car2);
b=car2.isAntique();
if(b)
{
   System.out.println(car2.getModel()+" is Antique.");
}
else
{
   System.out.println(car2.getModel()+" is not Antique.");
}


Car cars[]={new Car("Volkswagen","Ameo",2019),new Car("Volkswagen","Polo",2016),new Car("BMW","Gran Coupe",2019),new Car("Hyundai","Grand i10",2019),new Car("Renault","Duster",2017)};

for(int i=0;i<cars.length;i++)
{
   System.out.println(cars[i]);
}
   }

}
__________________________

Output:

Car : Make=Ford, Model:T-Model, Year:1927
T-Model is Antique.
Car : Make=Honda, Model:Civic, Year:2018
Civic is not Antique.
Car : Make=Volkswagen, Model:Ameo, Year:2019
Car : Make=Volkswagen, Model:Polo, Year:2016
Car : Make=BMW, Model:Gran Coupe, Year:2019
Car : Make=Hyundai, Model:Grand i10, Year:2019
Car : Make=Renault, Model:Duster, Year:2017

_______________Could you plz rate me well.Thank You


Related Solutions

In java beginner coding language ,Write a class called Sphere that contains instance data that represents...
In java beginner coding language ,Write a class called Sphere that contains instance data that represents the sphere’s diameter. Define the Sphere constructor to accept and initialize the diameter, and include getter and setter methods for the diameter. Include methods that calculate and return the volume and surface area of the sphere. Include a toString method that returns a one-line description of the sphere. Create a driver class called MultiSphere, whose main method instantiates and updates several Sphere objects.
Write a class called Pen that contains the following information: Private instance variables for the price...
Write a class called Pen that contains the following information: Private instance variables for the price of the pen (float) and color of the pen (String). A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that stores the int value represented by this object. • A constructor that creates a MyInteger object for a default int value. What default value did you choose? What other values did you consider? Why did you make this choice? • A constructor that creates a MyInteger object for a specified int value. • A getter method, valueOf(), that returns value. • A setter method,...
Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
Write a java class called circle that represents a circle. It should have following three fields:...
Write a java class called circle that represents a circle. It should have following three fields: int x (x-coordinate), int y (y-coordinate), double radius (radius of the circle). Your circle object should have following methods: public int getRadius () public int getX () public int getY () public double area () public double perimeter() public String toString() write a client program called CircleClient that creates objects of the circle class called c1 and c2. Assign values to the fields when...
Write a class "car" with data fields "make" and "speed." The constructor should accept the "make"...
Write a class "car" with data fields "make" and "speed." The constructor should accept the "make" parameter. Be sure to use the "this" reference when setting the "make" parameter. The class should contain a static field for defaultSpeed set to 50. The class "car" should have a method "speed." The method should return the defaultSpeed. There should be an overloaded method for speed that takes a speed parameter. Finally, this class should take a getSpeed method that returns the speed....
Create a class called Sphere. The class will contain the following    Instance data double radius...
Create a class called Sphere. The class will contain the following    Instance data double radius Methods Constructor with one parameter which will be used to set the radius instance data Getter and Setter for radius             Area - calculate the area of the sphere (4 * PI * radius * radius)             Volume - calculate and return the volume of the sphere (4/3 * PIE * radius * radius * radius) toString - returns a string with the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT