LAB
EXERCISE 14: Bar Chart
Goal:
In this exercise, you will learn how to:
1.
Insert the Main Method (Main Entry Point to the Java Program)
2 Import a Java Class Library
3. Declare Variables and Initialize
4. Create a Constructor (Special Method)
5. Create a Void Method
6. Create an Array to
store data of the same data type
7. Use the Do While Loop and the For Loop for the
iteration of executing the code.
8. Create an Object from the Class
9. Call the Object’s Method
Program
Specifications:
Create
an application that prompts the user to enter today’s sales for five
scores. The program should then display
a simple bar graph comparing each store’s sales. Create each bar in the graph
by displaying a row of asterisks (*).
Each asterisk in the bar represents $100 in sales.
A. Pre-requisites:
1.
Create a folder on your desktop Exercise-14
2. Launch Java EE- Eclipse
Note: You will need to use the Java
Perspective Workbench for this exercise
3. Setup your Eclipse Workspace to point to the Exercise-14
folder
a. Select File-> Switch Workspace
b. Browse and select your Exercie-14 folder as your Workspace.
B. Requirements:
1. Create
a Java Project and name it as BarChart
2. Create
the first Class that will have the Main Method
a.
Name the Class as MainApp
b.
Choose the main method to insert into the
class
3. Create
the second Class that will have the Constructor and Method
a.
Name the Class as BarChart
b.
Do not select the main method stub
C. Requirements
for the BarChart Class:
1. Insert
the import java.util.Scanner class
which will allow data to be inputted from the console.
2. Declare
the variable- numStore and salesPer100 using
the integer data type.
3. Declare
the array storeArray[] using
the integer data type. This will store the monthly sales for each of the 5
stores.
4. Create
the Constructor called BarChart() that that will prompt the user
to input the data.
a. Initialize
the numStore to 1
b. Declare
the scanner input class objects - Scanner inputSales = new Scanner(System.in);
c. Used
the Do While
Loop to perform the following
iteration:
NOTE: The Do While Loop is a post-test loop which test the conditional expression
AFTER each iteration
1. Prompt the user to input the monthly
sales. Below is the code
2. Store the monthly sales in the array
System.out.print("Store " +
numStore + " Enter monthy
sales: $"); |
3. Increment numStore
to 1
d. Close the scanner input class object - inputSales.close()
5. Create
the Void Method called displayChart() that
will display the bar chart for the 5 stores
a. Initialize
the numStore to 1
b. Used
the Do While
Loop to perform the following
iteration:
NOTE: The Do While Loop is a post-test loop which
test the conditional expression AFTER each iteration
1. Store the value from the storeArray into the salesPer100 variable. Use numStore-1
for the array position
2. Display the store heading.
System.out.print("Store " +
numStore + ": " ); |
3. Use a For Loop to perform
the iteration of displaying the asterisk for the bar chart
System.out.print("*"); |
4. Insert a new line feed for the next store bar
chart
System.out.println(); // insert line feed |
5. Increment numStore
to 1
D. Requirements for the MainApp Class:
1. Add
comments (documentation)– Program Description, Author and Date
2. Create
the instance of object called store from the BarChart class
3. Display
the reporting header. Below is the code:
System.out.println(); |
4. Call
the object’s Void method store.displayChart() to display the bar chart of each store
E. Test:
1. Save your Java code
2. Compile and run your Java program.
3. Verify there is no syntax, logical or run-time
errors.
4. Use the following set of test data to
determine if the application is running properly:
F. Submit your exercise in the Canvas Lab
Exercise #14 Drop Box.
1. Submit the screen shot of the Eclipse
Workbench window showing the Console output screen.
You can use Paint (save as JPG) or Word to paste the screenshot.
2. Zip up and submit the compressed BarChart subfolder that is in the Exercise-14
folder.
NOTE: Right click on the subfolder and select
Send to “Compress Folder”. The file will
have a file extension of .zip.