Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 3rd, 2013, 12:42 PM
walvaryen walvaryen is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 4 walvaryen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old March 3rd, 2013, 12:46 PM
walvaryen walvaryen is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 4 walvaryen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 : *******

Reply With Quote
  #3  
Old March 3rd, 2013, 12:50 PM
walvaryen walvaryen is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 4 walvaryen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #4  
Old March 3rd, 2013, 01:06 PM
walvaryen walvaryen is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 4 walvaryen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #5  
Old March 6th, 2013, 02:08 PM
Wetmelon's Avatar
Wetmelon Wetmelon is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Location: London, ON
Posts: 322 Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level)Wetmelon User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 3 Days 9 h 49 m 24 sec
Reputation Power: 213
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Java Program

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap