1. This programming test is testing the ability of using variant-length argument and processing arrays.

Description

Complete the following class named ProgTest1 such that the class provides an instance method continue10. Calling continue10 with two or more int-type arguments will return true if there exist two or more consecutive 10 in the arguments.

public class ProgTest1 {

public static void main(String… args) {

ProgTest1 obj = new ProgTest1();

System.out.println(“The result of 10, 9, 10, 10 is “ +

obj.continue10 (10, 9, 10, 10));

System.out.println(“The result of 10, 8, 10, 7 is “ +

obj.continue10 (10, 8, 10, 7));

System.out.println(“The result of 10, 9, 10, 1, 10 is “ +

obj.continue10 (10, 9, 10, 1, 10));

}

// insert the class method continue100 here

}

Testing

Executing the class will output the following result.

The result of 10, 9, 10, 10 is true

The result of 10, 8, 10, 7 is false

The result of 10, 9, 10, 1, 10 is false

  1. This programming test is testing the ability of solving problem by using recursion.

Description

Complete the following class named ProgTest2 such that the class provides a class method sumDigits. Calling sumDigitswith a non-negative int-argument will return the sum of digits forming the argument. You must use recursive method. It means that you cannot use for-loop, while-loop and do-while loop. Note that the expression n % 10 yields the rightmost digit (126 % 10 is 6), while the expressionn / 10 removes the rightmost digit (126 / 10 is 12).

public class ProgTest2 {

public static void main(String… args) {

System.out.println(“The result of 126 is “ + sumDigits(126));

System.out.println(“The result of 49 is “ + sumDigits(49));

System.out.println(“The result of 12 is “ + sumDigits(12));

}

// insert the class method sumDigits here

}

Testing

Executing the class will output the following result.

The result of 126 is 9

The result of 126 is 13

The result of 12 is 3

  1. This programming test is testing the ability of using nested class to hide the detail of a internal used class.

Description

A polynomial is the sum of one or more terms. Each term consists of three information: the coefficient, the variable and the exponent, such as . Hence, a polynomial can be represented by using a 1-dimensional array in which each element saves a term.

Requirements

A. Design a class named Polynomial consisting of the following members.

(1) a private class constant MAX_NO_TERMS equal to 10

(2) aprivate inner class named Term which will be described later

(3) a private instance variable pointing to an array with Term element type: this array is used to save the terms of a polynomial

(4) a constructor without argument to create a Polynomial object to represent a polynomial with MAX_NO_TERM terms.

(5) a constructor consuming a two dimensional array (say poly: poly(0,0) and poly(0,1) representing the coefficient and exponent of the first term, poly(1,0) and poly(1,1)the coefficient and exponent of the second term, and so on) and converting the contents of the array to a Polynomial object.

(6) an instance method addTerm(intcoef, int exp) to insert a term to the Polynomial object. (You do not need to consider the case that the number of terms is larger than the array size.)

(7) an instance method evaluate(intval) to return the result of the Polynomial object if the variable is substituted by val.

B. The Term class consists of the following members.

(1) two private instance fields representing the coefficient and exponent of a term

(2) a constructor consuming two int arguments, where the first one is the coefficient of the term and the second one is the exponent.

(3) setters and getters for the instance fields

C. Write a Java source program including the class Polynomial and the following class ProgTest3.

public class ProgTest3 {

public static void main(String… args) {

Polynomial poly1 = new Polynomial(), poly2;

inttestPoly[][] = {{2, 5}, {1, 3}, {3, 1}, {4, 0}};

poly2 = new Polynomial(testPoly);

poly1.addTerm(5,2);

poly1.addTerm(4,0);

System.out.println(“The value of poly1 with x = 2 is “ +

poly1.evaluate(2));

System.out.println(“The value of poly2 with x = 2 is “ +

poly2.evaluate(2));

}

}

Testing

Executing ProgTest3 will output the following result.

The value of poly1 with x = 2 is 24

The value of poly2 with x = 2 is 82

  1. The factorial of a number N is the product of all the whole numbers between 1 and N. For example, 3 factorial is 1 * 2 * 3 = 6. Please write a program that can compute the factorial of a number. Your program should include a class and two different methods: a recursive method and an iterative method to compute the factorial.

The sample output is shown as follows:

  1. Given a polynomial function f(x) = 3x2+2x+1.Write a Java program that inputs an integer number x from the user and prints the value f(x).

Sample input / Sample output
3↵ / 34↵
10↵ / 321↵
  1. Write an application that uses an inner class to compute various values for its enclosing class. Given a driver class NestedClassTest and an outer class outer in which the inner class innershould be inserted. The inner class has the method avg that calculates the average value respectively. The output is shown as follows.

Output
Average: 4↵

class Outer {

intnums[];

Outer(int n[]) {

nums = n;

}

void analyze() {

Inner inObj = new Inner();

System.out.println("Average: " + inObj.avg());

}

// insert an inner class here

}

classNestedClassTest {

public static void main(String args[]) {

int x[] = { 2, 0, 1, 4, 9, 6, 8, 7 };

Outer outObj = new Outer(x);

outObj.analyze();

}

}

  1. Write a program that performs the following specified action.

(1)Declare the headers for the classes in the following class diagram.

(2)Declare toString methods for all the classes declared in action(1). Class Shape’s toString method shouldreturn the string "Shape". The toString method of each of the subclasses in the hierarchy should return astring containing the class’s name, the string " is a " and the result of a call to the superclass’stoStringmethod.

(3)Giventhe following application that creates one object of each of the classes Circle, Square, Sphere and Cube, andinvokes their toString methods. The output of the application is shown as follows.

Output
Circle: Circle is a TwoDimensionalShape is a Shape↵
Circle's area is 4.91↵
Square: Square is a TwoDimensionalShape is a Shape↵
Square's area is 6.25↵
Sphere: Sphere is a ThreeDimensionalShape is a Shape↵
Sphere's area is 176.71↵
Sphere's volume is 220.89↵
Cube: Cube is a ThreeDimensionalShape is a Shape↵
Cube's area is 150.00↵
Cube's volume is 125.00↵

public class ShapeTest

{

private Shape shapeArray[];

publicShapeTest()

{

shapeArray = new Shape[ 4 ];

shapeArray[ 0 ] = new Circle( 22, 88, 1.25 );

shapeArray[ 1 ] = new Square( 71, 96, 2.5 );

shapeArray[ 2 ] = new Sphere( 8, 89, 3.75 );

shapeArray[ 3 ] = new Cube( 79, 61, 5.0 );

} // end ShapeTest constructor

public void displayShapeInfo()

{

for ( inti = 0; ishapeArray.length; i++ )

{

System.out.printlnln(shapeArray[ i ].getName() +shapeArray[ i ] );

if ( shapeArray[ i ] instanceofTwoDimensionalShape )

{

TwoDimensionalShape current = ( TwoDimensionalShape ) shapeArray[ i ];

System.out.printf( "%s's area is %.2f\n", current.getName(), current.area() );

} // end if

if ( shapeArray[ i ] instanceofThreeDimensionalShape )

{

ThreeDimensionalShape current = ( ThreeDimensionalShape ) shapeArray[ i ];

System.out.printf( "%s's area is %.2f\n%s's volume is %.2f\n", current.getName(), current.area(), current.getName(), current.volume() );

} // end if

} // end for

} // end method displayShapeInfo

// create ShapeTest object and display info

public static void main( String args[] )

{

ShapeTest driver = new ShapeTest();

driver.displayShapeInfo();

} // end main

} // end class ShapeTest

  1. Write a program that prompts the user for a value greater than 10 as an input(you should loop until the user enters a valid value) and finds the square root of that number and the square root of the result, and continues to find the square root of the result until we reach a number that is smaller than 1.01, The problem should output how many times the square root operations was performed.
  1. Consider a frame of bowling pins shown below, where each * represents a pin:

*

* *

** *

* * * *

* * * * *

There are five rows and a total of fifteen pins.

If we had only the top four rows then there would be a total of 10 pins.

If we had only the top three rows then there would be a total of 6 pins.

If we had only the top two rows then there would be a total of 3 pins.

If we had only the top rows then there would be a total of 1 pin.

Write a recursive function that takes as input the number of rows n and outputs the total number of pins that would exist in a pyramid with n rows. Your program should allow for values of n that are larger than 5.

  1. Write a class CA containing a method prime(int X) to find all primes less than X. Write another class CB inheriting from CA and add a method output() in CB to display all primes returned by prime( int X). Your program should allow a input number less than 65535.
  1. Given the definition of a 2D array such as the following,

String [] [] data = {

{“A”, “B”},

{“1”, “2”},

{“XX”, “YY”, “ZZ”}

};

Write a recursive program that outputs all combinations of each subarray in order. In the previous example, the desired output is

A 1 XX

A 1 YY

A 1 ZZ

A 2 XX

A 2 YY

A 2 ZZ

B 1 XX

B 1 YY

B 1 ZZ

B 2 XX

B 2 YY

B 2 ZZ

Your program should work with arbitrarily sized arrays in either dimension. For example, the following data

String [] [] data = {

{“A”},

{“1”},

{“2”},

{“XX”, “YY”}

};

should output

A 1 2 XX

A 1 2 YY

  1. This programming test is testing the ability of using inheritance and exception handling.

Description

There are two types of clocks, 12-hour and 24-hour. The 12-hour clocks are a time convention in which that the 24 hours of a day are divided into two periods, am and pm, and each period consists of 12 hours numbered. No matter a clock is 12-hour or 24-hour, a clock is characterized by hour, minute and second, setters and getters for these attributes and other services such as equals, hashCodeandtoString. To simplify the coding, you do not need to override the hashCode method. A 12-hour clock object needs an additional attribute, the am/pm period.

Requirements

A. Design an abstract class named Clock fulfills the following requirements.

(1) a instance field with int-type named hour, which can be inherited by any class but not accessible by class not in the same package, to represent hour of a clock

(2) an instance field with int-type named minute, which can be inherited by any class but not accessible by class not in the same package, to represent minute of a clock

(3) an instance field with int-type named second, which can be inherited by any class but not accessible by class not in the same package, to represent second of a clock

(4) a constructor without argument to set the hour, minute and second to 0.

(5) a constructor with three int-type parameters representing the initial value of the fields hour, minute and second. Note that if the initial value for any field is not legal, such as the value for minute is less than 0 or greaer than 60, set the initial value as 0.

(6) setters and getters for all the instance fields. Note that if the value to be set for any field is not legal, throw an InvalidValuesException with error message “Invalid value setting”.

B. Design a class named Clock12 and inheriting from Clock. It must fulfill the following requirements.

(1) a private instance field named period to represent the period, either am or pm

(2) a constructor without argument to set the hour, minute and second to 0 and period to am

(3) a constructor with three int-type and one String-type parameters representing the initial value of the fields hour, minute, second and period. Note that if the initial value for any field of hour, minuteand second is not legal, such as the value for minute is less than 0 or greater than 59, set the initial value as 0. If the initial value for period field is not legal, set the initial value as am.

(4) overriding the equals function

(5) overriding the toString function to return a string of the form hh:mm:ss-[am/pm], for example 11:38:40-am.

(6) overriding the setter for field hour. Note that if the value to be set for any field is not legal, throw an InvalidValuesException with error message “It is a 12-hour clock”.

C. Design a class named Clock24 and inheriting from Clock. It must fulfill the following requirements.

(1) a constructor without argument to set the hour, minute and second to 0

(2) a constructor with three int-type parameters representing the initial value of the fields hour, minute and second. Note that if the initial value for any field is not legal, set the initial value as 0.

(3) overriding the equals function

(4) overriding the toString function to return a string of the form hh:mm:ss, for example 13:38:40.

D. Write a Java source program including the class Clock, Clock12, Clock24 and the following class ProgTest4.

public class ProgTest4 {

public static void main(String… args) {

Clock obj1, obj2, obj3, obj4;

obj1 = new Clock12();

obj2 = new Clock12(13, 38, 60, “mm”);

obj3 = new Clock24();

obj4 = new Clock24(15, 25, 40);

System.out.println(“Initial time of obj1: “ + obj1);

System.out.println(“Initial time of obj2: “ + obj2);

System.out.println(“Initial time of obj3: “ + obj3);

System.out.println(“Initial time of obj4: “ + obj4);

try {

obj1.setHour(13);

}

catch(InvalidValuesException e) {

System.out.println(e.getMessage());

obj1.setHour(0);

}

try {

obj1.setMinute(38);

}

catch(InvalidValuesException e) {

System.out.println(e.getMessage());

}

System.out.println(“obj1 equals obj2: “ + obj1.equals(obj2));

System.out.println(“obj3 equals obj4: “ + obj3.equals(obj4));

}

}

classInvalidValuesException extends Exception {

InvalidValuesException(String msg) {

super(msg);

}

}

Testing

Executing the class will output the following result.

Initial time of obj1: 00:00:00-am

Initial time of obj2: 00:38:00-am

Initial time of obj3: 00:00:00

Initial time of obj4: 15:25:40

It is a 12-hour clock

obj1 equals obj2: true

obj2 equals obj4: false

  1. Complete the following program. The output is shown as follows.

Output
object is triangle↵
Area is 6.0↵

object is rectangle↵
Area is 100.0↵

object is rectangle↵
Area is 10.0↵

object is triangle↵
Area is 12.5↵

object is square↵
Area is 16.0↵

abstract class TwoDShape {

private double width;

private double height;

private String name;

TwoDShape() {

width = height = 0.0;

name = "none";

}

TwoDShape(double w, double h, String n) {

width = w;

height = h;

name = n;

}

TwoDShape(double x, String n) {

width = height = x;

name = n;

}

doublegetWidth() { return width; }

doublegetHeight() { return height; }

voidsetWidth(double w) { width = w; }

voidsetHeight(double h) { height = h; }

String getName() { return name; }

abstract double area();

}

class Triangle extends TwoDShape {

Triangle() {

super();

}

Triangle(double w, double h) {

super(w, h, "triangle");

}

Triangle(double x) {

super(x, "triangle");

}

// insert statement(s) here

}

class Rectangle extends TwoDShape {

Rectangle() {

super();

}

Rectangle(double w, double h) {

super(w, h, "rectangle");

}

Rectangle(double x) {

super(x, "rectangle");

}

// insert statement(s) here

}

classDynShapes {

public static void main(String args[]) {

TwoDShapeshapes[] = new TwoDShape[5];

shapes[0] = new Triangle(3.0, 4.0);

shapes[1] = new Rectangle(10);

shapes[2] = new Rectangle(5, 2);

shapes[3] = new Triangle(5.0);

shapes[4] = new TwoDShape(4, "square") {

// insert statement(s) here

};

for(TwoDShapeobj : shapes) {

System.out.println("object is " + obj.getName());

System.out.println("Area is " + obj.area());

System.out.println();

}

}

}