Results 1 to 7 of 7
  1. #1
    Rhez is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default Project: CarProject problem

    Hello, I seem to have a problem here:
    Here is a code template I copied for my assignment:
    Class Car:
    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; }  
    }
    Class TestCar:
    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()); 
      } 
    }
    The resulting 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
    Now the problem is in the output:
    Java Code:
    Mileage: -9
    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:
        public void setMileage(int ml)
        {
            if (mileage >= 10000 && mileage < 0)
            {
                mileage = 0;
            }
            else
            {
                mileage = ml;
            }
        }
    I want the resulting output to look like this:
    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
    What is the problem here? (Also, what type of problem is this called (if there was already a related thread posted)?)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Java Code:
    if (mileage >= 10000 && mileage < 0)
    hm, should that be &&?

    Also, what type of problem is this called
    it's a logic problem, and there are many examples of this posted.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

    if (mileage >= 10000 && mileage < 0)
    Once the mileage is -9 the if (mileage >= 10000) evaluate as false.

  4. #4
    Rhez is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    So you're saying I should change the && into ||; tried that but the output is still the same.
    If I changed this:
    Java Code:
        public void setMileage(int ml)
        {
            if (mileage >= 10000 && mileage < 0)
            {
                mileage = 0;
            }
            else
            {
                mileage = ml;
            }
        }
    To this:
    Java Code:
        public void setMileage(int ml)
        {
            if (mileage < 10000 && mileage >= 0)
            {
                mileage = ml;
            }
            else
            {
                mileage = 0;
            }
        }
    which is what I basically had previously before modifying it, it still outputs the same result.

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    Java Code:
    if (mileage < 10000 && mileage >= 0)
            {
                mileage = ml;
            }
    for mileage is 0
    mileage set to ml which is -9


    should you check the value of input parameter or variable of that object?

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  7. #7
    Rhez is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default [SOLVED] Project: CarProject problem

    Looks like I had to still find out with the help of my teacher...
    I changed 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;
            }
        }
    To this:
    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;
            }
        }
    (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:
    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

  1. Replies: 1
    Last Post: 10-13-2009, 10:38 AM
  2. Replies: 3
    Last Post: 09-07-2009, 08:27 AM
  3. Replies: 0
    Last Post: 02-24-2009, 02:40 PM
  4. my project problem??help please
    By sheckoo in forum New To Java
    Replies: 4
    Last Post: 12-06-2008, 01:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •