Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

problems with arrays program

Base Class

public class Item
{
private String description; //variable to store the name
private double dCost; //variable to store the price


//Constructor to set the item
//Data members description and dCost are set according to the parameters
//Postcondition: description = name; dCost = price;
public Item(String names, double costs)
{
names = description;
costs = dCost;

}
//Method to return the name
//Postcondition: The value of description is returned
public String getName()
{
return description;
}
//Method to return the price
//Postcondition: The value of dCost is returned
public double getPrice()
{
return dCost;
}
//Method to set the item
//Data members description and dCost are set according to the parameters
//Postcondition: description = name; dCost = price;
public void setItem(String names, double costs)
{
description = names;
dCost = costs;

}
//Method to print the name of the item
public String printName()
{
return (description);
}
//Method to print the price of the item
public double printPrice()
{
return (dCost);
}
//Method to print the Item
public String printItem()
{
return (description + " " + dCost);
}

}



Derived class:

public class MallItem extends Item
{

private Item []names;
private Item []costs;
private int mallStoreNumber;

public MallItem(String names, double costs, int stores)
{
super(names, costs);
mallStoreNumber = stores;
}

public void printInfo()
{
System.out.println(super.toString());
System.out.println(mallStoreNumber);
}

public void setInfo(String names, double costs, int stores)
{
super.setItem(names,costs);
mallStoreNumber = stores;
}

public int getStoreNumber()
{
return mallStoreNumber;
}

public void setMallStoreNumber(int stores)
{
mallStoreNumber = stores;
}

}



import java.io.*;

public class MallList
{
private int length;
int maxSize = 5;
private Item[] names;
private Item[] costs;
private MallItem list[];

public MallList(String[] names, double[] costs, int[] stores)
{
list = new MallItem[5];
for(int i = 0; i < 5; i++)
list[i] = null;

length = 0;

}

public void print()
{
for(int i = 0; i < list.length;i++)
{
list[i].printInfo();
}
}

public boolean isFull()
{
return (length == maxSize);
}

public void insert(MallItem mI)
{
list[length] = mI;
length++;
}


}

Testing class
import java.io.*;

public class TestListStudent
{
public static void main (String [] args)
{
String[] names = {"jeans", "shirt", "boots"};
double[] costs = {39.99, 24.75, 105.00};
int[] stores = {16, 29, 3};
MallList listOne = new MallList(names, costs, stores);


System.out.println("Is the list full? " + listOne.isFull());//check to see if list is full
listOne.print();
}
}

Can someone please tell me what I'm doing wrong or give me examples to give me ideas on how to correct what I'm doing wrong. I know I must not be setting the array objects up right. Not sure if my constructor is right in my MallList class.

I have much more to do to this program...I just want to get to a point where it is printing the items out as follows:(formatting need not be an issue, I already know how to do that.)
jeans 39.99 16
shirt 24.75 29
boots 105.00 3
[4074 byte] By [schnag] at [2007-11-11 10:31:04]
# 1 Re: problems with arrays program
Yup. You're missing out some steps, here.

Can you explain in simple English what you are trying to design in creating your MallList class? What is the basic data this class is operating on? How are you holding that basic data so that you can do stuff with it as the need arises? These considerations will lead you to good design.

Your MallList constructor is not constructing anything other than an empty array of null MallList objects.

Think about changing the names of either your data fields or your parameters/arguments. Just because you use a variable name in a class (as an argument to its constructor) doesn't include the data referred to by that name into the class. You need to provide a mechanism for the class to "own" that data.

Remember to create the array(s) which will hold objects, create each object which will be stored in the array, and then add the object into the array.

All that said, it looks to me that you want to create a list (implemented as an array) of MallItem objects. Not an array of names and an array of prices and an array of stores that are organized under a name. This is an application of a basic tenet of object oriented programming.
nspils at 2007-11-11 22:31:23 >
# 2 Re: problems with arrays program
I got it working thanks for your help...Just working on somemore methods with it now.
schnag at 2007-11-11 22:32:34 >