Results 1 to 13 of 13
- 12-06-2010, 04:21 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
- 12-06-2010, 04:22 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
There are other parts to this class tester my main concern right now is getting the average to work.
-
You seem to be doing int division which is an int divided by an int, and this will always return an int. So 2/5 is 0, 3/7 is 0, 12/9 is 1, etc...
One way to correct this is to cast either the numerator or the denominator as a double:
Also, please read up on how to use code tags in my signature link below.Java Code:average = (test1+ test2+ finalExam)/(double)3;
Luck.
- 12-06-2010, 04:58 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Use CODE tags to preserve indentation on your code.
I don't think you really want t1, t2 and f to be fields in your class. You'll see why a little later.Java Code:public class Student { private String name; private int test1, test2, finalExam; private double average; private int t; private int t1, t2; private int f;
This constructor needs to initialize test1, test2 and finalExam as well.Java Code:public Student() { name = ""; test1 = 0; test2 = 0; finalExam= 0; } public Student(String name) { this.name = name; }
This is backwards. You want setGrades to set the fields on your class to the values passed in the parameters. The way you have written this method, the parameters will be re-assigned to the values already stored in the fields, and then thrown away. Note that the t1, t2 and f in this method are not the fields you declared in the class.Java Code:public void setGrades(int t1, int t2, int f) { t1 = test1; t2 = test2; f = finalExam; }
Keep this constructor close to the other two, in order to avoid confusion. Also, this code is broken -- take another good look at it.Java Code:public Student(String name, int test1, int test2, int finalExam) { this.name = name; test1= t1; test2= t2; finalExam = f; }
One trick you can use is to have your other two constructors call this one, like this:
That way you don't need to worry about forgetting to initialize fields, or initializing them differently in different constructors. Your other getters and setters are OK.Java Code:[COLOR="RoyalBlue"] public Student(String name) { Student(name, 0, 0, 0); } public Student() { Student("", 0, 0, 0); // or even Student(""); } [/COLOR]
First of all, you need to divide by 3.0 rather than 3, or else you'll get an int result rather than a double. But where do you call doAverage() anyway? If you're going to store your average as a field, then you should re-compute it every time one of its inputs changes. That means add a doAverage() call to your test1, test2 and finalExam setters. Or another strategy is to not store it as a field at all, and simply compute it in your getAverage() method. So instead of this:Java Code:private void doAverage() { average = (test1+ test2+ finalExam)/3; }
...you would do this:Java Code:public double getAverage() { return average; }
Hope that helps.Java Code:[COLOR="RoyalBlue"] public double getAverage() { double average = (test1+ test2+ finalExam) / 3.0; return average; } [/COLOR]
-Gary-Last edited by gcalvin; 12-06-2010 at 05:00 PM.
- 12-06-2010, 06:30 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Thank guys, this is very helpful, but how exactly would I use doAverage() in my setters, I want to use it to recalculate my average everytime a test is changed.
Would this be better for the method setGrades()?
public void setGrades(int t1, int t2, int f)
{
if(0 <= test1 && test1 < 100)
t1 = test1;
else
t1 = 0;
if(0 <= test2 && test2 < 100)
t2 = test2;
else
t2 = 0;
if(0 <= finalExam && finalExam < 100)
f = finalExam;
else
f = 0;
}
- 12-06-2010, 07:20 PM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Please learn to use CODE tags when posting code in the forum.
Three problems with this code:Java Code:public void setGrades(int t1, int t2, int f) { if (0 <= test1 && test1 < 100) t1 = test1; else t1 = 0; if (0 <= test2 && test2 < 100) t2 = test2; else t2 = 0; if (0 <= finalExam && finalExam < 100) f = finalExam; else f = 0; }
1) Your actual class fields are not t1, t2 and f, but test1, test2 and finalExam. You have your assignments backward, as I pointed out in your original code.
2) You should be doing this validation in your individual setter methods, then calling those setter methods from this method.
3. Get in the habit of using braces even where they are not strictly necessary (as illustrated above). It will save you headaches in the not-very-long run.Java Code:[COLOR="RoyalBlue"] public void setTest1(int test1) { if (test1 >= 0 && test1 <= 100) { this.test1 = test1; } else { this.test1 = 0; // but do you really want to do this? } doAverage(); } // write setTest2 and setFinal the same way public void setGrades(int t1, int t2, int f) { setTest1(t1); setTest2(t2); setFinal(f); } [/COLOR]
-Gary-
- 12-06-2010, 11:56 PM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
What does your code look like now? Have you implemented the fixes I discussed? What output is your code generating?
-Gary-
- 12-07-2010, 12:14 AM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
You're doing quite a few things wrong, so take them one at a time and look at them carefully. Don't be in a hurry -- it only makes it take longer.
Let's start at the beginning. First of all, fix your indentation. Sloppy style leads to sloppy thinking.
What are those last four instance variables -- t, t1, t2 and f -- for? I anticipate the answer "my code won't compile unless I have them there!" to which I answer, if the code won't compile without those instance variables, the code is wrong. Get rid of those instance variables, and fix the code.Java Code:public class Student { private String name; private int test1, test2, finalExam; private double average; private int t; private int t1, t2; private int f;
Do that much first, post your code again, and we'll take the next step.
OK, I'll give you a little more to look at:
Look closely at those last five lines. Take a careful look at the right-hand side especially.Java Code:s2gn = Student2.getName(); s2gt1 = Student2.getTest1(); s2gt2 = Student2.getTest2(); s2gf = Student2.getFinal(); s2avg = Student2.getAverage(); System.out.println("Student2.getName()= " + s1gn); System.out.println("Student2.getTest1()= " +s1gt1); System.out.println("Student2.getTest2()= " + s1gt2); System.out.println("Student2.getFinal()= " + s1gf); System.out.println("Student2.getAverage()= " + s1avg);
-Gary-
- 12-07-2010, 12:17 AM #9
Member
- Join Date
- Dec 2010
- Posts
- 45
- Rep Power
- 0
- 12-07-2010, 01:46 AM #10
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Ok first off, thanks for all your help, you helped me to pay attention things I wouldn't normally.
I have everything up and working but one thing, how do I implement my toString in the studenttester?
The toString() method should return a String in the form:
“Name has test grades test1, test2 and finalExam and an average of average.”
- 12-07-2010, 01:48 AM #11
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
here's what I have in my student class
[code]
public String toString()
{
String str;
str= name + "has test grades " + test1 +" , "+ test2 +" and " + finalExam +"and an average of " + average + ".";
return str;
}
[code/]
how do I use that in the studenttester?
- 12-07-2010, 01:53 AM #12
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
That's so simple I don't even feel bad about just giving you the answer.
An object's toString() method is automatically invoked by Java when you pass that object to a method that requires a String.Java Code:System.out.println(Student1);
By the way, it's bad style to start variable names with a capital letter. Start your classes with caps, and start your variables with lower-case letters.
-Gary-
- 12-07-2010, 02:47 AM #13
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Need Urgent Help in Implementing Interface Class
By yel_hiei in forum New To JavaReplies: 4Last Post: 07-29-2010, 01:42 PM -
How to assign unique id to each object created from same class
By srisar in forum New To JavaReplies: 2Last Post: 02-18-2010, 05:26 PM -
URGENT: How to include a library in Java class path?
By gsmurthy30 in forum New To JavaReplies: 3Last Post: 09-15-2008, 04:58 PM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM -
create a tree when a new class is created
By osval in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 08:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks