Program 1 (25 points): An academic advisor wants a program that will compute a s
ID: 3887284 • Letter: P
Question
Program 1 (25 points): An academic advisor wants a program that will compute a student GPA by reading a file that contains the name of the courses taken, credit hours for each course, and grades earned from each course. The program will take, as run-time parameters, the folder directory where the grades file is stored and the first and last name of the student. For this testing your program, use the posted grades file called “grade/Irving-Whitewood.cvs”
To complete this assignment, we will use class Student.java completed from Chapter 10. The purpose for this is to reuse functionality we have already created. Create a new java file, called MyStudent.java that inherits from the Student class. Inside this new file, do the following:
1. Static properties and behaviors
a. Create a private static ArrayList of courses Î private static ArrayList courseList;
b. A public static method to add to the ArrayList Î public static void addCourse(String courseName, int creditHours, char letterGrade); You need to implement code that creates a Course object and adds that object to the courseList.
3. Non-static properties and behaviors
a. Create a public constructor that takes two Strings Î public MyStudent(String fname, String lname); (implementation details explained below): i. Determine the number of courses from the size of the courseList. ii. Call super(fname, lname, numberOfCourses)
b. A public method to compute GPA Î use the String toString() method (implementation details explained below): i. For each element in courseList 1. get courseName, creditHours, and letterGrade from the elemen 2. Call super.createCourse(courseName, creditHours, letterGrade) to create a course ii. return super.toString() to get the output Inside your ComputeStudentGPAProgram.java, write a main method that serves as the main launching code. This program will read the file (“grade/Irving-Whitewood.cvs”) that contains the grades for a student and the first and last name of the student. The program determines the GPA for the student. Here, you will read the csv file (“grade/Irving-Whitewood.cvs”) to determine the student’s GPA. For each line in the file, you will need store this information by calling method addCourse() from class MyStudent. Once you have completed reading the file, close the file, and then proceed to do the following: • invoke the toString() method of this object inside a System.out.println statement The output of your code will be: Output: Student {firstname}, {lastname} has a {GPA value} GPA Example: John Doe has a 3.4 GPA NOTE You will be provided with a file grade/Irving-Whitewood.cvs. Note that you cannot hardcode the first name and last name into your program directly. Your program MUST read the filename during the code execution. The structure of file Irving-Whitewood.cvs is: CourseName1,creditHours,grade CourseName2,creditHours,grade CourseName3,creditHours,grade CourseName4,creditHours,grade CourseName5,creditHours,grade Example: Calculus,4,A Physics,4,A Computer Science,4,B English,3,C AmericanHistory,2,B Inside your ComputeStudentGPAProgram, you will need to use a mechanism to parse each line. While there are a variety ways to accomplish this, the String.split() method is the most feasible.
public class Person
{
protected String name;
protected String address;
protected String MobileNumber;
protected String email;
public Person(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getMobileNumber()
{
return MobileNumber;
}
public void setMobileNumber(String MobileNumber)
{
this.MobileNumber = MobileNumber;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public String toString()
{
return this.getClass() + ": " + getName();
}
}
public class Student extends Person
{
public static final String FRESHMAN = "Freshman";
public static final String SOPHOMORE = "Sophomore";
public static final String JUNIOR = "Junior";
public static final String SENIOR = "Senior";
protected String status;
public Student(String name)
{
super(name);
}
public Student(String name, String status)
{
super(name);
this.status = status;
}
public String toString()
{
return this.getClass() + ": " + getName();
}
}
Explanation / Answer
Hi,
Please see below the classes:
Person.java
public class Person
{
protected String name;
protected String address;
protected String MobileNumber;
protected String email;
public Person(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getMobileNumber()
{
return MobileNumber;
}
public void setMobileNumber(String MobileNumber)
{
this.MobileNumber = MobileNumber;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public String toString()
{
return this.getClass() + ": " + getName();
}
}
Student.java
public class Student extends Person
{
public static final String FRESHMAN = "Freshman";
public static final String SOPHOMORE = "Sophomore";
public static final String JUNIOR = "Junior";
public static final String SENIOR = "Senior";
protected String status;
private int noOfCourses ;
private static int total ;
public Student(String name)
{
super(name);
total =0;
}
public Student(String name, String status)
{
super(name);
this.status = status;
total =0;
}
public Student(String fName, String lName,int noOfCourses)
{
super(fName +" "+lName);
this.status = status;
noOfCourses = noOfCourses;
total =0;
}
// method to create Course
public static Course createCourse(String courseName, int creditHours, char letterGrade){
//Creating course object
Course course = new Course(courseName, creditHours, letterGrade);
if(letterGrade == 'A'){
total = total + 100;
}
else if(letterGrade == 'B'){
total = total + 80;
}
else if(letterGrade == 'C'){
total = total + 60;
}
return course;
}
public int getNoOfCourses() {
return noOfCourses;
}
public void setNoOfCourses(int noOfCourses) {
this.noOfCourses = noOfCourses;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public String toString()
{
return "Name: "+this.name +" Total : "+this.getTotal();
}
}
Course.java
public class Course {
private String courseName;
private int creditHours;
private char letterGrade;
//Constructor
public Course(String courseName, int creditHours, char letterGrade) {
super();
this.courseName = courseName;
this.creditHours = creditHours;
this.letterGrade = letterGrade;
}
//Getters and Setters
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public int getCreditHours() {
return creditHours;
}
public void setCreditHours(int creditHours) {
this.creditHours = creditHours;
}
public char getLetterGrade() {
return letterGrade;
}
public void setLetterGrade(char letterGrade) {
this.letterGrade = letterGrade;
}
}
MyStudent.java
import java.util.ArrayList;
public class MyStudent extends Student {
private static ArrayList<Course> courseList = new ArrayList<Course>();
//Constructor
public MyStudent(String fname, String lname){
super(fname,lname, courseList.size());
}
// method to add to the ArrayList
public static void addCourse(String courseName, int creditHours, char letterGrade){
//Creating course object
Course course = new Course(courseName, creditHours, letterGrade);
//Adding to the courseList
courseList.add(course);
}
public void computeGPA(){
double GPA= 0;
for(Course course : courseList){ //looping through the courselist
super.createCourse(course.getCourseName(), course.getCreditHours(), course.getLetterGrade());
//getting GPA
}
if (this.getTotal()>400){
GPA = 5;
}
else if (this.getTotal()>300){
GPA = 4;
}
else if (this.getTotal()>200){
GPA = 3;
}
System.out.println(super.toString() );
System.out.println("GPA: "+GPA);
}
}
ComputeStudentGPAProgram.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.sun.org.apache.bcel.internal.generic.FNEG;
public class ComputeStudentGPAProgram {
/**
* @param args
*/
public static void main(String[] args) {
//Reading the content of a file named "Irving-Whitewood.cvs"
BufferedReader br = null;
FileReader fr = null;
int lineCount =0;
String fileName = args[0];
String FName = args[1];
String lName = args[2];
String fileContent = "";
try {
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
String [] lineArr = sCurrentLine.split(" ");
String courseName = lineArr[0];
int creditHours = Integer.valueOf(lineArr[1]);
String grade = lineArr[2];
MyStudent.addCourse(courseName, creditHours, grade.charAt(0));
}
lineCount++;
} catch (IOException e) {
System.out.println("Invalid File!");
}
//calling copmut GPA
MyStudent student = new MyStudent(FName,lName);
student.computeGPA();
}
}
Sample input file:
Irving-Whitewood.cvs
Calculus 4 A
Physics 4 A
Computer Science 4 B
English 3 C
AmericanHistory 2 B
Running the class:
javac ComputeStudentGPAProgram.java
java ComputeStudentGPAProgram Irving-Whitewood.cvs Kevin Peter
Sample output:
Name: Kevin Peter Total : 420
GPA: 5.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.