4 Replies - 2554 Views - Last Post: 09 October 2011 - 12:01 PM Rate Topic: -----

#1 dberger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 31-March 11

Pennies for pay

Posted 09 October 2011 - 07:24 AM

I have to write a program using the following parameters:

*************************
Write a program that calculates the amount 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 display a table showing the salary 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.

Input Validation: Do not accept a number less than 1 for the number of days worked.
Input and output should be done with Dialog and Message boxes. Your program should be well documented internally and externally
*************************

My question is where we have to display a table. How can I output a table with all of the days, pennies earned, and total on one output dialog? Is this possible?

Thanks in advance.

import javax.swing.JOptionPane;

public class PenniesForPay
{
	public static void main(String[] args)
    {
		
		String inputString;
	    int pennies;      // Penny accumulator
	    int totalPay;     // Total pay accumulator
	    int maxDays;      // Max number of days
	    int day;          // Day counter


	    inputString = JOptionPane.showInputDialog("For how many days will you work? ");
	    
	    maxDays = Integer.parseInt(inputString);
	   
	    // Validate the input
	    while (maxDays < 1){
	         inputString = JOptionPane.showInputDialog("The number of days " +
	               "must be at least 1.\nEnter the number of days: ");
	        
	         maxDays = Integer.parseInt(inputString);
	      }
	   
	      day = 1;
	      pennies = 1;
	      totalPay = 0;
	      
	      while (day <= maxDays)
	      {
	         // Display the day number and pennies earned.
	    	  JOptionPane.showMessageDialog(null, "Day:\t" + day + "\nPennies Earned:\t" + pennies);
	         
	         // Accumulate the total pay.
	         totalPay = totalPay + pennies;
	         
	         // Increment for the next day.
	         day++;
	         
	         // Double the number of pennies.
	         pennies = pennies * 2;
	      }
	     
	      JOptionPane.showMessageDialog(null, "Total pay: $" + totalPay / 100.0);
    }


}



Is This A Good Question/Topic? 0
  • +

Replies To: Pennies for pay

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5480
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Pennies for pay

Posted 09 October 2011 - 08:55 AM

Build the table as you loop through the calculation.
Add one line to the table for each iteration of the loop.
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: Pennies for pay

Posted 09 October 2011 - 09:00 AM

Instead of showing a dialog box for every day, I would just keep appending text to a String. When all the calculations are done, you display the content of the String

		StringBuffer result = new StringBuffer();
		
		while ( day <= maxDays ) {
			// Display the day number and pennies earned.
			result.append( "Day: " + day + " Pennies Earned: " + pennies + "\n" );

			// Accumulate the total pay.
			totalPay = totalPay + pennies;

			// Increment for the next day.
			day++;

			// Double the number of pennies.
			pennies = pennies * 2;
		}
		
		result.append( "Total pay: $" + totalPay / 100.0 );
		JOptionPane.showMessageDialog( null, result.toString() );


Was This Post Helpful? 0
  • +
  • -

#4 dberger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 31-March 11

Re: Pennies for pay

Posted 09 October 2011 - 11:44 AM

@CasiOo

Thanks, this was very helpful. I haven't learned this yet in my class but I am going to use your suggestion. My teacher decided to throw this in there even though it will not be covered till later in the semester. Everyone is in the class is having the same issue.
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: Pennies for pay

Posted 09 October 2011 - 12:01 PM

You wont learn to use every class in the java SDK, some things you have to learn by yourself :)

You could have done the same with a String instead of a StringBuffer. The only reason why I used a StringBuffer was because it is mutable while a String object is immutable.

Without using a StringBuffer, but instead using a String, it will look like
		String result = "";
		
		while ( day <= maxDays ) {
			// Display the day number and pennies earned.
			result += "Day: " + day + " Pennies Earned: " + pennies + "\n";

			// Accumulate the total pay.
			totalPay = totalPay + pennies;

			// Increment for the next day.
			day++;

			// Double the number of pennies.
			pennies = pennies * 2;
		}
		
		result += "Total pay: $" + totalPay / 100.0;
		JOptionPane.showMessageDialog( null, result );


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1