We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Pennies for Pay

gkilgore
gkilgore asked
on
Medium Priority
2,759 Views
Last Modified: 2013-11-24
Program should calculate the salary of a 30 day period when first day - add one penny, second day, add two pennies and continues to double each day.  Two tables should be created: one showing thesalary foreach day aand then the total pay at the end of the period.  Should be displayed in dollars not pennies.

Attached is a first draft.  Thank you in advance for your assistance.
public class Salary
{
	public static void main(String[] args)
	{
		int number;
		int sum;
		
		System.out.println(" Salary       Total pay");
		System.out.println("----------------------------");
		
		for(number = 1; number <= 30; number++)
		{
			System.out.println(number + "\t\t" + number);
		}
	}
}

Open in new window

Comment
Watch Question

Author

Commented:
Revised program requirement:  Need assistance completing the following program.  The program should calculate the amount a person would earn over a periodof time  if their salary is one penny the first day, two pennies the second day and continues to double each day.  Program will display the salary for each day and then show the total pay at the end of the period.  

Author

Commented:
Revised code.  This is a portion of the program, any additional steps I may have left off.
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
 
public class TotalSales
{
	public static void main(String[] args)
	{
		int days;
		double sales;
		double totalSales;
		String input;
		
		DecimalFormat dollar = new DecimalFormat("#,##0.00");
		
		input = JOptionPane.showInputDialog("For how many day " + 
														"do yoiu have sales figures");
		days = Integer.parseInt(input);
		
		totalSales = 0.0;
		
		for (int count = 1; count <= days; count++)
		{
			input = JOptionPane.showInputDialog("Enter the sales " +
															"for day " + count + ":");
			sales = Double.parseDouble(input);
			totalSales += sales;
		}
		
		JOptionPane.showMessageDialog(null, "The total sales are $" +
												dollar.format(totalSales));
		System.exit(0);
	}
}			

Open in new window

Author

Commented:
Last copy with correct data.  Please critique according to program specifications.  Thank you.
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
 
public class Salary
{
	public static void main(String[] args)
	{
		int days;
		double salary;
		double totalSalary;
		String input;
		
		DecimalFormat dollar = new DecimalFormat("#,##0.00");
		
		input = JOptionPane.showInputDialog("For how many day " + 
														"do you have salary for?");
		days = Integer.parseInt(input);
		
		totalSalary = 0.0;
		
		for (int count = 1; count <= days; count++)
		{
			input = JOptionPane.showInputDialog("Enter the salary " +
															"for day " + count + ":");
			salary = Double.parseDouble(input);
			totalSalary += salary;
		}
		
		JOptionPane.showMessageDialog(null, "The total salary $" +
												dollar.format(totalSalary));
		System.exit(0);
	}
}			

Open in new window

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
That's a lot of processing and code for a simple calculation.
For each day, the pay is 2 ^ (n - 1) and the total pay is ((2 ^ n) -1) .  So stic that in a FOR loop, using n as your index and go from 1 to 30 and, as my SQL mentor used to say, "the SQL to insert the data into each table is a trivial exercise left for the student.". ;-)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.