Question 1 [11 points]
i) State whether the following condition contains an error.
If it does, write the corrected form:
if (x and y > 0) ____________________________
ii) Write a simple (not compound) boolean expression to express the following:
x and y have the same sign ______________________________
iii) Write statements to define an array v of 1000 numbers of type int. Then use a for loop to initialize each element v[i] to 10 * i.
iv) What is printed by the following program segment:
int[] a = {1, 4, 16, 9} ;
int r = 0 ;
for (int i = 0; i < a.length ; i ++)
{ if ( i % 2 = = 0)
r = r + a[i] ;
else
r = r – a[i] ;
System.out.print(r + " " ) ; _____________________________
}
v) [4 points] What is the final value returned by the following recursive method when called with a = 4. Write also the detail of the returned expression after each recursive call.
public int mystery (int a)
{
if ( a = = 0)
return 0;
else
return a * a + mystery (a – 1);
}
Question 2[18 points]
i) [9 points] (a) Given the Point class specification on page 8, write a complete class called Rectangle. The class has 3 data fields: Point p (x, y) is the lower left-hand corner point of the rectangle, and w and h are its width and height respectively. The class has 3 constructors. The first constructor has 3 arguments, a Point representing the lower left corner point of the rectangle, and 2 doubles representing the width and height. The second constructor has 4 double arguments representing the coordinates x, and y of the lower left corner point, and the width and height. The third constructor constructs a default rectangle with the origin as the lower left corner point, with both width and height equal to 1. The class has 3 inquiry methods, as well as a toString() method.
Question 2(Cont.)
ii) [9 points] write a tester class called RectangleTester to test the Rectangle class. The class contains a single method main. Four double numbers which represent the x and y coordinates, and the width and height of the rectangle are entered on the command line. They are stored in the String array args of the main method. After converting them to double values, you should construct 3 Rectangle objects r1, r2, r3, by calling each of the 3 constructors with their appropriate arguments. Then, using one of these objects, display the results returned by all the methods in the Rectangle class. Also show in the tester class how to use the getX and getY methods in the Point class to display the coordinates of the lower left-hand point of that object.
Question 3 [12 points]
i) [3 points] Write statements to declare and initialize an array of type BankAccount called accounts, which contain 3 accounts. The first belongs to "Paul" with an account number 633 and a balance of $1000.0. The second belongs to "Dan" with an account number 313 and a balance of $500.0. The third belongs to "Lina" with an account number 786 and a balance of $2000.0.
ii) [ 6 points] Write a method firstName which returns the index (position) of the account whose name comes in first in the alphabetical order of an array b of type BankAccount . For example, for the above array, the method returns 1 because "Dan" comes first alphabetically.
public int firstName (BankAccount[] b)
iii) [3 points] Write a method inceaseBalance which takes an array of type BankAccount as well as the index of an account in the array, and increases that account by a given rate.
public void increaseBalance (BankAccount[] b, int index, double rate)
Question 4 [9 points]
i) [4 points] Write a recursive method power (x,y) that returns xy (x to the power y).
For example, power (3,4) = 3*3*3*3 .
Hint: The recursion step uses the relation: xy = x * xy-1
The base case is x1 = x.
public int power (int x, int y)
ii) [5 points] A palindrome is a string that is spelled the same way forward and backward. Some examples of palindromes are "radar", and "live on time emit no evil". Write a method that returns boolean value true if a given string is a palindrome.
public boolean palindrome (String s)
Question 5 [10 points]
i) [2 points] Using the Arc2D.Double class, write a statement to define the following Pac Man face. Note that the mouth is opened 90 degrees.
ii)[8 points] Write some statements in a for loop to define and draw 10 concentric and equidistant circles from the biggest to the smallest inside a graphical window of size 400 by 400. Fill each circle with a random color.
Brief Documentation for the Point and the BankAccount Classes
The Point class constructor and method prototypes
// construct a point object with given x, y coordinates public Point(double x, double y) // return the x-coordinate of the point public double getX() // return the y-coordinate of the point public String getY() // return a string representation of a point in form (x,y) public String toString()
The BankAccount class constuctor and method prototypes
// construct account with account number a, owner name n and initial balance b public BankAccount(int a, String n, double b) // withdraw given amount (return true if valid) public boolean withdraw(double amount) // deposit given amount public void deposit(double amount) // get the values of the three data fields public int getNumber() public String getName() public double getBalance()
// construct a KeyboardReader object public KeyboardReader() // read a line as an integer and return it public int readInt() // read a line as a double and return it public double readDouble() // read a line as a string and return it public String readLine()
my first java course final questionnaire
Posted by admin on May 25th, 2010

Posted in 

Your post is difficult to read.
I don’t understand what you are saying.
Valuable info. Lucky me I found your site by accident, I bookmarked it.