CS 111 – JAVA Exam21.07.2005

Name and Surname: ID:

Signature: GRADE:

Q1. (50 points) Write a complete JAVA class to represent complex numbers. Note that a complex number is in the form

where a is called the real part, and b is called the imaginary part. You are supposed to write the following methods:

  • The constructor with proper parameters
  • The toString() method. Make sure that your method should convert the following complex numbers properly:
  • i
  • –i
  • 1+i
  • –1-i
  • 0
  • 3
  • 2+5i
  • The getter methods getRealPart() and getImaginaryPart() to return the real part, a and imaginary part, b, respectively for a complex number, a + bi.
  • The equals method that compares the current complex number object with another complex number (given as the parameter).
  • The add method that adds the current complex number object with another complex number (given as the parameter) and returns the resulting sum.
  • The subtract method that subtracts another complex number (given as the parameter) from the current complex number object and returns the resulting difference.
  • The multiply method that multiplies the current complex number object with another complex number (given as the parameter) and returns the resulting product. Note that
  • The conjugate method that returns the conjugate of the current complex number object. Note that the conjugate of a + bi is a - bi.

(10 points bonus question)

Write a driver Java program for the above class that represents complex numbers. You should try every method (the constructor, getter methods, equals, add, subtract, multiply, conjugate) in your driver program and print the results with proper messages.

Q2. (50 points) Investigate the following program. Correct the errors, if any. Then, write the output.

public class ExamMethods {

public int arrayMethod( int[] a ) {

int i, k, t, r=0;

for (k=0;k<a.length/2;k++){

i=a.length-(k+1);

t=a[i];

a[i]=a[k];

a[k]=t;

r += t;

}

return r;

}

public void StringMethod( String S ) {

int k ;

String t;

System.out.println(S);

for (k=S.length()-1;k>=0;k--){

S=S.substring(0,k);

System.out.println(S);

}

}

public int primitiveMethod(double d){

int a, b, c;

if (d<0) d=-d;

a = (int) d;

b = a * 100;

c = (int) (d*100);

d = c-b;

return (int) d;

}

public static void main(String[] args) {

int [] arr1 = {-1,0,1,2,4};

int [] arr2 = {2,3,4,5};

int result1 = arrayMethod(arr1);

System.out.println("Results for first array:");

System.out.println("The method returned"+result1);

System.out.print("{"+arr1[0]);

for (int j=1;j<arr1.length;j++)

System.out.print(","+arr1[j]);

System.out.println("}\n");

int result2 = arrayMethod(arr2);

System.out.println("Results for second array:");

System.out.println("The method returned"+result2);

System.out.print("{"+arr2[0]);

for (int j=1;j<arr2.length;j++)

System.out.print(","+arr2[j]);

System.out.println("}\n");

String st="Computers";

st=StringMethod(st);

System.out.println("st after method call: "+st);

double num = -89.375;

int res = primitiveMethod(num);

System.out.println("num= "+num+" and res= "+res);

}

}

The output is

Page1