Solved

Pennies for Pay problem

Posted on 2009-07-01
6
1,159 Views
Last Modified: 2013-12-22
Im trying to write a program that calculates the amount a person would earn over a period of time if their salary is one penny the first day, two pennies the second day, and continues to double each day. The program has to show a table that displays the salary for each day, and then show the total pay at the end of the period. And the output has to be displayed in dollar amount and not the number of pennies. i've been having trouble figuring it out using one of the LOOP methods ( WHILE, DO-WHILE, FOR) for this program.
This is what i have so far,
 
 

import	java.util.*;

import	javax.swing.*;
 

public class PenniesForPay

{

	public static void main(String [] args)

	{

		int daysPay;

		double Days;

		

		System.out.println("This will calculate an individuals salary " +

						" by getting paid with pennies daily.");

											

		Scanner keyboard = new Scanner (System.in);

		

		System.out.println("How many days does the individual work?");

		Days = keyboard.nextInt();

		

		System.out.println("Days	Salary");

		System.out.println("------------------");

Open in new window

0
Comment
Question by:AgentC4
  • 4
  • 2
6 Comments
 

Author Comment

by:AgentC4
ID: 24759682
Can anyone please help me with this.
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 24759936
I would store your daily pay as a decimal (double) instead of int number of pennies, so basically as 0.01 instead of 1 to represent a pennies value in a dollar.

double daysPay; // daily pay
double totalPay = 0; // you can use a separate variable to hold total salary over period

A for loop would work well here, since the user will be entering a number of days:

for (int d=0; d<Days; d++) {
    // set daysPay
    // display day, which is d+1 OR adjust loop to start at 1 and go to d<=Days
    // display pay in pennies for that day
    // add daily pay to total amount for period
}

// display total amount for period

See how far that helps you get.  Hopefully you can learn for yourself, so that it really makes sense versus me just posting the answer.

One trick I found once you get this working is try to think of formulas you can use to create the daily pay that will take advantage of the loop index.

Best regards,
Kevin
0
 

Author Closing Comment

by:AgentC4
ID: 31599024
Thanks you do a better job at explaining this rather then my teacher which is sad.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:AgentC4
ID: 24760020
can you display the whole code for me though, i tried to figure it out and i've been trying since last night believe it or not. this would help me with all the other ones that i need to do. i really appreciate your help.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24760101

import java.util.Scanner;
 

public class PenniesForPay {

	public static void main(String[] args) {

		double daysPay; // daily pay

		double totalPay = 0; // salary over period

		double Days;
 

		System.out.println("This will calculate an individuals salary "

				+ " by getting paid with pennies daily.");
 

		Scanner keyboard = new Scanner(System.in);
 

		System.out.println("How many days does the individual work?");

		Days = keyboard.nextInt();
 

		for (int d = 0; d < Days; d++) {

			// set daysPay

			daysPay = 0.01 * Math.pow(2, d); // 1, 2, 4, 8, 16, ...
 

			// display day, which is d+1

			// display pay in pennies for that day

			System.out.printf("Day %1d salary is $%2s.\n", d + 1, daysPay);
 

			// add daily pay to total amount for period

			totalPay += daysPay;

		}
 

		// display total amount for period

		System.out.printf("Total salary is $%1s.\n", totalPay);

	}

}

Open in new window

0
 

Author Comment

by:AgentC4
ID: 24760200
Thanks Kevin, i really appreciate the help, this will help me study and figure out the other ones i have to do.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java and GPO 11 66
Java Timer (static) 9 36
HTTPSessionBindingListernter not configured in deployment descriptor 2 33
varialbe initialization 11 30
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

914 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

15 Experts available now in Live!

Get 1:1 Help Now