Solved

Java - customer "Integer" class

Posted on 2010-11-08
6
923 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:Ignyte_Software
  • 3
  • 2
6 Comments
 
LVL 7

Expert Comment

by:rodness
ID: 34088370
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
0
 
LVL 7

Expert Comment

by:rodness
ID: 34088395
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.
0
 

Author Comment

by:Ignyte_Software
ID: 34088490
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Accepted Solution

by:
rodness earned 500 total points
ID: 34088638
Instead of using double values in your GradeBook class, you need to use your immutable class.  The assignment text specifies this:  "Now, in an effort to preserve the work of the student from year to year, we need to create an immutable class called LastYearsGrades."

You used all integer values when you created your instance of GradeBook:
Gradebook MathI2010 = new Gradebook("Rob","Wells","001",94,91,87,91,97,95,91,85,86,93,91);

So essentially, you need to create LastYearsGrades which contains an immutable int value.  Which is coincidentally quite similar to the built in java.lang.Integer class... an immutable int value.  (This is what java.lang.Integer has to do with this... it's an example of what you need to mimic.)

e.g.:

final public class LastYearsGrades {
   private int grade;

   public LastYearsGrades( int grade ) {
      this.grade = grade;
   }

   // any other methods, but nothing that changes the value of "grade"
   // ...
}

Then modify your GradeBook to use this class instead of doubles.
0
 

Author Comment

by:Ignyte_Software
ID: 34088703
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
0
 
LVL 2

Expert Comment

by:rocman
ID: 34088739
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);
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
where is session ID cache stored 1 40
topping3 challenge 14 51
HashMap Vs TreeMap 12 49
Basic Java Case or If-Else statement... 3 5
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now