Results 1 to 3 of 3
- 10-31-2011, 08:23 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 16
- Rep Power
- 0
Why my constructor doesn't initialize these values?
Java Code:class BicycleDemo { public static void main (String[]args) { // Loon kaks erinevat jalgratta objekti Bicycle bike1 = new Bicycle(3,5,10); //Bicycle bike2 = new Bicycle(); //kasutan objektidel BicycleDemo meetodeid. bike1.printStates(); bike1.changeCadence(10); bike1.changeGear(1); bike1.speedUp(20); bike1.printStates(); bike1.applyBrakes(5); bike1.printStates(); } } class Bicycle extends BicycleDemo { int cadence ;//Object's state int speed ; int gear ; //Konstruktor 1 Bicycle (int sCadence,int sSpeed,int sGear) { sCadence = cadence; sSpeed = speed; sGear = gear; } void changeCadence (int newValue) { cadence =newValue; } void changeGear (int newValue) { gear = newValue; } void speedUp (int increment) { speed = speed + increment; } void applyBrakes (int increment) { speed = speed - increment; } void printStates() { System.out.println("Speed:" +speed+", gear:"+gear+",cadence:"+cadence); } }
I have set my constructor Bicycle(3,5,10), but printStates still gives value for speed,cadence and gear 0,0,0.
- 10-31-2011, 08:46 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: Why my constructor doesn't initialize these values?
You have your assignment statements the wrong way round. You're assigning the value of the instance variable to the argument variable, when you intend to do the opposite.Java Code:Bicycle (int sCadence,int sSpeed,int sGear) { sCadence = cadence; sSpeed = speed; sGear = gear; }
- 10-31-2011, 09:03 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Assigning array values to objects in a constructor
By Dreaming in forum New To JavaReplies: 8Last Post: 10-25-2011, 06:17 PM -
why constructor doesn't have any return type?
By kaka in forum New To JavaReplies: 1Last Post: 09-30-2010, 08:02 PM -
HashMap contains all values but doesn't show all values
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 05-10-2009, 11:35 PM -
Beginner; Create a class to store info and constructor to initialize
By badness in forum New To JavaReplies: 16Last Post: 05-08-2008, 09:45 PM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks