Question

Design a program that calculates the amount of money a person would earn over a period...

Design a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a dollar amount, not the number of pennies. Use while loop to code for this particular assignment in JAVA

The following code was programmed using a for loop


package salary;

import java.util.*;

public class Salary {

public static void salaryInfo(int n){

double currentSalary = 0.01;

double totalSalary = 0;

System.out.println("Day\tSalary(DollarAmount)");

for(int i = 1;i <= n;i++){

//Display day number and salary

System.out.println(i+"\t"+currentSalary);   

//Add current salary to total salary

totalSalary+=currentSalary;

//double the current salary

currentSalary*=2;

}

  

//Display total salary

System.out.println("Total Salary\t"+totalSalary);

}

  

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter number of days : ");

int n = in.nextInt();   

if(n>0&&n<=30)

salaryInfo(n);

}

}

Homework Answers

Answer #1

Solution:

Note : In the question, it is only asked to use while loop.

Code:

package salary;

import java.util.*;

public class Salary {

public static void salaryInfo(int n){

double currentSalary = 0.01;

double totalSalary = 0;

System.out.println("Day\tSalary(DollarAmount)");

int i=0;

while(i<n){

//Display day number and salary

System.out.println(i+"\t"+currentSalary);

//Add current salary to total salary

totalSalary+=currentSalary;

//double the current salary

currentSalary*=2;

i++; //increment i value

}

//Display total salary

System.out.println("Total Salary\t"+totalSalary);

}

public static void main(String[] args)

{

Scanner in = new Scanner(System.in);

System.out.println("Enter number of days : ");

int n = in.nextInt();

if(n>0&&n<=30)

salaryInfo(n);

}

}

/*

Output :

Enter number of days :   

5

Day Salary(DollarAmount)   

0 0.01   

1 0.02   

2 0.04   

3 0.08   

4 0.16   

Total Salary 0.31000000000000005

*/

Know the answer?
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
Similar Questions
In Java ReWrite the for loop program code example 3, above as a while loop. Code...
In Java ReWrite the for loop program code example 3, above as a while loop. Code Example 3 – Try it import java.util.Scanner; public static void main(String args[]) { int count = 0; int maxNumber = 0; int enteredNumber = -9999999; System.out.println("This program determines which entered number is the largest "); System.out.println("Enter count of numbers to check: "); Scanner scanIn = new Scanner(System.in); double count = scanIn.nextDouble(); scanIn.close(); // Print out message – input number – compare to see if...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the...
using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the countdown sequence for a rocket launch. However, it seems to loop infinitely. Fix the program so it counts down and terminates properly. starter code : import java.util.Scanner; /* * Counts down to a blastoff, starting from a given number. */ public class Countdown {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int countdown = 0;...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
Create a class named Purchase. Each Purchase contains an invoice number, amount of sale, and amount...
Create a class named Purchase. Each Purchase contains an invoice number, amount of sale, and amount of sales tax. Include set methods for the invoice number and sale amount. Within the set() method for the sale amount, calculate the sales tax as 5% of the sale amount. Also include a display method that displays a purchase’s details. Provided code: import java.util.*; public class CreatePurchase { public static void main(String[] args) { Scanner input = new Scanner(System.in); Purchase purch = new...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
//please debug program // need Scanner output import java.util.Scanner; // Display every character between Unicode 65...
//please debug program // need Scanner output import java.util.Scanner; // Display every character between Unicode 65 and 122 // Start new line after 20 characters public class DebugSix2 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); char letter; int a; final int MIN = 65; final int MAX = 122; final int NUMPERLINE = 200; final int STOPLINE1 = 0; final int STOPLINE2 = STOPLINE1 + NUMPERLINE; for(a = MIN; a <= MAX; a++) { letter...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT