Solved

Pennies for Pay

Posted on 2009-06-29
5
2,119 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

0
Comment
Question by:gkilgore
  • 4
5 Comments
 

Author Comment

by:gkilgore
ID: 24741182
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.  
0
 

Author Comment

by:gkilgore
ID: 24741402
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

0
 

Author Comment

by:gkilgore
ID: 24741421
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

0
 

Accepted Solution

by:
gkilgore earned 0 total points
ID: 24742052
Please disregard the other versions displayed earlier.  The listed program should calculate the amount a person would earn overa period of timne if their salary was one penny for the first day,l two pennies for the second day, and continues to double each day.  A table should be displayed showing the salary for each day and the total pay at the end of the period.  I think I have over coded this program.  Any feedback will be appreciated.
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?");
 
		
		System.out.println("Days                    Salary");
		System.out.println("------------------------------");		
		
													
		days = Integer.parseInt(input);
		
		totalSalary = 0.00;
				
		for (int count = 1; count <= days; count++)
		{
		
			input = JOptionPane.showInputDialog("Enter the salary " +
															"for day " + count + ":");
															
			System.out.print(count + "\t\t\t");	
			
														
			salary = Double.parseDouble(input);
			totalSalary += salary;
			
			System.out.println(salary);
			
		}
		
		System.out.println("Total Salary $ " + totalSalary); 
				
		System.exit(0);
	}
}			

Open in new window

0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 24742080
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.". ;-)
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
github account with ecipse 1 52
HSSFWorkbook cannot be resolved error 10 105
Unhandled exception type Exception 18 42
difference between sorce folder and folder in eclipise 3 28
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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

789 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