In: Computer Science
To do: Write a program names BarChartWhile that ask’s the user for today’s sales (to the closest $100) at five stores. The program should display a bar chart comparing each store’s sales. Create each bar in the chart by displaying a row of asterisks. Each asterisk should represent $100 of sales. You must use a while loop to display the chart. Actually – you will need five while loops, one for each store. Those of you with some programming background, you CANNOT use arrays, etc. You must use separate variables for each store.
To do: Repeat the program above but name it BarChartFor and use five for loops. The output will look identical.
Java language.
Sample output:
Today's sales for store 1 (rounded to the nearest 100)
300
Today's sales for store 2 (rounded to the nearest 100)
700
Today's sales for store 3 (rounded to the nearest 100)
1500
Today's sales for store 4 (rounded to the nearest 100)
2100
Today's sales for store 5 (rounded to the nearest 100)
200
SALES BAR CHART
Store 1 - Sales = 300:***
Store 2 - Sales = 700:*******
Store 3 - Sales = 1500:***************
Store 4 - Sales = 2100:*********************
Store 5 - Sales = 200:**
1.
import java.util.Scanner;
class BarChartWhile
{
public static void main (String[] args)
{
Scanner input = new
Scanner(System.in);
System.out.println("Today's sales for store 1 (rounded to the
nearest 100)");
int store1Sale =
input.nextInt();
System.out.println("Today's sales for store 2 (rounded to the
nearest 100) ");
int store2Sale =
input.nextInt();
System.out.println("Today's sales for store 3 (rounded to the
nearest 100) ");
int store3Sale =
input.nextInt();
System.out.println("Today's sales for store 4 (rounded to the
nearest 100) ");
int store4Sale =
input.nextInt();
System.out.println("Today's sales for store 5 (rounded to the
nearest 100) ");
int store5Sale =
input.nextInt();
System.out.println("SALES BAR CHART");
int i = 0;
System.out.print("Store 1 - Sales =
"+store1Sale+":");
while(i<store1Sale)
{
System.out.print("*");
i = i+100;
}
System.out.println();
i = 0;
System.out.print("Store 2 - Sales =
"+store2Sale+":");
while(i<store2Sale)
{
System.out.print("*");
i = i+100;
}
System.out.println();
i = 0;
System.out.print("Store 3 - Sales =
"+store3Sale+":");
while(i<store3Sale)
{
System.out.print("*");
i = i+100;
}
System.out.println();
i = 0;
System.out.print("Store 4 - Sales =
"+store4Sale+":");
while(i<store4Sale)
{
System.out.print("*");
i = i+100;
}
System.out.println();
i = 0;
System.out.print("Store 5 - Sales =
"+store5Sale+":");
while(i<store5Sale)
{
System.out.print("*");
i = i+100;
}
}
}
Output:
Today's sales for store 1 (rounded to the nearest 100) 300 Today's sales for store 2 (rounded to the nearest 100) 700 Today's sales for store 3 (rounded to the nearest 100) 1500 Today's sales for store 4 (rounded to the nearest 100) 2100 Today's sales for store 5 (rounded to the nearest 100) 200 SALES BAR CHART Store 1 - Sales = 300:*** Store 2 - Sales = 700:******* Store 3 - Sales = 1500:*************** Store 4 - Sales = 2100:********************* Store 5 - Sales = 200:**
2.
import java.util.Scanner;
class BarChartFor
{
public static void main (String[] args)
{
Scanner input = new
Scanner(System.in);
System.out.println("Today's sales for store 1 (rounded to the
nearest 100)");
int store1Sale =
input.nextInt();
System.out.println("Today's sales for store 2 (rounded to the
nearest 100) ");
int store2Sale =
input.nextInt();
System.out.println("Today's sales for store 3 (rounded to the
nearest 100) ");
int store3Sale =
input.nextInt();
System.out.println("Today's sales for store 4 (rounded to the
nearest 100) ");
int store4Sale =
input.nextInt();
System.out.println("Today's sales for store 5 (rounded to the
nearest 100) ");
int store5Sale =
input.nextInt();
System.out.println("SALES BAR CHART");
System.out.print("Store 1 - Sales =
"+store1Sale+":");
for(int
i=0;i<store1Sale;i=i+100)
{
System.out.print("*");
}
System.out.println();
System.out.print("Store 2 - Sales =
"+store2Sale+":");
for(int
i=0;i<store2Sale;i= i+100)
{
System.out.print("*");
}
System.out.println();
System.out.print("Store 3 - Sales =
"+store3Sale+":");
for(int
i=0;i<store3Sale;i=i+100)
{
System.out.print("*");
}
System.out.println();
System.out.print("Store 4 - Sales =
"+store4Sale+":");
for(int
i=0;i<store5Sale;i=i+100)
{
System.out.print("*");
}
System.out.println();
System.out.print("Store 5 - Sales =
"+store5Sale+":");
for(int
i=0;i<store5Sale;i = i+100)
{
System.out.print("*");
}
}
}
Output:
Today's sales for store 1 (rounded to the nearest 100) 300 Today's sales for store 2 (rounded to the nearest 100) 700 Today's sales for store 3 (rounded to the nearest 100) 1500 Today's sales for store 4 (rounded to the nearest 100) 2100 Today's sales for store 5 (rounded to the nearest 100) 200 SALES BAR CHART Store 1 - Sales = 300:*** Store 2 - Sales = 700:******* Store 3 - Sales = 1500:*************** Store 4 - Sales = 2100:********************* Store 5 - Sales = 200:**
Do ask if any doubt. Please up-vote.