Your demo code is required to (DO NOT manually print out, write a FOR loop to do it)
1. print all white furniture

  1. print the total cost of all furniture in the array. // How do i print these using my code:

    public class FurnitureDemo
    {
    public static void main (String[] args)
    {
    Furniture [] f = new Furniture [6];

        f[0] = new Chair (100, 1.56, "white", 1.5, 3);
        f[1] = new Chair (55, 2.52, "black", 2, 3);
        f[2] = new Table (300, 2.53,  "gray", 4, 3, 2.6);
        f[3] = new Table (220, 3.5, "brown",5, 3, 2.5);
        f[4] = new Table (250,4.5, "white", 5, 4, 2.6);
        f[5] = new Round_Table (500,5.5,"white",1.5, 2.4);
    
        for(int i=0;i<f.length;i++)
        {
            System.out.println(f[i].toString());
        }
    
    
    }
    

    }

To print only the "white" furniture, use an if statement inside your for loop to check if the color of f[i] is equal to "white". If it is, print out what you need to print.

To print the total cost, create a variable of type int before you start your for loop and set it to 0. Inside the for loop, add the price of f[i] to that variable. Then after the loop finishes, print out the variable.

I could have given code samples, but I don't know what's inside the Furniture classes or the other classes. Anyway, it's nothing too difficult and hopefully this answer can help you get it running yourself.

Here are the Classes, a code sample would be nice and greatful. thanks!!

public class Furniture implements Comparable
{
    private double price;
    private double weight;
    private String color;


/*
    public Furniture()
    {
        price = 0.0;
        weight = 0.0;
        color = "";
    }*/

    public Furniture(double p, double w, String c)
    {
        price = p;
        weight = w;
        color = c;
    }

    public double getPrice()
    {
        return price;
    }

    public double getWeight()
    {
        return weight;
    }

    public String getColor()
    {
        return color;
    }

    public String toString()
    {
        String str =  "Price: " + price + "\nWeight: " + weight + "\nColor: " + color;

        return str;
    }

    public boolean IsCheaper(Furniture f)
    {

        boolean test = false;
        for(int i = 0; i < array1.size() && !test; i++)
        {
        if(array1.get(i) == (array.get(i)))
        {
        test = true;
    }

}
----------------------------------------------------------------------------------
public class Table extends Furniture
{
    private double length;
    private double width;
    private double height;

    public Table(double p, double w, String c, double l, double wid, double h)
    {
        super(p,w,c);
        length = l;
        width = wid;
        height = h;
    }

    public double getLength()
    {
        return length;
    }

    public double getWidth()
    {
        return width;
    }

    public double getHeight()
    {
        return height;
    }

    public String toString()
    {
        String str = super.toString() + "\nLength: " + length + "\nWidth: " + width + "\nHeight: " + height;

        return str;
    }
}
-----------------------------------------------------------------------------
public class Chair extends Furniture
{
    private double radius;
    private double height;

    public Chair(double p, double w, String c, double r, double h)
    {
        super(p,w,c);
        radius = r;
        height = h;
    }

    public double getRadius()
    {
        return radius;
    }

    public double getHeight()
    {
        return height;
    }

    public String toString()
    {
        String str = super.toString() + "\nRadius: " + radius + "\nHeight: " + height;

        return str;
    }
}
-----------------------------------------------------------------------------
public class Round_Table extends Furniture
{
    private double radius;
    private double height;

    public Round_Table(double p, double w, String c, double r, double h)
    {
        super(p,w,c);
        radius = r;
        height = h;
    }

    public double getRadius()
    {
        return radius;
    }

    public double getHeight()
    {
        return height;
    }

    public String toString()
    {
        String str = super.toString() + "\nRadius: " + radius + "\nHeight: " + height;

        return str;
    }
}

Pretty much exactly what I thought. All it required was a simple if statement and adding to the price each time.

double price = 0;
for (int i = 0; i < f.length; i++) {
    if (f[i].getColor().equals("white") {
        System.out.println(f[i].toString());
    }
    price += f[i].getPrice();
}
System.out.println("Total price: " + price);

gavinflud:

Here at DaniWeb we try to help people learn Java and develop their Java skills. We do NOT do people's homework for them. In future please help by pointing people in the right direction - eg tell them which classes and methods they should read about, or give them some sample code that they will have to understand and adapt to thier needs. Don't just spoon-feed them a solution to copy and paste.

Here at DaniWeb we try to help people learn Java and develop their Java skills. We do NOT do people's homework for them. In future please help by pointing people in the right direction - eg tell them which classes and methods they should read about, or give them some sample code that they will have to understand and adapt to thier needs. Don't just spoon-feed them a solution to copy and paste.

To be fair, if you had read my original post, that's exactly what I did. He then PM'd me saying he'd included the other classes and that this was due for 12 o'clock. I assumed (since I did not post this until 9 hours after he PM'd me) that I could post the answer to the problem for others to see how it was solved, since it was likely of little use to oneoderja at that stage.

OK.
We do try to discourage people from using PMs as part of normal support activity. When we only see part of the conversation it's easy to become confused or arrive at the wrong conclusion.
Enough said. J.

Sorry about that, I can see how it would look like I was just spoon-feeding him the answers. By the time I seen that he had PM'd me it was 9 hours afterwards, so I assumed this answer would be of little use to him anyway. I was more leaving it there so the thread wouldn't remain unresolved for other users.

This article has been dead for over six months. Start a new discussion instead.