data structures

Discussion in 'Java' started by periklis.ioannou@gmail.com, Dec 15, 2006.

  1. Guest

    Hi all,

    i have an old exam question that i am just looking at and unfortunately
    i have typed into the compiler and it won't compile. There are to many
    bugs in the program a good friend of mine told me. I merely interested
    to know, what will be printed out and the intentions of the examiner
    for this question. I personally think that both statements will
    printout, but without it compiling it hard to tell. The second
    statement been the color one because it is incrementing! Am i right?

    Cheers,

    Peri.

    Please assume getters and setters methods exist.Here is the classes,
    and the harness class:

    import java.awt.*;
    import java.applet.*;

    public class LaundryItem
    {

    protected String washingInstructions;
    protected Color colour;

    public LaundryItem(String w, Color c)
    {

    System.out.println("Yay, something to wash!!!");
    washingInstructions=w;
    colour=c;
    }

    .........

    public String toString()
    {
    return getWashingInstructions() + "\n" +
    getColour().toString(); //The assumed getter methods
    }

    }

    /---------------------------------------------------------------------------------------------------------------------------------/

    public class WashingMachine
    {
    protected LaundryItem[] contents;
    protected int counter=0;

    public WashingMachine()
    {
    contents=new LaundryItem[10];
    }

    public void add(LaundryItem i)
    {
    contents[counter]=i;
    counter++;
    }

    public String toString()
    {
    String result= "";

    for(int c=0;c<counter; c++)

    {
    result = result + contents[c].toString() + "\n";

    }

    return result;

    }

    }

    /-----------------------------------------------------------------------------------------------------------------------------/

    public class Question1A
    {
    public static void main(String[] args)
    {
    WashingMachine x=new WashingMachine();
    LaundryItem i= new LaundryItem("dry clean only");

    x.add(i);
    x.add(i);
    System.out.println(x.toString());
    }

    }
     
    , Dec 15, 2006
    #1
    1. Advertising

  2. Guest

    It would print


    Yay, something to wash!!!
    dry clean only Red
    dry clean only Green


    But you need to denote its colour in example

    LaundryItem i= new LaundryItem("dry clean only", Color.black);

    you need to sent a colour because of LaundryItem's construction

    --> public LaundryItem(String w, Color c)
     
    , Dec 16, 2006
    #2
    1. Advertising

  3. Guest

    public LaundryItem(String w, Color c)
     
    , Dec 16, 2006
    #3
  4. programming Guest

    wrote:
    > public LaundryItem(String w, Color c)


    I'm sorry i left out some code as i was mucking around with it in the
    compiler. This is the line it is suppose to be:

    LaundryItem i= new LaundryItem("dry clean only", Color.red);

    Printed lines i thought were this:

    Yay, Something to Wash
    dry clean only red

    i am not sure were you got the green one from!

    Cheers,

    Peri
     
    programming, Dec 16, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dennis Gavrilov
    Replies:
    1
    Views:
    1,470
    Dennis Gavrilov
    Jul 24, 2003
  2. MIke Beam

    Data Structures help

    MIke Beam, Oct 10, 2003, in forum: Java
    Replies:
    5
    Views:
    535
    antroy
    Oct 14, 2003
  3. learningjava

    data structures in java reference

    learningjava, Oct 15, 2003, in forum: Java
    Replies:
    3
    Views:
    443
    Samuel Barber
    Oct 16, 2003
  4. tweak
    Replies:
    14
    Views:
    2,818
    Eric Sosman
    Jun 11, 2004
  5. Alfonso Morra
    Replies:
    11
    Views:
    753
    Emmanuel Delahaye
    Sep 24, 2005
Loading...

Share This Page