data structures

P

periklis.ioannou

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

}
 
E

Emre.Simtay

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)
 
P

programming

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top