0

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();

    }



4
  • The second part of a for clause indicates when the loop should continue, not when it should stop. Consider i < 4 rather than i >= 4. Commented Feb 5, 2022 at 18:37
  • probably you'll be interested in reading this docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html Commented Feb 5, 2022 at 19:01
  • It's obvious that the termination condition of the for loop is alwais false. But there's no clue what issues do you have with the display() method? How it's all connected with the OOP? Commented Feb 5, 2022 at 19:12
  • Please don't deface your question. Commented Feb 15, 2022 at 22:56

2 Answers 2

2
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.

Sign up to request clarification or add additional context in comments.

5 Comments

i have changed my code however it still does not display the stuff inside the display method
Can you share the screenshot of your output
I Have updated my question to contain the display method I was calling and also added the screenshot
I am not able to see the screenshot
hi could you please check again i believe it has now gone through
2

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.