I need some help putting this code into Java
#include %26lt;iostream%26gt;
using namespace std;
int main() {
int sales[9]= { 0 };
double gross=0, percent=0, salary=0;
int total=0;
int counter=0;
cout %26lt;%26lt; "For 5 peoples salary ranges, Enter the first gross" %26lt;%26lt; endl;
cin %26gt;%26gt; gross;
percent=gross*0.09;
salary=200+percent;
while (counter!=5) {
if (salary%26gt;=200 %26amp;%26amp; salary %26lt;= 299)
sales[0]++;
if (salary%26gt;=300 %26amp;%26amp; salary%26lt;=399)
sales[1]++;
if (salary%26gt;400 %26amp;%26amp; salary%26lt;=499)
sales[2]++;
if (salary%26gt;=500 %26amp;%26amp; salary%26lt;=599)
sales[3]++;
if (salary%26gt;=600 %26amp;%26amp; salary%26lt;=699)
sales[4]++;
if (salary%26gt;=700 %26amp;%26amp; salary%26lt;=799)
sales[5]++;
if (salary%26gt;=800 %26amp;%26amp; salary%26lt;=899)
sales[6]++;
if (salary%26gt;=900 %26amp;%26amp; salary%26lt;=999)
sales[7]++;
if (salary%26gt;=1000)
sales[8]++;
cout %26lt;%26lt; "Enter the next gross" %26lt;%26lt; endl;
cin %26gt;%26gt; gross;
counter++;
}
cout %26lt;%26lt; sales[0] %26lt;%26lt; " are in 200-299, " %26lt;%26lt; sales[1] %26lt;%26lt; " are in 300-399, " %26lt;%26lt; sales[2] %26lt;%26lt; " are in 400-499, " %26lt;%26lt; sales[3] %26lt;%26lt; " are in 500-599, " %26lt;%26lt; sales[4]
Translating a code from C++ to Java...?
Declarations of variables and main() as well as imports are also different. You can also clean up your if statements to make them more efficent (one comparison instead of two).
This is a sample class that I called stest (in stest.java) that will illustrate how to use the scanner class to read from stdin (I left out all error checking and it will allow multiple gross values to be placed on a single input line which will result in unneeded prompts, but this is just a sample). It also illustrates how you can make one test instead of two in each of your if statements.
import java.util.*;
import java.lang.*;
import java.io.*;
class stest
{
public static void main( String argv[] )
{
int sales[]; // declaration; allocation is later
String range_names[] = { "200-299", "300-399", "400-499", "500-599", "600-699", "700-799", "800-899", "900-999", "%26gt;1000" };
Scanner s; // input scanner object
int need = 5; // number of values needed
double percent = 0; // percentage commission maybe?
double salary = 0; // persons computed salary
double gross = 0; // gross sales maybe?
int idx; // index into sales
sales = new int[9]; // allocate the array (Id name this salarycount or something relative)
s = new Scanner( System.in ); // read from standard input (assume keyboard)
while( need %26gt; 0 )
{
System.out.printf( "enter gross sales for person %d: ", 6-need );
gross = s.nextInt( ); // get next int value from stdin (no error checking here)
percent = gross * 0.09; // duplication of your logic
salary = 200.0 + percent; // duplication of your logic
if( salary %26gt;= 1000 ) // working backwards needs just one
idx = 8; // comparison so more efficent
else
if( salary %26gt;= 900 )
idx = 7;
else
if( salary %26gt;= 800 )
idx = 6;
else
if( salary %26gt;= 700 )
idx = 5;
else
if( salary %26gt;= 600 )
idx = 4;
else
if( salary %26gt;= 500 )
idx = 3;
else
if( salary %26gt;= 400 )
idx = 2;
else
if( salary %26gt;= 300 )
idx = 1;
else
idx = 0;
sales[idx]++;
need--; // one less to do
}
for( idx = 0; idx %26lt; 9; idx++ )
System.out.printf( "salaries in the range of %s: %d\n", range_names[idx], sales[idx] );
}
}
==== output when run with random numbers ====
java stest
enter gross sales for person 1: 3000
enter gross sales for person 2: 7000
enter gross sales for person 3: 24000
enter gross sales for person 4: 1200
enter gross sales for person 5: 1680
salaries in the range of 200-299: 0
salaries in the range of 300-399: 2
salaries in the range of 400-499: 1
salaries in the range of 500-599: 0
salaries in the range of 600-699: 0
salaries in the range of 700-799: 0
salaries in the range of 800-899: 1
salaries in the range of 900-999: 0
salaries in the range of %26gt;1000: 1
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment