• 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
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Need verification with java program if it meets the criteria  RSS feed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i have created a program and was wondering if my program has the following criteria in it:

• Different data types (eg) int, double, string, char
• Input statements (eg) Scanner class
• Output Statements (eg) System.out.print
• Mathematical, relational & Boolean operators (eg) <, >, ==, != etc
• If statements
• Iteration statements (eg) do while, for loops

and my program:

 
Marshal
Posts: 64166
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid your code is illegible because of poor formatting.
Most of your lines are too long.
You haven't put spaces round operators.
You have too many comments which don't tell the reader anything. For example line 8 includes a comment you obviously needed once to remind you to record the name, but if you had a better variable name, e.g. customerName, you could havee deleted the comment. Similarly line& 12, where the comment doesn't seem to be accurate either.
Your main() method is much too long.
Your indentation is inconsistent: look at lines 1‑5 for example.
Don't call an import declaration “input”. You are writing jargon, and jargon should be very precise.

I am afraid I don't like the design of your ifs and loops. It is always possible to avoid an input mismatch exception with a Scanner. But you would have to use a strange‑looking loop. You have to read the token in the heading of the loop:-and similarly for numbers:-Both those loops would be better in a utility class. Around here, Anne‑Marie is a popular woman's name. Could your program sell her an ice cream?

That lot should do for a start.
 
Jordan Belford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I am afraid your code is illegible because of poor formatting.
Most of your lines are too long.
You haven't put spaces round operators.
You have too many comments which don't tell the reader anything. For example line 8 includes a comment you obviously needed once to remind you to record the name, but if you had a better variable name, e.g. customerName, you could havee deleted the comment. Similarly line& 12, where the comment doesn't seem to be accurate either.
Your main() method is much too long.
Your indentation is inconsistent: look at lines 1‑5 for example.
Don't call an import declaration “input”. You are writing jargon, and jargon should be very precise.

I am afraid I don't like the design of your ifs and loops. It is always possible to avoid an input mismatch exception with a Scanner. But you would have to use a strange‑looking loop. You have to read the token in the heading of the loop:-and similarly for numbers:-Both those loops would be better in a utility class. Around here, Anne‑Marie is a popular woman's name. Could your program sell her an ice cream?

That lot should do for a start.



First of all of I'm a beginner, so it would mean a lot if you could explain to me on how to fix these problems you mentioned, and second of all I can't see where you mentioned if i succeeded in following the critea I mentioned or not
 
Sheriff
Posts: 24374
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jordan Belford wrote:I can't see where you mentioned if i succeeded in following the critea I mentioned or not



Are you really asking whether you used if-statements (and so on) in that code which you wrote?
 
Campbell Ritchie
Marshal
Posts: 64166
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jordan Belford wrote:. . . explain to me on how to fix these problems you mentioned . . .

One step at a time. Start by getting rid of the unnecessary comments. The correct the spacing and indentation. At least then you can read the code.
Call imports imports.
Read the link I provided about the main method.

I can't see where you mentioned if i succeeded in following the critea I mentioned or not

I thought by “succeeded” you meant whether you had written those control structures well.

Why is there a quote mark after Z in line 102?
 
Jordan Belford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Jordan Belford wrote:. . . explain to me on how to fix these problems you mentioned . . .

One step at a time. Start by getting rid of the unnecessary comments. The correct the spacing and indentation. At least then you can read the code.
Call imports imports.
Read the link I provided about the main method.

I can't see where you mentioned if i succeeded in following the critea I mentioned or not

I thought by “succeeded” you meant whether you had written those control structures well.

Why is there a quote mark after Z in line 102?



how to i shorten coding to be below 80 characters? how should it look like? and how do i comment properly, do I have to comment for every line?
 
Campbell Ritchie
Marshal
Posts: 64166
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stage 1: incorrect use of \n:-Stage 2; \n replaced by %n after printf():-Stage 3: Remove unnecessary and not very accurate comment:-Stage 4: Put new lines at end of line rather than beginning, just as everybody else does it:-Stage 5: Put entire argument onto line by itself, with additional indentation:-Stage 6: Still too long. Break String at some conventiient point and join it back together with + operator:-Bingo! The javac tool will preceive that as two String literals joined by + and will see that as a compile‑time constant and will reassemble the String before creating the bytecode.
 
Campbell Ritchie
Marshal
Posts: 64166
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use comments to explain what isn't obvious from reading the code. What the code in my previous post does is obvious to all reading it, so it doen't need a comment.
 
Jordan Belford
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Use comments to explain what isn't obvious from reading the code. What the code in my previous post does is obvious to all reading it, so it doen't need a comment.



is my code better or is there any other thing i should change?

 
Campbell Ritchie
Marshal
Posts: 64166
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Between lines 69 and 78, you are initialising the price thrice . . . to the same price. Why do you want all those ifs? The same thing happens elsewhere.
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!