Reputation: 11
public class Test10
{
public static void main( String[] args )
{
Thing2 first = new Thing2( 1 );
Thing2 second = new Thing2( 2 );
Thing2 temp = second;
second = first;
first = temp;
System.out.println( first.toString() );
System.out.println( second.toString() );
Thing2 third = new Thing2( 3 );
Thing2 fourth = new Thing2( 4 );
third.swap1( fourth );
System.out.println( third.toString() );
System.out.println( fourth.toString() );
second.setCount( fourth.getCount() );
third = first;
System.out.println( third == first );
System.out.println( fourth == second );
System.out.println( first.toString().equals( third.toString() ) );
System.out.println( second.toString().equals( fourth.toString() ) );
System.out.println( first.toString() );
System.out.println( second.toString() );
System.out.println( third.toString() );
System.out.println( fourth.toString() );
first = new Thing2( 1 );
second = new Thing2( 2 );
first.swap2( second );
System.out.println( first.toString() );
System.out.println( second.toString() );
}
}
class Thing2
{
private int count;
public Thing2( int count )
{
this.count = count;
}
public int getCount()
{
return this.count;
}
public void setCount( int count )
{
this.count = count;
}
public String toString()
{
String s = " ";
switch( this.count )
{
case 1:
s = s + "first ";
case 2:
s = s + "mid ";
break;
case 3:
s = s + "last ";
break;
default:
s = s + "rest ";
break;
}
return s;
}
public void swap1( Thing2 t2 )
{
int temp;
temp = this.getCount();
this.setCount( t2.getCount() );
t2.setCount( temp );
}
public void swap2( Thing2 t2 )
{
Thing2 temp;
Thing2 t1 = this;
temp = t1;
t1 = t2;
t2 = temp;
}
}
Given the following definition of class Thing2, what is the output of the Java application Test10?
Hey guys, this is for one of my classes. There are two classes (listed above), Thing2 and Test10. This is the output, but I don't understand how to get to the output (i.e. what points to what and what's the order everything gets resolved in?). Thanks!
mid
first mid
rest
last
true
false
true
true
mid
last
mid
last
first mid
mid
Upvotes: 0
Views: 195
Reputation: 4462
3 suggestions:
The case 1 in switch doesn't contain a break. So, when case 1 is satisfied the control will flow to case 2 and will even execute the case 2.
Use some print statements so that you know what you are printing on console.
Your question is not exact. You have the code. You have the output. And you are saying you don't understand how to get to the output. May be you need to read through the code and understand how the control flows in a java program. If you are finding some particular control flow construct then we can help. With what you have told we don't know what problem you are facing. Still then, I will suggest you the "Divide and Rule" strategy. Comment out everything and execute small logical pieces of code, when you understand the first small part, uncomment a few lines, run and see the output, then may be you can relate what's getting executed and what is getting printed. Example: just deal with one swap function first.
Upvotes: 1
Reputation: 3225
One trick is that case 1
in your switch statement does not have a break statement, so the code continue to the case 2
instruction and creates the string "first mid" instead of "first" for Thing(1)
it might help to add comments to your print statements like this
System.out.println("First is " + first.toString());
System.out.println("third == first?" + (first == third));
One you do that, add more print statements.
Think of each variable as a big arrow.
The statement a = new Thing(2)
makes a Thing with the word "mid" and points the a
arrow at it.
The statement a = b
take whatever the b
arrow was looking at and points the a
arrow at the same thing.
Upvotes: 2