Home > OS >  Java - OOP array of objects and display them
Java - OOP array of objects and display them

Time:02-06

I have created an array of objects(cups) which I have set a volume and capacity and colour too, all of this is displayed by calling .display() However I can get the display() to do this in this instance when trying to loop through the array of cups. I get no output to show the cups capacity and colour from the display() method, I have added the two bits of code, one in the cups class that contains the display method and the first one which shows me trying to create an array and display each cup. I would deeply appreciate any help as this is a part of a school assignment.

screenshot of current output

ScreenShot of output

    public static void main(String[] args) 
    {

        //Makes Array of cups
        Cup []  Cups = new Cup[4];

        // Creates 4 Cups with 250 capacity and black colour
        for(int i = 0 ; i > 4 ; i  ) 
        {
            Cups[i] = new Cup (250, ContainerColour.BLACK );
            Cups[i].display();
        }
        
        //Teapot, Wash and then fill with tea
        TeaCup myTeaCup = new TeaCup();
        
        myTeaCup.Wash();
        myTeaCup.Fill();
        myTeaCup.display();
     }


my cups oop code :


    // ---------------------------------------------------------------------
    // class variables
    // ---------------------------------------------------------------------

    private ContainerColour myColour;
    private int iCapacity;
    private int iVolume;
    private int iTemp;
    private int iFill;
    private String SzMaterial;

    private Boolean bEmpty;

    // ---------------------------------------------------------------------
    // Constructors
    // ---------------------------------------------------------------------

    public Cup()
    {
        super();
        reset();
        return;

    }

    public Cup (Cup original) {
        iCapacity = original.getCapacity();
        iVolume = original.getVolume();
        SzMaterial = original.getMaterial();
    }

    public Cup(int size) 
    {
        this();
        super.setCapacity(size);
        setColour(ContainerColour.NOT_SET);
        return;
    }

    public Cup (int size , ContainerColour colour)
    {
        this (size);
        setColour(colour);
        return;
    }


    //setters

    public void setColour(ContainerColour c) 
    {
        myColour = c;
    }

    //getters


    public ContainerColour getColour()
    {
        return(this.myColour) ;
    }


    //main

    public void display() 
    {

        System.out.println("Cup: \nCapacity = "   getCapacity());

        System.out.println("Volume = "   getVolume());
        System.out.println("Colour = "   getColour() );
        if(iVolume == 0) {
            bEmpty = true;
        }
        else {
            bEmpty = false;
        }

        System.out.println("Empty = "   bEmpty );
        return;
    }


    public static void main(String[] args) {

        Cup myCup = new Cup();
        myCup.setCapacity(250);
        myCup.setColour(ContainerColour.WHITE);
        myCup.Wash();
        myCup.fill();
        myCup.display();

    }



CodePudding user response:

public static void main(String[] args) 
{

    //Makes Array of cups
    Cup []  Cups = new Cup[4];

    // Creates 4 Cups with 250 capacity and black colour
    for(int i = 0 ; i < 4 ; i  ) 
    {
        Cups[i] = new Cup (250, ContainerColour.BLACK );
        Cups[i].display();
    }
    
    //Teapot, Wash and then fill with tea
    TeaCup myTeaCup = new TeaCup();
    
    myTeaCup.Wash();
    myTeaCup.Fill();
    myTeaCup.display();
 }

So if of all you made mistake in coding loop it should be less than 4 and secondly array index start with zero it means when you reach 3 you will iterate all objects in array Cups.

CodePudding user response:

Instead of hard-coding the 4, use the actual array itself to determine the length and use that in your for loop:

for(int i=0; i<Cups.length; i  ) {
    Cups[i] = new Cup (250, ContainerColour.BLACK );
    Cups[i].display();
}
  •  Tags:  
  • Related