Hello everyone,
Please am having problem with a program to calculate CGPA.
I want to collect the data using array from the main CgpaCalculator class
and pass it to CGPA class. Please i need your help to move on with this program.
This the code for the two classes below:
package cgpacalculator;
import javax.swing.JOptionPane;
/**
*
* @author SURDYHEY
*/
public class CGPACalculator {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String name = JOptionPane.showInputDialog("Enter your name:");
int level = Integer.parseInt(JOptionPane.showInputDialog(
"What level are you?"));
int courseNumb = Integer.parseInt(JOptionPane.showInputDialog(
"How many courses do you offered?"));
int semester = Integer.parseInt(JOptionPane.showInputDialog(
"Which semester are you?\n"
+ "1-First semester\n"
+ "2-Second semester\n"));
String[] courseCode = new String[courseNumb];
String[] courseTitle = new String[courseNumb];
int[] courseUnit = new int[courseNumb];
double[] score = new double[courseNumb];
CGPA cgpaData = new CGPA();
for (int index = 0; index < courseNumb; index++) {
courseCode[index] = JOptionPane.showInputDialog("Enter course " +
(index + 1) + " code:").toUpperCase();
courseTitle[index] = JOptionPane.showInputDialog("Enter course " +
(index + 1) + " title:").toUpperCase();
courseUnit[index] = Integer.parseInt(JOptionPane.showInputDialog(
"Enter course " + (index + 1) + " unit:"));
score[index] = Double.parseDouble(JOptionPane.showInputDialog(
"Enter course " + (index + 1) + " score:"));
}
cgpaData.setName(name);//This is where am having problem cgpaData.setCourseCode(courseCode); JOptionPane.showMessageDialog(null, cgpaData.getCourseCode());
cgpaData.setLevel(level);
cgpaData.setCourseNumb(courseNumb);
cgpaData.setSemester(semester);
JOptionPane.showMessageDialog(null, cgpaData.getName() + " "+
cgpaData.getLevel() + " "+ cgpaData.getCourseNumb() +" "+
cgpaData.getSemester());
}
}
.................................................. ................
package cgpacalculator;
import javax.swing.JOptionPane;
public class CGPA {
//Fields
private String name;
private int level;
private int courseNumb;
private String semester;
private String[] courseCode = new String[courseNumb];
String[] courseTitle = new String[courseNumb];
int[] units = new int[courseNumb];
private double[] score = new double[courseNumb];
char[] courseGrade = new char[courseNumb];
double [] gradePoints = new double[courseNumb];
public void setCourseCode(String[] userCourseCode) {
for (int index = 0; index < courseNumb; index++) {
courseCode[index] = userCourseCode[index];
JOptionPane.showInputDialog(courseCode[index]);
}
}
public void setCourseTitle(String userCourseTitle[]) {
courseTitle = userCourseTitle;
}
public void setCourseUnit(String userCourseUnit[]) {
courseCode = userCourseUnit;
}
public int totalCourseUnits() {
int totalUnits = 0;
for (int index = 0; index <= courseNumb; index++) {
totalUnits += units[index];
}
return totalUnits;
}
char letterGrade = 0;
public char getLetterGrade() {
double point =0.0;
for (int index = 0; index < courseNumb; index++) {
if (score[index] >= 70) {
letterGrade = 'A';
point = 5.0;
} else if (score[index] >= 60) {
letterGrade = 'B';
point = 4.0;
} else if (score[index] >= 50) {
letterGrade = 'C';
point = 3.0;
} else if (score[index] >= 45) {
letterGrade = 'D';
point = 2.0;
} else if (score[index] >= 40) {
letterGrade = 'E';
point = 1.0;
} else if (score[index] >= 0) {
letterGrade = 'F';
point = 0.0;
}
}
return letterGrade;
}
public double getTotalGradePoints()
{
double point =0.0;
double totalGradePoints=0.0;
for(int index=0; index<courseNumb; index++){
switch(letterGrade)
{
case 'A':
point=5.0;
gradePoints[index] = units[index] * point;
case 'B':
point=4.0;
gradePoints[index] = units[index] * point;
case 'C':
point=3.0;
gradePoints[index] = units[index] * point;
case 'D':
point=2.0;
gradePoints[index] = units[index] * point;
case 'E':
point=1.0;
gradePoints[index] = units[index] * point;
case 'F':
point=0.0;
gradePoints[index] = units[index] * point;
default:
JOptionPane.showMessageDialog(null, "Invalid Grade");
}
totalGradePoints +=gradePoints[index];
}
return totalGradePoints;
}
public double getGPA()
{
double gpa = getTotalGradePoints()/totalCourseUnits();
return gpa;
}
//Setters or Mutators
public void setName(String UserName) {
name = UserName;
}
public void setLevel(int UserLevel) {
level = UserLevel;
}
public void setCourseNumb(int userCourseNumb) {
courseNumb = userCourseNumb;
}
public void setSemester(int userSemester) {
if (userSemester == 1) {
semester = "First Semester";
} else if (userSemester == 2) {
semester = "Second Semester";
}
}
//Getters or Accessors
public String getName() {
return name;
}
public int getLevel() {
return level;
}
public int getCourseNumb() {
return courseNumb;
}
public String getSemester()
{
return semester;
}
//I'mhaving a problem here
public String[] getCourseCode() {
return courseCode;
}
}