[java hw help] printing objects that are not stored

Cheetoz

[H]ard|Gawd
Joined
Mar 3, 2003
Messages
1,972
the test class has
Code:
		a.add("toothbrush", 3.99);
		a.add ("paste", 7.79);

And in the end, I need to print everything that has been added,
toothbrush............$3.99
paste....................$7.79

I created the class with such
Code:
    void add(String name, double cost)
    {
        this.name = this.name + "\n" + name;
        this.cost = cost;
    }

And while that sort of works for the name, although still wrong, it won't work for the cost. I'm stumped. Can anyone point me in the right direction?
 
So what you need to do is store things, yes? Or are you not allowed to store them? That seems like a silly limitation.

I'd suggest you use some sort of data structure (like two List<T>s, say) to store the items. Then when the item adding is done, traverse the lists and output the items and costs in order.
 
Can you show us more code? The fact there's an 'add' method in the example would imply you're adding it to something that stores items. For a problem like this, I'd think the simplest approach would be to create a class that represents an item and store it in a sequential container, like java.util.ArrayList.
 
We haven't learned those other things, but I found out I do have use toString. But looking at my notes, it used for objects, and reading online it states it represents an object.

I don't know, we weren't really taught to use it. I have tried this

Code:
    void add(String name, double cost)
    {
        this.name = name;
        this.cost = cost;
        toString(name, cost);
    }
    
    static String toString(String name, double cost)
    {
        return name + "........" + cost;
    }

and then the testClass would have something like
Code:
    String a = Cashier.toString();
            
            
	public static void main(String[] arg)
	{
		Cashier a = new Cashier();
		a.add("item1", 5.26);
		a.add ("item2", 65.27);
		System.out.println(a);
       }

so ideally would be when add() is ran, it takes the give name and cost that it received, and hands it over to String toString(), which would then return its values when called by String c = Cashier.toString(); in the test class.

But, I guess thats not how it works.
 
I think you need to get clarification of the assignment. Are you supposed to keep track of the items individually and then join together a string at the end, or build up a string as you go?
 
We haven't learned those other things, but I found out I do have use toString.

toString() is a public member of of java.lang.Object, the root of the Java class hierarchy. All classes extend Object or another class that extends Object somewhere in its inheritance). If you don't know what that means, its ok don't fret!

To override the toString() method you need to create a method with the following signature in your class.

public String toString()

notice, no parameters and its not static. The cool part about this is you can now use the an object of your class any place where the JVM would expect a String input and it will automatically call the toString() method.

Code:
public class MyClass {

    @Override
    public String toString(){
        return "I am an Object of myClass";
    }

    public static void main(String args[]){
        MyClass myObject = new MyClass();
        System.out.println(myObject);
    }

}

Output:
Code:
>java MyClass
I am an Object of myClass

In order to make use of this though you need to first figure out the whole data structure thing. BillLeeLee pointed you in the right direction. Once you have done that you can iterate over the list and print out the objects.

Can you show us more code? The fact there's an 'add' method in the example would imply you're adding it to something that stores items. For a problem like this, I'd think the simplest approach would be to create a class that represents an item and store it in a sequential container, like java.util.ArrayList.
 
Boy, just missing one day of class created such a disaster. I finally figured out how it works. Thanks for everyones help.
 
Back
Top