Question & Answer: model through a prompt. Also code a method called objectState that returns the formatted output for the fiel…..

Please help me answer these coding questions, I have 45 minutes to answer. Java format please.

31. Car is a class program. Car has a no-args/empty constructor and an overloaded constructor that accepts the year, make, and model and stores each in their respective fields (class-levell variables) of the same name. It has a carsBought method that returns the number purchased for that specific year, make, and model through a prompt. Also code a method called objectState that returns the formatted output for the fields according to the specs below. points) FORMATTED OUTPUT SPECS: Year, Make, & Model: 9999 Xxxxxxxxxx Xxxxxxxxxx
media%2F540%2F540567e5-ecf9-40e9-9147-86

31. Car is a class program. Car has a no-args/empty constructor and an overloaded constructor that accepts the year, make, and model and stores each in their respective fields (class-levell variables) of the same name. It has a carsBought method that returns the number purchased for that specific year, make, and model through a prompt. Also code a method called objectState that returns the formatted output for the fields according to the specs below. points) FORMATTED OUTPUT SPECS: Year, Make, & Model: 9999 Xxxxxxxxxx Xxxxxxxxxx

Expert Answer

 

Explanation::

  • Below both file code are present i.e Car.java and TestCar.java
  • Comments are prsent in codes for better understanding.

Code for Car.java ::

import java.util.*;

public class Car{

/* Following are class instance variables*/

int year;

String make,model;

Car(){

/*This is default constructor*/

}

Car(int newyear,String newMake,String newModel){

/* This is parameterized constructor also called as overloaded constructor*/

year=newyear;

make=newMake;

model=newModel;

}

int carsBought(){

Scanner sc=new Scanner(System.in);

System.out.print(“Enter cars bought for year “+year+” ::”);

return sc.nextInt();

}

String objectStat(){

String s=”Year, Make, & Model: “;

String p=Integer.toString(year);

return s+p+” “+make+” “+model;

}

}

Code for TestCar.java::

import java.util.*;

public class TestCar{

public static void main(String args[]){

/*

* Creating two objects of car named c1 and c2 with given arguments

*/

Car c1=new Car(1955,”Ford”,”Thunderbird”);

Car c2=new Car(1984,”Aston Martin Series 2″,”DB4″);

/*For both objects methods are called from class car.

* carsBought method prompts for user input.

*/

int carType1=c1.carsBought();

String s1=c1.objectStat();

System.out.println(s1);

System.out.println(“Number Purchased: “+carType1);

System.out.println();

System.out.println();

int carType2=c2.carsBought();

String s2=c2.objectStat();

System.out.println(s2);

System.out.println(“Number Purchased: “+carType2);

}

}

Output::

Question & Answer: model through a prompt. Also code a method called objectState that returns the formatted output for the fiel..... 1

Still stressed from student homework?
Get quality assistance from academic writers!