Total value 75 points.
- (3 points) Write a Java statements to declare variables a, b, and constant varriable num of type float. In the statement, initialize a to the value 7.4 , b to the value 3.5, and num to the value 2.9.
float a = 7.4, b = 3.5;
final float num = 2.9 ;
- (2 points) Assuming your answer to question 1. Show two different ways to multiply the variable a by 2.
a = a * 2 ;
a *= 2 ;
- (2 points) Using an if/else control structure, set the value of an integer variable x to twice its value if it is less than 10 and half its value if it is greater than or equal to 10;
if (x < 10)
x*= 2;
else
x /=2;
- (3 points) Write a private Java method which takes two integers as parameters and returns an integer value. The return value must be 0 if the integers are equal, +1 if the first is greater than the second, and –1 if the second is greater than the first.
Private int method6(int a , int b) {
if (a = = b) return 0 ;
else
if (a > b) return 1 ;
else return –1;
}
- (10 points) Write a protected Java method which takes an integer as a parameter and returns a string of asterisks the length of which is equal to the parameter value passed in. (e.g. if the value 5 is passed in, then the string "*****" is returned).
Protected String method8(int x) {
String rstring = new String();
for (int i = 0 ; i < x; i++)
rstring += "*" ; // rstring = new String(rstring + "*") ;
return rstring;
}
- (10 points) Write a paint method which takes a Graphics object g as a parameter and uses g.drawString(); to display a 12x12 addition table, for the integers 1 through 12, on the graphics object in tabular form. Recall that g.drawstring() takes three parameters : a string, an x position, and a y position. Row and column labels are not required, but may be included for 5 extra credit points).
Public void paint(Graphics g) {
Int xpos = 20, ypos = 20 ;
For (int y = 1; y <= 12; y++) {
Xpos = 20 ;
For (int x = 1 ;x<=12;x++) {
g.drawstring(Integer.toString(x + y),xpos,ypos);
xpos += 20 ;
}
ypos += 20;
}
}
- (10 points) Write a public Java method which takes an array of integers as a parameter and returns true if the sum of the elements in the first half of the array is greater than the sum of the elements in the second half of the array, and returns false otherwise.
Public Boolean method10(int a[]) {
Boolean rval = false;
Int sum1 = 0, sum2 = 0;
For(I = 0 ; I<(a.length /2); I++) sum1 += a[I];
For(int j = (a.length / 2); j < a.length; j++) sum2 += a[j] ;
If (sum1 > sum2) rval = true ;
Return rval ;
}
- (5 points) Consider the following code :
public class C1 {
double a = 10.7 ;
}
public class C2 extends C1 {
String a = "twenty" ;
public void myMeth() {
for (int a = 0 ; a < 5 ; a++)
System.out.println(" " + a + " "+ this.a + " " + super.a);
}
}
What is printed out when the myMeth() method is invoked on an object of type C2?
0 twenty 10.7
1 twenty 10.7
2 twenty 10.7
3 twenty 10.7
4 twenty 10.7
- (5 points) Define a new class called "Student" containing two protected instance variables : a string for the student’s name and an integer for the student’s number
Public class Student {
String studentName ;
Int studentNumber
}
- (5 points) Define a constructor which takes no arguments for the class Student which sets the student number to zero and the name to an empty string.
Public Student() {studentNumber = 0 ; studentName = new String(); }
- (5 points) Define a constructor for Student which takes two parameters : a string and an integer. Set the instance variables to the corresponding values.
Public Student(String name , int number) {
StudentNumber = number;
StudentName = new String(name);
}
- (5 points) Define a subclass "GradStudent" which inherets from "Student" but adds a string for the advisor’s name.
Public class GradStudent extends Student {
String advisorName ;
}
- (5 points) Define a constructor for GradStudent which takes no arguments.
Public GradStudent() {advisorName = new String();}
- (5 points) Define a constructor for GradStudent which takes three arguments : a string for the student’s name, an integer for the student’s number, and a second string for the advisor’s name.