- /**
- * Class ProgressiveIncomeTax - write
a description of the class here
- *
- * @author tkrogers
- * @version 10-14-11
- */
- import java.awt.* ;
- import java.awt.event.* ;
- import javax.swing.* ;
-
- // Do not alter the following code
///////////////////////////////////////////////////////////////////////
- public class
ProgressiveIncomeTax extends JPanel
implements ActionListener {
- private
JTextField input ;
- private
JTextArea output ;
- private int
onSwitch1,
-
onSwitch2 ;
- private double
incomeDollars,
-
taxableIncome,
-
tax1,
-
tax2,
-
totalTax,
-
nominalTaxRate ;
-
- public static void
main ( ) {
-
ProgressiveIncomeTax pix = new
ProgressiveIncomeTax ( ) ;
-
pix.createAndShowGUI ( ) ;
- }
-
- // constructor
- public
ProgressiveIncomeTax ( ) {
- setLayout( new
GridLayout ( 2,1 ) ) ;
- input =
new JTextField( "
Enter income in $ ", 40 ) ;
-
input.addActionListener ( this ) ;
- output =
new JTextArea ( "
Income " + incomeDollars + "\n",5, 40
) ;
-
output.setEditable ( false ) ;
- JScrollPane
scrollPane = new JScrollPane ( input ) ;
- add ( scrollPane
) ;
- add ( output ) ;
- }
-
- // Creates a
graphical user interface.
- private static
void createAndShowGUI ( ) {
- JFrame frame =
new JFrame ( "Progressive
Tax Calculator" ) ;
-
frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ) ;
-
// Add contents to the window.
- frame.add(new
ProgressiveIncomeTax ( ) ) ;
- //
Displays the window.
- frame.pack ( ) ;
- frame.setVisible
( true ) ;
- }
-
- // Outputs results when
<enter> is pressed after inputting an income.
- public void
actionPerformed ( ActionEvent e ) {
- String s =
input.getText ( ) ;
- s = s.substring
( 21 ).trim ( ) ;
- incomeDollars =
Double.parseDouble ( s ) ;
- input.setText (
" Enter income in $ " ) ;
- calculateTax ( )
;
- output.setText (
" Income $" + incomeDollars +
-
"\n Taxable Income $" + taxableIncome +
-
"\n Total Tax $" + totalTax +
-
"\n Nominal Tax Rate " + nominalTaxRate +
"%" ) ;
- }
-
- // Sets the number
of decmal places show for doubles.
- public double
decimalPlaces ( int decimals,
double x) {
-
double d = Math.pow ( 10, decimals ) ;
- x = ( (
int ) ( (x + .001) * d ) ) / d ;
-
return x ;
- }
-
- //Calculates the
tax
- public void
calculateTax ( ) {
- taxableIncome =
( incomeDollars - 19999.99 ) * onSwitch1 ( ) ;
- taxableIncome =
decimalPlaces ( 2, taxableIncome ) ;
- tax1 =
taxableIncome *.25 ;
- tax2 = (
taxableIncome -10000 ) * 0.10 * onSwitch2 ( ) ;
- totalTax = tax1
+ tax2 ;
- roundUpTax ( ) ;
-
calculateNominalTaxRate ( ) ;
- }
- // Complete the code shown below. Do not
alter the above code.
-
/////////////////////////////////////////////////////////////////////////////////////////////////////
-
- //Calculates the
on-switch for the 1st tax rate.
- public int
onSwitch1 ( ) {
- // Add the
required code.
-
return 0 ;
- }
-
- //Calculates the the
on-switch for the 2nd tax rate.
- public int
onSwitch2 ( ) {
- // Add the
required code.
-
return s ;
- }
-
- // Rounds tenths
of cents upward
- public void
roundUpTax ( ) {
- // Add the
required code.
- }
-
- //Calculates the
the nominal tax rate.
- public void
calculateNominalTaxRate ( ) {
- // Add the
required code.
- }
- }