Link to home
Start Free TrialLog in
Avatar of Brayn66
Brayn66

asked on

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

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!
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>int annualSalary = (startSalary + salaryIncrease);

should be

double annualSalary = (startSalary + salaryIncrease);



Avatar of Brayn66
Brayn66

ASKER

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

Please post your corrected code. Yes it will need a loop
Avatar of Brayn66

ASKER

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
Missing semicolon at the end of that line
The years parameter should really be an int
Avatar of Brayn66

ASKER

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();
            }
      }

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
Avatar of Brayn66

ASKER

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
Well i thought it should display years 1 to 6, but i could be wrong ;-)
Avatar of Brayn66

ASKER

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
Yes, that sounds logical, but i'm wondering if your interpretation is just too sophisticated ;-) The spec is just not very clear
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
Avatar of Brayn66

ASKER

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();
Avatar of Brayn66

ASKER



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
Avatar of Brayn66

ASKER

I am raising the points to 500 for help on completing the existing program. lauren
Avatar of Brayn66

ASKER

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();

            }
      }

 
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 ;-)
Avatar of Brayn66

ASKER

I just got home and will test. Thank you - I'll be back soon...lauren
Avatar of Brayn66

ASKER

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
 
Avatar of Brayn66

ASKER

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???
Avatar of Brayn66

ASKER

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
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:  ");
Avatar of Brayn66

ASKER

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 :(
 
Avatar of Brayn66

ASKER

I got the day off :)   : )
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);
}
Make sure you test that, as i haven't ;-)
Avatar of Brayn66

ASKER

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  
You put the function in the wrong place. It should be outside of main on its own before the closing class brace
Avatar of Brayn66

ASKER

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 :(
No - you've still go them nested. Should be

public class Foo {
    public static void main(String[] args) {}
    public static void bar() {}
}
Avatar of Brayn66

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brayn66

ASKER

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


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
Avatar of Brayn66

ASKER

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
 
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)
Avatar of Brayn66

ASKER

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
>>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
Avatar of Brayn66

ASKER

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){
^
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