Question: Given main(), complete the Car class (in file Car.java) with constructors, accessors, and modifiers. The class has three attributes, Make (String), year (int) and price

Given main(), complete the Car class (in file Car.java) with constructors, accessors, and modifiers. The class has three attributes, Make (String), year (int) and price (int). Also complete a toString method to output the make, year, and price all on different lines as shown below. Ex: If the input is:

Toyota 2011 18000 

where "Toyota" is the make, 2011 is the car's year, and 18000 is the purchase price, the toString method will output all data on one line like this:

Make: Toyota, Model year: 2011, Purchase price: 18000

CarValue.java

import java.util.Scanner;

public class CarValue { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); Car myCar = new Car(); String make = scnr.next(); int userYear = scnr.nextInt(); int userPrice = scnr.nextInt(); myCar.setMake(make); myCar.setModelYear(userYear); myCar.setPurchasePrice(userPrice); System.out.print(myCar); Car myCar2 = new Car("Chevrolet", 2020, 45000); System.out.println(); System.out.println("My old car was a " + myCar.getMake() + ". My new car is a " + myCar2.getMake()); System.out.println("My old car was a " + myCar.getModelYear() + ". My new car is a " + myCar2.getModelYear()); System.out.println("My old car cost " + myCar.getPurchasePrice() + ". My new car cost " + myCar2.getPurchasePrice()); } }

Car.java

public class Car { private int modelYear; private int purchasePrice; private String make;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!