Results 1 to 4 of 4

Thread: Stupid Prob

  1. #1

    Thread Starter
    Lively Member tHa-gRiM's Avatar
    Join Date
    Sep 2005
    Posts
    126

    Stupid Prob

    So I'm in a Java class, and I did all this but the Gpa isnt returning a result, it just returns 0.0 when it gets ran, and for the life of me I cant find the problem, here the code.

    Code:
    import javax.swing.JOptionPane;
    public class ShowStudent
    {
    	public static void main(String[] args)
    	{
    		Student oneStudent = new Student();
    		int showId;
    		int showHours;
    		int showEarned;
    		double showGpa;
    		oneStudent.setidNum(Integer.parseInt(JOptionPane.showInputDialog(null, "ID Number: ")));
    		oneStudent.setcHours(Integer.parseInt(JOptionPane.showInputDialog(null, "Total Credit Hours Earned: ")));
    		oneStudent.setpEarned(Integer.parseInt(JOptionPane.showInputDialog(null, "Total Points Earned: ")));
    		showId = oneStudent.getidNum();
    		showHours = oneStudent.getcHours();
    		showEarned = oneStudent.getpEarned();
    		showGpa = oneStudent.getGpa();
    		JOptionPane.showMessageDialog(null, "ID Number: " + showId + "\nTotal Credit Hours Earned: " + showHours + "\nTotal Points Earned: " + showEarned 
    		+ "\nGPA: " + showGpa);		
    	}
    }
    Code:
    public class Student
    {
        private int idNum;
        private int cHours;
        private int pEarned;
        private double Gpa;
        public int getidNum()
        {
    	    return idNum;
        }
        public int getcHours()
        {
    	    return cHours;
        }
        public int getpEarned()
        {
    	    return pEarned;
        }
        public double getGpa()
        {
            Gpa = cHours/pEarned;
            
    	    return Gpa;
        }
        public void setidNum(int showId)
        {
    	    idNum = showId;
        }
        public void setcHours(int showHours)
        {
    	    cHours = showHours;
        }
        public void setpEarned(int showEarned)
        {
    	    pEarned = showEarned;
        }
        public void setGpa(double showGpa)
        {
    	    Gpa = showGpa;
        }
    }

  2. #2
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: Stupid Prob

    In your Student Class

    the calculation

    Code:
    Gpa = cHours/pEarned;
    is double = int/int

    now the int/int will return an int value (in your case cHours is less than pEarned so the result is 0.something, but the something is being trimmed off since it is an int)

    to prevent this you must first cast the variables to double and then divide, this can be done as follows

    Code:
    Gpa = (double)(cHours)/(double)(pEarned);

  3. #3
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Re: Stupid Prob

    Code:
    Gpa = (double)(cHours)/(double)(pEarned);
    Really you only need to cast one of the values. Java will automatically promote the other int to a double. Not a big deal, just being picky.

    Also, it is good programming practice to check for possible division by zero exceptions before doing any division. You might just want to add a little if...else before you divide to see if pEarned is 0.

    This doesn't necessarily pertain to your problem, but it might cause some down the line.

  4. #4
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: Stupid Prob

    yea i guess you only have to do one of them, or multiplying one of them by 1.0 or something like that works too.. I guess we all have our own little way of doing things like this

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width