Note: Do refer to the comments to better understand the working of the code.
Don't use plagiarized sources. Get Your Custom Essay on
Question: Part I: Create an application to record student courses and grades. The application should also p……
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!
===============
Course.java
===============
package coursehubs;
public class Course {
// private data fields
private String studentName;
private String courseTitle;
private int credit;
private String finalGrade;
// Constructor to store values
public Course(String studentName, String courseTitle, int credit, String finalGrade) {
super();
this.studentName = studentName;
this.courseTitle = courseTitle;
this.credit = credit;
this.finalGrade = finalGrade;
}
// Getter and Setter methods
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getCourseTitle() {
return courseTitle;
}
public void setCourseTitle(String courseTitle) {
this.courseTitle = courseTitle;
}
public int getCredit() {
return credit;
}
public void setCredit(int credit) {
this.credit = credit;
}
public String getFinalGrade() {
return finalGrade;
}
public void setFinalGrade(String finalGrade) {
this.finalGrade = finalGrade;
}
// Check if two courses are equal
public boolean equals(Course other) {
return this.courseTitle.equals(other.courseTitle) && this.studentName.equals(other.studentName);
}
// String representation
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(“Student: ” + this.studentName + “n”);
sb.append(“Course: ” + this.courseTitle + “n”);
sb.append(“Credits: ” + this.credit + “n”);
sb.append(“Grade: ” + this.finalGrade + “n”);
return sb.toString();
}
}
===============
Transcript.java
===============
package chegg;
import java.util.ArrayList;
import java.util.Comparator;
class NameComparator implements Comparator<Course>{
// Custom comparator to sort by names
@Override
public int compare(Course o1, Course o2) {
return o1.getStudentName().compareTo(o2.getStudentName());
}
}
public class Transcript {
Validator validator;
ArrayList<Course> courses;
public Transcript(Validator validator) {
super();
this.validator = validator;
this.courses = new ArrayList<>();
}
public boolean addCourse(Course c) {
// Validate course before adding
if(this.validator.Validate(c)) {
this.courses.add(c);
return true;
}else {
return false;
}
}
public void delete(Course c) {
// Delete the course from transcript
int index = -1;
for(int i=0; i<this.courses.size(); ++i) {
if(c.equals(this.courses.get(i))) {
index = i;
break;
}
}
if(index > -1) {
this.courses.remove(index);
}
}
public boolean search(Course c) {
// Search for a specific course
for(int i=0; i<this.courses.size(); ++i) {
if(this.courses.get(i).equals(c)) {
return true;
}
}
return false;
}
public void print() {
// Print the transcript
System.out.println(“Transcript”);
for(int i=0; i<this.courses.size(); ++i) {
System.out.println(this.courses.get(i));
}
}
public void sort() {
// Sort using custom comparator
this.courses.sort(new NameComparator());
}
}
===============
Validator.java
===============
package chegg;
import java.util.Set;
public class Validator {
private Set<Integer> validCredits;
private Set<String> validGrades;
public Validator(Set<Integer> validCredits, Set<String> validGrades) {
super();
this.validCredits = validCredits;
this.validGrades = validGrades;
}
public Set<Integer> getValidCredits() {
return validCredits;
}
public void setValidCredits(Set<Integer> validCredits) {
this.validCredits = validCredits;
}
public Set<String> getValidGrades() {
return validGrades;
}
public void setValidGrades(Set<String> validGrades) {
this.validGrades = validGrades;
}
public boolean Validate(Course c) {
// Validate grades and credits
if(this.validCredits.contains(c.getCredit()) && this.validGrades.contains(c.getFinalGrade())) {
return true;
}else {
return false;
}
}
}
===============
TranscriptApp.java
===============
package chegg;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class TranscriptApp {
public static void main(String[] args) {
// Custom grades as per requirement
Set<String> validGrades = new HashSet<>();
validGrades.add(“AA”);
validGrades.add(“AB”);
validGrades.add(“BB”);
validGrades.add(“BC”);
validGrades.add(“CC”);
validGrades.add(“CD”);
validGrades.add(“DD”);
validGrades.add(“F”);
// Custom credits as per requirement
Set<Integer> validCredits = new HashSet<>();
validCredits.add(1);
validCredits.add(2);
validCredits.add(3);
validCredits.add(4);
// Creating some sample courses
Course c1 = new Course(“Chegg Student 1”, “Computer Systems”, 4, “AA”);
Course c2 = new Course(“Chegg Student 2”, “Information Systems”, 3, “BC”);
Course c3 = new Course(“Chegg Student 3”, “Systems Security”, 5, “E”);// Should be F, should not be 5
// Prepare validator
Validator validator = new Validator(validCredits, validGrades);
System.out.println(“C1 validated: ” + validator.Validate(c1));
System.out.println(“C2 validated: ” + validator.Validate(c2));
System.out.println(“C3 validated: ” + validator.Validate(c3));
c3 = new Course(“Chegg Student 3”, “Systems Security”, 3, “F”);
System.out.println(“C3 validated: ” + validator.Validate(c3));
Transcript transcript = new Transcript(validator);
transcript.addCourse(c3);
transcript.addCourse(c2);
transcript.addCourse(c1);
System.out.println(“Before sorting!”);
transcript.print();
System.out.println(“After sorting!”);
transcript.sort();
transcript.print();
transcript.delete(c3);
transcript.print();
System.out.println(“Transcript contains c3: ” + transcript.search(c3));
// Ask for user input
transcript = new Transcript(validator);
Scanner in = new Scanner(System.in);
System.out.print(“Add more records (y/n): “);
String more = in.nextLine();
while(!more.equals(“n”)) {
System.out.print(“Enter Name: “);
String name = in.nextLine();
System.out.print(“Enter title: “);
String title = in.nextLine();
System.out.print(“Enter Credits: “);
int credits = Integer.parseInt(in.nextLine());
System.out.print(“Enter Grade: “);
String grade = in.nextLine();
Course temp = new Course(name, title, credits, grade);
transcript.addCourse(temp);
System.out.print(“Add more records (y/n): “);
more = in.nextLine();
}
transcript.print();
}
}
Do Upvote if this helps!