3 Replies - 353 Views - Last Post: 09 April 2010 - 09:56 PM Rate Topic: -----

#1 snowball14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-April 10

Arrays

Posted 09 April 2010 - 08:26 PM

Can anyone tell me what I am doing wrong? The first part of the program works but I can't get it to calculate weekly pay range or the salesperson count. And I'm not getting any errors when I compile the program.



import java.util.Scanner;

public class SalesCommission{
		
  public static void countRanges(){		
	Scanner input= new Scanner(System.in);                                 
	final double commissionRateOnGrossSales= 0.09;                          
	final double baseWeeklySalary= 200.0;                                   
	final double weeklyPayRangeIncrement= 100.0;                            
 	double salesAmount;                                                      
	double weeklyPay;                                                        
	final int weeklyPayRanges= 9;                                         
	int weeklyPayRange;                                                      
	final String salesAmountPrompt="Enter salesperson %2d's weekly sales, or <ctrl>z to stop: "; 
	int salespersonNumber= 1;                                                   
	final int countOfWeeklyPayByRange[]; countOfWeeklyPayByRange = new int [9];           
	System.out.println("Task 10-02, Ch07, Programmed by Pam Thomas");             
	System.out.println("Weekly Sales and Weekly Pay by Salesperson:");          
	  int countOfWeeklyPayByRanges[] = {0, /* $200-$299 */ 0, /* $300-$399 */ 0, /* $400-$499 */ 0, /* $500-$599 */ 0, /* $600-$699 */ 0, /* $700-$799 */ 0, /* $800-$899 */ 0, /* $900-$999 */ 0}; /* $1000 and over */
	for(weeklyPayRange=0; weeklyPayRange<=countOfWeeklyPayByRange.length-1;weeklyPayRange++){     
		countOfWeeklyPayByRange [weeklyPayRange]= 0;                                         
		}// end for                                                                                  
	System.out.printf("\t                                           " +salesAmountPrompt, 			salespersonNumber);
	while (input.hasNext()){                                                            
	salesAmount=input.nextDouble();                                                     
	weeklyPay=baseWeeklySalary + (salesAmount*commissionRateOnGrossSales);               
	weeklyPayRange=(int)(weeklyPay/weeklyPayRangeIncrement);                      
	if (weeklyPayRange > (countOfWeeklyPayByRange.length + 1)){     
	  weeklyPayRange=(countOfWeeklyPayByRange.length + 1); 		
	}// end if                                                          
	++ countOfWeeklyPayByRange[weeklyPayRange-2];                               
 	System.out.printf("\tWeekly Pay for Salesperson %2d is $%,8.2f, "+ salesAmountPrompt, 	salespersonNumber, weeklyPay, ++salespersonNumber); 
	}// end while                                                                 
	System.out.println("Salesperson Count by Weekly Pay Range");                       
	System.out.printf("\n\t%14s\t\t%11s","  Weekly Pay  ","Salesperson");                 
	System.out.printf("\n\t%14s\t\t%11s\n","    Range    ","   Count   ");              
	for (weeklyPayRange=0; weeklyPayRange<=countOfWeeklyPayByRange.length-2; weeklyPayRange++) { 
	System.out.printf("\n\t$%,5d - $%,5d\t\t%,6d",                                   
	(int)baseWeeklySalary +(weeklyPayRangeIncrement*weeklyPayRange),                    
	(int)99.0+baseWeeklySalary+(weeklyPayRangeIncrement*weeklyPayRange),                   				countOfWeeklyPayByRange[(int) weeklyPayRange]);                                    
	} // end for                                                                      
	System.out.printf("\n\t$1,000 and over\t\t%,6d", countOfWeeklyPayByRange			[(countOfWeeklyPayByRange.length-10)] );                                  
	System.out.println("End program");                                               
	} // main method
} // class SalesCommission


Is This A Good Question/Topic? 0
  • +

Replies To: Arrays

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Arrays

Posted 09 April 2010 - 09:31 PM

} // main method

cute :) Really a misleading comment

and where this main() method starts ?
The only method you have is countRanges() that is not called by anyone
Was This Post Helpful? 0
  • +
  • -

#3 snowball14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-April 10

Re: Arrays

Posted 09 April 2010 - 09:49 PM

I forgot to add this part.

public static void main(String args[]){
		SalesCommission mySalesCommission=new SalesCommission();                                     
		mySalesCommission.countRanges();                                                             
	} //end main
}// end class SalesCommissionTest

Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Arrays

Posted 09 April 2010 - 09:56 PM

Just a comment:

You make countRanges() static instead of non-static so you don't need to create a new Object as you have done, you merely have to call it from the class:

public static void main(String args[]){
                SalesCommission.countRanges();                                                             
        } //end main



Now, if you want multiple objects, you will want to make countRanges() non-static so that each object has its own version of the method.

Remember: static methods and variables are the same for EVERY object of that type, while non-static methods and variables are different for each object. This means that if you have a static variable and change the value in one object, all the other objects' variable is identically changed. So, static is not good if you want man objects.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1