• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trying to do my first Java program

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good morning,
I recently started learning Java in my college, and I've been given a task to complete the code for a class with a unidirectional association with another (OnlineCourse -> Authors), in this task I can only work with simple arrays, I can't use ArrayList nor Vector – using ArrayList is my next assignment. I have to work with three methods from the OnlineCourse. I have problems with the method addAuthor, this method adds one author (no duplicates) to the instance of OnlineCourse we are working with, the array has a capacity for  4 authors per OnlineCourse, if the limit is exceeded we simply won't add the last author.

The code for my method it's this:



All those println are for helping me debugging the problems. At the moment my method does as follows:
Adds correctly 4 authors. If one is duplicated it's overwrited by the next one, if there's no next one when I get the authors I can see a null in the list, and when I count the number of authors in the array the null count as one.
If I try to add a 5th author this author overwrites the last one I added, that's not correct, the correct workflow should be "discard" the 5th author.

I'm so sorry if my code is hideous, I'm learning and my training wheels have been stolen haha. I need help debugging the code for my method. If it helps I'll leave here the output.


 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 8 you are decrementing nbrAuthors. Why? How about
Line 11, if nbrAuthors is a quanity then this loop will go one too far.
Test for duplicate on lines 14-17 is ok, however for each author that is not a duplicate line 20 would be executed. This will overwrite authors[nbrAuthors] over and over. You shouldn't be doing this write inside the loop. Instead, setup a boolean variable, e.g. "isDuplicate", before the loop and inside your duplicate detection set this to true. Then only after the loop completes use this variable to decide whether or not to add the new author. Only if you actually add it do you want to increment nbrAuthors.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume that you've overridden equals() in order to make this work properly.
 
Jordi Sedo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:On line 8 you are decrementing nbrAuthors. Why? How about
Line 11, if nbrAuthors is a quanity then this loop will go one too far.
Test for duplicate on lines 14-17 is ok, however for each author that is not a duplicate line 20 would be executed. This will overwrite authors[nbrAuthors] over and over. You shouldn't be doing this write inside the loop. Instead, setup a boolean variable, e.g. "isDuplicate", before the loop and inside your duplicate detection set this to true. Then only after the loop completes use this variable to decide whether or not to add the new author. Only if you actually add it do you want to increment nbrAuthors.



Hello Carey, and a big thank you for your response. It helped me a lot. I'm afraid I didn't override the equals method on OnlineCourse.java, I have it overrided on Author.java, like this:


The decrement in the if condition at line 8, is where I try to avoid getting:


But my "solution" isn't the correct way to do that I assume. Any tip on how can I approach this problem?
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

After you modify your code please re-post it so we can stay in sync with your edits.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your equals() method is functionally correct but I might suggest some tweaks to make it easier to follow.
Note that the variable name should be firstName not firstname. You need to use camel case.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ooops
 
Jordi Sedo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Carey, i'll try to implement those improvements to the equals override, after your tips my method looks as follows, and it works like a charm:



It works great, thank you very much again. Now I'll be trying to print the authors in a course sorting by lastName, but I'll must do this alone or I'll never learn. Thank you Carey, you're great!
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of
use
 
Jordi Sedo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Done, it's for a technical reason or more like a good practise?
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jordi Sedo wrote:Done, it's for a technical reason or more like a good practise?

Functionally equivalent. Follows more normal Java convention.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, it's easier to make a mistake when writing if(isDuplicated!=true).  And it's advisable that if you can write something in code that's shorter (as long as you aren't obfuscating the meaning) it's better.
 
Jordi Sedo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, thank you Carey!
 
Jordi Sedo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:Also, it's easier to make a mistake when writing if(isDuplicated!=true).  And it's advisable that if you can write something in code that's shorter (as long as you aren't obfuscating the meaning) it's better.



Okay I'll try to work like that, thank you to you all!
See you in the forums
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This will cause an out of bounds error. Also, if you find a duplicate you still want to break out of the loop.
 
Jordi Sedo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll put that break on his place. That makes me think, I read that using break in java wasn't a good practise, but on my case it's the only way, am I right?
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Another way to do it without using break.
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Jordi Sedo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to you all! You helped me a lot!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic