Final

advertisement

Name : .....................................................................

ID # : .....................................................................

Comp 240 Final

June 10, 2005

1/7

State clearly all of your assumptions.

Good luck.

Q1.

Check all that apply... (30 pt)

Q1 (30) Q2 (10) Q3 (30) Q4 (30) Sum

1.a.

(5 pt)

Declaring a method final means:

 it will prepare the object for garbage collection.

 it cannot be accessed from outside its class.

 it cannot be overloaded.

 it cannot be overridden.

1.b.

(5 pt)

An interface may contain:

 private static data and public abstract methods.

 only public abstract methods.

 public static final data and public abstract methods.

 private static data and public final methods.

1.c.

(5 pt)

Which of the following is/are possible?

 A class that implements two interfaces.

 A class that inherits from two classes.

 A class that inherits from one class, and implements an interface.

1.d.

(5 pt)

A class that implements an interface but does not declare all of the interface’s methods must be declared:

 public.

 interface.

 abstract.

 final.

1.e.

(5 pt)

Constants declared in an interface are implicitly _______ .

 private.

 static.

 abstract.

1.f.

(5 pt)

Which statements below is/are true?

 Superclass finalizers should always be called as the last statement of a subclass finalizer.

 Superclass constructors should always be called as the first statement of a subclass constructor.

 Finalizers and constructors should always be declared protected so subclasses have access to the method.

 Finalizers should have the modifier keyword “final” in the method declaration.

Name : .....................................................................

ID # : .....................................................................

Q2.

What is the output... (10 pt)

2.a.

(5 pt)

Show the output of running the class Test in the following code: interface A { void print();

} class C {} class B extends C implements A { public void print() { }

} public class Test { public static void main(String[] args) {

B b = new B();

}

} if (b instanceof A)

System.out.println("b is an instance of A"); if (b instanceof C)

System.out.println("b is an instance of C");

2.b.

(5 pt)

What is the output of running the class C. public class C { public static void main(String[] args) {

}

Object[] o = {new A(), new B()};

System.out.print(o[0]);

System.out.print(o[1]);

} class A extends B { public String toString() { return "A";

}

}

} class B { public String toString() { return "B";

}

Comp 240 Final

June 10, 2005

2/7

Name : .....................................................................

ID # : .....................................................................

Q3.

Coding… (30 pt)

3.a.

(3 pt)

What is wrong with the following code segment?

Scanner input = new Scanner( System.in ); double grade = input.nextDouble(); switch (grade / 10) { case 9: case 10:

System.out.println("Letter grade: A"); break; case 8:

System.out.println("Letter grade: B"); break; case 7:

System.out.println("Letter grade: C"); break; case 6:

System.out.println("Letter grade: D"); break; default:

System.out.println("Letter grade: F"); break;

}

3.b.

(5 pt)

Complete the following code segment to display the following random numbers: i.

1000  n  1112 ii.

-1  n  1 iii.

n = 6, 10, 14, 18, 22

Random r = new Random();

// 1000, 1001, 1002, ..., 1112

System.out.println(

// -1, 0, 1

System.out.println(

// 6, 10, 14, 18, 22

System.out.println(

);

);

);

Comp 240 Final

June 10, 2005

3/7

Name : .....................................................................

ID # : .....................................................................

Comp 240 Final

June 10, 2005

4/7

3.c.

(5 pt)

Fill out the following code segment to calculate the sum of two real numbers and print out the operation details as shown in the sample output:

(You may assume that these numbers are smaller than 100, and have only two digits after the decimal point)

14.23

+ 9.56

------

23.79

Scanner input = new Scanner( System.in ); double a = input.nextDouble(); double b = input.nextDouble();

System.out.printf(

System.out.printf(

System.out.printf(

System.out.printf(

);

);

);

);

3.d.

(7 pt)

Write a method average that returns the average of a list of integer numbers where the length of the list may be unknown. The calculation should only consider the positive integers in the list. That is, the average of the list “5, -3, 0, 2” is calculated as “3.5”

The method header is given for your convenience. public double average( int... list )

Name : .....................................................................

ID # : .....................................................................

Comp 240 Final

June 10, 2005

5/7

3.e.

(10 pt)

Complete the following code segment to discover the maximum element of a two-dimensional array. The array elements are non-negative integer numbers.

The test application that will utilize this method is also given below. public class ArrayOperations { public int maximumElement( )

} // end of class ArrayOperations

} public class ArrayTest { public static void main(String[] args) {

ArrayOperations mat = new ArrayOperations(); int m1[][] = { {3, 6, 7}, {5, 2, 5} };

System.out.println( mat.maximumElement( m1 ) );

}

Name : .....................................................................

ID # : .....................................................................

Comp 240 Final

June 10, 2005

6/7

Q4.

Programming… (30 pt)

Warning:

 You will NOT receive any partial credit if your code does not compile correctly.

 Read the whole question before starting to solve the problem.

Write an application named “PieTest.java” to draw a pie having equal-sized slices.

4.a.

PiePanel class (4 pt)

Create a class “PiePanel” which extends JPanel, that will perform the actual drawing. This class should hold the number of slices as an instance variable.

You should provide a corresponding constructor, that takes the number of slices as an argument. Here, it is evident that the pie should have at least two slices.

Comments: (do not fill this space)

4.b.

get and set methods (3 pt)

Create corresponding get and set methods for the instance variables.

Comments: (do not fill this space)

4.c.

paintComponent method (15 pt)

Create the paintComponent method that will actually draw the pie together with its equal-sized slices.

4.c.i.

Resizeable pie (6 pt)

Whenever the panel is resized, the pie should also grow or shrink to fit into the frame containing the panel.

The borders of the pie should touch exactly to the borders of the panel. Moreover, it must be centered both horizontally and vertically.

Comments: (do not fill this space)

4.c.ii.

Slices (9 pt)

Draw equal-sized slices that divide the pie. Each slice must have the same angle. The number of slices is determined by the instance variable.

Comments: (do not fill this space)

Name : .....................................................................

ID # : .....................................................................

Comp 240 Final

June 10, 2005

7/7

4.d.

PieTest application (8 pt)

Create an application “PieTest” that utilizes this PiePanel.

First, ask the user with an input message dialog “how many slices” the pie should have. Afterwards, create a frame that will hold an instance of the PiePanel which is created using the user’s input.

Show the frame, and test whether your sliced pie can be resized correctly.

Comments: (do not fill this space)

Hint:

Use sin(  ) and cos(  ) functions to determine the end-points of “slice” lines. r

(x, y) = ( r cos  r sin  r sin (  )

 r cos (  )

Sample outputs:

Download