Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Promblem with CGPA calculator

  1. #1
    Junior Member
    Join Date
    Nov 2017
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Promblem with CGPA calculator

    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:"));


    }


         //This is where am having problem
            cgpaData.setCourseCode(courseCode);      
            JOptionPane.showMessageDialog(null, cgpaData.getCourseCode());
    cgpaData.setName(name);
    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;
    }

    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Promblem with CGPA calculator

    Please explain what the problem is.
    If there are errors, copy the full text of the error message and paste it here.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2017
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Promblem with CGPA calculator

    The problem am having is how can i pass the array data(i.e courseCode, courseTitle and score) to another class using setters and getters.
    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:"));
     
     
    }
     
     
         //This is where am having problem
            cgpaData.setCourseCode(courseCode);      
            JOptionPane.showMessageDialog(null, cgpaData.getCourseCode());
     
    cgpaData.setName(name);
    cgpaData.setLevel(level);
    cgpaData.setCourseNumb(courseNumb);
    cgpaData.setSemester(semester);
    JOptionPane.showMessageDialog(null, cgpaData.getName() + " "+
    cgpaData.getLevel() + " "+ cgpaData.getCourseNumb() +" "+
    cgpaData.getSemester());

    }

    }
    ....

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Promblem with CGPA calculator

    how can i pass the array data
    What is the name of the array variable and
    what is the name of the class you are trying to pass it to?

    Please wrap ALL posted code in code tags, not just one section.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. calculator
    By sabir_wahab27 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 18th, 2014, 06:27 AM
  2. Calculator
    By Vibex in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 14th, 2012, 05:23 PM
  3. Help with a calculator
    By Joshroark10 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 10th, 2012, 03:59 PM
  4. student CGPA
    By adamu adamu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 8th, 2011, 06:52 AM
  5. [SOLVED] Calculator help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 04:27 PM