• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Help With Java Classes  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to make a java class that has a student, two test grades, and a student ID to identify the student, then i need another student with the same parameters, but the student ID goes up by one(student1 = 1000, student2 = 1001, etc...) it needs to compare the two id`s and make sure they arent the same student either.

Please HELP!!!
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what have you done so far and where are you stuck.
NotACodeMill
TellTheDetails
 
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should make a student class that has two test grades and a static id. Then you can compare individual instances of the students in your main class when you create them. You should give more info on what you have done so far.

Alan
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Smith wrote:You should make a student class that has two test grades and a static id. Then you can compare individual instances of the students in your main class when you create them. You should give more info on what you have done so far.

Alan

The ID field should not be static. It should be an instance field that is unique for each student.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can however use a static nextID field.
 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can however use a static nextID field.

This is what I was going for as he wants it to auto increment.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is what I meant
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To compare two objects for equality you can override equals method.

Thanks
Athira
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Smith wrote:
Campbell Ritchie wrote:You can however use a static nextID field.

This is what I was going for as he wants it to auto increment.

Personally, I find that pattern of limited use on its own, since it only generates unique sequence numbers for a JVM session.
Unless you also have some way of persisting the last number generated (not quite as straightforward as you might think, particularly if you don't want to persist for every new ID), they will start from 0 again when the class is reloaded.

An alternative worth considering might be a custom SequenceNumber class or factory. Yet another, if the only requirement for the ID is that it is unique, is a UID or UUID.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!