Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: how/where to add new method and attributes

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how/where to add new method and attributes

    hi

    new-noob here looking for some1 to explain these to me:

    having code (found in one tutorials) and it works all-right want to add some new methods and attributes but need you to point me into right direction, please.

    public class Bicycle {
        int cadence = 0;
        int speed = 0;
        int gear = 1;
        int sits = 0;
        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 sitsNumber(int newValue){
        	sits = newValue;
        }
        void printStates() {
             System.out.println("cadence:" +
                 cadence + " speed:" + 
                 speed + " gear:" + gear + " sits: " + sits );
        }
    }
     
    class BicycleDemo {
        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();
            bike1.sitsNumber(1);
     
            bike2.changeCadence(50);
            bike2.speedUp(10);
            bike2.changeGear(2);
            bike2.changeCadence(40);
            bike2.speedUp(10);
            bike2.changeGear(3);
            bike2.printStates();
            bike2.sitsNumber(2);
     
        }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,961
    My Mood
    Hungover
    Thanks
    133
    Thanked 553 Times in 474 Posts

    Default Re: how/where to add new method and attributes

    Not really sure what you're asking. What have you tried? What happened when you tried it?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how/where to add new method and attributes

    hi Kevin,

    I am trying to add an bicycle carrier option and wonder how to have it displayed that bicke1 has carrier and bicke2 doesn't? it will be string not integer

    public class Bicycle {
    int cadence = 0;
    int speed = 0;
    int gear = 1;
    int sits = 0;
    String carrier = "";
    bike1.changeCadence(50);
    bike1.speedUp(10);
    bike1.changeGear(2);
    bike1.printStates();
    bike1.sitsNumber(1);
    bike1.carrier ("yes");
    Last edited by me.; October 18th, 2012 at 09:19 AM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,961
    My Mood
    Hungover
    Thanks
    133
    Thanked 553 Times in 474 Posts

    Default Re: how/where to add new method and attributes

    And what happened when you tried that? Are you also adding a method called carrier() that takes a String argument? That seems strange to me- take a look at how the other methods set variable values.

    Also, if your variable should hold yes/no values, you should use a boolean instead of a String.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. attributes and properties in jsp page
    By god1gracious in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: December 30th, 2011, 02:31 AM
  2. key with 2 attributes?
    By ober0330 in forum Collections and Generics
    Replies: 1
    Last Post: October 10th, 2011, 11:47 AM
  3. Get CSS Attributes using Java
    By justinmifsud in forum Object Oriented Programming
    Replies: 2
    Last Post: February 15th, 2011, 04:08 PM
  4. javax.print setting attributes
    By prodtechjoshim in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 4th, 2011, 05:54 AM
  5. Cannot access class attributes
    By Cyburg in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 29th, 2010, 07:30 AM