Help please!! Best and correct answer gets 900 points!! Thanks in advance!! In t
ID: 3772003 • Letter: H
Question
Help please!! Best and correct answer gets 900 points!! Thanks in advance!!
In the Grade Report programming example, the class Student contains two array instance variables, coursesEnrolled and courseGrades, to store the courses a student is taking and the grades in those courses. Redo the grade Report programming example by defining the CLASS: CourseAndGrade that has two instance variables--courseEnrolled of type Course and courseGrade of type char. Add appropriate constructors and methods in this CLASS to manipulate the instance variables. In the CLASS Student, use an array instance variable cousesEnrolled of type CourseAndGrade to store the couses a student is taking and the grade for each course.
Needed Codes:
Course.Java:
import java.io.*;
public class Course
{
private String courseName; //object to store the
//course name
private String courseNo; //object to store the
//course number
private int courseCredits; //variable to store the
//course credits
//Default Constructor
//The object is initialized to the default values.
//Postcondition: courseName = ""; courseNo = "";
// courseCredits = 0;
public Course()
{
courseName = "";
courseNo = "";
courseCredits = 0;
}
//Constructor
//The object is initialized according to the parameters.
//Postcondition: courseName = cName; courseNo = cNo;
// courseCredits = credits;
public Course(String cName, String cNo, int credits)
{
courseName = cName;
courseNo = cNo;
courseCredits = credits;
}
//Method to set the course information
//The course information is set according to the
//parameters.
//Postcondition: courseName = cName; courseNo = cNo;
// courseCredits = credits;
public void setCourseInfo(String cName, String cNo,
int credits)
{
courseName = cName;
courseNo = cNo;
courseCredits = credits;
}
//Method to set the course Name
//Postcondition: courseName = cName;
public void setCourseName(String cName)
{
courseName = cName;
}
//Method to set the course Number
//Postcondition: courseNo = cNo;
public void setCourseNumber(String cNo)
{
courseNo = cNo;
}
//Method to set the course credits
//Postcondition: courseCredits = credits;
public void setCourseCredits(int credits)
{
courseCredits = credits;
}
//Method to return the course information as a string
//Postcondition: The course information is returned
// as a string.
public String toString()
{
return String.format("%-12s%-15s%4s", courseNo,
courseName, courseCredits);
} //end toString
//Method to return the course name
//Postcondition: The value of courseName is returned.
public String getCourseName()
{
return courseName;
}
//Method to return the course number
//Postcondition: The value of courseNo is returned.
public String getCourseNumber()
{
return courseNo;
}
//Method to return the credit hours
//The value of the private data member courseCredits
//is returned.
public int getCredits()
{
return courseCredits;
}
//Method to copy a course’s information.
//otherCourse is copied into this course
//Postcondition: courseName = otherCourse.courseName;
// courseNo = otherCourse.courseNo;
// courseCredits = otherCourse.courseCredits;
public void copyCourseInfo(Course otherCourse)
{
courseName = otherCourse.courseName;
courseNo = otherCourse.courseNo;
courseCredits = otherCourse.courseCredits;
}
}
GradeReportProgram.java;
import java.io.*;
import java.util.*;
public class GradeReportProgram
{
public static void main(String[] args) throws
FileNotFoundException
{
int noOfStudents;
double tuitionRate;
Scanner inFile =
new Scanner(new FileReader("stData.txt"));
PrintWriter outFile =
new PrintWriter("sDataOut.out");
noOfStudents = inFile.nextInt(); //get the number
//of students
tuitionRate = inFile.nextDouble(); //get the tuition
//rate
Student[] studentList =
new Student[noOfStudents];
for (int i = 0; i < studentList.length; i++)
studentList[i] = new Student();
getStudentData(inFile, studentList);
printGradeReports(outFile, studentList, tuitionRate);
inFile.close();
outFile.close();
}
public static void getStudentData(Scanner inpFile,
Student[] sList)
{
//Local variables
String fName; //variable to store the first name
String lName; //variable to store the last name
int ID; //variable to store the student ID
int noOfCourses; //variable to store the number of courses
char isPaid; //variable to store Y/N; that is,
//is the tuition paid?
boolean isTuitionPaid; //variable to store true/false
String cName; //variable to store the course name
String cNo; //variable to store the course number
int credits; //variable to store the course credit hours
char grade; //variable to store the course grade
Course[] courses = new Course[6]; //array of objects to
//store the course
//information
char[] courseGrades = new char[6];
for (int i = 0; i < 6; i++)
courses[i] = new Course();
for (int count = 0; count < sList.length; count++)
{
//Step 1
fName = inpFile.next();
lName = inpFile.next();
ID = inpFile.nextInt();
isPaid = inpFile.next().charAt(0);
if (isPaid == 'Y') //Step 2
isTuitionPaid = true;
else
isTuitionPaid = false;
noOfCourses = inpFile.nextInt(); //Step 3
for (int i = 0; i < noOfCourses; i++) //Step 4
{
cName = inpFile.next();
cNo = inpFile.next();
credits = inpFile.nextInt();
courseGrades[i] = inpFile.next().charAt(0);
courses[i].setCourseInfo(cName, cNo, credits);
}
sList[count].setInfo(fName, lName, ID,
noOfCourses, isTuitionPaid,
courses, courseGrades); //Step 5
}//end for
} //end getStudentData
public static void printGradeReports(PrintWriter outpFile,
Student[] sList,
double tuitionRate)
{
for (int count = 0; count < sList.length; count++)
{
outpFile.print(sList[count]);
if (sList[count].getIsTuitionPaid())
outpFile.printf("Midsemester GPA: %.2f%n",
sList[count].getGpa());
else
{
outpFile.println("*** Grades are being held for "
+ "not paying the tuition. ***");
outpFile.printf("Amount Due: $%.2f%n",
sList[count].billingAmount(tuitionRate));
}
outpFile.println("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-"
+ "*-*-*-*-*-*-*-*-*-*- ");
}
} //end printGradeReports
}
Person.java:
public class Person
{
private String firstName; //store the first name
private String lastName; //store the last name
//Default constructor
//Initialize firstName and lastName to an empty string.
//Postcondition: firstName = ""; lastName = "";
public Person()
{
firstName = "";
lastName = "";
}
//Constructor with parameters
//Set firstName and lastName according to the parameters.
//Postcondition: firstName = first; lastName = last;
public Person(String first, String last)
{
setName(first, last);
}
//Method to output the first name and last name
//in the form firstName lastName.
public String toString()
{
return (firstName + " " + lastName);
}
//Method to set firstName and lastName according to
//the parameters.
//Postcondition: firstName = first; lastName = last;
public void setName(String first, String last)
{
firstName = first;
lastName = last;
}
//Method to return firstName.
//Postcondition: The value of firstName is returned.
public String getFirstName()
{
return firstName;
}
//Method to return lastName.
//Postcondition: The value of lastName is returned.
public String getLastName()
{
return lastName;
}
}
Student.java:
import java.io.*;
public class Student extends Person
{
private int sId; //variable to store the
//student ID
private int numberOfCourses; //variable to store the number
//of courses
private boolean isTuitionPaid; //variable to indicate whether
//the tuition is paid
private Course [] coursesEnrolled; //array to store
//the courses
private char [] courseGrades; //array to store the
//course grades
//Default constructor
//Postcondition: The instance variables are initialized.
public Student()
{
super();
numberOfCourses = 0;
sId = 0;
isTuitionPaid = false;
coursesEnrolled = new Course[6];
for (int i = 0; i < 6; i++)
coursesEnrolled[i] = new Course();
courseGrades = new char[6];
for (int i = 0; i < 6; i++)
courseGrades[i] = '*';
}
//Method to set a student's information
//Postcondition: The instance variables are set according
// to the parameters.
public void setInfo(String fName, String lName, int ID,
int nOfCourses, boolean isTPaid,
Course[] courses, char[] cGrades)
{
setName(fName, lName); //set the name
sId = ID; //set the student ID
isTuitionPaid = isTPaid; //set isTuitionPaid
numberOfCourses = nOfCourses; //set the number of courses
for (int i = 0; i < numberOfCourses; i++) //set the array
{
coursesEnrolled[i].copyCourseInfo(courses[i]);
courseGrades[i] = cGrades[i];
}
sortCourses(); //sort the array coursesEnrolled
}
//Method to set a student’s ID
//Postcondition: sId = ID;
public void setStudentId(int ID)
{
sId = ID;
}
//Method to set whether the tuition is paid
//Postcondition: isTuitionPaid = isTPaid;
public void setIsTuitionPaid(boolean isTPaid)
{
isTuitionPaid = isTPaid;
}
//Method to set the number of courses taken
//Postcondition: numberOfCourses = nOfCourses;
public void setNumberOfCourses(int nOfCourses)
{
numberOfCourses = nOfCourses ;
}
//Method to set the courses enrolled
//Postcondition: The array courses is copied into the
// array coursesEnrolled, the array cGrades is
// copied into the array courseGrades, and these
// arrays are sorted.
public void setCoursesEnrolled(Course[] courses,
char[] cGrades)
{
for (int i = 0; i < numberOfCourses; i++)
{
coursesEnrolled[i].copyCourseInfo(courses[i]);
courseGrades[i] = cGrades[i];
}
sortCourses();
}
//Method to return a student's grade report as a string
//Postcondition: If the instance variable isTuitionPaid
// is true, the grades are returned; otherwise
// three stars are returned.
public String toString()
{
String gReport;
gReport = "Student Name: "
+ super.toString() + " "
+ "Student ID: " + sId + " "
+ "Number of courses enrolled: "
+ numberOfCourses + " "
+ String.format("%-12s%-15s%-8s%-6s%n",
"Course No", "Course Name",
"Credits", "Grade");
for (int i = 0; i < numberOfCourses; i++)
{
gReport = gReport + coursesEnrolled[i];
if (isTuitionPaid)
gReport = gReport
+ String.format("%8s%n", courseGrades[i]);
else
gReport = gReport
+ String.format("%8s%n", "***");
}
gReport = gReport
+ " Total number of credit hours: "
+ getHoursEnrolled() + " ";
return gReport;
} //end toString
//Method to get a student’s ID
//Postcondition: The value of sId is returned.
public int getStudentId()
{
return sId;
}
//Method to return whether the tuition is paid
//Postcondition: The value of isTuitionPaid is returned.
public boolean getIsTuitionPaid()
{
return isTuitionPaid;
}
//Method to get the number of courses taken
//Postcondition: The value of numberOfCourses is returned.
public int getNumberOfCourses()
{
return numberOfCourses;
}
//Method to get a copy of a course taken
//Postcondition: A copy of coursesEnrolled[i]
// is returned.
public Course getCourse(int i)
{
Course temp = new Course();
temp.copyCourseInfo(coursesEnrolled[i]);
return temp;
}
//Method to return a course grade
//Postcondition: The value of courseGrades[i] is returned.
public char getGrade(int i)
{
return courseGrades[i];
}
//Method to return the credit hours in which a
//student is enrolled
//Postcondition: The total credits are calculated
// and returned
public int getHoursEnrolled()
{
int totalCredits = 0;
int i;
for (i = 0; i < numberOfCourses; i++)
totalCredits += coursesEnrolled[i].getCredits();
return totalCredits;
}
//Method to return the grade point average
//Postcondition: The GPA is calculated and returned
public double getGpa()
{
double sum = 0.0;
for (int i = 0; i < numberOfCourses; i++)
{
switch (courseGrades[i])
{
case 'A':
sum += coursesEnrolled[i].getCredits() * 4;
break;
case 'B':
sum += coursesEnrolled[i].getCredits() * 3;
break;
case 'C':
sum += coursesEnrolled[i].getCredits() * 2;
break;
case 'D':
sum += coursesEnrolled[i].getCredits() * 1;
break;
case 'F':
break;
default:
System.out.println("Invalid Course Grade");
}
}
return sum / getHoursEnrolled();
}
//Method to return the tuition fees
//Postcondition: The billing amount is calculated
// and returned.
public double billingAmount(double tuitionRate)
{
return tuitionRate * getHoursEnrolled();
}
//Method to sort the courses
//Postcondition: The array coursesEnrolled is sorted.
// The grades for each course, in the
// array courseGrades, are also reorganized.
private void sortCourses()
{
int minIndex;
Course temp = new Course(); //variable to swap data
String course1;
String course2;
char tempGrade;
for (int i = 0; i < numberOfCourses - 1; i++)
{
minIndex = i;
for (int j = i + 1; j < numberOfCourses; j++)
{
//get course numbers
course1 =
coursesEnrolled[minIndex].getCourseNumber();
course2 = coursesEnrolled[j].getCourseNumber();
if (course1.compareTo(course2) > 0)
minIndex = j;
}//end for
temp.copyCourseInfo(coursesEnrolled[minIndex]);
coursesEnrolled[minIndex].copyCourseInfo(coursesEnrolled[i]);
coursesEnrolled[i].copyCourseInfo(temp);
tempGrade = courseGrades[minIndex];
courseGrades[minIndex] = courseGrades[i];
courseGrades[i] = tempGrade;
}//end for
}//end sortCourses
}
stData.txt:
Lisa Miller 890238 Y 4
Mathematics MTH345 4 A
Physics PHY357 3 B
ComputerSci CSC478 3 B
History HIS356 3 A
Bill Wilton 798324 N 5
English ENG378 3 B
Philosophy PHL534 3 A
Chemistry CHM256 4 C
Biology BIO234 4 A
Mathematics MTH346 3 C
Dandy Goat 746333 Y 6
History HIS101 3 A
English ENG328 3 B
Mathematics MTH137 3 A
Chemistry CHM348 4 B
ComputerSci CSC201 3 B
Business BUS128 3 C
Explanation / Answer
Answer:
Note: Code is modified as per the above classes which has already implemented
Code:
public class CourseAndGrade
{
private int numberOfCourses;
private Course [] courseEnrolled;
private char [] courseGrade;
public CourseAndGrade()
{
super();
courseEnrolled = new Course[6];
for (int i = 0; i < 6; i++)
courseEnrolled[i] = new Course();
courseGrade = new char[6];
for (int i = 0; i < 6; i++)
courseGrade[i] = '*';
}
public void setCoursesEnrolled(Course[] courses,char[] cGrades)
{
for (int i = 0; i < numberOfCourses; i++)
{
courseEnrolled[i].copyCourseInfo(courses[i]);
courseGrade[i] = cGrades[i];
}
sortCourses();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.