Create a CollegeCourse class. The class contains fields for the course ID (for e
ID: 3933772 • Letter: C
Question
Create a CollegeCourse class. The class contains fields for the course ID (for example, "CIS 210"), credit hours (for example, 3), and a letter grade (for example, 'A'). Include get and set methods for each field. Create a Student class containing an ID number and an array of five CollegeCourse objects. Create a get() and set() method for the Student ID number. Also create a get() method that returns one of the Student's CollegeCourses; the method takes an integer argument and returns the CollegeCourse in that position (0 through 4). Next, create a set() method that sets the value of one of the Student's CollegeCourses; the method takes two arguments-a CollegeCourse and an integer representing the CollegeCourse's position (0 through 4). Save the files as CollegeCourse.java and Student.java. Write an application that prompts a professor to enter grades for five different courses each for 10 students. Prompt the professor to enter data for one student at a time, including student ID and course data for five courses. Use prompts containing the number of the student whose data is being entered and the course number-for example, "Enter ID for student #s", where s is an integer from 1 through 10, indicating the student, and "Enter course ID #n", where n is an integer from 1 through 5, indicating the course number. Verify that the professor enters only A, B, C, D, or F for the grade value for each course. Save the file as InputGrades.java.Explanation / Answer
Solution:
package chegg;
import java.util.Arrays;
public class Student {
int id;
// array of five course objects
CollegeCourse[] obj = new CollegeCourse[5];
// getter and setter
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public CollegeCourse getObj(int _id) {
return obj[_id];
}
public void setObj(CollegeCourse _obj, int pos) {
this.obj[pos] = _obj;
}
@Override
public String toString() {
return "Student [id=" + id + ", obj=" + Arrays.toString(obj) + "]";
}
}
___________________________________________________________________
package chegg;
public class CollegeCourse {
String CIS;
int creditHours;
String grade;
// getter and setter
public String getCIS() {
return CIS;
}
public void setCIS(String cIS) {
CIS = cIS;
}
public int getCreditHours() {
return creditHours;
}
public void setCreditHours(int creditHours) {
this.creditHours = creditHours;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
@Override
public String toString() {
return "CollegeCourse [CIS=" + CIS + ", creditHours=" + creditHours + ", grade=" + grade + "]";
}
}
__________________________________________________________________________
package chegg;
import java.util.Scanner;
public class InputGrades {
// I am doing it for 2 students here.Please modify it for 10 later.
public static void main(String[] args) {
int countStu = 0;
System.out.println("Please enter grades for five courses per student: ");
while (countStu < 2) {
Student stuObj = new Student();
CollegeCourse courseObj = new CollegeCourse();
int countCourseId = 0;
countStu++;
Scanner scan = new Scanner(System.in);
System.out.println("Enter ID for student #" + countStu + " : ");
stuObj.id = scan.nextInt();
while (countCourseId < 5) {
countCourseId++;
System.out.println("Enter course ID #" + countCourseId + " : ");
courseObj.CIS = scan.next();
System.out.println("Enter grade " + countCourseId + " : ");
while (!scan.hasNext("[ABCDFabcdf]")) {
System.out.println("Please enter correct grade [A/B/C/D/F]");
scan.next();
}
courseObj.grade = scan.next();
stuObj.obj[countCourseId - 1] = courseObj;
}
System.out.println("you have entered : " + stuObj);
}
}
}
_____________________________________________________________
Sample run:
Please enter grades for five courses per student:
Enter ID for student #1 :
123
Enter course ID #1 :
fgh567
Enter grade 1 :
a
Enter course ID #2 :
fgh5678
Enter grade 2 :
c
Enter course ID #3 :
qwe435
Enter grade 3 :
a
Enter course ID #4 :
fgh567
Enter grade 4 :
a
Enter course ID #5 :
hgjy67
Enter grade 5 :
x
Please enter correct grade [A/B/C/D/F]
a
you have entered : Student [id=123, obj=[CollegeCourse [CIS=hgjy67, creditHours=0, grade=a], CollegeCourse [CIS=hgjy67, creditHours=0, grade=a], CollegeCourse [CIS=hgjy67, creditHours=0, grade=a], CollegeCourse [CIS=hgjy67, creditHours=0, grade=a], CollegeCourse [CIS=hgjy67, creditHours=0, grade=a]]]
Enter ID for student #2 :
999
Enter course ID #1 :
gfht334
Enter grade 1 :
a
Enter course ID #2 :
ghj678
Enter grade 2 :
c
Enter course ID #3 :
jhu888
Enter grade 3 :
h
Please enter correct grade [A/B/C/D/F]
f
Enter course ID #4 :
hfj444
Enter grade 4 :
d
Enter course ID #5 :
hu777
Enter grade 5 :
a
you have entered : Student [id=999, obj=[CollegeCourse [CIS=hu777, creditHours=0, grade=a], CollegeCourse [CIS=hu777, creditHours=0, grade=a], CollegeCourse [CIS=hu777, creditHours=0, grade=a], CollegeCourse [CIS=hu777, creditHours=0, grade=a], CollegeCourse [CIS=hu777, creditHours=0, grade=a]]]