Solved

Pennies for Pay

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Multiple issues with Java 14 42
Annotation to do Dependency injection in spring 3 37
zeroFront challenge 7 70
count11 challenge 6 47
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now