User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 455,985 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,801 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 735 | Replies: 4
Reply
Join Date: Nov 2006
Posts: 19
Reputation: speterson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
speterson speterson is offline Offline
Newbie Poster

Troubleshooting Random Salary

  #1  
Dec 3rd, 2007
For a homework assignment, I have to create a random salary generator which is pretty
easy. We have to create a random number of employees, 3-15 only and then the number of
years of that salary with a limit of 3-15. This is easy so far. After this, we have to loop the
salary through the employees and the years and have this save to a file. That is a little
more tricky, but I have got that too. The salary has to be between 40k and 100k. After the
file is created and saved, the program has to read the data from the file and calculate the
average salary. The average salary will determine a salary grade and all of this has to be
output on the screen. I have no problems with the file creation or reading, and the
averaging and grading scheme work fine.

With this description, you could say I have the program, but the problem I am having is that
for each employee, the salary has to increase from the previous salary, not decrease. I
need to figure out how to create a test to make sure that not only is the salary within the
range, but that the previous collected salary is smaller than the current created salary (per
employee).

This means that employee 1 could have 45k, 49k, and 80k and employee 2 could have 40k,
45k, and 68k, but employee 1 can't have 89k, 45k, 67k, etc etc.


The code uses 5 seperate classes for the program:

The first main class

  1.  
  2. import java.text.DecimalFormat;
  3.  
  4. public class Assignment10 {
  5.  
  6. public static void main(String[] args) {
  7. Assignment10 myAssignment10 = new Assignment10();
  8. myAssignment10.process();
  9. }
  10.  
  11. public Assignment10(){ }
  12.  
  13. public void process(){
  14. RecordHandler myRecordHandler = new RecordHandler();
  15. myRecordHandler.cleanup();
  16. prepare(myRecordHandler);
  17. use(myRecordHandler);
  18. }
  19.  
  20. public void prepare(RecordHandler myRecordHandler){
  21. myRecordHandler.open();
  22. SalaryGenerator mySalaryGenerator = new SalaryGenerator();
  23.  
  24. int numberOfTeamWorkers = 0;
  25. while (numberOfTeamWorkers < 3 || numberOfTeamWorkers > 15){
  26. numberOfTeamWorkers = mySalaryGenerator.getSalary(15);
  27. }
  28.  
  29. int numberOfYears = 0;
  30. while (numberOfYears < 3 || numberOfYears > 15){
  31. numberOfYears = mySalaryGenerator.getSalary(15);
  32. }
  33.  
  34.  
  35. //THIS TESTS TO SEE HOW MANY WORKERS AND YEARS THERE ARE
  36. //System.out.println("# of workers: " + numberOfTeamWorkers + " with # of years: " + numberOfYears);
  37.  
  38.  
  39. for (int i = 0; i < numberOfTeamWorkers; i++){
  40. String line = "";
  41. for (int j = 0; j < numberOfYears; j++){
  42. line = line + mySalaryGenerator.getSalary() + " ";
  43. }
  44. if (i == (numberOfTeamWorkers -1)){
  45. myRecordHandler.append(line);
  46. }else{
  47. myRecordHandler.append(line+ "\n");
  48. }
  49. }
  50.  
  51. myRecordHandler.close();
  52. }
  53.  
  54. public void use(RecordHandler myRecordHandler){
  55. myRecordHandler.open();
  56. Editor myEditor = new Editor();
  57. Calculations myCalculations = new Calculations();
  58. DecimalFormat formater = new DecimalFormat("###,###,##0.00");
  59. int i = 1;
  60. String readLine = myRecordHandler.read();
  61. while(readLine != null){
  62. int counter = myEditor.getCounter(readLine);
  63. int[] Salaries = myEditor.parse(readLine, counter);
  64. int total = myCalculations.getTotalSalaries(Salaries);
  65. double averageSalary = myCalculations.getAverageSalary(Salaries);
  66. String salaryGrade = myCalculations.getSalaryGrade(averageSalary);
  67.  
  68.  
  69. System.out.println("Worker " + i + " average:\t" + "$" + formater.format(averageSalary) + "\t\t" + salaryGrade);
  70.  
  71. readLine = myRecordHandler.read();
  72. i++;
  73. }
  74.  
  75. myRecordHandler.close();
  76. System.out.println("\n\n");
  77. }
  78. }
  79.  

The second file is:

  1.  
  2. public class Calculations {
  3.  
  4. public Calculations(){ }
  5.  
  6. public double getAverageSalary(int[] Salaries){
  7.  
  8. int total = getTotalSalaries(Salaries);
  9. double averageSalary = (double) total / Salaries.length;
  10. return averageSalary;
  11. }
  12.  
  13. public int getTotalSalaries(int[] Salaries){
  14. int total = 0;
  15. for (int i = 0; i < Salaries.length; i++){
  16. total = total + Salaries[i];
  17. }
  18. return total;
  19. }
  20.  
  21. public String getSalaryGrade(double averageSalary){
  22.  
  23. String salaryGrade = "";
  24.  
  25. if(averageSalary >= 90000.0){
  26. salaryGrade = "salary grade 12";
  27. }else if (averageSalary >= 80000.0){
  28. salaryGrade = "salary grade 11";
  29. }else if (averageSalary >= 70000.0){
  30. salaryGrade = "salary grade 10";
  31. }else if (averageSalary >= 60000.0){
  32. salaryGrade = "salary grade 9";
  33. }else if (averageSalary >= 50000.0){
  34. salaryGrade = "salary grade 8";
  35. }else {
  36. salaryGrade = "salary grade 7";
  37. }
  38.  
  39. return salaryGrade;
  40.  
  41. }
  42. }
  43.  

The third file is:

  1.  
  2. public class Editor {
  3.  
  4. public Editor(){ }
  5.  
  6. public int getCounter(String input){
  7. int count = 1;
  8. boolean endOfLine = false;
  9. while(!endOfLine){
  10. input = input.substring(input.indexOf(" ")).trim();
  11. count++;
  12. if (input.indexOf(" ") < 0){
  13. endOfLine = true;
  14. }
  15. }
  16.  
  17. return count;
  18. }
  19.  
  20.  
  21. public int[] parse(String input, int count){
  22. int[] Salaries = new int[count];
  23.  
  24. for (int i = 0; i < (count - 1); i++){
  25. int score = Integer.parseInt(input.substring(0, input.indexOf(" ")).trim());
  26. input = input.substring(input.indexOf(" ")).trim();
  27. Salaries[i] = score;
  28. }
  29.  
  30. Salaries[count - 1] = Integer.parseInt(input.trim());
  31.  
  32. return Salaries;
  33. }
  34.  
  35. }
  36.  



The fourth file is:


  1.  
  2.  
  3. import java.io.*;
  4.  
  5. public class RecordHandler {
  6.  
  7. private RandomAccessFile myAccess;
  8.  
  9. public RecordHandler(){ }
  10.  
  11. public void cleanup(){
  12. File myFile = new File("stevenRecord.smp");
  13. myFile.delete();
  14. }
  15.  
  16. public void open(){
  17. try {
  18. myAccess = new RandomAccessFile("stevenRecord.smp", "rwd");
  19. } catch (FileNotFoundException e) {
  20. e.printStackTrace();
  21. }
  22. }
  23.  
  24. public void close(){
  25. try {
  26. myAccess.close();
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31.  
  32. public void append(String inputString){
  33. try {
  34. myAccess.writeBytes(inputString);
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39.  
  40. public String read(){
  41. try {
  42. return myAccess.readLine();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. return "";
  47. }
  48. }
  49.  
  50.  

The last file is:

  1.  
  2. import java.util.Random;
  3.  
  4. public class SalaryGenerator {
  5.  
  6. private Random myRandom = new Random();
  7.  
  8. public SalaryGenerator(){ }
  9.  
  10. public int getSalary(){
  11. int Salary = 0;
  12. while (Salary < 40000 || Salary > 100000){
  13. Salary = myRandom.nextInt(200000);
  14. Salary = Salary * 2;
  15. }
  16. return Salary;
  17. }
  18.  
  19. public int getSalary(int limit){
  20. int Salary = myRandom.nextInt(limit);
  21. while (Salary < 5){
  22. Salary = myRandom.nextInt(limit);
  23. }
  24. return Salary;
  25. }
  26.  
  27. public int getSalary(int high, int low){
  28. int Salary = myRandom.nextInt(high);
  29. while (Salary < low){
  30. Salary = myRandom.nextInt(high);
  31. }
  32. return Salary;
  33. }
  34.  
  35. }
  36.  
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Random Salary

  #2  
Dec 3rd, 2007
With this description, you could say I have the program, but the problem I am having is that
for each employee, the salary has to increase from the previous salary, not decrease. I
need to figure out how to create a test to make sure that not only is the salary within the
range, but that the previous collected salary is smaller than the current created salary (per
employee).

Come up with some pseudo code...
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,076
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 306
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Random Salary

  #3  
Dec 3rd, 2007
Perhaps your SalaryGenerator should take the current salary as a parameter and add a random amount to it, a randomRaise() perhaps.
Reply With Quote  
Join Date: Nov 2006
Posts: 19
Reputation: speterson is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
speterson speterson is offline Offline
Newbie Poster

Re: Random Salary

  #4  
Dec 3rd, 2007
Originally Posted by iamthwee View Post
Come up with some pseudo code...

I have already included the actual code for the program so I do not know what help pseudo
code would be. I think the problem has something to do with the random generator class
and how it is set up.

Originally Posted by Ezzaral View Post
Perhaps your SalaryGenerator should take the current salary as a parameter and add a random amount to it, a randomRaise() perhaps.

Could you show pseudo code or something how to do this to verify that the salary did indeed
increase from the previous one for that particular employee.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,076
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 306
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Random Salary

  #5  
Dec 3rd, 2007
It's not a matter of verifying a random change after it's done. Generate the random salary progression by starting with a base and adding a random raise to it. Assigning a series of random salaries to an employee makes little sense if an upward progression is desired (and I think most employees would agree that is the only acceptable progression )
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 9:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC