Results 1 to 13 of 13
  1. #1
    johnjacob is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default Urgent/ Help with average method in created class

    thank you all
    Last edited by johnjacob; 12-07-2010 at 03:05 AM. Reason: code tags added

  2. #2
    johnjacob is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    There are other parts to this class tester my main concern right now is getting the average to work.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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:

    Java Code:
    average = (test1+ test2+ finalExam)/(double)3;
    Also, please read up on how to use code tags in my signature link below.

    Luck.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Use CODE tags to preserve indentation on your 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;
    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 Student()
        {
            name = "";
            test1 = 0;
            test2 = 0;
            finalExam= 0;
        }
    
        public Student(String name)
        {
            this.name = name;
        }
    This constructor needs to initialize test1, test2 and finalExam as well.
    Java Code:
        public void setGrades(int t1, int t2, int f)
        {
            t1 = test1;
            t2 = test2;
            f = finalExam;
        }
    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 Student(String name, int test1, int test2, int finalExam)
        {
            this.name = name;
            test1= t1;
            test2= t2;
            finalExam = f;
        }
    Keep this constructor close to the other two, in order to avoid confusion. Also, this code is broken -- take another good look at it.

    One trick you can use is to have your other two constructors call this one, like this:
    Java Code:
    [COLOR="RoyalBlue"]    public Student(String name) {
            Student(name, 0, 0, 0);
        }
    
        public Student() {
            Student("", 0, 0, 0);
            // or even Student("");
        }
    [/COLOR]
    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:
        private void doAverage()
        {
            average = (test1+ test2+ finalExam)/3;
        }
    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:
        public double getAverage()
        {
            return average;
        }
    ...you would do this:
    Java Code:
    [COLOR="RoyalBlue"]    public double getAverage()
        {
            double average = (test1+ test2+ finalExam) / 3.0;
            return average;
        }
    [/COLOR]
    Hope that helps.

    -Gary-
    Last edited by gcalvin; 12-06-2010 at 05:00 PM.

  5. #5
    johnjacob is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    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;

    }

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Please learn to use CODE tags when posting code in the forum.


    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;
        }
    Three problems with this code:
    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.
    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]
    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.

    -Gary-

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    What does your code look like now? Have you implemented the fixes I discussed? What output is your code generating?

    -Gary-

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by johnjacob View Post
    here is my program so far can anyone tell me what im doing wrong
    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.
    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;
    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.

    Do that much first, post your code again, and we'll take the next step.

    Quote Originally Posted by johnjacob View Post
    here is what I have for my tester and for some reason student1 values are carrying over to student2
    OK, I'll give you a little more to look at:
    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);
    Look closely at those last five lines. Take a careful look at the right-hand side especially.

    -Gary-

  9. #9
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

  10. #10
    johnjacob is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    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.”

  11. #11
    johnjacob is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    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. #12
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by johnjacob View Post
    how do I use that in the studenttester?
    That's so simple I don't even feel bad about just giving you the answer.

    Java Code:
            System.out.println(Student1);
    An object's toString() method is automatically invoked by Java when you pass that object to a method that requires a String.

    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-

  13. #13
    johnjacob is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

Similar Threads

  1. Need Urgent Help in Implementing Interface Class
    By yel_hiei in forum New To Java
    Replies: 4
    Last Post: 07-29-2010, 01:42 PM
  2. Replies: 2
    Last Post: 02-18-2010, 05:26 PM
  3. URGENT: How to include a library in Java class path?
    By gsmurthy30 in forum New To Java
    Replies: 3
    Last Post: 09-15-2008, 04:58 PM
  4. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  5. create a tree when a new class is created
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 08:58 PM

Tags for this Thread

Posting Permissions

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