JAVA: Student, Course, Personal Data
Tarih: 9 Ocak 2014 Yorum: 0

I. Implement the PersonalData class with the following UML diagram.

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.

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

 

Consider the following example which demonstrates both ways of creating a PersonalData object.

 


Date date=new Date(80,4,1);
PersonalData p;
p=new PersonalData(date,12345678910);


PersonalData p;
p=new PersonalData(80,4,1,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.

 

PersonalData.java

/**
 * Personal Data. 
 */
	import java.util.Date;
public class PersonalData {
	// Create variables.
    private java.util.Date birthDate;
	private String address;
	private long ssn;

   	// Constructor 1.
   	public PersonalData(java.util.Date birthDate,long ssn){
		this.birthDate	= birthDate;
		this.ssn		= ssn;
	}

	// Constructor 2.
	public PersonalData(int year,int month,int day ,long ssn){
		this.birthDate 	= new Date(year,month,day);
		this.ssn		= ssn;
	}

	// Get birtdate.
	public Date getBirthDate(){
		return this.birthDate;
	}

	// Get address
	public String getAddress(){
		return this.address;
	}

	// Get SSN.
	public long getSSN(){
		return this.ssn;
	}

	// Set address.
	public void setAddress(String address){
		this.address	= address;
	}
} // End of PersonalData class.

 

Student.java

/**
 * Student. 
 */
public class Student{
	// Create variables.
    private String name;
	private long id;
	private double gpa;
	private PersonalData pd;

	// Constructor 1.
	public Student(String name, long id, double gpa, PersonalData pd){
		this.name	= name;
		this.id		= id;
		this.gpa	= gpa;
		this.pd		= pd ;
	}

	// Get student name.
	public String getName(){
		return this.name;
	}

	// Get student ID.
	public long getID(){
		return this.id;
	}

	// Get student GPA.
	public double getGPA(){
		return this.gpa;
	}

	// Get student personal data.
	public PersonalData getPersonalData(){
		return this.pd;
	}

	// Student information tostring.
	public String toString(){
		return "Name: " + this.name + " ID: " + this.id + " GPA: " + this.gpa;
	}
} // End of Student class.

 

Course.java

/**
 * Course. 
 */
public class Course{
	// Create variables and initialize.
	private String name;
	private int capacity = 40;
	private Student[] students= new Student[capacity];
	private int numberOfStudents;

	// Constructor 1.
	public Course(String name){
		this.name = name;
	}

	// Constructor 2.
	public Course(String name, int capacity){
		this.name 		= name;
		this.capacity 	= capacity;
	}

	// Get student number.
	public int getNumberOfStudents(){
		return this.numberOfStudents;
	}

	// Get course name.
	public String getCourseName(){
		return this.name;
	}

	// Get student.
	public Student[] getStudents(){
		return this.students;
	}

	// Student addition to course.
	public boolean addStudent(Student student){
		if(numberOfStudents < capacity){
			for(int i = 0; i < numberOfStudents; i++){
				if(student.equals(students[i]))
  				return false;
  			}
			students[numberOfStudents] = student;
			numberOfStudents++;
		return true;
		}
		return false;
	}

	// Student drop from course.
	public boolean dropStudent(Student student){
		for(int i = 0; i < numberOfStudents; i++){
			if(student.equals(students[i])){
				students[i] = null;
				while(i < numberOfStudents){
					students[i] = students[i+1];
					i++;
				}
				numberOfStudents--;
				return true;
			}
		}
	return false;
	}

	// Increase capacity of course.
	public void increaseCapacity(){
		capacity = capacity + 5;
	}

	// Get best student.
	public Student getBestStudent(){
		Student beststudent = students[0];
		for(int i=1; i < numberOfStudents; i++){
			if(students[i].getGPA() > students[i-1].getGPA())
				beststudent = students[i];
		}
		return beststudent;
	}

	// Get youngest student.
	public Student getYoungestStudent(){
		Student youngestStudent = students[0];
		for(int i = 0; i < numberOfStudents - 1; i++){
			if((students[i].getPersonalData().getBirthDate()).compareTo(students[i+1].getPersonalData().getBirthDate()) < 0)
				youngestStudent = students[i];
			else if(students[i].getPersonalData().getBirthDate().compareTo(students[i+1].getPersonalData().getBirthDate()) > 0)
				youngestStudent = students[i + 1];
		}
		return youngestStudent;
	}

	// Clear course.
	public void clear(){
		for(int i = 0; i < numberOfStudents; i++){
			students[i] = null;
		}
	}

	// Student list.
	public void list(){
		String result = "";
		for(int i = 0; i < numberOfStudents; i++){
			result += students[i] + "\n";
		}
		System.out.println(result);
	}

	// Course infrmation to string.
	public String toString(){
		return "Number of students " + this.numberOfStudents + "\ncapacity " + this.capacity + "\ncourse name " + this.name;
	}
} // End of Course class.

 

StudentTest.java

/**
 * Test. 
 */
public class StudentTest{
	public static void main(String[] args){

		// 5 students are created.
		Student student1 = new Student("Rodney McKay ",5005,3.90,new PersonalData(85,5,5,115));
		Student student2 = new Student("Daniel Jackson ",5006,2.80,new PersonalData(86,2,4,345));
		Student student3 = new Student("Samantha Carter ",5007,3.30,new PersonalData(87,7,6,123));
		Student student4 = new Student("George Hammond ",5008,2.20,new PersonalData(90,4,12,657));
		Student student5 = new Student("Jack O'Neill ",5009,2.50,new PersonalData(92,6,12,854));

		// Course CSE141 is created with a capacity of 3.
		Course course1 = new Course("CSE141",3);

		// Any 4 of the students is added to CSE141.
		course1.addStudent(student1);
		course1.addStudent(student2);
		course1.addStudent(student3);
		course1.addStudent(student4);

		// All students of CSE141 are printed on the screen.
		System.out.println("All students of course " + course1.getCourseName() + ": ");
		course1.list();

		// The capacity of CSE141 is increased.
		course1.increaseCapacity();

		// Remaining 2 students are added to CSE141.
		course1.addStudent(student4);
		course1.addStudent(student5);

		// All students of CSE141 are printed on the screen.
	  	System.out.println("All students of course "+course1.getCourseName() + ": ");
		course1.list();

		// Student with ID 5005 is dropped from CSE141.
		course1.dropStudent(student1);

		// All students of CSE141 are printed.
	  	System.out.println("All students of course "+ course1.getCourseName() + ": ");
		course1.list();

		// Number of students enrolled to CSE141 is printed.
		System.out.println(course1.getCourseName() + "'s number of students are " + course1.getNumberOfStudents() + ".");

		// CSE141's best student's birthdate's year is printed.
		System.out.println("\nBirth year of the best student of CSE141 is : " +course1.getBestStudent().getPersonalData().getBirthDate().getYear());

		// New course is created.
		Course course2 = new Course("CSE142");

		// All students enrolled in CSE141 are added to CSE142.
		Student[] students = course1.getStudents();
	  	for(int i=0; i < course1.getNumberOfStudents(); i++)
	  		course2.addStudent(students[i]);

		// All students of CSE141 are removed from the course.
		course1.clear();

		// Student with ID 5005 is dropped from CSE141 and result of the operation is printed on the screen.
		System.out.println("\nStudent with ID 5005 is dropped from " + course1.getCourseName() + " is " + course1.dropStudent(student1));

		// All students of CSE142 are printed on the screen.
	  	System.out.println("\nAll students of "+course2.getCourseName() + ": ");
		course2.list();

		// Best studen of CSE142 is dropped from CSE142.
		course2.dropStudent(course2.getBestStudent());

		// All students of CSE142 are printed on the screen.
	  	System.out.println("All students of " + course2.getCourseName() + ": ");
		course2.list();

		// GPA of youngest student CSE142 is printed on the screen.
		System.out.println("The GPA of youngest student CSE142 is " + course2.getYoungestStudent().getGPA());

		// Courses CSE141 and CSE142 are printed on the screen.
	  	System.out.println("\nAll students of " + course1.getCourseName() + ": ");
		course1.list();
		System.out.println("\nAll students of " + course2.getCourseName() + ": ");
		course2.list();
	} // End of main method.
} // End of StudentTest class.



Yorum Yok:


Yorum Yap:

Yorum yapabilmek için giriş yapmalısınız.




tema yapımcısı wordpress alexa bilgileri Webmaster Creative Commons v3 ile Lisanslanmıştır!


Akif ARSLAN 2012 - 2014 © Hiçbir Hakkı Saklı Değildir.
Sitede bulunan istediğiniz cümleyi veya içeriği, istediğiniz gibi, istediğiniz yerde, istediğiniz zaman ve istediğiniz kişilerle paylaşabilirsiniz.