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 9 of 9

Thread: Simple Code not working properly

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Simple Code not working properly

    This is my class:

    /**
     * The StudentReport1 class has fields for name, grade, school, of a student
     * 
     * @author Luis Fierro
     * @version 4/05/2014
     */
    public class StudentReport1
    {
        // instance variables - replace the example below with your own
        private String name;
        private String sGrade;
        private String sName;
     
        /**
         * Constructor for objects of class StudentReport1
         * @param n The student's name
         * @param sg The student's grade in school
         * @param sn The student's school name
         * 
         */
        public StudentReport1(String n, String sg, String sn)
        {
            // initialise instance variables
            name = n;
            sGrade = sg;
            sName = sn;
        }
     
        /**
         * setName method 
         * 
         * @param  n The student's name
         */
        public void setName(String n)
        {
            name = n;
        }
     
        /**
         * setSGrade method 
         * 
         * @param  sg The student's grade in school
         */
        public void setSGrade(String sg)
        {
            sGrade = sg;
        }
     
        /**
         * setSName method 
         * 
         * @param  sn The student's school name
         */
        public void setSName(String sn)
        {
            sName = sn;
        }
     
        /**
         * getName method 
         * @return n Return the name of the student 
         */
        public String getName()
        {
            return name;
        }
     
        /**
         * getSGrade method
         * @return sg Return the student's grade in school
         * 
         */
        public String getSGrade()
        {
            return sGrade;
        }
     
        /**
         * getSName method
         * @return sn Return the student's school name
         */
        public String getSName()
        {
            return sName;
        }
     
        public String toString()
        {
            return "Name: " + name + "\nGrade: " + sGrade + "\nSchool"
                    + sName;
        }
    }

    Then this is what I use to test my class:
    import javax.swing.*;
    /**
     * The  ReportGenerator class uses the StudentReport1 class to simulate the report 
     * of a student. Holds fields for number of classes,grades received, average, and gpa.
     * 
     * @author Luis Fierro
     * @version 4/16/2014
     */
    public class ReportGenerator
    {
     
        /**
         * getData method creates a StudentReport1 object
         * pobulated with data from the user
         * 
         * @return A reference to StudentReport1
         */
         public static StudentReport1 setData()
        {
            //variables to hold  the information
             String name;
             String schoolGrade;
             String schoolName;
     
            //read the information
            name=JOptionPane.showInputDialog("Enter your name: ");
            schoolGrade=JOptionPane.showInputDialog("Enter your grade in school: ");
            schoolName=JOptionPane.showInputDialog("Enter the name of your school: ");
     
            //create a StudentReport entry
            StudentReport1 data = new StudentReport1(name, schoolGrade, schoolName);
     
            //return a reference to the object
            return data;
     
        }
     
     
     
       public static double getAverage(double total, int numOfClasses)
       {
          return total / numOfClasses;
     
       }
     
       public static double getGpa(double average)
       {
          return (average*4)/100;
     
       }
     
     
       /**
         * showData method shows the data stored in  
         * the  StudentReport1 object
         * 
         * @param data The data to show 
         */
     
       public static void getData(StudentReport1 data, double average, double gpa)
     
       {
          JOptionPane.showMessageDialog(null, "Name :" + data.getName() + "\nGrade" 
                                        + data.getSGrade() + "\nSchool: "
                                        + data.getSName()
                                        + "\nAverage: " + average + 
                                        "\nGPA: " + gpa + ".");
       }
     
       public static void main(String [] args)
       {
     
          // declare variables
        int numOfGrades=0;       //number of classes of the student
        int grades;               //grades received in each class
        double average;                //average= sum of all grades / number of classes
        double gpa;                    //average converted to scale 1-4
        int total=0;                    //total of the grades 
        String trash;                   //to convert s  tring to double
     
     
         setData();
     
     
         //ask number of grades
          trash=JOptionPane.showInputDialog("Number of grades:");             
            numOfGrades=Integer.parseInt(trash);                                                                                                     
     
          //get the grades added together in order to calculate the average
            for (int i = 1; i <=numOfGrades; i++) 
                    {
     
                trash=JOptionPane.showInputDialog("Test " + i + " : " );
                grades=Integer.parseInt(trash);
     
     
                    total+=grades;  
     
                    }
     
            // Get the average
          average = getAverage(numOfGrades, total);
     
          // Get the gpa
          gpa = getGpa(average);
     
     
          JOptionPane.showMessageDialog(null, "The students average is: " + average + "\nThe student's GPA is: " + gpa);
     
        }
    }
    Everything compiles without errors, but when I run this in order to test everything it ask me my information and my grades
    on my exams, but then it only displays the gpa and average, it should display the user information too. And for some reason
    it is giving me some weird numbers for the average and the gpa but I checked my calculations and they are right.
    So basically my two questions are
    How can it display the user information too?
    Why the calculations don't work?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Simple Code not working properly

    This is a lot of code for other people to debug for you. You might want to work on isolating each problem and getting them into the form of an SSCCE or MCVE.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Gerock7 (May 1st, 2014)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple Code not working properly

    Can you copy the full contents of the command prompt window from when you execute the program and paste it here?

    Add println statements that print what is shown in the JOPtionPane windows.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Gerock7 (May 1st, 2014)

  6. #4
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Simple Code not working properly

    The student average is: 0.01
    The student GPA is: 4.0E-4
    When I just enter one test and 100 as the grade
    Attached Images Attached Images

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple Code not working properly

    Please copy and paste what is printed, not images. It is not possible to copy text from an image to include in a response.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Gerock7 (May 1st, 2014)

  9. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Simple Code not working properly

    I wrote above the image what I am getting.
    That is all I am getting:

    The student average is: 0.01
    The student GPA is: 4.0E-4
    When I just enter one test and 100 as the grade

  10. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple Code not working properly

    How are you trying to debug the code? I use println() statements to print out the values of variables are they are changed and used.
    Add some println() statements that print out the values of the all the variables used in the computations to compute the average. The print outs will show you what the code is doing and help you understand what the problem is.

    When I do a find with "student average" I do NOT find that String in the code?
    Is that exactly what the program displays? You need to copy and paste here EXACTLY what the program prints out so it can be used to search the code and find the statement that prints it.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #8
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Simple Code not working properly

    When I run the program:
    First window pops and ask name
    Second window school grade
    Third window school name
    Fourth window number of grades
    Fifth and possibly more windows Enter the grades
    Last window only displays GPA and average

    I tried entering one test score and I entered 100 as the grade
    The window displayed
    Why am I getting this?
    The student average is: 0.01
    The student GPA is: 4.0E-4

    --- Update ---

    It doesn't display all of the information of the user, only the average and gpa and for some reason is doing the calculations wrong

  12. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple Code not working properly

    doing the calculations wrong
    Did you try adding some println statements so you can see what the code is doing?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. oracle Reading Directly from a URL code not working properly
    By reapaz1 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 9th, 2013, 10:25 AM
  2. Simple code, cant get it working.
    By Malmen in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 8th, 2011, 03:28 PM
  3. Replies: 4
    Last Post: August 10th, 2011, 11:16 AM

Tags for this Thread