CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2011
    Posts
    2

    New to Java Need help with Homework

    I have my program that does what it needs to but I want it to be able to do one more thing and i can't seem to figure out how. This program ask what make,year,speed of a car, I also have where you can have the car accelerate or brake. The program works if you accelerate or brake once. I want to be able to accelerate or break 5 times then show the speed. Can somebody point me into the right direction? Thanks for the help!

    Code:
    import java.util.Scanner;
    public class Car{
        Scanner kb = new Scanner(System.in);
        private int yearModel;
        private String make;
        private int speed;
        private int acc;
        private int count = 0;
        public Car() {
            this.yearModel = yearModel;
            this.make = make;
            this.speed = speed;
        }
    
        
        
        public void SetMake(){
        this.make = kb.next();
        }
        public void SetYearModel(){
        this.yearModel = kb.nextInt();
        }
        public void SetSpeed(){
        this.speed = kb.nextInt();
        }
        
        
        
        
        public void Accel() {
           
            this.acc = kb.nextInt();
            while (count <= 5) 
            count++;
            {
                 
                if (this.acc == 5) {
                    this.speed = speed + 5;
    
                } else if (this.acc == -5) {
                    this.speed = speed - 5;
    
                } else if (this.acc == 0) {
                    this.speed = speed;
    
                } else {
                    System.out.println("Goodbye");
                    System.exit(0);
                }
            
              
        }
        }
      
            
        
        
        
        
    
        
    //    public String getMake() {
    //        return make;
    //        
    //    }
    //    public void setMake(String make) {
    //        this.make = make;
    //    }
    //    public int getSpeed() {
    //        return speed;
    //    }
    //    public void setSpeed(int speed) {
    //        this.speed = speed;
    //    }
    //    public int getYearModel() {
    //        return yearModel;
    //    }
    //    public void setYearModel(int yearModel) {
    //        this.yearModel = yearModel;
    //    }
    
        @Override
        public String toString() {
            return "Car {" + "Year Model: " + yearModel 
                    + ", Make: " + make 
                    + ", Speed=" + speed + '}';
        }
        
        
        
        
        
        
    }

    Code:
    public class CarDemo {
    
        
        public static void main(String[] args) {
            
         
            Car car1 = new Car();
            System.out.println("Enter Make:\t");
            car1.SetMake();
            System.out.println("Enter Year:\t");
            car1.SetYearModel();
            System.out.println("Enter Speed:\t");
            car1.SetSpeed();
            System.out.println("Enter 5 to accelerate Vehicle or Enter -5 to brake by 5 mph");
            System.out.println("Enter Acceleration:\t");
            car1.Accel();
            
            
            
            
            
            
            
            
            
            
            
            System.out.println(car1);

  2. #2
    Join Date
    May 2005
    Location
    San Antonio Tx
    Posts
    44

    Re: New to Java Need help with Homework

    the problem is that you are executing a loop with no body

    your loop is just this part
    Code:
      while (count <= 5) 
            count++;
    none of the if statements get executed

    you should put your increment at the end of the loop's body
    Code:
      while (count <= 5) 
    {
            //if statements{
            //}
    
            count++; 
    } //end while loop
    and by the way you have count = 0 and the while loop is set to test until <=5, so it will be executed 6 times not 5.

  3. #3
    Join Date
    Oct 2011
    Posts
    2

    Re: New to Java Need help with Homework

    Thanks small things make a difference.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured