Question

Taking this program how can the requirements under it be added in java? import java.util.Scanner; public...

Taking this program how can the requirements under it be added in java?

import java.util.Scanner;

public class RetirementSavings {
   private static final int MAX_SALARY = 300000;
   private static final double MAX_SAVING_RATE = 0.3;
   private static final double MAX_INTEREST_RATE = 0.2;
   private static final int MAX_YEAR_EMPLOYED = 40;

   public static void main(String[] args) {
       double savings_rate, interest_rate;
       int salary, years_employed;
       String name = ""; // name is initialized with an empty string
       Scanner scan = new Scanner(System.in);
       int position = 0;// this will be used to determine at which position the error has occurred
       System.out.print("Enter input: salary savings_rate interest_rate years_employed name(last,first)\n");
       try {

           String[] inputLine = scan.nextLine().split("\\s+");

           salary = Integer.parseInt(inputLine[0]);
           position++;
           if (salary < 0) {
               System.out.println("salary " + salary + " less than 0");
               System.exit(0);
           } else if (salary > MAX_SALARY) {
               System.out.println("salary " + salary + " exceeds maximum " + MAX_SALARY);
               System.exit(0);
           }

           savings_rate = Double.parseDouble(inputLine[1]);
           position++;
           if (savings_rate > 1) {
               savings_rate = savings_rate / 100;
           }
           if (savings_rate < 0) {
               System.out.println("savings rate " + savings_rate + " less than 0");
               System.exit(0);
           } else if (savings_rate > MAX_SAVING_RATE) {
               System.out.println("savings rate " + savings_rate + " exceeds maximum " + MAX_SAVING_RATE);
               System.exit(0);
           }

           interest_rate = Double.parseDouble(inputLine[2]);
           position++;
           if (interest_rate > 1) {
               interest_rate = interest_rate / 100;
           }
           if (interest_rate < 0) {
               System.out.println("interest rate " + interest_rate + " less than 0");
               System.exit(0);
           } else if (interest_rate > MAX_INTEREST_RATE) {
               System.out.println("interest rate " + interest_rate + " exceeds maximum " + MAX_INTEREST_RATE);
               System.exit(0);
           }

           years_employed = (int)Double.parseDouble(inputLine[3]);
           if(years_employed<Double.parseDouble(inputLine[3])) {
               //rounding up years employed
               years_employed++;
           }
           position++;
           if (years_employed < 0) {
               System.out.println("years employed " + years_employed + " less than 0");
               System.exit(0);
           } else if (years_employed > MAX_YEAR_EMPLOYED) {
               System.out.println("years employed " + years_employed + " exceeds maximum " + MAX_YEAR_EMPLOYED);
               System.exit(0);
           }

           if (inputLine.length == 5) {
               name = inputLine[4];
               position++;
           }
           if (name.isEmpty()) {// check if name is empty
               System.out.println("Error: Illegal input for name - exiting");
               System.exit(0);
           }

          

           System.out.println("Salary: " + salary);
           System.out.println("Savings rate: " + savings_rate);
           System.out.println("Interest rate: " + interest_rate);
           System.out.println("Years of Employment: " + years_employed);
           System.out.println("Name: " + name);
           double retirement_savings = 0;
           double interest = 0;
           double savingsPerYear = savings_rate * salary;
           for(int i=1;i<=years_employed;i++) {
               retirement_savings += savingsPerYear;
               interest = retirement_savings*interest_rate;
               retirement_savings+=interest;
           System.out.printf("Year: %d Intrest Earned: %.2f Balance: %.2f\n",i,interest, retirement_savings);
           }
       } catch (ArrayIndexOutOfBoundsException e) { // catch when inputLine is referenced with improper index due to
                                                       // not providing proper data
           System.out.println("Invalid number of inputs!");
           System.exit(0);
       } catch (NumberFormatException e) { // catch when data could not be converted to proper type
           String errorFieldName = null;

           if (position == 0) {
               errorFieldName = "salary";
           } else if (position == 1) {
               errorFieldName = "savings rate";
           } else if (position == 2) {
               errorFieldName = "interest rate";
           } else if (position == 3) {
               errorFieldName = "years employed";
           }
           System.out.println("Error: Illegal input for " + errorFieldName + " - exiting");
           System.exit(0);
       } finally {
           scan.close();// close scanner anyway
       }
   }
}

Requirements:

you will read in a list of a maximum of 5 employees with data into an array and output the results based on their total retirement savings. Feel free to size your array with a maximum size of 5.

You can either define your outer loop to only read in 5 values or make it flexible and use the "quit" string to terminate. You have flexibility there.

Sort the results based on the total retirement savings and then output the information based on descending order (highest to lowest) as shown below.

Use whatever sort method you are comfortable with (selection sort, bubble sort, Arrays.sort, etc.).

Things to consider:

  • You will need to remove the prompt for input line. Just cut/paste the data below into NetBeans window and display the results.
  • You will most likely need to use multiple arrays or a multi-dimensional array for this assignment but there's always more than one way to program. Use whatever works for you and seems the easiest.
  • Please follow the output format shown below as a guideline.

Feel free to use methods if it helps you out.

Use the below lines as input for testing:

70000 10 6 31.2 Smith,John
70000 5 6 31 Trump,Don
70000 10 10 25 Pence,Mike
100000 5 5 20 Brady,Tom
300000 10 6 30 Larson,Dr
quit (this is optional. you just need to read in data for 5 employees).

Run

Here's the output using the data above. Notice that the output is sorted based on the Total Retirement Savings.

run:
70000 10 6 31.2 Smith,John
70000 5 6 31 Trump,Don
70000 10 10 25 Pence,Mike
100000 5 5 20 Brady,Tom
300000 10 6 30 Larson,Dr
quit
Done with input
1: Larson,Dr Salary 300000.00 Savings Rate:0.10 Interest Rate:0.06 Years:30.000000 Balance: 2514050.32
2: Pence,Mike Salary 70000.00 Savings Rate:0.10 Interest Rate:0.10 Years:25.000000 Balance: 757272.36
3: Smith,John Salary 70000.00 Savings Rate:0.10 Interest Rate:0.06 Years:32.000000 Balance: 674402.15
4: Trump,Don Salary 70000.00 Savings Rate:0.05 Interest Rate:0.06 Years:31.000000 Balance: 314614.22
5: Brady,Tom Salary 100000.00 Savings Rate:0.05 Interest Rate:0.05 Years:20.000000 Balance: 173596.26
BUILD SUCCESSFUL (total time: 1 second)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

==============================

UPDATED JAVA PROGRAM

==============================

import java.text.DecimalFormat;
import java.util.Scanner;

public class RetirementSavings {
private static final int MAX_SALARY = 300000;
private static final double MAX_SAVING_RATE = 0.3;
private static final double MAX_INTEREST_RATE = 0.2;
private static final int MAX_YEAR_EMPLOYED = 40;

private static final int MAX_INPUT = 5;

public static void main(String[] args) {
double savings_rate, interest_rate;
int salary, years_employed;
String name = ""; // name is initialized with an empty string
Scanner scan = new Scanner(System.in);
int position = 0;// this will be used to determine at which position the error has occurred
System.out.print("Enter input: salary savings_rate interest_rate years_employed name(last,first)\n");

//introduce 2 arrays
//one to hold total retirement savings
//other to hol output data
double totalSavings[] = new double[MAX_INPUT];
String outputData[] = new String[MAX_INPUT];

//decimal format for amount field
DecimalFormat df = new DecimalFormat("#.00");

try {
     
   //introducing for loop for the program
   for(int j = 1; j <= MAX_INPUT; j++){
       String input = scan.nextLine();
         
       String[] inputLine = input.split("\\s+");
  
   salary = Integer.parseInt(inputLine[0]);
   position++;
   if (salary < 0) {
   System.out.println("salary " + salary + " less than 0");
   System.exit(0);
   } else if (salary > MAX_SALARY) {
   System.out.println("salary " + salary + " exceeds maximum " + MAX_SALARY);
   System.exit(0);
   }
  
   savings_rate = Double.parseDouble(inputLine[1]);
   position++;
   if (savings_rate > 1) {
   savings_rate = savings_rate / 100;
   }
   if (savings_rate < 0) {
   System.out.println("savings rate " + savings_rate + " less than 0");
   System.exit(0);
   } else if (savings_rate > MAX_SAVING_RATE) {
   System.out.println("savings rate " + savings_rate + " exceeds maximum " + MAX_SAVING_RATE);
   System.exit(0);
   }
  
   interest_rate = Double.parseDouble(inputLine[2]);
   position++;
   if (interest_rate > 1) {
   interest_rate = interest_rate / 100;
   }
   if (interest_rate < 0) {
   System.out.println("interest rate " + interest_rate + " less than 0");
   System.exit(0);
   } else if (interest_rate > MAX_INTEREST_RATE) {
   System.out.println("interest rate " + interest_rate + " exceeds maximum " + MAX_INTEREST_RATE);
   System.exit(0);
   }
  
   years_employed = (int)Double.parseDouble(inputLine[3]);
   if(years_employed<Double.parseDouble(inputLine[3])) {
   //rounding up years employed
   years_employed++;
   }
   position++;
   if (years_employed < 0) {
   System.out.println("years employed " + years_employed + " less than 0");
   System.exit(0);
   } else if (years_employed > MAX_YEAR_EMPLOYED) {
   System.out.println("years employed " + years_employed + " exceeds maximum " + MAX_YEAR_EMPLOYED);
   System.exit(0);
   }
  
   if (inputLine.length == 5) {
   name = inputLine[4];
   position++;
   }
   if (name.isEmpty()) {// check if name is empty
   System.out.println("Error: Illegal input for name - exiting");
   System.exit(0);
   }
  
  
   //Commenting of the below print statements
   //System.out.println("Salary: " + salary);
   //System.out.println("Savings rate: " + savings_rate);
   //System.out.println("Interest rate: " + interest_rate);
   //System.out.println("Years of Employment: " + years_employed);
   //System.out.println("Name: " + name);
     
   double retirement_savings = 0;
   double interest = 0;
   double savingsPerYear = savings_rate * salary;
   for(int i=1;i<=years_employed;i++) {
   retirement_savings += savingsPerYear;
   interest = retirement_savings*interest_rate;
   retirement_savings+=interest;
   //System.out.printf("Year: %d Intrest Earned: %.2f Balance: %.2f\n",i,interest, retirement_savings);
   }
     
   //store total retirement saving for this employee
   totalSavings[j-1]= retirement_savings;
   //store output data for this employee
   outputData[j-1] = name+
                      " Salary "+df.format(salary)+ //format through decimalFormat
                      " Savings Rate:"+String.format("%1.2f", savings_rate)+
                      " Interest Rate:"+String.format("%1.2f", interest_rate)+
                      " Years:"+String.format("%2.6f", (float)years_employed)+ //show years with 6 places after decimal point
                      " Balance:"+df.format(retirement_savings);
     
   }
     
   System.out.println("Done with input.");
     
   //sort the totalSavings and outputData based on descending order of
   //total retirement savings
   //using bubble sort
   for(int i = 0; i < MAX_INPUT ; i++){
       for(int j = 0; j < MAX_INPUT; j++){
           if(totalSavings[i] > totalSavings[j]){
               double temp = totalSavings[i];
               totalSavings[i] = totalSavings[j];
               totalSavings[j] = temp;
                 
               String tempData = outputData[i];
               outputData[i] = outputData[j];
               outputData[j] = tempData;
           }
       }
   }
     
   //print output
   for(int i = 1; i <= MAX_INPUT; i++){
       System.out.println(i+":"+outputData[i-1]);
   }
     
     
     
} catch (ArrayIndexOutOfBoundsException e) { // catch when inputLine is referenced with improper index due to
// not providing proper data
System.out.println("Invalid number of inputs!");
System.exit(0);
} catch (NumberFormatException e) { // catch when data could not be converted to proper type
String errorFieldName = null;

if (position == 0) {
errorFieldName = "salary";
} else if (position == 1) {
errorFieldName = "savings rate";
} else if (position == 2) {
errorFieldName = "interest rate";
} else if (position == 3) {
errorFieldName = "years employed";
}
System.out.println("Error: Illegal input for " + errorFieldName + " - exiting");
System.exit(0);
} finally {
scan.close();// close scanner anyway
}
}
}

=============================

OUTPUT

=============================

Enter input: salary savings_rate interest_rate years_employed name(last,first)
70000 10 6 31.2 Smith,John
70000 5 6 31 Trump,Don
70000 10 10 25 Pence,Mike
100000 5 5 20 Brady,Tom
300000 10 6 30 Larson,Dr
Done with input.
1:Larson,Dr Salary 300000.00 Savings Rate:0.10 Interest Rate:0.06 Years:30.000000 Balance:2514050.32
2:Pence,Mike Salary 70000.00 Savings Rate:0.10 Interest Rate:0.10 Years:25.000000 Balance:757272.36
3:Smith,John Salary 70000.00 Savings Rate:0.10 Interest Rate:0.06 Years:32.000000 Balance:674402.15
4:Trump,Don Salary 70000.00 Savings Rate:0.05 Interest Rate:0.06 Years:31.000000 Balance:314614.22
5:Brady,Tom Salary 100000.00 Savings Rate:0.05 Interest Rate:0.05 Years:20.000000 Balance:173596.26

Add a comment
Know the answer?
Add Answer to:
Taking this program how can the requirements under it be added in java? import java.util.Scanner; public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public...

    // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public class String { public static void main(String[] args) { double Sside, Rlength, Rwidth, Tbase, Theight, Area, Tarea, Rarea; String input; Scanner keyboard = new Scanner(System.in); System.out.print("To Calculate the are of Square Enter 'Square', For Rectangle Enter 'Rectangle', For Triangle Enter 'Triangle'"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("SQUARE")) { System.out.println("Square Side"); Sside = keyboard.nextInt(); Tarea = Sside * Sside; System.out.println("Side = " + Tarea ); }...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • Java // Topic 2c // Program reserves airline seats. import java.util.Scanner public class Plane {    //...

    Java // Topic 2c // Program reserves airline seats. import java.util.Scanner public class Plane {    // checks customers in and assigns them a boarding pass    // To the human user, Seats 1 to 2 are for First Class passengers and Seats 3 to 5 are for Economy Class passengers    //    public void reserveSeats()    {       int counter = 0;       int section = 0;       int choice = 0;       String eatRest = ""; //to hold junk in input buffer       String inName = "";      ...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • import java.util.Scanner; public class creditScore { public static void main(String[]args) { // declare and initialize variables...

    import java.util.Scanner; public class creditScore { public static void main(String[]args) { // declare and initialize variables int creditScore; double loanAmount,interestRate,interestAmount; final double I_a = 5.56,I_b = 6.38,I_c = 7.12,I_d = 9.34,I_e = 12.45,I_f = 0; String instructions = "This program calculates annual interest\n"+"based on a credit score.\n\n"; String output; Scanner input = new Scanner(System.in);// for receiving input from keyboard // get input from user System.out.println(instructions ); System.out.println("Enter the loan amount: $"); loanAmount = input.nextDouble(); System.out.println("Enter the credit score: "); creditScore...

  • I need the following java code commented import java.util.Scanner; public class Main { public static void...

    I need the following java code commented import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); int productNo=0; double product1; double product2; double product3; double product4; double product5; int quantity; double totalsales=0; while(productNo !=0) System.out.println("Enter Product Number 1-5"); productNo=input.nextInt(); System.out.println("Enter Quantity Sold"); quantity=input.nextInt(); switch (productNo) { case 1: product1=1.00; totalsales+=(1.00*quantity); break; case 2: product2=2.00; totalsales+=(2.00*quantity); break; case 3: product3=6.00; totalsales+=(6.00*quantity); break; case 4: product4=23.00; totalsales+=(23.00*quantity); break; case 5: product5=17.00; totalsales+=(17.00*quantity); break;...

  • import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 {    public static void main(String[] args)    {...

    import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 {    public static void main(String[] args)    {        String name;        String answer;        int correct = 0;        int incorrect = 0;        Scanner phantom = new Scanner(System.in);        System.out.println("Hello, What is your name?");        name = phantom.nextLine();        System.out.println("Welcome " + name + "!\n");        System.out.println("My name is Danielle Brandt. "            +"This is a quiz program that...

  • Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public...

    Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public class Project2 { //Creating an random class object static Random r = new Random(); public static void main(String[] args) {    char compAns,userAns,ans; int cntUser=0,cntComp=0; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in);       System.out.println("*************************************"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes , N = No...

  • How can I split this program into two classes? import java.io.*; public class Quiz { static...

    How can I split this program into two classes? import java.io.*; public class Quiz { static LineNumberReader cin = new LineNumberReader(new InputStreamReader(System.in)); public static void main(String[] args) { int score = 0; final int NumberofQuestions = 5; int num;    System.out.println("\nWelcome to CSC 111 Java Quiz\n");    String[][] QandA = { {"All information is stored in the computer using binary numbers: ","true"}, {"The word \"Public\" is a reserved word: ","false"}, {"Variable names may begin with a number: ","false"}, {"Will the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT