Solved

Java - customer "Integer" class

Posted on 2010-11-08
6
935 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 45
Why my table column Id is not passed to java object? 4 44
Java Eclipse Loop 3 20
passing enum to a method 3 18
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…

839 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