0

This is part of a homework assignment.

For part of the assignment, I have to make several arrays, pass them to methods, modify them there, and, in general, play with them. Most of this I have done successfully, but I am having problem with the last part. I create four arrays of primitive types by four void methods. I then pass those arrays (and the object array) into another void method that builds an array of objects using the values from the arrays. When I try to use toString to print the state of those objects, it returns the state of the last object in the array every time (through testings, Ive found that after the BuildWine method finishes, the values of the Wine array are all equivalent to the values of the last object created. However, when the toString is printed within the method, it prints correctly.

I'm confused as to why arrays of primitives will be modified correctly when manipulated this way, but an array of objects is changed in such a strange manner (it should either remain blank, give an error, or something, not override everything with the value of the last object created). Thank you for the help.

public class LabSeven {


    public static void main(String[] args) {
        int[] wineAges = new int[5];
        String[] wineNames = new String[5];
        int[] wineQuantity = new int[5];
        double[] winePrice = new double[5];
        Wine[] wineList = new Wine[5]; 

        BuildAges(wineAges);
        BuildNames(wineNames);
        BuildQuantity(wineQuantity);
        BuildPrice(winePrice);
        BuildWine(wineList, wineAges, wineNames, wineQuantity, winePrice);

        for(int i = 0; i < wineList.length; i++){
            System.out.println(wineList[1].toString() + "\n");
        }
    }

    public static void BuildAges(int[] wineAges){
        wineAges[0] = 10;
        wineAges[1] = 23;
        wineAges[2] = 13;
        wineAges[3] = 25;
        wineAges[4] = 50;
    }

    public static void BuildNames(String[] wineNames){
        wineNames[0] = "Chardonay";
        wineNames[1] = "Riesling";
        wineNames[2] = "Merlot";
        wineNames[3] = "Chianti";
        wineNames[4] = "Pinot Noir";
    }

    public static void BuildQuantity(int[] wineQuantity){
        wineQuantity[0] = 10;
        wineQuantity[1] = 14;
        wineQuantity[2] = 4;
        wineQuantity[3] = 7;
        wineQuantity[4] = 1;
    }

    public static void BuildPrice(double[] winePrice){
        winePrice[0] = 100.50;
        winePrice[1] = 75.50;
        winePrice[2] = 45.50;
        winePrice[3] = 200.50;
        winePrice[4] = 1250.50;
    }

    public static void BuildWine(Wine[] wineList, int[]wineAges, String[] wineNames, int[] wineQuantity, double[] winePrice){
        for (int i = 0; i < wineAges.length; i++){
            wineList[i] = new Wine(wineAges[i], wineNames[i], wineQuantity[i], winePrice[i]);
            //System.out.println(wineList[i].toString() +"\n");
        }
    }
}




public class Wine {
    static int age;
    static String type;
    static int quantity;
    static double price;

    public Wine(int wineAge, String wineType, int wineQuantity, double winePrice){
        age = wineAge;
        type = wineType;
        quantity = wineQuantity;
        price = winePrice;
    }

    public String toString(){
        String status = ("Wine's Age: " + age + "\nWine's Type: " + type + "\nWine's Quantity: " + quantity + "\nWine's Price: " + price);

        return status;
    }

}
2
  • mark what you mean in your code Commented Apr 24, 2013 at 23:15
  • Also note that System.out.println(wineList[1].toString() + "\n"); is accessing the same array element every time through the loop. It should be System.out.println(wineList[i].toString() + "\n");. Commented Apr 24, 2013 at 23:29

1 Answer 1

4

In your Wine class, you've marked all attributes as static, i.e. one value for the entire class, no matter how many instances you create. Your constructor is overwriting all values each time it's called.

Remove static from all 4 attributes in Wine, so that there is one value of each for each Wine object you create.

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

2 Comments

This fixed it right away, thank you. I wasn't thinking about the attributes of the Wine object, and assumed the problem was in the LabSeven class. I think I made them static to clear an eclipse warning without thinking about it.
It's a good idea to think about it before making fields static ;-)

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.