3 Replies Latest reply: May 12, 2012 10:32 PM by 587126 RSS

    Print object name

    587126
      Hi all,

      I am new to Java learning, I would like to print the object name in below program.

      Output should look like

      bike1 Cadence :40 speed :12 Gear :1
      bike2 Cadence :50 speed :60 Gear :2

      What are the changes I need to do to get the desired output.
      /*
       * To change this template, choose Tools | Templates
       * and open the template in the editor.
       */
      package abbhilash;
      
      /**
       *
       * @author admin
       */
      
      class Bicycle {
          int cadence = 0;
          int speed   = 0;
          int gear    = 1;
          
          void changeCadence(int newValue)
          {
              cadence = newValue;
          }
         
          void changeGear(int newValue)
          {
              gear =newValue;
          }
          
           void increaseSpeed(int increment)
          {
              speed = speed + increment;
          }
           
            void decreaseSpeed(int decrement)
          {
              speed = speed - decrement;
          }
            
            void printStatus()
            {
                System.out.println("Cadence :" + cadence + " speed :" + speed
                        + " Gear :" + gear);
            }
          
      } 
      
       class BicycleDemo {
           
           public static void main(String[] args)
           {
               //create two bicylce objects
               Bicycle bike1 = new Bicycle();
               Bicycle bike2 = new Bicycle();
               
               bike1.changeCadence(40);
               bike1.changeGear(1);
               bike1.increaseSpeed(12);
               bike1.printStatus();      
               
               bike2.changeCadence(50);
               bike2.changeGear(2);
               bike2.increaseSpeed(60);
               bike2.printStatus();
               
           }
          
      }
      Thanks
      Raghu
        • 1. Re: Print object name
          931373
          Hello Raghu,
          You can simply add a name field to your Bicycle class :
          private String name; 
          
          public void setName(String newName){
          this.name = newName; 
          }
          change you status mehtod :
            void printStatus()
                {
                    System.out.println(name + " Cadence :" + cadence + " speed :" + speed
                            + " Gear :" + gear);
                }
          in your main method , add name field :
           Bicycle bike1 = new Bicycle();
          bike1.setName("bike1") ;
          This should do the trick.
          • 2. Re: Print object name
            J2EE_Life
            Hi Raghu,

            Here you already have the object, so you have the object name as well. All you need is to print the name. So you can pass the name of object as parameter to printStatus() method and you are done. Code changes are below:
            void printStatus(String name)// here String parameter is added
                  {
                      System.out.println(name + " Cadence :" + cadence + " speed :" + speed
                              + " Gear :" + gear);
                  }
            And inside the main method call the printStatus() method with object name.

            bike2.printStatus("bike1 ");      // calling with the name of object
            bike2.printStatus("bike2 ");
            Amit
            • 3. Re: Print object name
              587126
              Thanks to both of you for suggesting two ways to do it.