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 5 of 5

Thread: please show me how to expand my code

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

    Default please show me how to expand my code

    hi

    managed to create simple class and few methods but would like to improve it some easy way of course to let say have the code asking me if bikes are available for rent using methods ?

    can you please show me how to do it in these code?

    class Bicycle {
        int cadence = 0;
        int speed = 0;
        int gear = 1;
        int price = 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 bickeprice(int newValue){		
        	price = newValue;
        }
        void printStates() {
             System.out.println("cadence:" +
                 cadence + " speed:" + 
                 speed + " gear:" + gear +  "  it costs:" + price);
        }
    }
    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.bickeprice(100);			
            bike2.changeCadence(50);
            bike2.speedUp(10);
            bike2.changeGear(2);
            bike2.changeCadence(40);
            bike2.speedUp(10);
            bike2.changeGear(3);
            bike2.bickeprice(150);			
            bike2.printStates();
        }
    }


    thank you!


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: please show me how to expand my code

    Quote Originally Posted by me. View Post
    hi

    managed to create simple class and few methods but would like to improve it some easy way of course to let say have the code asking me if bikes are available for rent using methods ?
    Your post shows the Bicycle class, but to see if Bicylces are available for rent you need another class that holds a collection of Bicycle objects, say called BikeShop or something like that. Then you give it Bicycle objects, perhaps held in an ArrayList<Bicycle> bikesForRent. Give BikeShop a method, say getNumberBikesForRent() and return the size of the ArrayList.

    can you please show me how to do it in these code?
    Most here believe that this first attempt must be your responsibility. That's how you learn.

    Also, you have two threads in this forum where you have not replied to replies given to you. You may wish to fix that and give them replies now. People appreciate that. Best of luck!

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

    Default Re: please show me how to expand my code

    hi curmudgeon,

    can you have a look on that code I wrote in a mean time and let me know what I need to change to get it working , please

    I will replay to previous threats now...

    thanks

    class Bicycle {
    int cadence = 0;
    int speed = 0;
    int gear = 1;
    int price = 0;
    String name; // added string name for the name of bike there are two different...


    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 bickeprice(int newValue){
    price = newValue;
    }
    void printStates() {
    System.out.println("bike:" + name
    "Cadence: " +
    cadence + " speed:" +
    speed + " gear:" + gear + " it costs:" + price);
    }
    int availability() { //made these for choose option
    System.out.print("there are only two bikes at that moment:");
    System.out.println("which one would you like to see?");
    System.out.println ("Continue? (1/2): ");
    char choice = scan.next().charAt(0);//scan for the char
    if(choice == '1'){
    bike1.printStates(); //starts the menu again if char == y
    }
    if (choice == '2'){
    bike1.printStates();
    }
    else {
    System.exit(0); //else exits
    }

    }
    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.name(giant); // these is the name for bike 1 but getting errors,
    bike1.changeCadence(50);
    bike1.speedUp(10);
    bike1.changeGear(2);
    bike1.printStates();
    bike1.bickeprice(100);
    bike2.changeCadence(50);
    bike2.name(xxx);
    bike2.speedUp(10);
    bike2.changeGear(2);
    bike2.changeCadence(40);
    bike2.speedUp(10);
    bike2.changeGear(3);
    bike2.bickeprice(150);
    bike2.printStates();
    }
    }

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

    Default Re: please show me how to expand my code

    Quote Originally Posted by curmudgeon View Post
    Your post shows the Bicycle class, but to see if Bicylces are available for rent you need another class that holds a collection of Bicycle objects, say called BikeShop or something like that. Then you give it Bicycle objects, perhaps held in an ArrayList<Bicycle> bikesForRent. Give BikeShop a method, say getNumberBikesForRent() and return the size of the ArrayList.
    ok so I created new class BikeShop

    public class BikeShop {
    public static void main(String[] args) {
    String[][] names = {
    {"giant ", "giantxxl" },
    {"black", "blue"}
    };
    //
    System.out.println(names[0][0] + names[1][0]);
    //
    System.out.println(names[0][2] + names[1][1]);
    }
    }
    but still cant get it working?

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: please show me how to expand my code

    Quote Originally Posted by me. View Post
    can you have a look on that code I wrote in a mean time and let me know what I need to change to get it working , please

    I will replay to previous threats now...
    I don't think that anyone wants threats on this site. Reply to threads, for sure, but leave threats alone.

    Please use code tags when posting your code, like you did in your first post to this question. Please don't post your code in quote tags as the code becomes unreadable. Also, and again, don't put bike availability code within the Bicycle class. Think concrete for a moment -- would an individual bicycle know how many of its brethren are available for rental? No, of course not. So a Bicycle class shouldn't know either. Use a BikeShop class. You seem to have tried to create a BikeShop class, but all I see is a main method. This class should not have a main method or any static methods, but again it should hold an array or ArrayList of Bicycle objects. Keep at it.

Similar Threads

  1. How do I expand this code? I have no idea what my teacher wants.
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 3rd, 2012, 10:18 PM
  2. Show files name in JFrame
    By altisw5 in forum AWT / Java Swing
    Replies: 15
    Last Post: November 30th, 2011, 08:30 AM
  3. Replies: 2
    Last Post: July 15th, 2011, 02:39 AM
  4. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 02:43 PM
  5. Show Text With cursor
    By ravjot28 in forum AWT / Java Swing
    Replies: 1
    Last Post: January 20th, 2010, 10:02 AM