This sample Java program, and the notes that follow, are used to demonstrate the ideas taught in
Unit 2. The line numbers are not part of the Java program - they are used
for reference purposes only. This program deals with a user purchasing multiple copies of one item.
- /*******************************
* Project Sample
* Programmer: MathBits
* Date: Unit 2
* Program: EbayPurchase
********************************/
- import java.util.Scanner; //needed for input from user
- import java.text.*; //needed for decimal control of money
- public class EbayPurchase {
- public static void main (String[ ] args) {
- final double
FLAT_SHIPPING = 7.95; //constant
- double
itemCost, totalCost;
-
int number;
- String name, address;
- // Get User Information
- Scanner reply1 = new Scanner(System.in);
- System.out.println("Enter your full name:");
- name = reply1.nextLine();
- System.out.println("Enter mailing address:");
- address = reply1.nextLine();
-
- // Obtain cost of item
- System.out.println("Enter
the cost of one item in decimal form, no $" );
- itemCost = reply1.nextDouble();
- // Obtain number of that item purchased
- System.out.println("Enter the number of that item purchased");
- number = reply1.nextInt();
- // Calculate the total cost
- totalCost = number*itemCost
+ FLAT_SHIPPING;
- // Print the total cost
- DecimalFormat decFor = new DecimalFormat("$#,##0.00");
- System.out.println("The total
bill will be "+ decFor.format(totalCost));
-
- reply1.close();
- }
- }
|
line 1: Leading documentation statements. Remember
that comment statements are ignored by the compiler. Comments are intended to be
messages to the
reader of the code. While a program should be somewhat "self-documenting",
well placed comments enhance the reader's understanding of the code (and
oftentimes your own understanding).
line 2: import java.util.Scanner; is included to allow for user input.
line 3: import java.text.*; is needed for decimal control of money.
line 5: Remember
that the name of the class must be the same as the name of the file containing
your Java code. In this situation, the program code was saved as EbayPurchase.java. Every Java program is a class. The French curly braces { }
are needed for all classes.
line 6: The main method
indicates where execution will start. Remember that Java is case sensitive
and get this line typed correctly. The French curly braces { } are needed for the main method.
public static void main ( String [ ] args)
line 8: The
reserved word final is used in Java to declare a constant variable.
final double FLAT_SHIPPING = 7.95;
A
double is used here to allow for a decimal value. All letters in a
constant variable are usually capitalized to distinguish them in a program.
line 9: These
variables are declared as doubles since they will contain decimal values.
double itemCost, totalCost;
line 10: The
number of items purchased is declared as an integer value.
int number;
line 11: A String variable is needed for the name and address. Notice that both variables are initialized on the same line.
String name, address;
lines 13,30,24,28,31: Comments used to segment a program add to its readability.
line 14: Establish a scanner to allow for user input. Since the String variables will be obtained before any numeric information, it will be safe to use the scanner "reply1" for all of the input.
lines 15-18: The user will be prompted for replies, the scanner obtains the replies and stores them in the indicated variable.
lines 21-22: Obtain the cost of the item from the user.
lines 25-26: Obtain the number of items purchased.
line 29: The
mathematical computations are done in this line. The cost for the stated
number of items is computed and the shipping fee is added. The final
answer is stored in the variable totalCost.
totalCost = number*itemCost
+ FLAT_SHIPPING;
lines 32: Set up the formatting from the DecimalFormat class to be used to print the money.
line 33: Prints the total bill.
line 35: Closes the scanner's input stream. |