HW5 Due Date: 20/12/2015, Midnight Write a ComplexNumber class

advertisement
HW5
Due Date: 20/12/2015, Midnight
1. Write a ComplexNumber class that is similar to the RationalNumber. The class
contains:


Private double data fields named as re and im (corresponding to real and imaginary
parts, respectively).
The following methods
o A constructor that creates a ComplexNumber object for the specified two
double parameters (public ComplexNumber(double r, double i)
o get methods for the real and imaginary parts (public double getReal(), public
double getImaginary() )
o public ComplexNumber reciprocal()
o public ComplexNumber add(ComplexNumber)
o public ComplexNumber subtract(ComplexNumber)
o public ComplexNumber multiply(ComplexNumber)
o public ComplexNumber divide(ComplexNumber)
o public boolean equals(ComplexNumber)
o public ComplexNumber conjugate()
o public double getAngle()
o public double getMagnitude()
o public String toString()
Implement the class. Write a test program that tests all methods in the class.
For any information regarding Complex Numbers you can visit Wikipedia
(http://en.wikipedia.org/wiki/Complex_number).
2. Write a program that finds nth smallest element among 15 integer values created
randomly from the range 1-100. The value n will be given by the user (1 through 15).
Searching the array must be performed by a method with the following header:
public static int findNthSmallestNumber(int n, int[ ] numbers)
In your program, firstly, random numbers must be created and stored in an array.
Secondly, the user is asked to enter an integer value. Thirdly, the method is invoked
with appropriate parameters. Lastly, the result returning from the method is
displayed on the screen.
Example: The user enters 5 and the program generates the following numbers.
15,18,5,7,45,78,54,91,63,24,18,77,61,83,65,
The program output should be 18. (5th smallest element).
Note that you are not allowed to use any sort method from the Java library.
3.
i.
Implement the PersonalData class with the following UML diagram.
PersonalData
java.util.Date:birthDate
String:address
long:ssn
PersonalData(java.util.Date,
long)
PersonalData(int,int,int,long)
getBirthDate():java.util.Date
getAddress():String
getSSN():long
setAddress(String): void
+
+
+
+
+
+
A PersonalData object is created using one of the two constructors. First constructor uses
java.util.Date and social security number (SSN) data. Second constructor uses three integers
and a long value. The three integers correspond to year, month and day values of a date,
while long value corresponds to the SSN. In the implementation of the second constructor,
you must use this keyword.
Consider the following example which demonstrates both ways of creating a PersonalData
object.
Date date=new Date(80,4,1);
PersonalData p;
PersonalData p;
p=new PersonalData(80,4,1,12345678910);
p=new PersonalData(date,12345678910);
Both of these ways create a PersonalData object having the date 01 May 1980 as the birth date
and 12345678910 as the SSN.
You can acquire the necessary information on java.util.Date class from the Java API. Your
compiler may warn you about its deprecation but ignore this warning.
ii.
Implement the Student class with the following UML diagram.
Student
+
+
+
+
name:String
id:long
gpa:double
PersonalData:pd
Student(String,long, double, PersonalData)
getName():String
getID():long
getGPA():double
+
+
getPersonalData():PersonalData
toString():String
A student object is created by specifying his/her name, ID, GPA and personal data.
A student object can be expressed as a String by simply concatenating its name, ID and GPA
successively.
iii.
+
+
+
+
+
+
+
+
+
+
+
+
+
Revise the Course class (given in Listing 10.6, 8th ed.) according to the following UML
diagram.
Course
name:String
students:Student[]
capacity:int
numberOfStudents:int
Course(String)
Course(String, int)
getNumberOfStudents():int
getCourseName():String
getStudents():Student[]
addStudent(Student):boolean
dropStudent(Student):boolean
increaseCapacity():void
getBestStudent():Student
getYoungestStudent():Student
clear():void
list():void
toString():String
Each course object has a capacity. A course is created in two ways; either by specifying only
its name or by specifying its name and capacity. Default capacity value for a course object is
40. You must use this keyword in the implementation of first constructor.
numberOfStudents holds the number of students currently enrolled to the course.
It uses an array of Students to store the students for the course.
addStudent(Student) method adds a student to the array. While adding the student to the
array, the capacity of the course should not be exceeded and a student cannot be added to
the course for the second time (remember that a student is uniquely identified by his/her
ID). If the student is added to the course, numberOfStudents is increased by one and the
method returns true, else it returns false.
dropStudent(Student) method deletes the student from the array. If the student is found in
the array, then the student is deleted from the array, numberOfStudents is decreased by one
and the method returns true, else the method returns false.
increaseCapacity() method increases the capacity of the course by 5.
getBestStudent() method returns the student with greatest GPA.
getYoungestStudent() method returns the youngest student. You may use the
compareTo(java.util.Date) method defined in java.util.Date class.
clear() method removes all students from the course.
list() method prints all students enrolled to the course to the screen.
A course object is expressed as a String by concatenating its name, number of students
enrolled to the course and each enrolled student’s ID, successively.
iv.
Write a test program in which the following are performed in order.

5 students are created. Let one of them has the ID 5005.

A course (let us call it CSE141) with a capacity of 3 is created

Any 4 of the students is added to CSE141.

All students of CSE141 are printed on the screen.

The capacity of CSE141 is increased.

Remaining 2 students are added to CSE141.

All students of CSE141 are printed on the screen.

Student with ID 5005 is dropped from CSE141.

All students of CSE141 are printed on the screen.

Number of students enrolled to CSE141 is printed.

Birth year of best student of CSE141 is printed on the screen. (You should use
getYear() method of java.util.Date class.)

A new course (let us call it CSE142) is created.

All students currently enrolled in CSE141 are added to CSE142. (You should use
getStudents() method).

All students of CSE141 are removed from the course.

Student with ID 5005 is dropped from CSE141 and result of the operation is printed
on the screen.

All students of CSE142 are printed on the screen.

Best student of CSE142 is dropped from CSE142.

All students of CSE142 are printed on the screen.

GPA of youngest student of CSE142 is printed on the screen.

Courses CSE141 and CSE142 are printed on the screen.
Download