Marshall Tigerus
Marshall Tigerus

Reputation: 3764

Array Modification in methods

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

}

Upvotes: 0

Views: 131

Answers (1)

rgettman
rgettman

Reputation: 178253

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.

Upvotes: 4

Related Questions