Results 1 to 14 of 14
Like Tree3Likes
  • 1 Post By jim829
  • 1 Post By gimbal2
  • 1 Post By kneitzel

Thread: Storing multiple int values...?

  1. #1
    Cellie is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Storing multiple int values...?

    Hello, I'm having trouble with the following assignment:

    Write a complete Student class that allows you to use the statements such as the following in methods in outside classes, with the obvious meanings. Then write an application program that creates one student object, reads in test grades until a negative "grade" is seen (as a signal that the list of grades has ended), then prints out all available information about the Student:
    Student bob = new Student ("Robert", "Newhart");
    bob.storeGrade (23); // bob scored 23 on this test
    return bob.getTotalGrades();; // total of test scores to date
    return bob.getAverageGrade();; // for all test scores to date
    return bob.getName();; // returns "Robert Newhart"

    What I need help with is in the Student class... I'm not sure how to go about storing the grades without renewing the value of storeGrade (I'm sure what I have right now is incorrect, but I'm stuck). This is what I have so far, as you can see I left some of the grade-related bits blank for now, but all I want to know is how I can store the grades:

    Java Code:
    public class Student extends Object
    {
             private int itsTotalGrades;
             private int itsAverageGrades;
             private String itsFirstName;
             private String itsLastName;
             private int storeGrade;
    
             public Student (String first, String last)
             {      super();
                     itsFirstName = first;
                     itsLastName = last;
             }        //======================
    
             public int storeGrade (int grade)
             {      storeGrade = grade;
             }       //======================
    
             public int getTotalGrades()
             {}
    
             public int getAverageGrade()
             {}
    
             public String getName()
             {      return itsFirstName + " " + itsLastName;
             }       //=====================
    }
    Any help would be greatly appreciated

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Storing multiple int values...?

    Have you learned about arrays or ArrayLists in class?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Cellie is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Re: Storing multiple int values...?

    No, I haven't yet... I'm guessing that would explain why I'm having trouble with this. I'll look up some info on arrays then. Thanks :)

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Storing multiple int values...?

    It seems odd to me that a teacher would give an assignment that would require something that has not been discussed in class. If you are allowed to use anything you want, then I would focus on ArrayLists.

    Regards,
    Jim
    Cellie likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Storing multiple int values...?

    Pardon my ignorance, but why exactly is an array/list a necessity here? Is the requirement to print out all grades too?

    The assignment reads to me like only the total and average should be printed; those can be calculated as the grades are added without having to keep track of all the individual grades themselves. Then you don't need an array/list. In fact you could still print out all the grades without them too, if you just stick them in a String/StringBuilder. Its not pretty, but at least then you don't have to rely on material not yet covered in class.
    Cellie likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Storing multiple int values...?

    Well, I feel stupid! You are of course correct. I manufactured a requirement that didn't exist (to print out the grades). Actually, I only feel a little stupid. I was responding to the OP's question and did not focus on the actual assignment.

    Regards,
    Jim
    Last edited by jim829; 05-20-2014 at 04:43 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Cellie is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Re: Storing multiple int values...?

    Ahhh I see, thanks for pointing that out. I thought it was necessary to keep track of the grades because of bob.storeGrade(23); in the directions, but I misunderstood why that was included.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Storing multiple int values...?

    Won't you get rounding errors building up if you simply calculate the average on the fly?
    Even if the datatype for the average wasn't an int.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: Storing multiple int values...?

    Quote Originally Posted by Tolls View Post
    Won't you get rounding errors building up if you simply calculate the average on the fly?
    Even if the datatype for the average wasn't an int.
    Yes it would, but only if the magnitude of the numbers would differ too much and the larger numbers would be added first. If all numbers were ints, there wouldn't be a problem.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Storing multiple int values...?

    You do not get rounding errors when you store the sum and the count. That way you can calculate the exact average whenever required.

    Or did I miss something?

    With kind regards,

    Konrad
    gimbal2 likes this.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: Storing multiple int values...?

    Quote Originally Posted by kneitzel View Post
    You do not get rounding errors when you store the sum and the count. That way you can calculate the exact average whenever required.

    Or did I miss something?
    Yep, suppose you have a decimal calculator with two digits precision and a one digit exponent, i.e. you can represent 99E9 and 0.9E-9; try to add the numbers, 23+0.5+0.5; 23.5 can't be represented (three digits needed), so the result will be 23 (which is wrong); adding those three numbers starting from the other end (smallest first), does work: 0.5+0.5= 1 and 23+1=24; all the intermediate results can be represented exactly. So if you have to add a list of numbers, add the smallest numbers first.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Storing multiple int values...?

    I still don't get your point.

    You can have:
    Java Code:
    int sumAllGrades = 0;
    int countAllGrades = 0;
    
    public void addGrade(int grade) {
      sumAllGrades += grade;
      countAllGrades++;
    }
    So there are no intermediate results to save. The average is calculated only when required:

    Java Code:
    public double averageGrade() {
      if (countAllGrades == 0) throw new Exception ("No Grades added");
      return (double) sumAllGrades / (double) countAllGrades;
    }
    
    Or
    public int averageGrade() {
      if (countAllGrades == 0) throw new Exception ("No Grades added");
      return (sumAllGrades + countAllGrades/2) / countAllGrades; // I add half of the count so I get the rounding. if sum/count is x.5 with the count/2 it will be x+1
    }
    Konrad

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Storing multiple int values...?

    Fun thread so far, I learned a thing or two already.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: Storing multiple int values...?

    Quote Originally Posted by kneitzel View Post
    I still don't get your point.
    There is no problem with ints (except for overflow), but there is an intermediate result (you're adding the individual grade to the sum in the loop; reread my example (see above) with floating point numbers.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Storing object with int values in Arraylist.
    By Zigster in forum New To Java
    Replies: 12
    Last Post: 05-26-2012, 10:49 PM
  2. problem with storing the returned values in an array
    By amrmb09 in forum Advanced Java
    Replies: 1
    Last Post: 11-26-2011, 02:17 AM
  3. XML read and storing values
    By LiveStrong-2011 in forum New To Java
    Replies: 1
    Last Post: 05-31-2011, 12:04 PM
  4. Storing Multiple 2d Arrays in a file.
    By onifiro in forum New To Java
    Replies: 1
    Last Post: 01-11-2011, 01:39 AM
  5. question on how to check values before storing
    By SMHouston in forum New To Java
    Replies: 19
    Last Post: 09-21-2009, 11:54 PM

Posting Permissions

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