thank you all
Printable View
thank you all
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.Code:average = (test1+ test2+ finalExam)/(double)3;
Luck.
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.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.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.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.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.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:Code:private void doAverage()
{
average = (test1+ test2+ finalExam)/3;
}
...you would do this:Code:public double getAverage()
{
return average;
}
Hope that helps.Code:[COLOR="RoyalBlue"] public double getAverage()
{
double average = (test1+ test2+ finalExam) / 3.0;
return average;
}
[/COLOR]
-Gary-
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;
}
Please learn to use CODE tags when posting code in the forum.
Three problems with this code: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.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-
What does your code look like now? Have you implemented the fixes I discussed? What output is your code generating?
-Gary-
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.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.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-
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.”
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?
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.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-
Will do, thanks for all of your help!