Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

First Program, any suggestions or corrections?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
 This is a first program, any suggestions or corrections?

 
Marshal
Posts: 14018
233
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I tried to create a Book object for Stephen King's "It", your code would insist on setting the title to "Harry Potter" instead. That's annoying.
 
Junilu Lacar
Marshal
Posts: 14018
233
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and Welcome to the Ranch!
 
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd suggest you compile the code, so you can see all the places where you misspelled variable names.

(There are numerous other errors, too, for the compiler to find.)

And... in the Javadoc comments for that constructor, the parameter names in the comments should be the same as the parameter names in the code.

And there's a copy-paste failure at line 51, although like Junilu I'm not a big fan of the idea of having all books default to being Harry Potter if the input data is a bit off.
 
Andre Paris
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:So if I tried to create a Book object for Stephen King's "It", your code would insist on setting the title to "Harry Potter" instead. That's annoying.



So what would you do differently?  Or are you just making a joke?
 
Junilu Lacar
Marshal
Posts: 14018
233
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Andre Paris wrote:So what would you do differently?  Or are you just making a joke?


Jokingly serious. You should give more sensible defaults or throw an IllegalArgumentException for the attributes that are mandatory if they are not provided. For string fields, "**UNKNOWN**" might be a more sensible default that you'd assign when the value provided is null or empty. I wouldn't use a condition like length < 3 though since that would preclude any legitimate titles like "It" and other similarly short titled books.
 
Paul Clapham
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exercises like this one aren't usually constrained by real-life data, they're just intended to give the learner something to practice with. In real life, for example, books can be written by a person with only one name (not first and last name). There were books written before 1998, and an ISBN has 13 digits. But if you have an assignment which says to use a particular validation, you should use that validation even if it's contrary to how things actually work in real life. That even holds true if you're working and writing an application for a company or other organization -- design errors like that will eventually be noticed.

So if your specs for the application say that if the user enters a title of less than three characters then it should be replaced by "Harry Potter" then you should definitely write code which does that. But if you're making up the specs, then be prepared for people questioning your specs when they disagree with reality.
 
Marshal
Posts: 65755
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course there is a way to avoid all those checks completely, to call new Book()
That will allow all your fields to be null (or 0 for primitives) and it will allow all sorts of strange errors or exceptions later on. I suggest you get rid of the no‑arguments constructor. If the instructions require it, query the instructions with your teacher.
 
Andre Paris
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an updated, also this is not by me, just a lazy friend who can't take the time to start his own account on this forum.  I have some programming experience but not much and not in Java.  thanks for all your help up to this point

 
Campbell Ritchie
Marshal
Posts: 65755
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you come across the this. idiom in constructors and setXXX methods?If you are using Strings and primitives the copying step becomes unnecessary. That has the advantage of avoiding awkward field names, as you have, and “exposes” the well‑chosen field names. It is the one circumstance where name shadowing is regarded as good style. Beware: that Java® Language Specification link may be difficult to read.
 
Junilu Lacar
Marshal
Posts: 14018
233
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you going to handle books that have multiple authors? What's the deal with that field being named totalISBN? Aren't ISBNs identifiers rather than numbers that you do math with?
 
Campbell Ritchie
Marshal
Posts: 65755
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The funny field name is intended to be different from the constructor parameter name. It shows how useful the this.xxx idiom is: you can then use the carefull‑chosen name for both field and parameter.

By the way: ISBNs are not numbers. You cannot do arithmetic with them. They are actually a special kind of text.
 
My pie came with a little toothpic holding up this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!