Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 14th, 2007, 10:57 PM   #1
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 342
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
first java assignment

This is my first programming assignment. I am new to java but have some backgroun in qbasic and C++. I would like your comments and suggestions for making this code better.
java Syntax (Toggle Plain Text)
  1. /*
  2. Author: *mrynit*
  3. Date: 1-14-07
  4. Purpose: print a table of items and prices for half and full day rentals.
  5. prompt the user to pick from 1,2,3 corisding to the equip items and H/F for
  6. half or full day rental. use a switch statment to pick items from list.
  7. use a do while loop based on a Y/N user entry.
  8. there is error checking, program works for only vaild entries.
  9. every thing that needs to be commented is, every thing else is strait forward
  10. */
  11.  
  12. import java.util.Scanner;//console I/O
  13. import java.text.*; //formating??
  14. import java.util.Locale; //US dollar format
  15.  
  16. public class EquipRental
  17. {
  18. public static void main(String[] args)
  19. {
  20. //***static prices for equip***
  21. //chose int but could use many different types.
  22. //dont need to be anal about every thing; this is java!
  23. final int RUG_CLEANER = 16;
  24. final int LAWN_MOWER = 12;
  25. final int PAINT_SPRAYER = 20;
  26. final int DEPOSIT = 30;
  27.  
  28. //intialize new scanner object
  29. Scanner scanner = new Scanner(System.in);
  30.  
  31. //intialize new US currency format
  32. NumberFormat nf = NumberFormat .getCurrencyInstance (Locale.US);
  33.  
  34. /***non static variables**
  35.   !!!!!!!!!
  36.   again must be decalered here and not inside the do while loop beace
  37.   its scope wont let it be used as a sentianl var.
  38.   !!!!!!!!!
  39.   again is the sentinal variable to exit the do loop. holds Y/N
  40.   */
  41. String again = "";
  42.  
  43. //the bill wont ever be big enough to use double
  44. //must be float beacue of multiplication used latter is a float opperation
  45. //i use * 1.5F to fore multiplication as a float not as defual double.
  46. float totalBill = 0.0F;
  47.  
  48. //print equip and price table. not the \t are seperated so it look better
  49. System.out.println("Piece of Equipment" + "\t" + "Half-Day" + "\t" + "Full-Day");
  50. System.out.println("1. Rug cleaner" + "\t\t" + "$16.00" + "\t\t" + "$24.00");
  51. System.out.println("2. Lanw mower" + "\t\t" + "$12.00" + "\t\t" + "$18.00");
  52. System.out.println("3. Paint sprayer" + "\t" + "$20.00" + "\t\t" + "$30.00");
  53. System.out.println("");//force newline
  54.  
  55.  
  56. //main purchase loop
  57. //assume that customer only buys one thing at a time so the bill only
  58. //shows one itme purchased + the deposit.
  59. do
  60. {
  61.  
  62. System.out.print("Select an item (1,2,3): ");
  63. int item = scanner.nextInt();
  64.  
  65. System.out.print("Select duration (H/F): ");
  66. String duration = scanner.next();
  67. System.out.println("");//force newline
  68.  
  69.  
  70. switch (item)
  71. {
  72. case 1:
  73. System.out.print("Rug cleaner" + "\t" );
  74. if (Character.toUpperCase(duration.charAt(0)) == 'H')
  75. {
  76. System.out.println(nf.format(RUG_CLEANER) + " (Half day rental)");
  77. totalBill = RUG_CLEANER;
  78. }
  79. else
  80. {
  81. System.out.println(nf.format(RUG_CLEANER * 1.5F) + " (Full day rental)");
  82. totalBill = (RUG_CLEANER * 1.5F);
  83. }
  84. break;
  85.  
  86. case 2:
  87. System.out.print("Lawn mower" + "\t" );
  88. if (Character.toUpperCase(duration.charAt(0)) == 'H')
  89. {
  90. System.out.println(nf.format(LAWN_MOWER) + " (Half day rental)");
  91. totalBill = LAWN_MOWER;
  92. }
  93. else
  94. {
  95. System.out.println(nf.format(LAWN_MOWER * 1.5F) + " (Full day rental)");
  96. totalBill = (LAWN_MOWER * 1.5F);
  97. }
  98. break;
  99.  
  100. case 3:
  101. System.out.print("Paint sprayer" + "\t" );
  102. if (Character.toUpperCase(duration.charAt(0)) == 'H')
  103. {
  104. System.out.println(nf.format(PAINT_SPRAYER) + " (Half day rental)");
  105. totalBill = PAINT_SPRAYER;
  106. }
  107. else
  108. System.out.println(nf.format(PAINT_SPRAYER * 1.5F) + " (Full day rental)");
  109. totalBill = (PAINT_SPRAYER * 1.5F);
  110. break;
  111.  
  112. default:
  113. System.out.println("No vaild entry.Run again using only 1,2,3");
  114. System.exit(0);
  115. break;
  116. }
  117.  
  118. totalBill += DEPOSIT;//auto type cast
  119. System.out.println("Deposit" + "\t\t" + nf.format(DEPOSIT) + "\n");
  120. System.out.println("Total" + "\t\t" + nf.format(totalBill) + "\n");
  121.  
  122. System.out.print("Another?(Y/N): ");
  123. again = scanner.next();//note how it is not declared here b/c scope.
  124. System.out.println("");//force newline
  125. }
  126. //is that too much for a condtional?
  127. while(Character.toUpperCase(again.charAt(0)) != 'N');
  128. System.out.println("Session Over");
  129. }
  130. }
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Jan 14th, 2007, 11:38 PM   #2
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
Not bad for a first programming assignment!

I only see one little thing. After a user enters invalid data, you have the System.exit(0); and then the break statement. Take out the break statement because it is unreachable. It's located in the default case.
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Jan 14th, 2007, 11:48 PM   #3
BinarySurfer
Programmer
 
BinarySurfer's Avatar
 
Join Date: Dec 2006
Posts: 53
Rep Power: 0 BinarySurfer is an unknown quantity at this point
Yeah, it looks great! As reggaeton_king said, the break statement after the System.exit(0); is unneeded. I suggest using a continue statement instead of killing the program, it makes it "user friendly".

Last edited by BinarySurfer; Jan 15th, 2007 at 12:43 AM. Reason: oops...
BinarySurfer is offline   Reply With Quote
Old Jan 15th, 2007, 1:03 AM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 909
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
The data type for equipment prices should be double. Also, when you print the list of prices, you should use the stored prices rather than hard-coding them:
Java Syntax (Toggle Plain Text)
  1. System.out.println("1. Rug cleaner" + "\t\t" + "$16.00" + "\t\t" + "$24.00");
should be
Java Syntax (Toggle Plain Text)
  1. System.out.println("1. Rug cleaner" + "\t\t" + nf.format(RUG_CLEANER) + "\t\t" + nf.format(RUG_CLEANER*1.5));
In Java using double rather than float is almost always preferable.

Just a nitpick, but to print a newline, all you need to do is exclude the parameter:

Java Syntax (Toggle Plain Text)
  1. System.out.println();
Your program resets the totalBill each time the user chooses to continue; if that is your intent, it is not a problem. However, you should alert the user if this is the case.

When you perform a mathematical operation with a floating-point data type (say, a double) and a non-floating-point data type (say, an int) the result will be a floating-point number, so the 1.5F in the following line of code could just as easily be 1.5:
Java Syntax (Toggle Plain Text)
  1. System.out.println(nf.format(RUG_CLEANER * 1.5F) + " (Full day rental)");
You do not need to use parentheses as often as you are; for example, you can remove them from the following line of code with no ill effect:
Java Syntax (Toggle Plain Text)
  1. totalBill = (LAWN_MOWER * 1.5F);
One last thing is that you should consider throwing an exception to terminate the application rather than printing an error message to stdout. If you stick with your way of exiting the program, you should note that System.exit(0) signals a successful execution while any other number signals that an error occured.

All in all, not bad for a first program.

Last edited by titaniumdecoy; Jan 15th, 2007 at 2:01 AM.
titaniumdecoy is offline   Reply With Quote
Old Jan 15th, 2007, 3:06 AM   #5
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 342
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
I have totalBill declared as float b/c i thought the bill would never get big enough to require a double varriable. so when I did my LAWN_MOWER * 1.5 I would get an error saying "found double, required float". I remembered that java by defualt is double so I added the F to 1.5 make it a float operation. That fixed my problem.

Quote:
Originally Posted by titaniumdecoy
Also, when you print the list of prices, you should use the stored prices rather than hard-coding them:
yeah thats not a bad idea, i never thought to do that.


Quote:
Originally Posted by titaniumdecoy
Your program resets the totalBill each time the user chooses to continue; if that is your intent, it is not a problem. However, you should alert the user if this is the case.
well by the wording of the question it seems like the user picks one item gets the bill and can run the whole all over again from the start with totalBill = 0.

Quote:
Originally Posted by titaniumdecoy
You do not need to use parentheses as often as you are; for example, you can remove them from the following line of code with no ill effect:
yeah i kinda do that just to emphasize a grouping of math

Quote:
Originally Posted by titaniumdecoy
One last thing is that you should consider throwing an exception to terminate the application rather than printing an error message to stdout. If you stick with your way of exiting the program, you should note that System.exit(0) signals a successful execution while any other number signals that an error occured.
not sure what you mean; could you expand a bit? the teacher said we didnt need to wory about error handling.

thanks for the help. I will be here alot over the next months
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Jan 15th, 2007, 5:36 AM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by mrynit View Post
not sure what you mean; could you expand a bit? the teacher said we didnt need to wory about error handling.
You probably haven't come across "exceptions" yet, though you may know about them from C++. They're a common way of handling problematic situations your program may get into.

titaniumdecoy is suggesting that instead of:
Java Syntax (Toggle Plain Text)
  1. System.out.println("No vaild entry.Run again using only 1,2,3");
  2. System.exit(0);
You instead write something like:
Java Syntax (Toggle Plain Text)
  1. throw new RuntimeException("No valid entry. Run again using only 1, 2, 3");
However, even if you don't use exceptions, it's more conventional to print to System.err on error messages, and to end with System.exit(1), to indicate there was a problem:
Java Syntax (Toggle Plain Text)
  1. System.err.println("No vaild entry.Run again using only 1,2,3");
  2. System.exit(1);
Arevos is offline   Reply With Quote
Old Jan 15th, 2007, 10:07 AM   #7
BinarySurfer
Programmer
 
BinarySurfer's Avatar
 
Join Date: Dec 2006
Posts: 53
Rep Power: 0 BinarySurfer is an unknown quantity at this point
Isn't killing the program a little drastic? Replacing System.exit(0) with a continue statement makes it run smoother when a incorrect entry is made instead of having to re-run the program over a simple mistake.
BinarySurfer is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming with Java: Tutorial ReggaetonKing Java 7 May 20th, 2008 11:58 AM
Special browser in Java (Project) stalefish Java 3 Feb 9th, 2008 5:22 PM
First Java Program duale2005 Java 3 May 22nd, 2006 6:17 PM
Java programmers, game developers, artists, be ware! RPG game team is recruiting! atcomputers.us Paid Job Offers 7 Sep 25th, 2005 8:25 PM
Begin my first lesson to learn Java satimis Java 7 Mar 3rd, 2005 3:45 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC