Open Image in new tab to show. Write a class called CarTest java using main meth
ID: 3836772 • Letter: O
Question
Open Image in new tab to show.
Write a class called CarTest java using main method Create two Car objects with the colors Red and White, respectively. Set features of the first car as follows by using set methods. Brand: Nissan Model: Juke Year: 2013 Kilometers (in thousands): 61.3 furl type (Gasoline/Diesel): Diesel Print information of the first car by using printInformation method. Calculate the sale price of the car by using priceOfCar method. Read information of the second car by using readInformation method Print information of the second car by using printInformation method. Calculate the rental price of the car by using priceOfCar method. Compare the two cars by using areCarsEqual method and print result as given In sample output. A sample start for Car.java and CarTestjava classes are provided below Car.java public class Car {//TODO: the methods and data about the Car class//will be declared in this file//variables public Strinp, color; private String brand;//constructor public Car(String c) { this.color = c; }//methods public String getBrand() { return this.brand; } } CarTest.java public class CarTest { public static void main(String[] args) {//TODO: you will write a main program//that uses and tests the Car class here Car cl = new Car("Red"); c1.SrtBrand ("Nissan"); } } You need to fill in these classes. Analyze sample outputs given below and write your codes accordingly. This red car is Nissan's "Juke". It has travelled 61300.0 KM since 2013 using diesel as fuel. Price for sale of this car is 105387.0 TL. Enter information of second car: Brand: Renault Yodel: Clio Year: 2001 Kilometers (in thousand): 180.5 Fuel Type (Gasoline/Diesel): Gasoline This white car is Renault's "Clio". It has travelled 180500.0 KM since 2001 using gasoline as fuel. Price for rent of this car is 67.75 TL. These cars do not have same features. This red car is Nissan's "Juke". It has travelled 61300.6 KM since 2013 using diesel as fuel. Price for sale of this car is 105387.0 TL. Enter information of second car: Brand: Nissan Model: Juke Year: 2013 Kilometers (in thousand): 61.3 Fuel Type (Gasoline/Diesel): Diesel This white car is Nissan's "Juke". It has travelled 61300.0 KM since 2013 using diesel as fuel. Price for rent of this car is 161.35 TL. These cars have same features.Explanation / Answer
import java.io.*;
import java.util.Scanner;
public class Car{ //Declaring the attributes of Car.
public String brand;
public String model;
public String year;
public String kilometer;
public String fuel;
public String color;
public String price;
Car(String c){ //Constructor with color specification.
this.color = c; //this pointer returns the currently invoking object.
}
//Setting the value of each attribute of Car.
public void setBrand(String b){
this.brand = b;
}
public void setModel(String m){
this.model = m;
}
public void setYear(String y){
this.year = y;
}
public void setPrice(String p){
this.price = p;
}
public void setKilometer(String k){
this.kilometer = k;
}
public void setFuel(String f){
this.fuel = f;
}
//Returns the specified attribute of currently invoking object.
public String getBrand(){
return this.brand;
}
public String getModel(){
return this.model;
}
public String getYear(){
return this.year;
}
public String getKilometer(){
return this.kilometer;
}
public String getFuel(){
return this.fuel;
}
public String getColor(){
return this.color;
}
public String getPrice(){
return this.price;
}
//Prints all the information of a car.
public void printInformation(){
System.out.println("This " + getColor() + "car is " + getBrand() + getModel() +"." + "It has travelled " + getKilometer() + "KM since " + getYear() + "using " + getFuel() + "as fuel.");
}
//To calculate and print proce of car.
public void priceOfCar(){
System.out.println(" Price for sale of this car is " + getPrice() + "TL");
}
//Reads all information entered by user and assigns to particular attribute.
public void readInformation(){
String br,fu,ye,ki,mo;
Scanner in = new Scanner(System.in);
System.out.println(" Enter information of second car:");
System.out.println(" Brand: ");
br = in.nextLine();
System.out.println(" Model:");
mo = in.nextLine();
System.out.println(" Year :");
ye = in.nextLine();
System.out.println(" Kilometer :");
ki = in.nextLine();
System.out.println(" Fuel Type :");
fu = in.nextLine();
setBrand(br);
setModel(mo);
setYear(ye);
setKilometer(ki);
setFuel(fu);
setPrice("67.75");
}
//To check whether two Cars are equal or not on the basis of the information provided.
public void areCarsEqual(Car x, Car y){
if (x.getBrand == y.getBrand)
{
if (x.getModel == y.getModel)
{
System.out.println(" The two cars have same model and same brand");
}
else{
System.out.println(" The two cars have same brand but the model is different");
}
}
else{
System.out.println(" The two cars have different model and brand");
}
}
}
public class CarTest{
public static void main(String args[]){
Car c1 = new Car("Red");//First car of color red.
Car c2 = new Car("white");//Second car of color White.
c1.setBrand("Nissan");
c1.setModel("Juke");
c1.setYear("2013");
c1.setFuel("diesel");
c1.setKilometer("61300.0");
c1.setPrice("105387.0");
c1.printInformation();
c1.priceOfCar();
c2.readInformation();
c2.setPrice("65.75");
c2.printInformation();
areCarsEqual(c1,c2);//To check whether c1 and c2 are equal or not.
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.