..\CSC120\Java\lec32_scope\src\MUPanel.java |
1
2
3
4
5
6 import java.awt.*;
7 import java.text.DecimalFormat;
8 import javax.swing.*;
9
10 public class MUPanel extends JPanel {
11
12 Double tax = 0.07;
13 DecimalFormat currency;
14
15
16
17 public MUPanel() {
18 setLayout(null);
19 setPreferredSize(new Dimension(800, 600));
20 setName("CSC 120 Lecture 32: Scope Examples");
21 setUp();
22 setUpGUI();
23 setBackground(Color.GREEN);
24
25
26 currency = new DecimalFormat("#.00");
27
28 Double amtDue = calculateCost(100.00);
29 System.out.println( "You owe $"
30 + currency.format(amtDue) + " using a tax rate of "
31 + tax );
32 outputArea.append("First version (does not use this.tax in the method):\n");
33 outputArea.append( "\n You owe $" + currency.format(amtDue) + " using a tax rate of " + tax + "\n\n\n");
34
35
36
37 Double amtDue2nd = calculateCost2ndVersion(100.00);
38 System.out.println( "You owe $"
39 + currency.format(amtDue2nd) + " using a tax rate of "
40 + tax );
41
42 outputArea.append("Second version (uses this.tax in the method):\n");
43 outputArea.append( "\n You owe $" + currency.format(amtDue2nd) + " using a tax rate of " + tax + "\n\n\n");
44
45
46
47 }
48
49
50 public Double calculateCost(Double beforeTaxAmt) {
51 Double tax = 0.10;
52 Double total;
53 total = beforeTaxAmt + beforeTaxAmt*tax;
54 return total;
55 }
56
57
58
59
60 public Double calculateCost2ndVersion(Double beforeTaxAmt) {
61 Double tax = 0.10;
62 Double total;
63 total = beforeTaxAmt + beforeTaxAmt*this.tax;
64 return total;
65 }
66
67
68
69
70
71 @Override
72 public void paintComponent(Graphics g) {
73 super.paintComponent(g);
74 }
75
76
77
78
79
80
81
82
83
84
85 public void setUp() {
86 for (Component c: getComponents())
87 c.setSize(c.getPreferredSize());
88 JFrame f = new JFrame(getName());
89 f.setContentPane(this);
90 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
91 f.pack();
92 f.setVisible(true);
93 }
94
95 public TextArea outputArea;
96
97 public void setUpGUI() {
98 outputArea = new TextArea();
99 outputArea.setBounds(15, 15, 770, 570);
100 outputArea.setFont(new Font("Monospaced", Font.PLAIN, 18));
101 add(outputArea);
102 }
103
104 public static void main(String args[]){new MUPanel();}
105
106 }
107