Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 compareId(Car otherCar) return

You can use String's compareTo to help with this comparison.

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

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

and 0 if the two cars have exactly the same identifier

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

Tester

Write a class with a main method to test Car. Do not put your main method in the Car class. Your tester should create at least two Car objects, set different values for the Identifier, Cost, Miles, etc. The tester should especially that the compare methods, getProfit and addMiles methods work as expected.

Grading Elements

class name, all constructors, all method names, all method signatures, visibility and return types are correct

constructors, getters and setters perform correct operation, return correct values

profit calculated correctly

accessors do not modify object state

addMiles performs correct operation

compareMPG, compareId, comparePrice, getProfit return proper values

separate tester class with main method tests at least two Cars, tests compare methods, addMiles, getProfit

Please follow all instructions.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions