Project: CarProject problem
Hello, I seem to have a problem here:
Here is a code template I copied for my assignment:
Class Car:
Code:
/**
* This class illustrates some more data types
*
* @author Richard
* @version 20090608
*/
public class Car
{
// int - a whole number with no fractional parts
// double - a real number with decimals
// boolean - a logical type (true or false)
// instance variables
private String model; // a String type (any text)
private int mileage; // distance travelled (ml/km as you wish)
private double mpg; // miles per gallon or litres per 100 km
private boolean sold; // has the car been sold yet?
/**
* Constructor for objects of class Car
*/
public Car()
{
// initialise instance variables
model = "";
mileage = 0;
mpg = 0.0;
sold = false;
}
/**
* Alternate constructor for objects of class Car
*/
public Car(String md, int ml, double mp, boolean sd)
{
// call set methods
setModel(md);
setMileage(ml);
setMpg(mp);
setSold(sd);
}
/**
* "Setter" and "getter" methods
*/
public void setModel(String md)
{
if (md.length() != 0)
{
model = md;
}
else
{
model = "(Error: Description Cannot Be Empty)";
}
}
public void setMileage(int ml)
{
if (mileage >= 10000 && mileage < 0)
{
mileage = 0;
}
else
{
mileage = ml;
}
}
public void setMpg(double mp)
{
if (mpg >= 10 && mpg <= 250)
{
mpg = mp;
}
else
{
mpg = 0;
}
}
public void setSold(boolean sd){ sold = sd; }
public String getModel(){ return model; }
public int getMileage(){ return mileage; }
public double getMpg(){ return mpg; }
// slightly different form here:
public boolean isSold(){ return sold; }
}
Class TestCar:
Code:
/**
* Class to test if the Car class setters are working
*
* @author Richard
* @version 20090609
*/
public class TestCar
{
Car myGoodCar;
Car myBadCar;
// Main calls the class constructor to do the work
public static void main(String[] args){ new TestCar(); }
public TestCar()
{
// Construct two car instances
myGoodCar = new Car("Ford Escort", 9500, 8.5, false);
myBadCar = new Car("", -9, 1209.5, true);
// Show the details at the console:
displayCar(myGoodCar);
displayCar(myBadCar);
}
private void displayCar(Car c)
{
System.out.println("Description: " + c.getModel());
System.out.println("Mileage: " + c.getMileage());
System.out.println("Mpg: " + c.getMpg());
System.out.println("Sold? " + c.isSold());
}
}
The resulting output:
Code:
Description: Ford Escort
Mileage: 9500
Mpg: 0.0
Sold? false
Description: (Error: Description Cannot Be Empty)
Mileage: -9
Mpg: 0.0
Sold? true
Now the problem is in the output:
And it clearly states in my setMileage that if the mileage is greater than or equal to 10000 and the mileage is less than 0, the mileage should output 0, else (if mileage is less than 10000 and the mileage is greater than or equal to 0), the mileage output would be whatever mileage is given:
Code:
public void setMileage(int ml)
{
if (mileage >= 10000 && mileage < 0)
{
mileage = 0;
}
else
{
mileage = ml;
}
}
I want the resulting output to look like this:
Code:
Description: Ford Escort
Mileage: 9500
Mpg: 0.0
Sold? false
Description: (Error: Description Cannot Be Empty)
Mileage: 0
Mpg: 0.0
Sold? true
What is the problem here? (Also, what type of problem is this called (if there was already a related thread posted)?)
[SOLVED] Project: CarProject problem
Looks like I had to still find out with the help of my teacher...
I changed this:
Code:
public void setMileage(int ml)
{
if (mileage >= 0 && mileage < 10000 )
{
mileage = ml;
}
else
{
mileage = 0;
}
}
public void setMpg(double mp)
{
if (mpg >= 10 && mpg <= 250)
{
mpg = mp;
}
else
{
mpg = 0;
}
}
To this:
Code:
public void setMileage(int ml)
{
mileage = ml;
if (mileage >= 0 && mileage < 10000 )
{
mileage = ml;
}
else
{
mileage = 0;
}
}
public void setMpg(double mp)
{
mpg = mp;
if (mpg >= 10 && mpg <= 250)
{
mpg = mp;
}
else
{
mpg = 0;
}
}
(What happened was I added mileage = ml to the start of setMileage and added mpg = mp to the start of setMpg, otherwise default would be "0") Which fixed my problem:
Code:
Description: Ford Escort
Mileage: 9500
Mpg: 0.0
Sold? false
Description: (Error: Description Cannot Be Empty)
Mileage: 0
Mpg: 0.0
Sold? true