use java in wiritng and running the codes. The following figure shows a UML diag
ID: 3804635 • Letter: U
Question
use java in wiritng and running the codes.
The following figure shows a UML diagram in which the class Student is inherited from the class Person
Person
// ivars - firstName: String
- lastName: String
- email: String
+ Person(String fname, String lname)
+ getFirstName(): String
+ getLastName(): String
+ getEmailAddress(): String
+ toString(): String
STUDENT CLASS INHERITS FROM THE PERSON CLASS
// class variables; initial value = 10023;
+ lastIdAssigend : int
// ivars
- studentId: int
- gpa : double
- numOfCredits : int
+ Student(String fname, String lname)
+ addCourse(int credits, double grade): void
+ getStudentId(): int
+ getGPA(): double
+ toString(): String
+ equals(Student anotherStuent): boolean
a. Implement a Person class. The person constructor takes two strings: a first name and a last name. The constructor initializes the email address to the first letter of the first name followed by first five letters of the last name followed by @tru.ca. If the last name has fewer than five letters, the email address will be the first letter of the first name followed by the entire last name followed by a @tru.ca. Examples:
b. Override Object’s toString method for the Person class. The toString method should return the present state of the object.
c. Now, create a Student class that is a subclass of Person and implements Comparable interface. d. The Student constructor will be called with two String parameters, the first name and last name of the student. When the student is constructed, the inherited fields lastName, firstName, and email will be properly initialized, the student’s gpa and number of credit will be set to 0. The variable lastIdAssigend will be properly incremented each time a Student object is constructed and the studentId will be set to the next available ID number as tracked by the class variable lastIdAssigend.
e. Override the object’s toString method for the Student class. The toString method should return the present state of the object. Note that it should use the toString() method from its superclass. f. The addCourse() method should update the credits completed, calculate, and update the gpa value.
Use the following values for grade:
Example GPA calculation:
GPA = 50.00 / 16 = 3.125; the getGPA() method should return this value.
g. Students are compared to each other by comparing GPAs. Implement the compareTo() method for the student class.
Now, test your code with the supplied client code (StudentClient.java). Note: You should not modify this client code. We will use the same client code to test your classes.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author mrahman
*/
public class StudentClient {
/**
* This method reads student's data from the "std.txt" file and
* stores them in an array list.
* The std.txt file format:
* First line contains first and last name of the student
* Second line contains the number of courses (n)
* Following 'n' lines, each contains credit (an integer) and grade(a double)
* values.
* There is no limit on number of students in the file. Example:
*
* Musfiq Rahman
* 4
* 3 3.0
* 4 4.0
* 3 4.0
* 4 4.0
*
* Adam Edge
* 5
* 3 4.0
* 3 2.5
* 1 4.0
* 4 3.5
* 3 3.0
*
* @param stds array list to store student's data
* @throws FileNotFoundException
*/
public static void readStudentsData(ArrayList stds) throws FileNotFoundException{
File f = new File("std.txt");
Scanner sk = new Scanner(f);
// process each studnets data
while(sk.hasNextLine()){
// first line should be the studnet's name
String l = sk.nextLine();
if(l.length() <= 0)
continue;
// Splits first and last name
String [] r = l.split(" ");
String fname = r[0];
String lname = r[1];
// Creat a studnet object with the first and last name
Student s = new Student(fname, lname);
// Now, read how many courses data are there for this student
int count = sk.nextInt();
// For each course, read the credit and the grade values
// for the student
for(int j=0; j int credits = sk.nextInt();
double gr = sk.nextDouble();
s.addCourse(credits, gr);
}
// Add the student's data to the Array list.
stds.add(s);
}
}
public static void main(String[] args) {
try {
// ArrayList for storing students data
ArrayList stds = new ArrayList();
// Read and store students data from the std.txt file
readStudentsData(stds);
// Sort studets data based on their gpa
Collections.sort(stds);
// Print all students data in increasing order of gpa value.
for(Student s:stds)
System.out.println(s);
} catch (FileNotFoundException ex) {
System.out.println("Data file (std.txt) not found.");
}
}
}
std.txt
Musfiq Rahman
4
3 3.0
4 4.0
3 4.0
4 4.0
Adam Edge
5
3 4.0
3 2.5
1 4.0
4 3.5
3 3.0
Sharukh Khan
3
3 3.0
4 4.0
4 3.5
Jabber Ali
4
3 1.0
1 1.0
4 4.0
4 4.0
Person
// ivars - firstName: String
- lastName: String
- email: String
+ Person(String fname, String lname)
+ getFirstName(): String
+ getLastName(): String
+ getEmailAddress(): String
+ toString(): String
Explanation / Answer
I used the StudentClient.java class and the same text file that was provided. Please find Person and Student classes below along with the output.
PROGRAM CODE:
Person.java
package student;
public class Person {
private String firstName;
private String lastName;
private String email;
public Person(String fname, String lname) {
firstName = fname;
lastName = lname;
if(lname.length()<5)
email = fname.charAt(0) + lname + "@tru.ca";
else
email = fname.charAt(0) + lname.substring(0, 5) + "@tru.ca";
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
@Override
public String toString() {
return " First Name = " + firstName + " Last Name = " + lastName + " Email = " + email;
}
}
Student.java
package student;
public class Student extends Person implements Comparable<Student>{
public static int lastIDAssigned = 10023;
private int StudentID;
private double GPA;
private int numOfCredits;
public Student(String fname, String lname) {
super(fname, lname);
GPA = 0.0;
numOfCredits = 0;
StudentID = lastIDAssigned++;
}
public void addCourse(int credits, double grade)
{
numOfCredits++;
GPA += credits * grade;
}
public int getStudentID() {
return StudentID;
}
public double getGPA() {
return GPA/16;
}
@Override
public String toString() {
String result = super.toString();
result += " Student ID = " + StudentID + " Number of Credits = " + numOfCredits;
result += " GPA = " + getGPA();
return result;
}
@Override
public boolean equals(Object obj) {
Student stud = (Student) obj;
if(stud.getLastName().equals(getLastName()) && stud.getFirstName().equals(getFirstName()) && stud.StudentID == StudentID)
return true;
else return false;
}
@Override
public int compareTo(Student other) {
if(GPA < other.GPA)
return -1;
else if(GPA == other.GPA)
return 0;
else return 1;
}
}
OUTPUT:
First Name = Jabber
Last Name = Ali
Email = JAli@tru.ca
Student ID = 10026
Number of Credits = 4
GPA = 2.25
First Name = Sharukh
Last Name = Khan
Email = SKhan@tru.ca
Student ID = 10025
Number of Credits = 3
GPA = 2.4375
First Name = Adam
Last Name = Edge
Email = AEdge@tru.ca
Student ID = 10024
Number of Credits = 5
GPA = 2.90625
First Name = Musfiq
Last Name = Rahman
Email = MRahma@tru.ca
Student ID = 10023
Number of Credits = 4
GPA = 3.3125
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.