datalogo
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Sample Program

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.

  1. /*******************************
    * Project Sample
    * Programmer:  MathBits
    * Date:  Unit 2
    * Program:  EbayPurchase
    ********************************/
  2. import java.util.Scanner; //needed for input from user
  3. import java.text.*; //needed for decimal control of money
  4. public class EbayPurchase {
  5.       public static void main (String[ ] args)  {
  6.            final double FLAT_SHIPPING = 7.95; //constant
  7.            double itemCost, totalCost;
  8.            int number;
  9.            String name, address;
  10.            // Get User Information
  11.            Scanner reply1 = new Scanner(System.in);
  12.            System.out.println("Enter your full name:");
  13.            name = reply1.nextLine();
  14.            System.out.println("Enter mailing address:");
  15.            address = reply1.nextLine();
  16.            // Obtain cost of item
  17.            System.out.println("Enter the cost of one item in decimal form, no $" );
  18.            itemCost = reply1.nextDouble();
  19.            // Obtain number of that item purchased
  20.            System.out.println("Enter the number of that item purchased");
  21.            number = reply1.nextInt();
  22.            // Calculate the total cost
  23.            totalCost = number*itemCost + FLAT_SHIPPING;
  24.            // Print the total cost
  25.            DecimalFormat decFor = new DecimalFormat("$#,##0.00");
  26.            System.out.println("The total bill will be "+ decFor.format(totalCost));
  27.            reply1.close();
  28.       }
  29.  }

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.

divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area, JavaMathBits.com.
Help us keep these resources free. Thank you.