Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Software Development
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard network printer problem ram registry router security slow software sound toshiba trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
Logic error in assignment program

Reply  
Thread Tools
CaseyRenee87's Avatar
Member with 40 posts.
 
Join Date: May 2009
Location: oklahoma
Experience: Intermediate
14-Nov-2009, 07:04 PM #1
Logic error in assignment program
hi everyone..I wrote a program for an assignment that calculates employee sales commissions, and determines how many employees earned salaries in different ranges using a one dimensional array. I've got it almost completely worked out..however there is a logical error, that when I run the program and test entering sales amounts in different ranges, the only one that works is the 200-299 range. the others remain at 0 even if i enter ranges in like 300 and above. What is wrong with my code..why it does this? Thanks in advance!

Program:

Code:
 
//Assigment 6952
/*
Casey Clark
student no. MA2008030031
*/
import java.util.Scanner; //imports scanner utility to obtain gross sales
public class Sales
{

 private int sales_Counter[]=new int[9];//initialize sales_counter array   
 private double salary;//variable declaration salary
 public double getSalary(int gross_sales)//method to calculate salary
 {
   double salary=((gross_sales*0.09)+200);
   return salary;
 }//end getSalary
 public void printResults(int counter_array[])//method to display results
 {
  for(int count=0; count <9; count++)
  {
    if (count < 8) System.out.printf("%4d%4d%4d\n", count*100 + 2 * 100, count*100 + 2 * 100 + 99, sales_Counter[count]);
    else System.out.printf("%4d%4s%4d\n", 1000, "+", sales_Counter[count]); // For 1000, puts to 1000   +
  }
 }//end printResults
 public void incrementArray(double salary) // method to determine which counter to increment
 {
   int salaryIndex = (int)(salary / 100 - 2); // converts salary to point to the index.  so 200 would be 0, 300 would be 1, etc.
   if(salaryIndex < 8) sales_Counter[salaryIndex]++; // various ranges
   else sales_Counter[8]++; // 1000+

//    if (salary >=1000.00){
//    sales_Counter[8]++;
//   }else if (salary >=900.00){
//    sales_Counter[7]++;
//   }else if (salary>=800.00){
//    sales_Counter[6]++;
//     }else if(salary>=700.00){
//      sales_Counter[5]++;
//      }else if(salary>=600.00){
//       sales_Counter[4]++;
//        }else if(salary>=500.00){
//         sales_Counter[3]++;
//              }else if(salary>=400.00){
//               sales_Counter[2]++;
//                }else if(salary>=300.00){
//                 sales_Counter[1]++;
//                }else if(salary>=200.00){
//                 sales_Counter[0]++;
//                }
 }//end incrementArray
 public void getGrossSales()//obtains input, salary, incrementArray & printResults
 {
   int gross_sales = 0;//variable for inputted gross sales
   double salary=0.0;
   Scanner input=new Scanner(System.in);

   System.out.printf("Enter gross sales amount: ");
   gross_sales = input.nextInt(); 
   while (gross_sales>=0)
   {
    salary=getSalary(gross_sales);
    incrementArray(salary);
    System.out.printf("Enter gross sales amount: ");
    gross_sales = input.nextInt(); 
   }//end while
   printResults(sales_Counter);
  }//end getGrossSales
 
 public static void main(String args[])
 {
   Sales salesObject = new Sales();
   salesObject.getGrossSales();
 }

}//end class Sales
CaseyRenee87's Avatar
Member with 40 posts.
 
Join Date: May 2009
Location: oklahoma
Experience: Intermediate
14-Nov-2009, 07:06 PM #2
also the comment slashes in front of the if elses' arent in my actual program i put them there for readability
IMM's Avatar
IMM IMM is offline IMM is authorized to help remove malware.
Malware Removal Specialist with 3,260 posts.
 
Join Date: Feb 2002
16-Nov-2009, 01:31 AM #3
Just glancing at it, when you convert to int seems to be the issue.
int salaryIndex = (int)(salary / 100 - 2);\
I think you probably want
int salaryIndex = ((int)salary/100) - 2;
There are a few unnecessary brackets in what I wrote -- but they never hurt.

You will still have an issue with producing -1 and -2 as an index for the lower salaries though and will have to fix the index for these cases in some manner.
perhaps something like?

int salaryIndex = ((int)salary/100) - 2;
if (salaryIndex < 0) salaryIndex = 0;

but I'm not going to read all of it
What I've mentioned won't fix the print stuff
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:08 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.