Return to Unit Menu | Java Main Page | | Terms of Use

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.

  1. /*******************************
    * Project Sample
    * Programmer:  Roberts
    * Date:  During Unit 2
    * Program:  EbayPurchase
  2. import*;

  3. import BreezyGUI.*;

  4. public class EbayPurchase
  5. {
  6.       public static void main (String[ ] args)
  7.      {
  8.            final double FLAT_SHIPPING = 7.95;
  9.            double itemCost, totalCost;
  10.            int number;
  11.            itemCost=Console.readDouble("Enter the cost of one item" );
  12.            number = Console.readInt("Enter the number of items purchased");
  13.            totalCost=number*itemCost + FLAT_SHIPPING;
  14.            System.out.println("The total bill will be "+ totalCost);
  15.       }
  16.  }

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*; is included whenever you deal with input and/or output.

line 3:  import BreezyGUI.*; is needed to deal with input, output and graphical user interface options.

line 4:  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  Every Java program is a class. 

line 5:  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.
                            public static void main ( String [ ] args)

line 7:
 French curly braces { } are needed for the main method.

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 variables 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;

lines 11, 12:  We are using the input routines from the BreezyGUI library to receive a double value entry and an integer value entry from the user.
                itemCost=Console.readDouble("Enter the cost of one item" );
                number = Console.readInt("Enter the number of items purchased");
Each of these commands will prompt the user as to what is needed and then store the answers in the variables itemCost and number, respectively.

line 13:  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 14:  Printing to the screen.  This statement will print the message "The total bill will be" followed by the price that was computed in the previous line.
                System.out.println("The total bill will be "+ totalCost);

lines 15 and 16:  These lines close the French curly braces.


Return to Unit Menu | Java Main Page | | Terms of Use