Results 1 to 7 of 7
  1. #1
    Mideoan is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default Getting my Accessors to work when creating a new Class.

    I've made a new class for a Car. I have the following code for it here. I don't believe I have any issues with the class. However, when I reference the class, I can't get my accelerator to add 5 mph nor can I get my brake to decelerate 5 mph.

    Java Code:
    public class Car {
     
    	private int yearModel;  // Holds the car's year model
    	private String make;  // Holds the make of the car
    	private int speed; // Holds the car's current speed
    	
    	// Constructor for Car
    	
    	Car(int year, String carMake)
    	{
    		yearModel = year;
    		make = carMake;
    		speed = 0 ;
    	}
    	
         // setyearModel to the Model year
    	public void setyearModel(int yModel)
    	{
    	yearModel = yModel;
    	}
    	// setMake to the make of the car
    	public void setMake (String carMake)
    	{
    	make = carMake;
    	}
    	// setSpeed to the speed of the car
    	public void setSpeed(int carSpeed)
    	{
    	speed = carSpeed;
    	}
    	  // returns the year of the model
    	public int getYearModel()
    	{
    	return yearModel;
    	}
    	// returns the make of the model
    	public String getMake ()
    	{
    	return make;
    	}
    	// returns the speed of the model
    	public int getSpeed ()
    	{
    	return speed;
    	}
    	// increase the speed by 5 when accelerated
    	public void AccelerateSpeed (int speed)
    	{
    	speed = speed  +5;
    
    	}
    	// decreases the speed by 5 when braked
    	public void BrakeSpeed ()
    	{
    	speed = speed -5;
    	
    	} 
    	
    	}
    Also, my counter isn't working. I want it do accelerate 5 times, and also decelerate 5 times.

    Java Code:
    import javax.swing.JOptionPane;
    public class Car_Class_Run {
    
    	
    	
    
    	public static void main(String[] args) {
    
    	Car myCar = new Car (2007, "Chevy");
    
    	int speed = myCar.getSpeed();
    	speed = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Speed" ));
    	
    	for (int count = 0; count < 5; count++)
    	
    	{
    
    	System.out.println("The" + " " + myCar.getYearModel() + " " + myCar.getMake() +
    	" " + "is going " + speed );
    	myCar.AccelerateSpeed(speed);
    	System.out.println("Your Speed now is: " + speed );
    	}
    
    	speed = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Speed" ));
    	for (int count = 0; count < 5; count++)
    	{
    
    	System.out.println("The" + " " + myCar.getYearModel() + " " + myCar.getMake() +
    	" " + "is going " );
    	myCar.BrakeSpeed();
    	System.out.println("Your Speed now is: " + speed);
    	}
    
    	}
    	}
    I've been playing around trying to plug things in here and there with no success. Staring blankly now after several hours and need some help! I'm a beginner, so take it easy on me please! :)

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Java Code:
    public void AccelerateSpeed (int speed)
    	{
    	speed = speed  +5;
    
    	}
    You are adding 5 to the local variable speed not the class member speed.

  3. #3
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Your Car code is fine. The problem seems to be that when you print out the speed, you're printing the value of the local speed variable instead of calling Car.getSpeed().

    There are some other anomalies (why does AccelerateSpeed() take an int value, which it ignores and just increments the speed by 5?)

    And FYI, don't capitalize the first letter of method names. It looks bad.

  4. #4
    Mideoan is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by NRitH View Post
    Your Car code is fine. The problem seems to be that when you print out the speed, you're printing the value of the local speed variable instead of calling Car.getSpeed().

    There are some other anomalies (why does AccelerateSpeed() take an int value, which it ignores and just increments the speed by 5?)

    And FYI, don't capitalize the first letter of method names. It looks bad.
    Again, I apologize I'm new. How do I call my Car.getSpeed() to show the acceleration/brake?

  5. #5
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by Mideoan View Post
    Again, I apologize I'm new. How do I call my Car.getSpeed() to show the acceleration/brake?
    That's exactly how:
    Java Code:
    	System.out.println("The" + " " + myCar.getYearModel() + " " + myCar.getMake() +
    	" " + "is going " + [B]myCar.getSpeed()[/B]);
    	myCar.AccelerateSpeed(speed);
    	System.out.println("Your Speed now is: " + [B]myCar.getSpeed()[/B]);
    	}
    
    	speed = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Speed" ));
    	for (int count = 0; count < 5; count++)
    	{
    
    	System.out.println("The" + " " + myCar.getYearModel() + " " + myCar.getMake() +
    	" " + "is going " + [B]myCar.getSpeed()[/B]);
    	myCar.BrakeSpeed();
    	System.out.println("Your Speed now is: " + [B]myCar.getSpeed()[/B]);
    	}

  6. #6
    Mideoan is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by NRitH View Post
    That's exactly how:
    Java Code:
    	System.out.println("The" + " " + myCar.getYearModel() + " " + myCar.getMake() +
    	" " + "is going " + [B]myCar.getSpeed()[/B]);
    	myCar.AccelerateSpeed(speed);
    	System.out.println("Your Speed now is: " + [B]myCar.getSpeed()[/B]);
    	}
    
    	speed = Integer.parseInt(JOptionPane.showInputDialog("Enter Your Speed" ));
    	for (int count = 0; count < 5; count++)
    	{
    
    	System.out.println("The" + " " + myCar.getYearModel() + " " + myCar.getMake() +
    	" " + "is going " + [B]myCar.getSpeed()[/B]);
    	myCar.BrakeSpeed();
    	System.out.println("Your Speed now is: " + [B]myCar.getSpeed()[/B]);
    	}
    Alright, that is a step in the right directions, as it at least kinda works on the to decelerate. This is what is happening in my console box.
    Java Code:
    The 2007 Chevy is going 0
    Your Speed now is: 0
    The 2007 Chevy is going 0
    Your Speed now is: 0
    The 2007 Chevy is going 0
    Your Speed now is: 0
    The 2007 Chevy is going 0
    Your Speed now is: 0
    The 2007 Chevy is going 0
    Your Speed now is: 0
    The 2007 Chevy is going 0
    Your Speed now is: -5
    The 2007 Chevy is going -5
    Your Speed now is: -10
    The 2007 Chevy is going -10
    Your Speed now is: -15
    The 2007 Chevy is going -15
    Your Speed now is: -20
    The 2007 Chevy is going -20
    Your Speed now is: -25
    What I want it do to for example is, when I type in 50, it will show:
    Your 2007 Chevy is going: 50
    Your speed now is: 55
    Your 2007 Chevy is going: 55
    Your speed now is: 60
    etc

    I just don't understand how calling the get.Speed made that kinda work. So confused.

  7. #7
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Ah, I see what's going on. You have three different speed variables:

    1. An instance variable in the Car class
    2. A local variable in main()
    3. An argument in Car.AccelerateSpeed()

    What's happening is that they're walking on each other:
    1. speed in main() has local scope, so it's what was getting printed in your original code. Changing the calls to Car.getSpeed() made the program get the value of the Car object's speed instance variable. That's now fixed.
    2. Now the argument speed in Car.AccelerateSpeed() is hiding the Car's speed instance variable. In other words, your AccelerateSpeed() implementation just increments the speed you passed in by 5, not the Car object's instance variable. Rename the argument to increase or something, then change the implementation to

    Java Code:
    speed += increase;

Similar Threads

  1. How can I make this Java class work
    By ruchir123 in forum New To Java
    Replies: 2
    Last Post: 12-10-2010, 11:53 PM
  2. Why the web browser needs to refresh first so that creating table may work? Pls help.
    By MarkSquall in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-29-2010, 11:14 AM
  3. Calling accessors
    By incog03 in forum New To Java
    Replies: 2
    Last Post: 07-28-2009, 07:02 AM
  4. Replies: 6
    Last Post: 03-11-2009, 04:38 PM
  5. Using accessors properly
    By LifeWithJava in forum New To Java
    Replies: 2
    Last Post: 12-23-2008, 02:49 PM

Posting Permissions

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