Link to home
Start Free TrialLog in
Avatar of Ignyte_Software
Ignyte_Software

asked on

Java - customer "Integer" class

I have a friend asking me how to accomplish the following in Java:

Create a "Gradebook" class that has a student's last name, first name, ID, and places for five test grades and six assignment grades.  From this class create an instance of it.  Now, in an effort to preserve the work of the student from year to year, we need to create an immutable class called LastYearsGrades.  From that class create an instance for MathI + the previous year (e.g., MathI2010 for the last year's math grades.)  Fill that grade book with the grades from the first instance of the grade book class that you created.

Can someone show me:

1.  What the Gradebook class would look like
2.  How to create an instance of it
3.  Create an immutable class (custom "Integer" class) called LastYearsGrades
4.  Create an instance o fthe LastYearsGrades class for MathI2010
5.  Fill in the grade book with the grades from the first instance of the grade book class that was created (steps 1 & 2).

I have the following:

class GradeBook{
   String firstname;
   String lastname;
   String ID;
   double testgrade1;
   double testgrade2;
   double testgrade3;
   double testgrade4;
   double testgrade5;
   double assignmentgrade1;
   double assignmentgrade2;
   double assignmentgrade3;
   double assignmentgrade4;
   double assignmentgrade5;
   double assignmentgrade6;


   public gradebook(String firstname, String lastname, String ID, Double, testgrade1, . . . ){

   }

}


Gradebook MathI2010 = new Gradebook("Rob","Wells","001",94,91,87,91,97,95,91,85,86,93,91);


But I don't understand what is being asked regarding the LastYearsGrades class, how to make it immutable, why it has to be an Integer class, and how to make it an Integer class.
Avatar of rodness
rodness
Flag of United States of America image

Immutable means you can't change the value of the object once you've created it.

E.g., if you look at the code for java.lang.Integer, there are two things of note:

1) the class is declared "final", e.g. "final class Integer extends Number...." which means that it can't be subclassed.

2) there are no "setValue" members or anything like that.

Integer four = new Integer( 4 );
cannot do:   four.setValue( 5 );  // no methods to do something like this... and wouldn't make sense even if you could.

You can't change the value of the Integer object... you would have to make another one instead:
four = new Integer( 5 );


This makes sense in the context of last year's grades.... you don't want to be able to change them after the fact, that would be cheating.

By a custom Integer class, they mean to make LastYearsGrades a class that behaves like java.lang.Integer:  can't derive from it to change its behavior (make the class declaration final) and that you shouldn't be able to set the value by any means other than a constructor.

Another explanation here:
http://www.javapractices.com/topic/TopicAction.do?Id=29
ps- you look like you're on the right track with the GradeBook.  Just make sure that you keep your capitalization correct (I assume that wasn't a copy and paste you did above).

for example, you declare:

class "GradeBook" ...

with constructor "gradebook"

and when you create the new one you call "new Gradebook".

This won't work unless those are all exactly the same.
Avatar of Ignyte_Software
Ignyte_Software

ASKER

I understand what Immutable means, and I understand that an Integer class is immutable, but I do not understand what Integer has to do with a custom class called LastYearsGrades.

So the root of my question is still not answered.  You got me back to the same point I'm at when you said:

"By a custom Integer class, they mean to make LastYearsGrades a class that behaves like java.lang.Integer:  can't derive from it to change its behavior (make the class declaration final) and that you shouldn't be able to set the value by any means other than a constructor."

I do not understand how to make "public final class LastYearsGrades" as an integer, and why make the class an Integer?

So I still have no idea how to create a custom integer class called LastYearsGrades that is an Integer class, and what Integer has to do with this class.
ASKER CERTIFIED SOLUTION
Avatar of rodness
rodness
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Gotcha!  Thanks.  This whole time I thought it was asking me to make the CLASS LastYearsGrades immutable, but what it means is to assign the attributes as Integer, and the integer CLASS in Java is immutable.  I kept thinking it wanted me to make something like:

final public class LastYearsGrades INTEGER {

which made no sense to me
I think in this context, the Integer class is just being given as an example of immutability. The LastYearsGrades class needs to be immutable *like* Integer, but not *be* an extension of Integer.

Anyway, I suggest you use the above class declaration ("public final class LastYearsGrades") with a constructor like ... public LastYearsGrades(GradeBook currentGrades)

You would then use the getter methods from GradeBook to initialise all the corresponding private grades in LastYearsGrades. So, to preserve the year's grades, you would then have code like ...

GradeBook currentMathIGrades = new GradeBook(...);
...
LastYearsGrades mathI2010 = new LastYearsGrades(currentMathIGrades);