Results 1 to 9 of 9
  1. #1
    me.
    me. is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default newebie question about classes

    hi all,

    new to java and already have difficulties with my code.

    having code with two classes:

    Java Code:
    class Bicycle {
        int cadence = 0;
        int speed = 0;
        int gear = 1;
    
        		
        		
        void changeCadence(int newValue) {
             cadence = newValue;
        }
        void changeGear(int newValue) {
             gear = newValue;
        }
        void speedUp(int increment) {
             speed = speed + increment;   
        }
        void applyBrakes(int decrement) {
             speed = speed - decrement;
        }
        
        
        void printStates() {
             System.out.println("cadence:" +
                 cadence + " speed:" + 
                 speed + "  name" + name  
     + gear);
        }
    }
    and

    Java Code:
    public class bikes extends Bicycle {
    	
    	
    	    public static void main(String[] args) {
    
    	        // Create two different 
    	        // Bicycle objects
    	        Bicycle bike1 = new Bicycle();
    	        Bicycle bike2 = new Bicycle();
    	        // Invoke methods on 
    	        // those objects
    	        bike1.changeCadence(50);
    	        bike1.speedUp(10);
    	        bike1.changeGear(2);
    	        bike1.printStates();
    	        
    	        bike2.changeCadence(50);
    	        bike2.speedUp(10);
    	        bike2.changeGear(2);
    	        bike2.changeCadence(40);
    	        bike2.speedUp(10);
    	        bike2.changeGear(3);
    	        bike2.printStates();
    	    }
    	}
    i've been trying to add names to both bikes but dont know how to do it.

    tried adding
    Java Code:
    static String[] name = { "giant", "giant cc"};
    to Bicycle class but couldnt display names
    than tried to create new class Bikenames with just names as strings but also couldnt get names printed...

    can you explain how it must be done,please.
    Thank you.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,984
    Rep Power
    9

    Default Re: newebie question about classes

    Why do you have an array of names?

    See the other variables in the Bike class? You'll need another one to hold the name, and appropriate methods for setting and accessing it.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    me.
    me. is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: newebie question about classes

    Quote Originally Posted by KevinWorkman View Post
    Why do you have an array of names?
    tried to use arrays but maybe string will do?

    String []name ={"Giant ","Giant XXL"};
    and appropriate methods for setting and accessing it.
    yes :) thats another problem...

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,984
    Rep Power
    9

    Default Re: newebie question about classes

    The name needs to be an attribute inside the Bike class. Then you can instantiate a Bike and give it whatever name you want using a constructor or setter method.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    me.
    me. is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: newebie question about classes

    Quote Originally Posted by KevinWorkman View Post
    The name needs to be an attribute inside the Bike class.
    Then you can instantiate a Bike and give it whatever name you want using a constructor or setter method.
    tried
    bike1.name();
    bike1.newname();

    and dont even know what else but been playing with it and just cant get it working maybe need break

    can you write it as example, please

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,984
    Rep Power
    9

    Default Re: newebie question about classes

    You have an example of other attributes inside the bike class. Model what you do off of how it already works. Each bike has a cadence, right? How is that handled?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    me.
    me. is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: newebie question about classes

    but cadence is integer and names are string
    and when I tried using
    bike1.names();

    was getting error


    also

    void changename(string newValue)
    { name = newValue;}
    didnt work

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default Re: newebie question about classes

    You have to realise that "didn't work" is essentially meaningless to us.
    You need to provide us with the errors you are getting.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,984
    Rep Power
    9

    Default Re: newebie question about classes

    Quote Originally Posted by me. View Post
    but cadence is integer and names are string
    So? How do you declare a String?

    Quote Originally Posted by me. View Post
    and when I tried using
    bike1.names();

    was getting error


    also


    didnt work
    Why would those work? Where do you see a names() method? Or a cadence() method for that matter? You can't just make up syntax and expect it to work. Where did you declare a name variable?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Question on Singleton + Private Inner Classes
    By fatabass in forum New To Java
    Replies: 5
    Last Post: 09-11-2012, 10:45 AM
  2. Classes Help!!! two classes with object question.
    By stuckonjava in forum New To Java
    Replies: 16
    Last Post: 02-10-2012, 01:39 AM
  3. Question about abstract classes and methods
    By mwr1976 in forum New To Java
    Replies: 8
    Last Post: 11-21-2011, 01:42 PM
  4. Objects & Classes - Beginner question
    By jonytek in forum New To Java
    Replies: 4
    Last Post: 02-09-2011, 12:49 PM
  5. question about abstract classes
    By TheFlying_Boy in forum New To Java
    Replies: 6
    Last Post: 07-08-2009, 07:19 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
  •