Solved

Java - customer "Integer" class

Posted on 2010-11-08
6
926 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 74
Permutation and Combination 9 71
@SBGen Method 3 35
couple of eclipse 5 19
Dependencies in Software Design In software development, the idea of dependencies (http://en.wikipedia.org/wiki/Coupling_%28computer_programming%29) is an issue of some importance. This article seeks to explain what dependencies are and where they …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

914 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

20 Experts available now in Live!

Get 1:1 Help Now