Results 1 to 7 of 7
Thread: Project: CarProject problem
- 10-13-2009, 03:01 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
Project: CarProject problem
Hello, I seem to have a problem here:
Here is a code template I copied for my assignment:
Class Car:
Class TestCar:Java 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; } }
The resulting output:Java 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()); } }
Now the problem is in the output:Java Code:Description: Ford Escort Mileage: 9500 Mpg: 0.0 Sold? false Description: (Error: Description Cannot Be Empty) Mileage: -9 Mpg: 0.0 Sold? true
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:Java Code:Mileage: -9
I want the resulting output to look like this:Java Code:public void setMileage(int ml) { if (mileage >= 10000 && mileage < 0) { mileage = 0; } else { mileage = ml; } }
What is the problem here? (Also, what type of problem is this called (if there was already a related thread posted)?)Java Code:Description: Ford Escort Mileage: 9500 Mpg: 0.0 Sold? false Description: (Error: Description Cannot Be Empty) Mileage: 0 Mpg: 0.0 Sold? true
-
hm, should that be &&?Java Code:if (mileage >= 10000 && mileage < 0)
it's a logic problem, and there are many examples of this posted.Also, what type of problem is this called
- 10-13-2009, 04:32 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Logical evaluation make lots of problems in coding. As Fubarable says it should be || (OR) operator.
&& behave in this way, if the left side condition is evaluated to false compiler not looking into the right side condition at all. Because what ever the evaluate there (true/false) the whole expression is false.
Once the mileage is -9 the if (mileage >= 10000) evaluate as false.if (mileage >= 10000 && mileage < 0)
- 10-13-2009, 05:43 AM #4
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
So you're saying I should change the && into ||; tried that but the output is still the same.
If I changed this:
To this:Java Code:public void setMileage(int ml) { if (mileage >= 10000 && mileage < 0) { mileage = 0; } else { mileage = ml; } }
which is what I basically had previously before modifying it, it still outputs the same result.Java Code:public void setMileage(int ml) { if (mileage < 10000 && mileage >= 0) { mileage = ml; } else { mileage = 0; } }
- 10-13-2009, 06:11 AM #5
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
for mileage is 0Java Code:if (mileage < 10000 && mileage >= 0) { mileage = ml; }
mileage set to ml which is -9
should you check the value of input parameter or variable of that object?
- 10-14-2009, 04:10 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes better to check the initial values then. At the second code segment you send here return the same value, -9, means that it returns the ml.
- 10-15-2009, 04:51 AM #7
Member
- Join Date
- Oct 2009
- Posts
- 17
- Rep Power
- 0
[SOLVED] Project: CarProject problem
Looks like I had to still find out with the help of my teacher...
I changed this:
To this:Java 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; } }
(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:Java 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; } }
Java Code:Description: Ford Escort Mileage: 9500 Mpg: 0.0 Sold? false Description: (Error: Description Cannot Be Empty) Mileage: 0 Mpg: 0.0 Sold? true
Similar Threads
-
deploy existing web project[class reference problem]
By runfast in forum EclipseReplies: 1Last Post: 10-13-2009, 10:38 AM -
Stream problem when I export my project in a .jar executable archive
By Arsenicmic in forum Advanced JavaReplies: 3Last Post: 09-07-2009, 08:27 AM -
hi iam new to this iam doing project , and had this problem pls give solution
By srikanthkgm in forum NetBeansReplies: 0Last Post: 02-24-2009, 02:40 PM -
my project problem??help please
By sheckoo in forum New To JavaReplies: 4Last Post: 12-06-2008, 01:14 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks