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.
- /*******************************
* Project Sample
* Programmer: Roberts
* Date: During Unit 2
* Program: EbayPurchase
********************************/
-
import java.io.*;
-
import BreezyGUI.*;
- public class EbayPurchase
- {
- public static void main (String[ ] args)
- {
- final double
FLAT_SHIPPING = 7.95;
- double
itemCost, totalCost;
-
int number;
- itemCost=Console.readDouble("Enter
the cost of one item" );
-
number = Console.readInt("Enter the number of items purchased");
- totalCost=number*itemCost
+ FLAT_SHIPPING;
- System.out.println("The total
bill will be "+ totalCost);
- }
- }
|
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.io.*; 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
EbayPurchase.java. 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.
|