Results 1 to 7 of 7
- 02-24-2011, 05:55 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
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.
Also, my counter isn't working. I want it do accelerate 5 times, and also decelerate 5 times.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; } }
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! :)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); } } }
- 02-24-2011, 06:01 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
You are adding 5 to the local variable speed not the class member speed.Java Code:public void AccelerateSpeed (int speed) { speed = speed +5; }
- 02-24-2011, 06:02 AM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
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.
- 02-24-2011, 05:26 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
- 02-24-2011, 06:49 PM #5
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
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]); }
- 02-24-2011, 09:53 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
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.
What I want it do to for example is, when I type in 50, it will show: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
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.
- 02-24-2011, 10:35 PM #7
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
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
-
How can I make this Java class work
By ruchir123 in forum New To JavaReplies: 2Last Post: 12-10-2010, 11:53 PM -
Why the web browser needs to refresh first so that creating table may work? Pls help.
By MarkSquall in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 03-29-2010, 11:14 AM -
Calling accessors
By incog03 in forum New To JavaReplies: 2Last Post: 07-28-2009, 07:02 AM -
Unreachable Statement (accessors) - works as a main method!
By thomase in forum New To JavaReplies: 6Last Post: 03-11-2009, 04:38 PM -
Using accessors properly
By LifeWithJava in forum New To JavaReplies: 2Last Post: 12-23-2008, 02:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks