Solved

Java - customer "Integer" class

Posted on 2010-11-08
6
943 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

617 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