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.

 Add This Thread To: Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb
 « Previous Thread | Next Thread » Thread Tools Search this Thread Rate Thread Display Modes
 Dev Shed Forums Sponsor:
#1
March 3rd, 2013, 12:42 PM
 walvaryen
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.

#2
March 3rd, 2013, 12:46 PM
 walvaryen
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 : *******

#3
March 3rd, 2013, 12:50 PM
 walvaryen
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?

#4
March 3rd, 2013, 01:06 PM
 walvaryen
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.

#5
March 6th, 2013, 02:08 PM
 Wetmelon
Contributing User

Join Date: Oct 2009
Location: London, ON
Posts: 322
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.

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Homework - Java Program

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 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