Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sophie, Sally and Jack have decided to open a used car lot. The first thing they

ID: 3722430 • Letter: S

Question

Sophie, Sally and Jack have decided to open a used car lot. The first thing they'll need is a Car class.

Write a class Car. The purpose of a Car object is to represent a single automobile in an inventory of a used car lot. The Car class has the following specification:

instance variable of type String for the car's identifier

instance variable of type int for the car's total miles

instance variable of type double for the car's miles per gallon (we'll consider only planet-killing fossil fuel consuming evil cars for this exercise)

instance variable of type int for the cost to Sophie, Sally and Jack to acquire the car

instance variable of type int for the price Sophie, Sally and Jack will sell the car

instance variable of type boolean indicating if the car has been sold or not

three constructors:

a no-argument constructor

a constructor that takes a single String parameter representing the car's identifier

a constructor that takes five parameters, one for the identifier, a second parameter for the car's initial mileage, and a third parameter for the car's Miles per Gallon, a fourth for the car's cost and the fifth for the car's price.

accessors

getters for all instance variables:

getCost

getIdentifier

getMilage

getMPG

getPrice

isSold  (note that for boolean values, we usually use boolean isSold() instead of boolean getSold())

int getProfit (this method returns the value price - cost if the car is sold, and 0 for unsold cars).

int compareMPG(Car otherCar) returns

a negative value (exact value doesn't matter--only that it be less than zero) if the current car gets fewer miles per gallon than otherCar

a positive number (exact value doesn't matter--only that it be greater than zero) if the current car gets more miles per gallon than otherCar

0 if the two cars have exactly the same MPG (given that MPG is a double number, this will probably occur relatively infrequently)

int compareMiles(Car otherCar) return

a negative value (exact value doesn't matter--only that it be less than zero) if the current car has fewer miles than otherCar

a positive number (exact value doesn't matter--only that it be greater than zero) if the current car has more miles than otherCar

and 0 if the two cars have exactly the same miles

int comparePrice(Car otherCar) returns

a negative value (exact value doesn't matter--only that it be less than zero) if the current car's price is less than the price of otherCar

a positive number (exact value doesn't matter--only that it be greater than zero) if the current car's price is more than the price of otherCar

0 if the two cars have exactly the same price

toString that returns something similar to "Car id=Ford1950, milage=63000, mpg=13.0, cost=60, price=210, is not sold" or "Car id=Ford1950, milage=63000, mpg=13.0, cost=60, price=210, is sold, profit=150"

mutators

setters for all instance variables

setCost

setIdentifier

setMiles

setMPG

setPrice

setSold

void addMiles(int milesToAdd) adds the given value to the car's miles value

Explanation / Answer

package hw1p2;

public class Car {

String identifier;

int millege;

double MPG;

int cost; //price for the car cost to sally, sphoie

int price; //price for which sophie. sally will sell the car

boolean isSold;

public Car(){

this.identifier = "";

this.millege = 0;

this.MPG = 0;

this.cost = 0;

this.price = 0;

}

public Car(String ident) {

this.identifier = ident;

this.millege = 0;

this.MPG = 0;

this.cost = 0;

this.price = 0;

}

public Car(String ident, int m, double mpg, int cost, int price) {

this.identifier = ident;

this.millege = m;

this.MPG = mpg;

this.cost = 0;

this.price = 0;

}

public String getIdentifier() {

return identifier;

}

public int getMillege() {

return millege;

}

public double getMPG() {

return MPG;

}

public int getCost() {

return cost;

}

public int getPrice() {

return price;

}

public boolean isSold() {

return isSold;

}

public int getProfit() {

if(!this.isSold())

return 0;

else

return this.getPrice()-this.getCost();

}

int compareMPG(Car otherCar) {

if(this.getMillege() < otherCar.getMillege())

return -1;

else if(this.getMillege() == otherCar.getMillege() )

return 0;

else

return 1;

}

int compareMiles(Car otherCar) {

if(this.getMillege() < otherCar.getMillege())

return -1;

else if(this.getMillege() == otherCar.getMillege())

return 0;

else

return 1;

}

int comparePrice(Car otherCar) {

if(this.getPrice() < otherCar.getPrice())

return -1;

else if(this.getPrice() == otherCar.getPrice())

return 0;

else

return 1;

}

public void setIdentifier(String identifier) {

this.identifier = identifier;

}

public void setMillege(int millege) {

this.millege = millege;

}

public void setMPG(double mPG) {

MPG = mPG;

}

public void setCost(int cost) {

this.cost = cost;

}

public void setPrice(int price) {

this.price = price;

}

public void setSold(boolean isSold) {

this.isSold = isSold;

}

void addMiles(int milesToAdd) {

this.setMillege( this.getMillege()+milesToAdd);

}

public String toString() {

String sold;

if(this.isSold())

sold = "is sold";

else

sold = "is not sold";

return "Car ID="+this.getIdentifier()+", milage= "+this.getMillege()+", mpg="+this.getMPG()+", Cost="+this.getCost()+", price="+this.getPrice()

+" "+sold;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote