Output
Last Name ______________________ First__________________ Date: 9/15/08
1. Given MyInterface and two classes that implement it, write the output generated by the
program in the TestOneAndTwo class.
// In file MyInterface.java
public interface MyInterface
{
public String mystery( );
}
// In file One.java
public class One implements MyInterface
{
private String id;
public One( String ID )
{
id = ID;
}
public String mystery( )
{
return "> " + id + " <";
}
}
// In file Two.java
public class Two implements MyInterface
{
public String mystery( )
{
return "different";
}
}
// In file TestOneAndTwo.java
public class TestOneAndTwo
{
public static void main( String[] args )
{
MyInterface one = new One( "Kim" );
MyInterface anotherOne = new One( "Devon" );
MyInterface two = new Two( );
System.out.println( one.mystery( ) );
System.out.println( anotherOne.mystery( ) );
System.out.println( two.mystery( ) );
}
}
Practice Interface & Classes
2. What would be the output of the following program (line for line)?
public class MyClass
{ public int myInt = 42;
public int[] myIntArray = {1, 2, 3, 4};
public MyClass myNext = null;
public String toString()
{ String result = new String("");
result += myInt;
for (int i = 0; i<4; i++)
result +=(" " + myIntArray[i]);
return result;
}
public static void main(String[] argv)
{ MyClass inst1 = new MyClass();
MyClass inst2 = new MyClass();
MyClass inst3 = inst1;
inst3.myNext = inst1;
inst2.myNext = inst3;
inst1.myIntArray[0] = 9;
inst2.myIntArray[1] = 11;
inst3.myIntArray[2] = 13;
inst1 = inst2;
inst2.myNext = inst1;
inst3.myNext = inst1.myNext;
inst1.myNext.myIntArray[3] = 15;
inst2.myNext.myInt =21;
inst3.myNext.myInt= 23;
System.out.println(inst1);
System.out.println(inst2);
System.out.println(inst3);
}// end main
}// end class
3. Completely write a class named QuizAverager that implements QuizAveragerInterface . A
QuizAverager object must maintain the count and average of a set of quiz scores. The test driver on the
back of this page must generate the output shown in comments.
public interface QuizAveragerInterface
{
// Add a quiz so the count and average can be maintained
public void add( double test );
// Return the average of all quizzes so far (may result in division by 0)
public double getAverage( );
// Return the number of quizzes added so far
public int getCount( );
}
public class TestQuizAverager
{ // Test driver
public static void main(String[] args)
{
QuizAverager qa = new QuizAverager( );
System.out.println( qa.getCount( ) ); // 0
qa.add( 20.0 );
System.out.println( qa.getCount( ) ); // 1
System.out.println( qa.getAverage( ) ); // 20.0
qa.add( 11.0 );
System.out.println( qa.getCount( ) ); // 2
System.out.println( qa.getAverage( ) ); // 15.5
}
}
Answers
1.
> Kim <
> Devon <
different
2.
23 1 11 3 15
23 1 11 3 15
42 9 2 13 4
3.
public class QuizAverager implements QuizAveragerInterface
{
private int n;
private double total;
public QuizAverager( )
{
n = 0;
total = 0.0;
}
public double getAverage( )
{
return total / n;
}
public int getCount( )
{
return n;
}
public void add( double test )
{
n = n + 1;
total = total + test;
}
}
Powered by TCPDF (www.tcpdf.org)