Use Java to create two classes for tracking students. One class is the Student c
ID: 3744960 • Letter: U
Question
Use Java to create two classes for tracking students. One class is the Student class that holds student data; the other is the GradeItem class that holds data about grades that students earned in various courses.
Problem Scenario
CU Boulder needs a Java programmer to assist them with creating a grade book application. You have graciously volunteered to help. The university wants to track their students and their grades in various courses. For each student, the university wants to track this information:
1. Student id (String – a unique value for each student)
2. Student first name (String)
3. Student last name (String)
4. Student email address (String – a unique value for each student)
The university wants to make sure that they have a correct email address for each student. They want you to verify there is an “@” in the email address.
For each grade item, the university wants to track this information:
1.Grade Item id (Integer – a unique value for each grade item)
2.Student id (String – should match a student in the student list)
3.Course id (String)
4.Item type (String – must be one of the following: HW, Quiz, Class Work, Test, Final)
5.Date (String – format yyyymmdd)
6.Maximum score (Integer – must be greater than 0)
7.Actual score (Integer – must be in the range of 0 to maximum score)
Program Requirements
You have to create two classes for the problem scenario given above. The classes are:
A Student class
A Grade Item class
These two classes will be used in subsequent projects to instantiate the Student and Grade Item objects, and will not contain a main method or any I/O code. Your Student and Grade Item classes should each contain these methods:
Constructor
Get methods for each private variable
A toString method to display an instance of an object
An equals method to determine whether two instances of an object contain exactly the same data
Note: We will assume that our objects are immutable (unchanged once an object is instantiated) so we don’t need any set methods.
Constructors in each class should validate the data as follows:
In the Student class
Ensure all values for String fields are not blank. These are the student id, first name, last name and email address.
Ensure the student email address contains the “@” character. Hint: Use the contains method in the String class.
In the Grade Item class
Ensure all values for String fields are not blank. These are the student id, course id, item type and date.
All Grade item types must be one of the following: HW, Quiz, Class Work, Test, or Final. Use an array to store the types and search that array for a valid type. The entries are casesensitive (“HW” does not equal “Hw”).
Grade Item class: Maximum score must be greater than 0.
Grade Item class: Actual score must be in the range of 0 to maximum score.
If any of the data in either class is invalid, the constructor in which the error was detected must throw the IllegalArgumentException exception.
Note: Data required to initialize the fields will be passed by a method (defined in a subsequent project) to the constructor using arguments. Do not display any error messages or use the Scanner class to ask user for input.
Explanation / Answer
import java.lang.*;
import java.util.*;
//Student class
class Student{
private String id;
private String firstName;
private String lastName;
private String email;
public Student(String ID,String fname,String lname,String Email){
//If any string data is null then throw Exception
if(ID==null||fname==null||lname==null||Email==null)
throw new IllegalArgumentException();
//If email doesn't contain @ then throw exception
else if(!Email.contains("@"))
throw new IllegalArgumentException();
//Else assign to instance data
else{
this.id=ID;
this.firstName=fname;
this.lastName=lname;
this.email=Email;
}
}
//Getter method for each private field
public String getID(){
return id;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String getEmail(){
return email;
}
@Override
public String toString(){
return "ID:"+this.id+"First Name"+firstName+"Last Name"+this.lastName+" Email:"+this.email;
}
@Override
public boolean equals(Object o){
Student e=(Student)o;
if(id.equals(e.id))
return true;
else
return false;
}
}
//Class Grade
class Grade{
public static String[]arr ={"HW", "Quiz", "Class Work", "Test","Final"};
private int item_ID;
private String StudentID;
private String CourseID;
private String itemType;
private String Date;
private int maxScore;
private int actualScore;
public Grade(int AID,String SID,String CID,String it,String dt,int mx,int ac){
//Check the condition
if(SID==null||CID==null||it==null||dt==null||AID==0||mx<=0||ac<=0||ac>mx)
throw new IllegalArgumentException();
else if( ! Arrays.asList(arr).contains(it))
throw new IllegalArgumentException();
else{
item_ID=AID;
StudentID=SID;
CourseID=CID;
itemType=it;
Date=dt;
maxScore=mx;
actualScore=ac;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.