Results 1 to 6 of 6
  1. #1
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Default Allowing users to create another instance of a class

    Greetings all,
    So I am not sure if I am asking this question correctly, but here goes:

    Code is below. Question is.... How can I make it so that in the Main I ask a user to create a new car and they are able to make a new instance of the car. I guess I don't understand how to make multiple instances of an object without defining it with a name or something.... In my Main below, I have created the new objects, and right now totally clueless as to how to give the user an option to make one more instance or 10 more instances or 100 more instances of the object... Like If this program was for buying cars, I would want the user to be able to input their cars information.... So I guess I am asking how to dynamically instantiate an object???? not sure if that is the correct way to ask the question. The code below is just a main and a class from an old project that I am using here as examples of trying to say instantiate a new CarClass object or many new CarClass objects.

    Thanks in advance for any assistance, tips or guidance.
    Wally

    Java Code:
    package steadmanproject1_car_class;
    
    public class CarClass
    {
      //Initial Values
    
      private String myColor;
      private String myMake;
      private String myModel;
      private String myYear;
      private String myOwner;
      private int mySpeed;
    
      // 5 Element Constructor - Speed set to 0
      public CarClass(String color, String make, String model, String year, String owner)
      {
        myColor = color;
        myMake = make;
        myModel = model;
        myYear = year;
        myOwner = owner;
        mySpeed = 0;
      }
    
      //SET STATEMENTS - allow the attribute values to be changed
      public void setColor(String newColor)
      {
        myColor = newColor;
      }
    
      public void setMake(String newMake)
      {
        myMake = newMake;
      }
    
      public void setModel(String newModel)
      {
        myModel = newModel;
      }
    
      public void setYear(String newYear)
      {
        myYear = newYear;
      }
    
      public void setOwner(String newOwner)
      {
        myOwner = newOwner;
      }
      // END OF SET STATEMENTS
    
      //GET STATEMENTS - will return the values of attributes
      public String getColor()
      {
        return myColor;
      }
    
      public String getMake()
      {
        return myMake;
      }
    
      public String getModel()
      {
        return myModel;
      }
    
      public String getYear()
      {
        return myYear;
      }
    
      public String getOwner()
      {
        return myOwner;
      }
      // END OF GET STATEMENTS
    
      //While I don't have a set method, I created the get method for speed so that you could check the speed individually.
      public int getSpeed()
      {
        return mySpeed;
      }
    
      //accelerate and brake methods change the mySpeed variable accordingly 
      public void accelerate()
      {
        mySpeed += 5;
      }
    
      // Brake methods checks to ensure that vehicle is moving and if not it let's the user know they are stopped.
      public void brake()
      {
        if (mySpeed == 0)
        {
          System.out.printf("%s you are already stopped, so pressing the brakes is a waste of time.\n", myOwner);
        } else
        {
          mySpeed -= 5;
        }
      }
    
      //This attribute is used to see details about each object
      public void printAttributes()
      {
        if (mySpeed == 0)
        {
          System.out.printf("%s is parked in a %s %s %s %s\n", myOwner, myColor, myYear, myMake, myModel);
        } else
        {
          System.out.printf("%s is driving a %s %s %s %s going %d miles an hour\n", myOwner, myColor, myYear, myMake, myModel, mySpeed);
        }
      }
    }
    And the Main code is:
    Java Code:
    package steadmanproject1_car_class;
    
    
    public class SteadmanProject1_car_class
    {
    
      public static void main(String[] args)
      {
        /* This exercise demonstrates the use of a class and the set and get methods
         * it also allows for an accelerate and brake method which change the speed setting of the objects
         * 
         * You can copy and paste the below line in this comment and change the newName to an unused
         * name of your choice and then change the variables in the () and you will create a new instance that
         * will be able to access the methods located in the CarClass
         * 
         * CarClass newName = new CarClass("color","make","model","year","owner");
         */
    
        //Creating 2 instances of CarClass
        CarClass carOne = new CarClass("Blue", "Saturn", "Aura", "2007", "Wally");
        CarClass carTwo = new CarClass("Green", "Chevrolet", "Tahoe", "2005", "Debbie");
    
        //Viewing the attributes of both CarClasses
        carOne.printAttributes();
        carTwo.printAttributes();
    
        System.out.print("\n"); // the New line is just added for readability in output
    
        //View the current values of attributes for carOne
        System.out.printf("The color of carOne is: %s\n", carOne.getColor());
        System.out.printf("The make of carOne is: %s\n", carOne.getMake());
        System.out.printf("The model of carOne is: %s\n", carOne.getModel());
        System.out.printf("The year of carOne is: %s\n", carOne.getYear());
        System.out.printf("%s is the owner of carOne.\n", carOne.getOwner());
    
        // I did add a getSpeed method to the CarClass just so that the speed could be viewed as an individual element.
        System.out.printf("The speed of carOne is %d\n", carOne.getSpeed());
    
    
        System.out.print("\n"); // the New line is just added for readability in output
    
        System.out.print("Changing the attributes of carOne\n");
        //Changing attributes
        carOne.setColor("Purple");
        carOne.setOwner("Fred");
        carOne.setMake("Ford");
        carOne.setModel("F150");
        carOne.setYear("2009");
    
        System.out.print("\n"); // the New line is just added for readability in output
    
        System.out.printf("The color of carOne is now: %s\n", carOne.getColor());
        System.out.printf("The make of carOne is now: %s\n", carOne.getMake());
        System.out.printf("The model of carOne is now: %s\n", carOne.getModel());
        System.out.printf("The year of carOne is now: %s\n", carOne.getYear());
        System.out.printf("%s is now the owner of carOne.\n", carOne.getOwner());
        // I did add a getSpeed method to the CarClass just so that the speed could be viewed as an individual element.
        System.out.printf("The speed of carOne is %d\n", carOne.getSpeed());
    
    
        System.out.print("\n"); // the New line is just added for readability in output
    
        //Adding speed to the CarClass
        carOne.accelerate();
        carOne.accelerate();
    
        //showing that the CarClass is no longer parked, but is in fact moving
        carOne.printAttributes();
    
        //Reducing the speed of CarClass
        carOne.brake();
    
        //CarTwo is not moving so when attempting to brake, user receives a different message
        System.out.print("\n"); // the New line is just added for readability in output
        carTwo.brake();
        carTwo.accelerate();
        carTwo.accelerate();
        carTwo.accelerate();
        carTwo.printAttributes();
    
    
        //Showing that the speed is reduced
        System.out.print("\n"); // the New line is just added for readability in output
        carOne.printAttributes();
        carOne.brake();
        carOne.printAttributes();
        carOne.brake();
    
        System.out.print("\n"); // the New line is just added for readability in output
      }
    }

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Allowing users to create another instance of a class

    Hi wfsteadman,

    You're most of the way there already. All you would need to do is ask the user to input the details and the create a new instance of the object using these values as parameters. Each time a new object is constructed this can be held in an array. If you want to save the objects into a file you will need to look into IO.

    Regards.

  3. #3
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Default Re: Allowing users to create another instance of a class

    I am still a bit confused as I don't know how I would instantiate the objects....

    Java Code:
    CarClass carOne = new CarClass("Blue", "Saturn", "Aura", "2007", "Wally");
        CarClass carTwo = new CarClass("Green", "Chevrolet", "Tahoe", "2005", "Debbie");
    I statically input the above CarClass objects, but I would not know how many entries a user could make so I guess I am puzzled by how to create the name of the CarClass instance?

    Would it be using a Static variable attached to some type of name like

    int counter = 1;

    String instanceName = "car" + counter;

    then
    CarClass instanceName = new CarClass();

    I would think it would just make a CarClass with the literal name instanceName and would not the the input of the variable?

    Any help would be appreciated.

    Thanks
    Wally

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default Re: Allowing users to create another instance of a class

    You'd use an array or ArrayList.
    Java Code:
    CarClass[] cars = new CarClass[<some suitable number>];
    or
    Java Code:
    ArrayList<CarClass> cars = new ArrayList<CarClass>();
    Then ask the user to populate them.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Allowing users to create another instance of a class

    Hi Wally,

    Have a look at the following and see if you can work through it.
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    In reference to the example given you will need an array of objects as opposed to ints.

    Regards.

  6. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Allowing users to create another instance of a class

    Of the two options highlighted by Tolls, fixed arrays are the easiest to understand but ArrayLists are the better choice due to the additional functionality.

    Regards.

Similar Threads

  1. Replies: 2
    Last Post: 08-15-2011, 08:05 AM
  2. create users on the server site?
    By nakres in forum New To Java
    Replies: 2
    Last Post: 03-25-2011, 09:31 AM
  3. Replies: 5
    Last Post: 08-25-2009, 08:43 AM
  4. create new instance of variable class
    By Fedor in forum New To Java
    Replies: 5
    Last Post: 04-12-2009, 08:13 PM
  5. create Instance of class in Javascript
    By TDMaster in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-09-2009, 04:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •