Member Avatar for mehnihma

Can someone help me with this?
I have proble with Array Lists ,
As you can see in the ouput I get same values but I need to put in different values , what am I doing wrong?

Thanks in advance and Happy New Year!

import java.util.*;

   public class Inventory
   
   {
      public static ArrayList<Boat> boat = new ArrayList<Boat>(); // arraylist which holds objects of any type of boat
   
   // Boat boat = new Boat(); //create an object of the Boat class
      public static SailBoat sailboat = new SailBoat(); //create an object of the SailBoat class
      public static PowerBoat powerboat = new PowerBoat(); //create an object of the PowerBoat class
   
   
   
      public static void main (String args[])
      {
      
      
      // A 22 ft. blue power boat with a 60 horsepower engine
         powerboat.setLengthBoat(22);
         powerboat.setColour("blue");
         powerboat.setSizeOfEngine(60);
      
         boat.add(powerboat);	//adds first powerboat
      
      // An 18 ft. white sail boat with 1 sail
         sailboat.setColour("white");
         sailboat.setLengthBoat(18);
         sailboat.setNumSails(1);
      
         boat.add(sailboat); // adds second SailBoat
      
      
      
         	//prints out the data 
         System.out.println();
         System.out.println();
      		
         for(int i = 0; i < boat.size(); i++) 
         {		
            if(boat.get(i) instanceof SailBoat) 
            {	
               System.out.println("SailBoat:");
            }
            else if(boat.get(i) instanceof PowerBoat) 
            {		
               System.out.println("PowerBoat:");
            }	
         		
            System.out.println(boat.get(i).toString());
         		
         }//end for
      
      
      }
   }

OUTPUT:

PowerBoat:
Colour=white Lenght=18 Engine Size=60 Price= Price=$11,600.00

SailBoat:
Colour=white Lenght=18 Num Sails= 1 Price= $20,000.00

The problem may be in the Boat class - did you declare colour and length as static by any chance?

Member Avatar for mehnihma

Yes I think I have done it right?

public static String colour; // A String that holds the colour of the boat
      public static int lengthBoat; // An integer that contains the length of the boat.

OK, no, that's exactly the wrong thing to do, and that's why "in the output I get same values".
static means there is just value for that variable, and all the instances share that one value. You don't want that, you want each instance to have its own colour and length

Member Avatar for mehnihma

OK i get it now

Thanks

Member Avatar for mehnihma

Ok Now I geto that right but now I getn similiar problem again as you can see in the output

import java.util.*;

   public class Inventory
   
   {
   // arraylist which holds objects of any type of boat
      public static ArrayList<Boat> boat = new ArrayList<Boat>(); 
   
   // Boat boat = new Boat(); //create an object of the Boat class
      public static SailBoat sailboat = new SailBoat(); //create an object of the SailBoat class
      public static PowerBoat powerboat = new PowerBoat(); //create an object of the PowerBoat class
   
   
   
      public static void main (String args[])
      {
      
      
      // A 22 ft. blue power boat with a 60 horsepower engine
         powerboat.setLengthBoat(22);
         powerboat.setColour("blue");
         powerboat.setSizeOfEngine(60);
      
         boat.add(powerboat);	//adds first powerboat
      
      // An 18 ft. white sail boat with 1 sail
         sailboat.setColour("white");
         sailboat.setLengthBoat(18);
         sailboat.setNumSails(1);
      
         boat.add(sailboat); // adds second SailBoat
			
		// A 42 ft. red sail boat with a 3 sails
			sailboat.setColour("red");
         sailboat.setLengthBoat(42);
         sailboat.setNumSails(3);
      
         boat.add(sailboat); // adds third SailBoat
			
		// A 35 ft. yellow power boat with an 80 horsepower engine	
			powerboat.setLengthBoat(35);
         powerboat.setColour("yellow");
         powerboat.setSizeOfEngine(80);
      
         boat.add(powerboat);	//adds fourth powerboat
			
		// A 50 ft. red power boat with a 120 horsepower engine
		powerboat.setLengthBoat(50);
         powerboat.setColour("red");
         powerboat.setSizeOfEngine(120);
      
         boat.add(powerboat);	//adds fifth powerboat
		
		// A 33 ft. blue sail boat with 2 sails
		sailboat.setColour("blue");
         sailboat.setLengthBoat(33);
         sailboat.setNumSails(2);
      
         boat.add(sailboat); // adds sixth SailBoat
		
		// A 14 ft. white power boat with a 10 horsepower engine
		powerboat.setLengthBoat(14);
         powerboat.setColour("white");
         powerboat.setSizeOfEngine(10);
      
         boat.add(powerboat);	//adds seventh powerboat

      
      
      
         	//prints out the data 
         System.out.println();
         System.out.println();
      		
         for(int i = 0; i < boat.size(); i++) 
         {		
                     		
            System.out.println(boat.get(i).toString());
         		
         }//end for
      
      
      
      }
   }

OUTPUT:

Colour=white Lenght=14 Engine Size=10 Price= Price=$9,400.00

Colour=blue Lenght=33 Num Sails= 2 Price= $37,000.00

Colour=blue Lenght=33 Num Sails= 2 Price= $37,000.00

Colour=white Lenght=14 Engine Size=10 Price= Price=$9,400.00

Colour=white Lenght=14 Engine Size=10 Price= Price=$9,400.00

Colour=blue Lenght=33 Num Sails= 2 Price= $37,000.00

Colour=white Lenght=14 Engine Size=10 Price= Price=$9,400.00

Looks like you have made exactly the same mistake with the variables in your subclasses.

Member Avatar for mehnihma

I have removed static form all of them except here

can you explain please?

Sorry. I don't understand that last post. Where's "here"?

Member Avatar for mehnihma

In this Inventory class
sorry

OK, so now you have no "static"s anywhere in Boat, SailBoat or PowerBoat, yes?
So what's still wrong?

Member Avatar for mehnihma

My output is sill wrong. It doubles and it is not in order is should be

Colour=white Lenght=14 Engine Size=10 Price= Price=$9,400.00

Colour=blue Lenght=33 Num Sails= 2 Price= $37,000.00

Colour=blue Lenght=33 Num Sails= 2 Price= $37,000.00

Colour=white Lenght=14 Engine Size=10 Price= Price=$9,400.00

Colour=white Lenght=14 Engine Size=10 Price= Price=$9,400.00

Colour=blue Lenght=33 Num Sails= 2 Price= $37,000.00

Colour=white Lenght=14 Engine Size=10 Price= Price=$9,400.00

Can you please post the complete current code? It's impossible to diagnose any further without it.

Member Avatar for mehnihma

Class Boat

public class Boat
   {
   
      public String colour; // A String that holds the colour of the boat
      public int lengthBoat; // An integer that contains the length of the boat.
   	// private boolean colour = true;
   
   
   // Default constructor which sets the colour to “white” and the length to 0.
      public Boat ()
      {
         colour = "white";
         lengthBoat = 0;
      }
   
   // Constructor with 2 parameters - a string for the colour of the boat, an integer for the length of the boat.
      public Boat (String colour, int lengthBoat) 
      {
         this.colour = colour;
         this.lengthBoat = lengthBoat;
      }
   
   
   // A mutator. Returns true if colour is white, red, blue or yellow and sets attribute value. False otherwise
      public boolean setColour(String colour)
      
      {
        
         if (colour.equals("white") || colour.equals("red") || colour.equals("blue") || colour.equals("yellow"))
         {
            this.colour = colour;
            return true;
         }
         else
         {
            return false;
         }
      }
   
   
   // An accessor. Returns the colour.
      public String getColour()
      
      {
         return colour;
      
      }
   
   
   // A mutator. Returns true if the length is between 0 and 50 inclusive and sets the attribute value. False otherwise.	
      public boolean setLengthBoat(int lengthBoat)
      {
      
         if (lengthBoat <= 50 && lengthBoat >= 0)
         {
            this.lengthBoat = lengthBoat;
            return true;
         }
         else
         {
            return false;
         }
      }	
   
   // An accessor. Returns the length.
      public int getLengthBoat()
      
      {
         return lengthBoat;
      }
   
   
   // Returns the string formatted as follows: “Color = colour Length = length”
   
      public String toString()
      {
         return "Colour=" + colour + " Lenght=" + lengthBoat;
      }
   
   }

Class SailBoat

public class SailBoat extends Boat
   {
   
   // attributes
   
      public int numberOfSails; // An integer which holds the number of sails on the sail boat
   	//public boolean nsails = true;
   
   // Methods
   
   
   // Default constructor which calls the default constructor of the Boat class and sets the number of sails to 1.
   
      public SailBoat()
      {
         super ();
      
         this.numberOfSails = 1;
      }
   
   /* Constructor with 3 parameters - a string for the colour of the boat, 
   * an integer for the length of the boat and an integer for the number of sails. 
   * The colour and the length are passed to the 2 parameter constructor for the Boat class. 
   *  The number of sails initializes the attribute in the SailBoat class using the mutator. 
   */
   
      public SailBoat(String colour, int lenghtboat, int numberOfSails)
      {
         super(colour, lenghtboat);
         this.numberOfSails = numberOfSails;
      }
   
   
   // Returns true if number of sails is 1 – 4 and sets attribute value.
   
      public boolean setNumSails(int numberOfSails)
      {
         if (numberOfSails >= 1 || numberOfSails <=4)
         {
            this.numberOfSails = numberOfSails;
            return true;
         }
         else
            return false;
      }
   
   // Returns the integer number of sails.
      public int getNumSails()
      {
         return numberOfSails;
      }
   
	
   
   // Calculates the price of the SailBoat as follows: length of boat x $1000 + number of sails x $2000.
   
      public double calcPrice()
      {
         return (lengthBoat * 1000) + (numberOfSails * 2000);
      }
   
   //Returns the string formatted as follows: 
   //super class’s toString method + “ Num Sails=” + number of sails + “ Price=” + price of sail boat.
   //Use accessor method, do not access attribute directly.  
   
      public String toString()
      {
      
         return super.toString() + " Num Sails= " + getNumSails() + " Price= $" + String.format("%,.2f\n",calcPrice());
      
      }
   
   
   }}

class PowerBoat

public class PowerBoat extends Boat
   
   {
   
   // Attributes
      public int sizeofEngine; // An integer which holds the size (number of horsepower) of the engine.
   
   // Methods
   
   //	Default constructor which calls the default constructor of the Boat class and sets the size of the engine to 5.
   
      public PowerBoat()
      {
         sizeofEngine = 5;
      }
   
   // Constructor with 3 parameters - a string for the colour of the boat, 
   // an integer for the length of the boat and an integer for the size of the engine The colour and the length 
   // are passed to the 2-parameter constructor in the Boat class. The size of the engine initializes 
   // the attribute in the PowerBoat class using the mutator.
   
      public PowerBoat (String colour, int lengthBoat, int sizeofEngine) 
      {
         this.colour = colour;
         this.lengthBoat = lengthBoat;
         this.sizeofEngine = sizeofEngine;
      }
   
   
   // Returns true if input is 1 – 350, sets value of attribute
   
      public boolean setSizeOfEngine(int sizeofEngine)
      {
         if (sizeofEngine >= 1 || sizeofEngine <= 350)
         {
            this.sizeofEngine = sizeofEngine;
            return true;
         }
         else
            return false;
      } // end boolean
   
   // Returns the size of the engine
   
      public int getSizeOfEngine()
      {
         return sizeofEngine;
      }
   
   // Calculates the price of the PowerBoat as follows: $5000 + length of boat x $300 + size of engine x $20
      public double calcPrice()
      {
         return 5000 + lengthBoat * 300 + sizeofEngine * 20;
      }
   
   
   // Returns the string in the following format: super class’s toString method + “ Engine Size=” + size of engine + “ Price=” + price of power boat.
   // Use accessor method, do not access attribute directly.
      public String toString()
      {
         return super.toString() +  " Engine Size=" + getSizeOfEngine() + " Price=" + " Price=$" + String.format("%,.2f\n",calcPrice());
      }
   
   }

Class Inventory

import java.util.*;

   public class Inventory
   
   {
   // arraylist which holds objects of any type of boat
      public static ArrayList<Boat> boat = new ArrayList<Boat>(); 
   
   
      public static SailBoat sailboat = new SailBoat(); //create an object of the SailBoat class
      public static PowerBoat powerboat = new PowerBoat(); //create an object of the PowerBoat class
   
   
   
      public static void main (String args[])
      {
      
      
      // A 22 ft. blue power boat with a 60 horsepower engine
         powerboat.setLengthBoat(22);
         powerboat.setColour("blue");
         powerboat.setSizeOfEngine(60);
      
         boat.add(powerboat);	//adds first powerboat
      
      // An 18 ft. white sail boat with 1 sail
         sailboat.setColour("white");
         sailboat.setLengthBoat(18);
         sailboat.setNumSails(1);
      
         boat.add(sailboat); // adds second SailBoat
      	
      // A 42 ft. red sail boat with a 3 sails
         sailboat.setColour("red");
         sailboat.setLengthBoat(42);
         sailboat.setNumSails(3);
      
         boat.add(sailboat); // adds third SailBoat
      	
      // A 35 ft. yellow power boat with an 80 horsepower engine	
         powerboat.setLengthBoat(35);
         powerboat.setColour("yellow");
         powerboat.setSizeOfEngine(80);
      
         boat.add(powerboat);	//adds fourth powerboat
      	
      // A 50 ft. red power boat with a 120 horsepower engine
         powerboat.setLengthBoat(50);
         powerboat.setColour("red");
         powerboat.setSizeOfEngine(120);
      
         boat.add(powerboat);	//adds fifth powerboat
      
      // A 33 ft. blue sail boat with 2 sails
         sailboat.setColour("blue");
         sailboat.setLengthBoat(33);
         sailboat.setNumSails(2);
      
         boat.add(sailboat); // adds sixth SailBoat
      
      // A 14 ft. white power boat with a 10 horsepower engine
         powerboat.setLengthBoat(14);
         powerboat.setColour("white");
         powerboat.setSizeOfEngine(10);
      
         boat.add(powerboat);	//adds seventh powerboat
      
      
      
      
         	//prints out the data 
         System.out.println();
         System.out.println("Print all boats:");
      		
         for(int i = 0; i < boat.size(); i++) 
         {		
                     		
            System.out.println(boat.get(i).toString());
         		
         }//end for
      
      // stil TODO
		
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println();
      
      }// end main
   } // end class Inventory

The problem is in the way you set up your test data.
You create one PowerBoat, set its values, add it to the list. The first element of the list is now a reference to that PowerBoat
You then change the values of that same PowerBoat, and add it to the list again. Now both the elements of the list refer to the same one PowerBoat, which has the latest values.
You can re-use your powerBoat variable, but you have to create a new PowerBoat for each one you want to add to boat.
ditto Sailboat, of course.

Member Avatar for mehnihma

If I got this right
public static PowerBoat powerboat1 = new PowerBoat();
public static PowerBoat powerboat2 = new PowerBoat();

and so on?

Member Avatar for mehnihma

And why I cant get the order right?

If I got this right
public static PowerBoat powerboat1 = new PowerBoat();
public static PowerBoat powerboat2 = new PowerBoat();

and so on?

yes, that's one way that will work

And why I cant get the order right?

Fix the first problem first, then look at the order again.

Member Avatar for mehnihma

yes, that's one way that will work

what is the better way?

Again I get the error

Just one variable for each kind of boat, and re-use it for multiple new boats.

"Again I get the error" I guess you know what you mean, but I don't. The boats will be stored in the list in the order that you add them.

ps: I'm finishing now. See you next week. Good luck
J

Member Avatar for mehnihma

Just one variable for each kind of boat, and re-use it for multiple new boats.

"Again I get the error" I guess you know what you mean, but I don't. The boats will be stored in the list in the order that you add them.

ps: I'm finishing now. See you next week. Good luck
J

How to do it with one variable?

Ok thanks

All the best :D

You can have the same variable refer/point to different objects over time.
You can create an object with a variable and add it to the array list.
Now the array list has the reference to that object.
You can then create a new object with the variable.
pseudo code
begin loop
var = new SomeObject(); // create an object and save its reference in var
... fill in the object at var with values
anArrayList.add(var); // save the reference to the object in the array list
end loop // loop back to reuse var to refer to a new object

when you exit the loop, anArrayList will contain references to many different objects

Member Avatar for mehnihma

Ok I got it now, but dont know how to do it with variable,
I have created an object for every input

but dont know how to do it

Please explain what " to do it" means?

Can you post your code and explain your problems?

Member Avatar for mehnihma

Just one variable for each kind of boat, and re-use it for multiple new boats.

"Again I get the error" I guess you know what you mean, but I don't. The boats will be stored in the list in the order that you add them.

ps: I'm finishing now. See you next week. Good luck
J

whole code is on page 2

I am asking how to do it with just one variable, now I have done it by creating an object for each input

now I have done it by creating an object for each input

Yes that is the way you would do it. For every input, you would create a new object.
But you can use the same variable to refer to each new object.

pseudo code
begin loop
theOnlyVar = new SomeObject(); // create a new object, save its reference in theOnlyVar
... fill in the object at theOnlyVar with values
theOnlyVar.setValues(some values);
anArrayList.add(theOnlyVar); // save the reference to the object in the array list
end loop // loop back to reuse theOnlyVar to refer to a new object

when you exit the loop, anArrayList will contain references to many different objects

Member Avatar for mehnihma

that is same as creating objects for every value, I think

Creating a new object for every new value is a normal way to save all the values.

I'm confused about your problem.

Are you confusing the values of variables and the values of objects? They are two different things and are in two different parts of memory.
The value of a variable can be the address of an object. The variable's value can be changed to have the address of another object. It does not always have to point to only one object.

Member Avatar for mehnihma

I was thinking on a paramerterized constructor but I figured it now I think :D

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.