Solved

Display a salary schedule based on three inputs: starting salary, percentage increase and number of years in schedule.

Posted on 2004-09-26
41
951 Views
Last Modified: 2008-03-06
Hey! It is me again with an assignment to do the following:

  Teachers are paid on a schedule that provides a salary based on the number of years of teachinng experience.
   For example, a beginning teacher might be paid $20,000 for the first year.
   For each year of experience after this up to 10 years, a 2% increase over the preceding value is received.


1.  Write a program that displays a salary schedule for teachers in a school district.
2.  The inputs are the starting salary, the percentage increase, and the number of years in the schedule.
3.  Each row in the schedule should contain the year number and the salary for that year.
4.  The program should display the pay schedule based on the user’s input but for no more than 10 years.
5.  Inputs should be interactive.

I think that I am having most difficulty trying to understand how to implement a loop (if-else, while or for) system that will display the ten years worth of data into the terminal window once it executes.
I am not sure if my variable declarations are going in the right direction so that things calculate correctly and when they should.  I am looking for advice or steering or some understanding.
Any and all help is appreciated!!

lauren


My current source code is as follows:

import TerminalIO.KeyboardReader;
public class teacherpay {
      public static void main( String [] args) {

      KeyboardReader reader = new KeyboardReader();

      double startSalary;
      double percentageIncrease;
      double yearsWorked;

      //get user imputs

      System.out.print("Please enter your starting salary: $");
      startSalary = reader.readDouble();         // starting salary entered by the user.
      System.out.println("");

      System.out.print("Please enter the increase percentage: % ");
      percentageIncrease = reader.readDouble();       // percent increase entered by the user.
      System.out.println("");

      System.out.print("Please enter the amount of experience in number of years worked:  ");
      yearsWorked = reader.readDouble();      // years worked entered by the user.
      System.out.println("");

      double salaryIncrease = (startSalary * percentageIncrease);
      int annualSalary = (startSalary + salaryIncrease)

      System.out.print("The new annual salary is: $" + annualSalary);
      annualSalary = reader.readDouble();
      System.out.println("");
            }
      }


The latest error message:

C:\ex\teacherpay.java:37: unexpected type
required: class
found   : value
      int annualSalary = (startSalary + salaryIncrease)
                                                             ^
C:\ex\teacherpay.java:41: possible loss of precision
found   : double
required: int
      annualSalary = reader.readDouble();
                                                                 ^
2 errors

Tool completed with exit code 1


THANKS!
0
Comment
Question by:Brayn66
  • 22
  • 19
41 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12156053
>>int annualSalary = (startSalary + salaryIncrease);

should be

double annualSalary = (startSalary + salaryIncrease);



0
 

Author Comment

by:Brayn66
ID: 12156149
I actually had it that way but tried another variable type to see if any changes occurred (which it did). I should have changed it back before I posted the question - sorry!.
Any ideas on the other error, and do you think that it needs a loop system?  lauren

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12156173
Please post your corrected code. Yes it will need a loop
0
 

Author Comment

by:Brayn66
ID: 12156191
import TerminalIO.KeyboardReader;

public class teacherpay {
      public static void main( String [] args) {

      KeyboardReader reader = new KeyboardReader();


      double startSalary;
      double percentageIncrease;
      double yearsWorked;


      //get user imputs

      System.out.print("Please enter your starting salary: $");
      startSalary = reader.readDouble();         // starting salary entered by the user.
      System.out.println("");

      System.out.print("Please enter the increase percentage: % ");
      percentageIncrease = reader.readDouble();       // percent increase entered by the user.
      System.out.println("");

      System.out.print("Please enter the amount of experience in number of years worked:  ");
      yearsWorked = reader.readDouble();      // years worked entered by the user.
      System.out.println("");

      double salaryIncrease = (startSalary * percentageIncrease);
      double annualSalary = (startSalary + salaryIncrease)

      System.out.print("The new annual salary is: $" + annualSalary);
      annualSalary = reader.readDouble();
      System.out.println("");

            }
      }

.......................................

With 1 error:

C:\ex\teacherpay.java:37: unexpected type
required: class
found   : value
      double annualSalary = (startSalary + salaryIncrease)
                                           ^
1 error
Tool completed with exit code 1
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12156218
Missing semicolon at the end of that line
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12156220
The years parameter should really be an int
0
 

Author Comment

by:Brayn66
ID: 12156523
When I changed it  [  double annualSalary = (startSalary + salaryIncrease)  ]   to an int I get the following error code:

C:\ex\teacherpay.java:33: possible loss of precision
found   : double
required: int
      yearsWorked = reader.readDouble();      // years worked entered by the user.
                                                                 ^
1 error
Tool completed with exit code 1v

However,
 I changed it back and now some of the requirements for the program will now execute in the terminal window(the missing semicolon was a problem).  The three imputs are accepted and the caculations listed in the program occur.  The years entered for experience (for the yearsWorked variable) need a way to function for the program design requirements.

This I believe is where I should implement the loop. I was thinking that since the while statement's looping capabilities will execute until an untrue condition presents itself.  I am not sure that this is the program for the loop method that I need.  I want the program to execute by calculating the pay and refering to the pay schedule (in the correct years experience location) based on the user inputs.

Do I need to write a count controlled loop that will count the number (up to only ten years worth of calculations)?? After this is established, my biggest challenge will be to properly display the schedule in the terminal window.?.

lauren


ps: this is the latest corrected code:


import TerminalIO.KeyboardReader;

public class teacherpay {
      public static void main( String [] args) {

      KeyboardReader reader = new KeyboardReader();

      double startSalary;
      double percentageIncrease;
      double yearsWorked;

      //get user imputs

      System.out.print("Please enter your starting salary: $");
      startSalary = reader.readDouble();         // starting salary entered by the user.
      System.out.println("");

      System.out.print("Please enter the increase percentage: % ");
      percentageIncrease = reader.readDouble();       // percent increase entered by the user.
      System.out.println("");

      System.out.print("Please enter the amount of experience in number of years worked:  ");
      yearsWorked = reader.readDouble();      // years worked entered by the user.
      System.out.println("");

      double salaryIncrease = (startSalary * percentageIncrease);
      double annualSalary = (startSalary + salaryIncrease);

      System.out.print("The new annual salary is: $" + annualSalary);
      annualSalary = reader.readDouble();
      System.out.println();
            }
      }

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12156542
OK - leave it as a double for the moment.

The next thing you need to do is to figure out how to make the calculation.

The way i read the spec, is - no you don't loop through until 10, you simply disallow anything greater than ten. You loop until counter == years worked
0
 

Author Comment

by:Brayn66
ID: 12156570
That is the way I interpeted it.

For example, if a user enters a $23,000 staring salary, a 2% increase and 6 years experience then I think the  pay schedule should display years 7-10 in the terminal window(year preceding the pay).

Is this what you think??
lauren
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12156581
Well i thought it should display years 1 to 6, but i could be wrong ;-)
0
 

Author Comment

by:Brayn66
ID: 12156619
I was thinking that a new teacher coming in and seeing a schedule might see where he/she lines up on the pay schedule based on his/her 6 years experience..

year 1
year 2
year 3
year 4
year 5
year 6    $23,000
year 7    $23,460
year 8    $23,929.20
year 9    $24,407.78
year10   $24,895.94


This is what I am envisioning the schedule to look like.

The teacher accepts a job (he/she has six years prior experience that he /she brings to the table) for $23,000 annually with % 2 pay increase yearly.

Year one through five is exempt because he/she has surpassed that level of pay.

Is this logical?
How is a loop implemented that loops to a certain count (like year 6)??
lauren
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12156675
Yes, that sounds logical, but i'm wondering if your interpretation is just too sophisticated ;-) The spec is just not very clear
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12156703
It's difficult to account for the ten years thing though, so you could be right. Let's see what the others think. Got to clock off - it's past 01:00
0
 

Author Comment

by:Brayn66
ID: 12156770
According to CEHJ (who has helped me so far on this one) I may be thinking too deep- very possible! lauren

If you have some insight please share!!

The program should display the pay schedule based on the user’s input but for no more than 10 years.
Inputs should be interactive.

Teachers are paid on a pay schedule that provides a salary based on experience in years.  For example. a teacher may have a starting salary of $20,000 the first year.  For each year of experience after this up to 10 years, a 2% increase over the preceding value is received.

1.  Write a program that displays a salary schedule for teachers.

2.  The inputs are: the starting salary, the percentage increase, and the number of years in the schedule.

3.  Each row in the schedule should contain the year number and the salary for that year.

 ***for the record, if a new teacher comes to town and she has 5 years of experience, lets say that her starting pay will be in accordance with the 5th year of pay in that particular school district. However, when the user inputs this information, the program will still output the full ten years of pay schedule for the new teacher*******

I have some new variables (double annualSalary1   through   double annualSalary10) that I have added in hopes of emerging or connecting them with a loop system.?.  
 
Here is my latest code:

import TerminalIO.KeyboardReader;

public class teacherpay {
      public static void main( String [] args) {

      KeyboardReader reader = new KeyboardReader();


      double startSalary;
      double percentageIncrease;
      double yearsWorked;


      //get user imputs

      System.out.print("Please enter your starting salary: $");
      startSalary = reader.readDouble();         // starting salary entered by the user.
      System.out.println("");

      System.out.print("Please enter the increase percentage: % ");
      percentageIncrease = reader.readDouble();       // percent increase entered by the user.
      System.out.println("");

      System.out.print("Please enter the amount of experience in number of years worked:  ");
      yearsWorked = reader.readDouble();      // years worked entered by the user.
      System.out.println("");

      double salaryIncrease = (startSalary * percentageIncrease);
      double annualSalary1 = (startSalary);
      double annualSalary2 = ((annualSalary1 * percentageIncrease) + annualSalary1);
      double annualSalary3 = ((annualSalary2 * percentageIncrease) + annualSalary1);
      double annualSalary4 = ((annualSalary3 * percentageIncrease) + annualSalary1);
      double annualSalary5 = ((annualSalary4 * percentageIncrease) + annualSalary1);
      double annualSalary6 = ((annualSalary5 * percentageIncrease) + annualSalary1);
      double annualSalary7 = ((annualSalary6 * percentageIncrease) + annualSalary1);
      double annualSalary8 = ((annualSalary7 * percentageIncrease) + annualSalary1);
      double annualSalary9 = ((annualSalary8 * percentageIncrease) + annualSalary1);
      double annualSalary10 = ((annualSalary9 * percentageIncrease) + annualSalary1);

      System.out.println("The teacher pay schedule based on your inputs are:  year 1   $" + annualSalary1);
      System.out.println("The teacher pay schedule based on your inputs are:  year 2   $" + annualSalary2);
      System.out.println("The teacher pay schedule based on your inputs are:  year 3   $" + annualSalary3);
      System.out.println("The teacher pay schedule based on your inputs are:  year 4   $" + annualSalary4);
      System.out.println("The teacher pay schedule based on your inputs are:  year 5   $" + annualSalary5);
      System.out.println("The teacher pay schedule based on your inputs are:  year 6   $" + annualSalary6);
      System.out.println("The teacher pay schedule based on your inputs are:  year 7   $" + annualSalary7);
      System.out.println("The teacher pay schedule based on your inputs are:  year 8   $" + annualSalary8);
      System.out.println("The teacher pay schedule based on your inputs are:  year 9   $" + annualSalary9);
      System.out.println("The teacher pay schedule based on your inputs are: year 10  $" + annualSalary10);

      System.out.println();
0
 

Author Comment

by:Brayn66
ID: 12156827


I have made some progress.
I corrected the calculation area of the pay schedule and I have shortened the output into what resembles a pay schedule.
Here's where I need some advice:
1. The calculation does not seem to work correctly after the first few years.
2. I need help on a looping mechanism that will suffice the program requirements (listed above).

The source code:

import TerminalIO.KeyboardReader;

public class teacherpay {
      public static void main( String [] args) {

      KeyboardReader reader = new KeyboardReader();


      double startSalary;
      double percentageIncrease;
      double yearsWorked;


      //get user imputs

      System.out.print("Please enter your starting salary: $");
      startSalary = reader.readDouble();         // starting salary entered by the user.
      System.out.println("");

      System.out.print("Please enter the increase percentage: % ");
      percentageIncrease = reader.readDouble();       // percent increase entered by the user.
      System.out.println("");

      System.out.print("Please enter the amount of experience in number of years worked:  ");
      yearsWorked = reader.readDouble();      // years worked entered by the user.
      System.out.println("");

      double salaryIncrease = (startSalary * percentageIncrease);
      double annualSalary1 = (startSalary);
      double annualSalary2 = ((annualSalary1 * percentageIncrease) + annualSalary1);
      double annualSalary3 = ((annualSalary2 * percentageIncrease) + annualSalary2);
      double annualSalary4 = ((annualSalary3 * percentageIncrease) + annualSalary3);
      double annualSalary5 = ((annualSalary4 * percentageIncrease) + annualSalary4);
      double annualSalary6 = ((annualSalary5 * percentageIncrease) + annualSalary5);
      double annualSalary7 = ((annualSalary6 * percentageIncrease) + annualSalary6);
      double annualSalary8 = ((annualSalary7 * percentageIncrease) + annualSalary7);
      double annualSalary9 = ((annualSalary8 * percentageIncrease) + annualSalary8);
      double annualSalary10 = ((annualSalary9 * percentageIncrease) + annualSalary9);

      System.out.println();
      System.out.println("The teacher pay schedule based on your inputs are:");
      System.out.println();
      System.out.println("year 1   $" + annualSalary1);
      System.out.println("year 2   $" + annualSalary2);
      System.out.println("year 3   $" + annualSalary3);
      System.out.println("year 4   $" + annualSalary4);
      System.out.println("year 6   $" + annualSalary6);
      System.out.println("year 7   $" + annualSalary7);
      System.out.println("year 8   $" + annualSalary8);
      System.out.println("year 9   $" + annualSalary9);
      System.out.println("year 10  $" + annualSalary10);

      System.out.println();
      System.out.println();
      System.out.println();
      System.out.println();

            }
      }



Thank you for helping!! lauren
0
 

Author Comment

by:Brayn66
ID: 12156876
I am raising the points to 500 for help on completing the existing program. lauren
0
 

Author Comment

by:Brayn66
ID: 12157198
Basically this programs requirements seem to have been fullfilled without including a loop system.

Unfortunately the chapter I am studying is involved with loops and I think the purpose of this assignment was to use a loop (but it did not specifically request a loop in the requirements).

    I would appreciate an example of a loop that I could use so as to shorten my source code by removing repetitive tasks and variables.
    A loop that could somehow include results for the variable: double yearsWorked. This is part of the input for the number of years of experience the teacher has. It currently seems to lack any value in the program.
I am sure the input is there for a loop system.

The way the current source code executes is displayed as follows:

Your teacher pay schedule based on your inputs are:
      year 1  pay is   $ .....
                year 2  pay is   $ .....
      year 3  pay is   $ .....
      year 4  pay is   $ .....
      year 6  pay is   $ .....
      year 7  pay is   $ .....
      year 8  pay is   $ .....
                year 9  pay is   $ .....      
                year 10 pay is  $ .....
                                                           ..... the correct output displays.


My current source code for the program:

import TerminalIO.KeyboardReader;

public class teacherpay {
      public static void main( String [] args) {

         KeyboardReader reader = new KeyboardReader();

      double startSalary;
      double percentageIncrease;
      double yearsWorked;                     //this  variable doesn't seems to serve a purpose in the program.

         //get user imputs

      System.out.print("Please enter your starting salary: $");
      startSalary = reader.readDouble();         // starting salary entered by the user.
      System.out.println("");

      System.out.print("Please enter the increase percentage: % ");
      percentageIncrease = reader.readDouble();       // percent increase entered by the user.
      System.out.println("");

      System.out.print("Please enter the amount of experience in number of years worked:  ");
      yearsWorked = reader.readDouble();      // years worked entered by the user.
      System.out.println("");

      double salaryIncrease = (startSalary * percentageIncrease);
      double annualSalary1 = (startSalary);
      double annualSalary2 = ((annualSalary1 * percentageIncrease) + annualSalary1);
      double annualSalary3 = ((annualSalary2 * percentageIncrease) + annualSalary2);
      double annualSalary4 = ((annualSalary3 * percentageIncrease) + annualSalary3);
      double annualSalary5 = ((annualSalary4 * percentageIncrease) + annualSalary4);
      double annualSalary6 = ((annualSalary5 * percentageIncrease) + annualSalary5);
      double annualSalary7 = ((annualSalary6 * percentageIncrease) + annualSalary6);
      double annualSalary8 = ((annualSalary7 * percentageIncrease) + annualSalary7);
      double annualSalary9 = ((annualSalary8 * percentageIncrease) + annualSalary8);
      double annualSalary10 = ((annualSalary9 * percentageIncrease) + annualSalary9);


      System.out.println();
      System.out.println("Your teacher pay schedule based on your inputs are:");
      System.out.println();
      System.out.println("year 1  pay is   $" + annualSalary1);
      System.out.println("year 2  pay is   $" + annualSalary2);
      System.out.println("year 3  pay is   $" + annualSalary3);
      System.out.println("year 4  pay is   $" + annualSalary4);
      System.out.println("year 6  pay is   $" + annualSalary6);
      System.out.println("year 7  pay is   $" + annualSalary7);
      System.out.println("year 8  pay is   $" + annualSalary8);
      System.out.println("year 9  pay is   $" + annualSalary9);
      System.out.println("year 10 pay is   $" + annualSalary10);

      System.out.println();
      System.out.println();
      System.out.println();
      System.out.println();

            }
      }

 
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12158231
After thinking about the spec, i think you're right. Your loop should be

final int MAX_YEARS = 10;
for(int i = yearsWorked;i <= MAX_YEARS;i++) {
      // print and do calc
}

Let's say yearsWorked == 6

Year        End year salary
-----
  6          xxxxxxxxx
  7          yyyyyyyyy
  8          etc
  9
10

Incidentally, that's another good reason why yearsWorked needs to be an int ;-)
0
 

Author Comment

by:Brayn66
ID: 12164360
I just got home and will test. Thank you - I'll be back soon...lauren
0
 

Author Comment

by:Brayn66
ID: 12165904
I am sorry I didn't respond. I had interrupted service due to the hurricane.

I contacted my instructor and the program must contain a control structure that repeats an operation or action for the number of times specified by a user. He says the correct structures are the WHILE structure or the FOR structure.

I see that this is what you are using above. I am having trouble implementing this because I am not sure of what code to keep and to cut.
I know that I should keep my initial variables and my user input statements but what about my later variables and output statements?

I have been in this Java class for about a month and we have gone over the following:
background, basic first program, language and syntax.
We are studying control statements now.

I think that the final method you have above is about 150 pages ahead of my current spot in the book. If possible, I think I should use a  simplier method ;-) if there is one?  

current source code:
import TerminalIO.KeyboardReader;

public class teacherpay {
      public static void main( String [] args) {

      KeyboardReader reader = new KeyboardReader();

      double startSalary;
      double percentageIncrease;
      int yearsWorked;

      //get user imputs

      startSalary = reader.readDouble("Please enter your starting salary: $");
      System.out.println("");

      percentageIncrease = reader.readDouble("Please enter the increase percentage: % ");
      System.out.println("");

      yearsWorked = reader.readDouble("Please enter the number of years worked:  ");
      System.out.println("");

      double salaryIncrease = (startSalary * percentageIncrease);
      double annualSalary1 = (startSalary);
      double annualSalary2 = ((annualSalary1 * percentageIncrease) + annualSalary1);
      double annualSalary3 = ((annualSalary2 * percentageIncrease) + annualSalary2);
      double annualSalary4 = ((annualSalary3 * percentageIncrease) + annualSalary3);
      double annualSalary5 = ((annualSalary4 * percentageIncrease) + annualSalary4);
      double annualSalary6 = ((annualSalary5 * percentageIncrease) + annualSalary5);
      double annualSalary7 = ((annualSalary6 * percentageIncrease) + annualSalary6);
      double annualSalary8 = ((annualSalary7 * percentageIncrease) + annualSalary7);
      double annualSalary9 = ((annualSalary8 * percentageIncrease) + annualSalary8);
      double annualSalary10 = ((annualSalary9 * percentageIncrease) + annualSalary9);

      System.out.println("Your teacher pay schedule based on your inputs are:");
      System.out.println();
      System.out.println("year 1  pay is   $" + annualSalary1);
      System.out.println("year 2  pay is   $" + annualSalary2);
      System.out.println("year 3  pay is   $" + annualSalary3);
      System.out.println("year 4  pay is   $" + annualSalary4);
      System.out.println("year 6  pay is   $" + annualSalary6);
      System.out.println("year 7  pay is   $" + annualSalary7);
      System.out.println("year 8  pay is   $" + annualSalary8);
      System.out.println("year 9  pay is   $" + annualSalary9);
      System.out.println("year 10 pay is   $" + annualSalary10);
      System.out.println();

         final int MAX_YEARS = 10;
      for(int i = yearsWorked;i <= MAX_YEARS;i++) {
           // print and do calc
            }
      }
}

with error code:

C:\ex\teacherpay.java:31: possible loss of precision
found   : double
required: int
      yearsWorked = reader.readDouble("Please enter the number of years worked:  ");
                                                                  ^
1 error
Tool completed with exit code 1

...this has to do with the int yearsWorked variable. It should be a int on the line for years worked entered by user. However, when I change it to:

         yearsWorked = reader.readDouble("Please enter the number of years worked:  ");
      System.out.println("");

I get the following error:

C:\ex\teacherpay.java:31: cannot resolve symbol
symbol  : method readint (java.lang.String)
location: class TerminalIO.KeyboardReader
      yearsWorked = reader.readint("Please enter the number of years worked:  ");
                                                ^
1 error
Tool completed with exit code 1

I am a little lost. lauren
 
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Brayn66
ID: 12166037
I created a for loop and implemented it into the code (this saved a bunch of typing and excess code!) but it is not executing.

The new code:

import TerminalIO.KeyboardReader;

public class teacherpayy {
public static void main( String [] args) {

KeyboardReader reader = new KeyboardReader();


double startSalary;
double percentageIncrease;
int yearsWorked;


      //get user imputs


startSalary = reader.readDouble("Please enter your starting salary: $");
System.out.println("");

percentageIncrease = reader.readDouble("Please enter the increase percentage: % ");
System.out.println("");

yearsWorked = reader.readDouble("Please enter the number of years worked:  ");     // line 24
System.out.println("");

int yearsWorked;         //line 27
            
   for(yearsWorked = 0; yearsWorked<10; yearsWorked = yearsWorked+1)
   System.out.println("This is your annual salary based on the number of years worked: " + yearsWorked);
            }
      }


The errors:
C:\ex\teacherpayy.java:24: possible loss of precision
found   : double
required: int
      yearsWorked = reader.readDouble("Please enter the number of years worked:  ");
                                       ^
C:\ex\teacherpayy.java:27: yearsWorked is already defined in main(java.lang.String[])
      int yearsWorked;
                    ^
2 errors
Tool completed with exit code 1


This is back to what I said in my last about the int yearsWorked variable. I have a problem initializing, declaring???
0
 

Author Comment

by:Brayn66
ID: 12166287
This post looks the same as the last except I have revised my for loop. I am having the same problem. lauren

import TerminalIO.KeyboardReader;

public class teacherpayy {
      public static void main(String [] args) {

      KeyboardReader reader = new KeyboardReader();


      double startSalary;
      double percentageIncrease;
      int yearsWorked;


      //get user imputs


      startSalary = reader.readDouble("Please enter your starting salary: $");
      System.out.println("");

      percentageIncrease = reader.readDouble("Please enter the increase percentage: % ");
      System.out.println("");

      yearsWorked = reader.readDouble("Please enter the number of years worked:  ");
      System.out.println("");


final int MAX_YEARS = 10;
            for(int i = yearsWorked;i <= MAX_YEARS; i = i++)

            System.out.println("This is your annual salary based on the number of years worked: " + yearsWorked);

            }
      }



Error codes:
C:\ex\teacherpayy.java:24: possible loss of precision
found   : double
required: int
      yearsWorked = reader.readDouble("Please enter the number of years worked:  ");
                                                                 ^
1 error

Tool completed with exit code 1
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12167099
That class KeyboardReader that you're using is non-standard, but you'll probably find it has a readInt method, so try

yearsWorked = reader.readInt("Please enter the number of years worked:  ");
0
 

Author Comment

by:Brayn66
ID: 12168559
Okay. I have changed the code as above and it compiles and executes however I need to find a way to incoprporate the calculations into the program.


here is the source:
import TerminalIO.KeyboardReader;

public class teacherpayy {
      public static void main(String [] args) {

      KeyboardReader reader = new KeyboardReader();


      double startSalary;
      double percentageIncrease;
      int yearsWorked;


      //get user imputs


      startSalary = reader.readDouble("Please enter your starting salary: $");
      System.out.println("");

      percentageIncrease = reader.readDouble("Please enter the increase percentage: % ");
      System.out.println("");

      yearsWorked = reader.readInt("Please enter the number of years worked:  ");
      System.out.println("");


final int MAX_YEARS = 10;
            for(int i = yearsWorked;i <= MAX_YEARS; i = i++)

            System.out.println("This is your annual salary based on the number of years worked: " + yearsWorked);

            }
      }




I sent the following examples of a while and a for to my instructor for his review:

For instance, here is a for loop that will display the years 1-10   :
 
class ForTest  {
 public static void main(String args[]){

  int yearsWorked;
  for(yearsWorked = 1; yearsWorked<11; yearsWorked = yearsWorked+1)
  System.out.println("Year " + yearsWorked);
}
 }



At the same time, here is a while loop that will display  the years 1-10  :
 
class CountTest  {
 public static void main(String args[]){

 int count = 1;
 while(count < 11) {
  System.out.println("Year " + count);
  count++;
 }
 }
}
 

The response from my instructor was:
In order to use a loop, you have to know the beginning condition and the ending condition. The beginning condition or bound is 1. The ending bounding is whatever the number of years is that the user indicates.
 
The code that you state in your message appears to be correct other than you need to modify it to use the upper bound based on a user's input. Once this is done, all you need to do is add the statements that you want to execute inside of the loop.

Just food for thought...
Thank you, lauren

ps- we are on differing time schedules I think.  I am trying to get the day off from work now.
If not, I will be back in 6 hours :(
 
0
 

Author Comment

by:Brayn66
ID: 12168586
I got the day off :)   : )
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12168642
OK - i'm going out and i'll be back at about that time too. This is the 'hard' bit:

public static double calculateSalaryFor YearN(double startingSalary, double percentageIncrease, int yearN) {
      percentageIncrease = (percentageIncrease / 100) + 1;
      return startingSalary * Math.pow(percentageIncrease, yearN - 1);
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12168645
Make sure you test that, as i haven't ;-)
0
 

Author Comment

by:Brayn66
ID: 12172353
Here is the charges that you have shown.
I have a couple of compile errors that won't allow me to execute.

My code:

import TerminalIO.KeyboardReader;

public class teacherpayy {
      public static void main(String [] args) {

      KeyboardReader reader = new KeyboardReader();

      double startSalary;
      double percentageIncrease;
      int yearsWorked;

      //get user imputs

      startSalary = reader.readDouble("Please enter your starting salary: $");
      System.out.println("");

      percentageIncrease = reader.readDouble("Please enter the increase percentage: % ");
      System.out.println("");

      yearsWorked = reader.readInt("Please enter the number of years worked:  ");
      System.out.println("");

public static double calculateSalaryFor YearN(double startingSalary, double percentageIncrease, int yearN) {
     percentageIncrease = (percentageIncrease / 100) + 1;
     return startingSalary * Math.pow(percentageIncrease, yearN - 1);
       }     //this is line 34
       }    // this is line 35


My compile errors:


C:\ex\teacherpayy.java:31: illegal start of expression
public static double calculateSalaryFor YearN(double startingSalary, double percentageIncrease, int yearN) {
^
C:\ex\teacherpayy.java:35: ';' expected
}
^
C:\ex\teacherpayy.java:35: '}' expected
}
 ^
3 errors
Tool completed with exit code 1


Why do you think I get the error "illegal start of expression" error.
The other two look like I am missing something like the statements.

lauren  
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12173262
You put the function in the wrong place. It should be outside of main on its own before the closing class brace
0
 

Author Comment

by:Brayn66
ID: 12174599
See if this looks like the right direction.  Thanks,lauren

Source code:


import TerminalIO.KeyboardReader;

public class teacherpay  {
      public static void main(String args[])
      public static double calculateSalaryFor YearN(double startingSalary, double percentageIncrease, int yearN) {
     percentageIncrease = (percentageIncrease / 100) + 1;
     return startingSalary * Math.pow(percentageIncrease, yearN - 1);{

      KeyboardReader reader = new KeyboardReader();

      double startSalary;
      double percentageIncrease;
      int yearsWorked;


      //get user imputs


      startSalary = reader.readDouble("Please enter your starting salary: $");
      System.out.println("");

      percentageIncrease = reader.readDouble("Please enter the increase percentage: % ");
      System.out.println("");

      yearsWorked = reader.readInt("Please enter the number of years worked:  ");
      System.out.println("");

            for(yearsWorked = 1; yearsWorked<11; yearsWorked = yearsWorked+1)

            System.out.println("Year " + yearsWorked);
}
      }
}


my errors:
C:\ex\teacherpay.java:8: ';' expected
      public static double calculateSalaryFor YearN(double startingSalary, double percentageIncrease, int yearN) {
        ^
1 error
Tool completed with exit code 1

This thing is going to give me a nervous breakdown :(
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12174661
No - you've still go them nested. Should be

public class Foo {
    public static void main(String[] args) {}
    public static void bar() {}
}
0
 

Author Comment

by:Brayn66
ID: 12174812
okay, I have changed and here's the latest:

import TerminalIO.KeyboardReader;

public class teacherpay {
    public static void main(String[] args) {
   
   KeyboardReader reader = new KeyboardReader();

double startSalary;
double percentageIncrease;
int yearsWorked;

      //get user imputs

      startSalary = reader.readDouble("Please enter your starting salary: $");
      System.out.println("");

      percentageIncrease = reader.readDouble("Please enter the increase percentage: % ");
      System.out.println("");

      yearsWorked = reader.readInt("Please enter the number of years worked:  ");
      System.out.println("");

          for(yearsWorked = 1; yearsWorked<11; yearsWorked = yearsWorked+1)
            System.out.println("Year " + yearsWorked);

      //does the above for loop have value for this program or am I using just the loop you have recently provided?


            }

                          public static void bar() {     //I don't understand the 'bar' or this listing ?
            }
}

  public static double calculateSalaryFor YearN(double startingSalary, double percentageIncrease, int yearN) {
     percentageIncrease = (percentageIncrease / 100) + 1;
     return startingSalary * Math.pow(percentageIncrease, yearN - 1);{

My errors were:

C:\ex\teacherpay.java:35: 'class' or 'interface' expected
      public static double calculateSalaryFor YearN(double startingSalary, double percentageIncrease, int yearN) {
                      ^
1 error
Tool completed with exit code 1
I am off on the flow of the program. lauren
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 12175052
My illustration is to show you where the braces are meant to be - the names are meaningless
0
 

Author Comment

by:Brayn66
ID: 12187110
I am not sure where I am with this point thing. I have ask you a ton of questions and you have been helpful and
generous.  I have not mastered this program with the for loop, but because you helped me I want to award these points to you (CEHJ). I do want to post this information about my code's status:

I think my formula(s) needs some direction and my loop statements are not showing up. Thanks, lauren

import TerminalIO.KeyboardReader;
public class teacherpay {
      public static void main(String [] args) {

      KeyboardReader reader = new KeyboardReader();
      double startSalary;
      double percentageIncrease;
      double calculateSalary;
      int yearsWorked;
      double yearN;

      startSalary = reader.readDouble("Please enter your starting salary: $ ");
      System.out.println("");

      percentageIncrease = reader.readDouble("Please enter a whole number for the increase percentage: % ");
      System.out.println("");

      yearsWorked = reader.readInt("Please enter the number of years worked:  ");
      System.out.println("");

    percentageIncrease = (percentageIncrease / 100);
    return startSalary * Math.pow(percentageIncrease, yearN - 1);{


  for(yearsWorked = 1; yearsWorked<=10; yearsWorked = yearsWorked+1)
 System.out.println("This is your annual salary based on the number of years worked:  " + yearsWorked);

}}
}

I get the following error code:

C:\ex\teacherpay.java:29: cannot return a value from method whose result type is void
    return startSalary * Math.pow(percentageIncrease, yearN - 1);{
                       ^
1 error
Tool completed with exit code 1


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12188526
There are some important (but simple) holes in your knowledge. You should try to fill them. Start here:

http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/prog.html#class

(i *think* it's possible to skip part one if you're in a hurry). Don't worry about points and my time - we're here to help. Come back to me later
0
 

Author Comment

by:Brayn66
ID: 12197041
I have studied the link you gave for some time and have learned alot of the basics that I had missed. The text book we use is considerably more complicated to digest. - thanks!
I then revisited my code and found some of the more obvious errors in my design. I am displaying the bottom output statement and the loop is working.


Here is the source:

import TerminalIO.KeyboardReader;
public class teacherpay {
      public static void main(String [] args) {

      KeyboardReader reader = new KeyboardReader();

        double startSalary;
        double percentageIncrease;
        double calculateSalary;
        int yearsWorked;
        double yearN;

        startSalary = reader.readDouble("Please enter your starting salary: $ ");
      System.out.println("");

         percentageIncrease = reader.readDouble("Please enter a whole number for the annual increase percentage: % ");
      System.out.println("");

      yearsWorked = reader.readInt("Please enter the number of years worked:  ");
      System.out.println("");
      System.out.println();

   percentageIncrease = (percentageIncrease / 100);

  for(yearsWorked = 1; yearsWorked<=10; yearsWorked = yearsWorked+1)
  System.out.println("Annual salary for year:  " + yearsWorked);
      System.out.println();
      System.out.println();
      System.out.println();
}}





No compile error this time :)
I am having problems comprehending how the loop is activated via a users input. The yearsWorked variable doesn't seem to have a purpose in the program.
I need to integrate a formula with the loop so that the salaries with projected increases calculates and displays in the schedule format.

My formula(s) is/are a big concern.
You have provided me with possible formulas (like the ones below) and I am not sure where or how they fit in.



double salaryIncrease = (startSalary * percentageIncrease);
yearN = ((startSalary * percentageIncrease) + startSalary);

final int MAX_YEARS = 10;
  for(int i = yearsWorked;i <= MAX_YEARS; i = i++)
   System.out.println("This is your annual salary based on the number of years worked: " +
   yearsWorked);

public static double calculateSalaryFor YearN(double startingSalary, double percentageIncrease, int yearN) {
     percentageIncrease = (percentageIncrease / 100) + 1;
     return startingSalary * Math.pow(percentageIncrease, yearN - 1);

Thank you!! lauren
 
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12197670
Better. Try to get the indentation of your code correct. e.g. you shouldn't have two braces together like this:

}}

Blocks should be indented. Look at the examples. When you've done that, make sure that last loop is operating *exactly* as you wanted to (i suspect not)
0
 

Author Comment

by:Brayn66
ID: 12198839
I'll check the braces. When you say "last loop", does this mean that there can be more than one in the program?

Also, I was just going to let the loop calculate and display for ten years since the spec is questionable.

lauren
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12198981
>>does this mean that there can be more than one in the program?

There can be as many as you want

>>Also, I was just going to let the loop calculate and display for ten years

Yes, i think years of experience to ten years
0
 

Author Comment

by:Brayn66
ID: 12205476
I think I am getting close. I am having a problem fiquring out a way to get the annual Salary to incrementally add and build for the ten year schedule. Thank you! lauren



import TerminalIO.KeyboardReader;
public class _startsalary {
      public static void main(String [] args) {

      KeyboardReader reader = new KeyboardReader();

      double startSalary;
      double percentageIncrease;
      int annualSalary;
      int yearsWorked;
      double salaryRange;
      double yearN;   // extra variable declared

      startSalary = reader.readDouble("Please enter your starting salary: $ ");
      System.out.println("");

      percentageIncrease = reader.readDouble("Please enter a whole number for the annual increase percentage: % ");
      System.out.println("");

      yearsWorked = reader.readInt("Please enter the number of years worked:  ");
      System.out.println("");
      System.out.println();

   percentageIncrease = (percentageIncrease / 100) + 1;
   annualSalary = ((startSalary * percentageIncrease) + startSalary);
   salaryRange = ((annualSalary * percentageIncrease) + annualSalary) + 1;

  for(yearsWorked = 1; yearsWorked<=10; yearsWorked = yearsWorked+1)

public static double calculateSalaryFor annualSalary(double startSalary, double percentageIncrease, int annualSalary){

return annualSalary * Math.pow(percentageIncrease, annualSalary - 1);

}

  System.out.println("Annual salary for year:  " + yearsWorked + ": " + salaryRange+1);
      System.out.println();
      System.out.println();
      System.out.println();
                  }
}




My error:

C:\ex\_startsalary.java:36: illegal start of expression
public static double calculateSalaryFor annualSalary(double startSalary, double percentageIncrease, int annualSalary){
^
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12206583
Yes - you still have not appreciated how code blocks and methods work in connection with the curly braces of code blocks. Read the tut about methods again, paying particular attention to the positions of the curly braces
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now