The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Homework - Java Program
Discuss Java Program in the Java Help forum on Dev Shed. Java Program Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 3rd, 2013, 12:42 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 4
Time spent in forums: 27 m 55 sec
Reputation Power: 0
|
|
|
Homework - Java Program
Hi there. I'm wondering if anyone can help me here. I believe I've got the basics... but can't figure out how to complete it. Here's what it SHOULD do:
Write a Java program to collect the sales data from three stores of a given company. The program will ask the user to enter the number of items sold at each store. The program should display a bar chart to illustrate the sales at each store. A bar chart is a row of printed asterisks -- each asterisk represents the sale of one item. To make the bar chart easier to read, every tenth asterisk is replaced by an 'x'. Here is a sample run of the program:
Enter today's sales for store 1: 27
Enter today's sales for store 2: 15
Enter today's sales for store 3: 34
SALES BAR CHART
Store 1: *********x*********x*******
Store 2: *********x*****
Store 3: *********x*********x*********x****
Your program should validate the data entered for each store. The store data should be a value zero or greater (i.e., reject negative values). Here is a sample run of the program in which the user attempted to enter invalid data for store 2 -- eventually the user entered the valid value of 23:
Enter today's sales for store 1: 9
Enter today's sales for store 2: -2
Invalid input - enter a value of zero or greater: -1
Invalid input - enter a value of zero or greater: 23
Enter today's sales for store 3: 16
SALES BAR CHART
Store 1: *********
Store 2: *********x*********x***
Store 3: *********x******
Start with the following code -- add your code to the main method:
import java.util.Scanner;
public class BarChart
{
public static void main(String [] args)
{
}
}
Here is what I have:
import java.util.Scanner;
public class BarChart
{
public static void main(String [] args)
{
int store1;
int store2;
int store3;
int bar1;
int bar2;
int bar3;
// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter today's sales for store 1: ");
store1 = keyboard.nextInt();
while (store1 < 0)
{
System.out.print("Invalid input - enter a value of zero or greater: "); // Completion of WHILE loop for less than 0.
store1 = keyboard.nextInt();
}
System.out.print("Enter today's sales for store 2: ");
store2 = keyboard.nextInt();
while (store2 < 0)
{
System.out.print("Invalid input - enter a value of zero or greater: "); // Completion of WHILE loop for less than 0.
store2 = keyboard.nextInt();
}
System.out.print("Enter today's sales for store 3: ");
store3 = keyboard.nextInt();
while (store3 < 0)
{
System.out.print("Invalid input - enter a value of zero or greater: "); // Completion of WHILE loop for less than 0.
store3 = keyboard.nextInt();
}
System.out.println("\n\nSALES BAR CHART\n\n");
bar1 = store1/100;
System.out.println("store 1 : ");
for(int i = 1; i <= bar1; i++)
System.out.println("*");
bar2 = store2/100;
System.out.println("\nStore 2 : ");
for(int i = 1; i <= bar2; i++)
System.out.println("*");
bar3 = store3/100;
System.out.println("\nStore 3 : ");
for(int i = 1; i <= bar3; i++)
System.out.println("*");
}
}
Everything works, but this is what I get on a run:
Enter today's sales for store 1: 12
Enter today's sales for store 2: 15
Enter today's sales for store 3: 22
SALES BAR CHART
store 1 :
Store 2 :
Store 3 :
I'm thoroughly lost on the asterick/plus part. If anyone could offer advice, or point me in the right direction, I'd really really appreciate it.
|

March 3rd, 2013, 12:46 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 4
Time spent in forums: 27 m 55 sec
Reputation Power: 0
|
|
|
Okay, a little bit closer.. Issue is revolving around the asterick bar chart.
updated code:
System.out.println("\n\nSALES BAR CHART\n\n");
bar1 = store1;
System.out.print("store 1 : ");
for(int i = 1; i <= bar1; i++)
System.out.print("*");
bar2 = store2;
System.out.print("\nStore 2 : ");
for(int i = 1; i <= bar2; i++)
System.out.print("*");
bar3 = store3;
System.out.print("\nStore 3 : ");
for(int i = 1; i <= bar3; i++)
System.out.print("*");
Ouput:
Enter today's sales for store 1: 22
Enter today's sales for store 2: 45
Enter today's sales for store 3: 7
SALES BAR CHART
store 1 : **********************
Store 2 : *********************************************
Store 3 : *******
|

March 3rd, 2013, 12:50 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 4
Time spent in forums: 27 m 55 sec
Reputation Power: 0
|
|
|
It seems this is factored off maybe modulus? i%10 to break down, then everytime i=0 print an x? That works in my mind.. but I don't know what process to do to actually make this do what I want. Any suggestions?
|

March 3rd, 2013, 01:06 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 4
Time spent in forums: 27 m 55 sec
Reputation Power: 0
|
|
|
Figured it out.. Don't think it's the CLEANEST way.. but it gets the job done.. nested FOR loop with an IF/ELSE clause:
System.out.println("\n\nSALES BAR CHART\n\n");
bar1 = store1;
System.out.print("store 1 : ");
for(int i = 1; i <= bar1; i++)
if (i%10 >=1)
System.out.print("*");
else
System.out.print("x");
bar2 = store2;
System.out.print("\nStore 2 : ");
for(int i = 1; i <= bar2; i++)
if (i%10 >=1)
System.out.print("*");
else
System.out.print("x");
bar3 = store3;
System.out.print("\nStore 3 : ");
for(int i = 1; i <= bar3; i++)
if (i%10 >=1)
System.out.print("*");
else
System.out.print("x");
Completed:
Enter today's sales for store 1: -5
Invalid input - enter a value of zero or greater: -4
Invalid input - enter a value of zero or greater: 12
Enter today's sales for store 2: -45
Invalid input - enter a value of zero or greater: -5
Invalid input - enter a value of zero or greater: 33
Enter today's sales for store 3: -4
Invalid input - enter a value of zero or greater: 106
SALES BAR CHART
store 1 : *********x**
Store 2 : *********x*********x*********x***
Store 3 : *********x*********x*********x*********x*********x*********x*********x*********x*********x*********x ******
----jGRASP: operation complete.
|

March 6th, 2013, 02:08 PM
|
 |
Contributing User
|
|
Join Date: Oct 2009
Location: London, ON
|
|
For the record, using code tags will make everything much prettier
Code:
System.out.println("\n\nSALES BAR CHART\n\n");
bar1 = store1;
System.out.print("store 1 : ");
for(int i = 1; i <= bar1; i++)
if (i%10 >=1)
System.out.print("*");
else
System.out.print("x");
bar2 = store2;
System.out.print("\nStore 2 : ");
for(int i = 1; i <= bar2; i++)
if (i%10 >=1)
System.out.print("*");
else
System.out.print("x");
bar3 = store3;
System.out.print("\nStore 3 : ");
for(int i = 1; i <= bar3; i++)
if (i%10 >=1)
System.out.print("*");
else
System.out.print("x");
etc. If you copy & paste directly from your editor into the code tags it'll preserve formatting
__________________
<Tetrad> the program I just wrote 1) compiled the first time without any errors and 2) worked like it was supposed to
<Tetrad> I don't know whether to be proud or scared to death
Quote: | Originally Posted by DaWei_M That covers a multitude of your sins, right there. |
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|