|
-
Feb 12th, 2007, 08:56 AM
#1
Thread Starter
Lively Member
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;
}
}
-
Feb 13th, 2007, 10:58 PM
#2
Addicted Member
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);
-
Feb 16th, 2007, 11:21 PM
#3
Lively Member
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.
-
Feb 17th, 2007, 12:12 AM
#4
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|