• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a class with an array  RSS feed

 
jihwan kim
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I am working on a mini project and was wondering if I could get some guidance on creating a class that fields an array.
My program Is suppose to read from a txt file into an array. I know how to do parallel arrays, but after hours of coding I am starting to see some problems with respect to complexity and ambiguity.

If I created a class called Compensation I know that my basic constructor, that has 4 variables, would look something like this:




My question is how do I make it so that when I read in the data these 4 variables will be in the array Compensation?
Would stating

solve my issue?

I am having trouble with arrays. I dont even know If I should be dealing with arrays, however it seems like the most reasonable thing I should be doing. Reading through other forums, and through "Absolute Java 5th ed. Savitch,Mock" hasnt helped me understand it fully.

Most Appreciated
Jihwan


 
jihwan kim
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update I created a MainComp(ensation) and a Compensation class.
Does it look like I am heading in the right direction? It compiles lol, but Iam having trouble testing this because i dont know how to print the variables from the array in format.







Apologies if this looks atrocious.

Regards
Jihwan
 
Neelesh Srinivasan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey I got a solution for your problem
Find the code below

Compensation.java




CompTester.java




I created 2 different arrays to make sure that data is actually read from the file.

All the Best !!!
 
Neelesh Srinivasan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have overridden the toString Method so that you can directly print the object.
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neelesh Srinivasan wrote:I have overridden the toString Method so that you can directly print the object.

Neelesh, please use code tags when posting java code.

I have done this for now. It renders the code easier to read and debug if required.

[Edit by Winston] Thanks for that Ashish. I've edited Neelesh's original post, so your copy is just taking up space, so I've removed it.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neelesh Srinivasan wrote:Hey I got a solution for your problem

Neelesh, I'm sure your motives were the best, but we don't like people to post "solutions" here; especially in the 'Beginning Java' section.

I've left yours because, TBH, I don't think it addresses all the problems outlined in the thread (and that's another problem with posting "solutons" - they're likely to be scrutinized heavily ). you should also be aware that they may simply be deleted by moderators.

I suggest you read the DontBeACodeMill (←click) page.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jihwan kim wrote:Does it look like I am heading in the right direction?

Well, your Compensation class looks reasonable; although I suspect that Rank should probably be an enum, rather than a String.
Have a look at the StringsAreBad page, and see if you don't agree.

It compiles lol,

Hey, don't knock it. Most beginners write FAR too much code before compiling. My usual rule of thumb is "compile every 10 lines - or new method".

but Iam having trouble testing this because i dont know how to print the variables from the array in format.

Could you show us what you tried?

Just a few other minor points:
  • numberUsed is redundant, because it contains exactly the same value as data.length.
  • fields and parameters should start with LOWERCASE letters, so Rank should be rank; and the same for all your other Compensation fields.
  • Compensation data[] = new Compensation[204];
    is generally frowned on, even though it's legal. Use:
    Compensation[] data = new Compensation[204];
  • instead.

    HIH

    Winston
     
    Marshall Blythe
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    jihwan kim wrote:
    Iam having trouble testing this because i dont know how to print the variables from the array


    You could override the toString() method in your Compensation class to generate your desired string representation. You can then call this method on each Compensation instance you create and print the string as desired (e.g. to the console or a log file).

    jihwan kim wrote:



    Are you certain that you will never need to store more than 204 Compensation objects? If I run your code with an input file containing more than 204 lines of data then an ArrayIndexOutOfBoundsException occurs at runtime. You could add code to guard against this, but perhaps using a resizable Collection class would be better than using a fixed length array.

    jihwan kim wrote:



    Java naming conventions suggest that instance variable names should begin with a lowercase letter, so you should rename Rank to rank, Salary to salary, etc.. Also, what is the purpose of the data variable? Why would a Compensation instance need an array of other Compensation instances?
     
    Neelesh Srinivasan
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes Winston Sir and Ashish Sir, I will follow the protocols from now on. Thanks a lot
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!