//Chapter 14, Introduction to Swing Components; Debugging Exercises //Java Progr
ID: 3686715 • Letter: #
Question
//Chapter 14, Introduction to Swing Components; Debugging Exercises
//Java Programming; Joyce Farrell, 8th Ed
//print output; need to fix error
//4 debugging Exercises; 4 debugged exercises are related to 4 problems
1 // Creates a frame with a specified size
2 // Twice as tall as wide
3 // The size is a constructor argument
4 import javax.swing.*;
5 public class DebugFourteen1 extends JFrame
6 {
7 public DebugFourteen1(int size)
8 {
9 super("This is my frame");
10 setSize(size, size * 2);
11 setDefaultOperation();
12 }
13 public static void main(String[] args)
14 {
15 DebugFourteen1 frame = new DebugFourteen1(200);
16 }
17 }
18
======================
1 // Displays list of payment options
2 // - credit card, check or cash
3 // Displays fee for using each - 5%, 2% or 0%
4
5 import javax.swing.*;
6 import java.awt.*;
7 import java.awt.event.*;
8
9 public class DebugFOurteen2 extends JFrame implements ItemListener
10 {
11 FlowLayout flow = new FlowLayout();
12 JComboBox payMethod = new JComboBox();
13 JLabel payList = new JLabel("Pay List");
14 JTextField totFees = new JTextField(25);
15 String pctMsg = new String("per cent will be added to your bill");
16 int[] fees = {5, 2, 0};
17 int feePct = 0;
18 String output;
19 int fee = 0;
20 public DebugFourteen2()
21 {
22 super("Pay List");
23 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
24 setLayout(flow)
25 payMethod.addItemLisener(payMethod);
26 add(payList);
27 add(payMethod);
28 payMethod.addItem("Credit card");
29 payMethod.addItem("Check");
30 payMethod.addItem("Cash");
31 add(totFees);
32 }
33 public static void main(String[] arguments)
34 {
35 JFrame cframe = new DebugFourteen2();
36 cframe.setSize(350,150);
37 cframe.setVisible(true);
38 }
39 @Override
40 public void itemStateChanged()
41 {
42 Object source = list.getSource();
43 if(source = payMethod)
44 {
45 int fee = payMethod.getSelectedIndex();
46 feePct = fees[x];
47 output = feePct + " " + pctMsg;
48 totFees.setText(output);
49 }
50 }
51 }
==================
1 // User selects pizza topping and sees price
2 import javax.swing.*;
3 import java.awt.*;
4 import java.awt.event.*;
5 public class DebugFourten3 extends JFrame implements ItemListener
6 {
7 FlowLayout flow = new FlowLayout();
8 JComboBox pizzaBox = new JComboBox();
9 JLabel toppingList = new JLabel("Topping List");
10 JLabel aLabel = new JLabel("Paulos's American Pie");
11 JTextField totPrice = new JTextField(10);
12 int[] pizzaPrice = {7, 10, 10, 8, 8, 8, 8};
13 int totalPrice = 0;
14 String output;
15 int pizzaNum;
16 public DebugFourteen3()
17 {
18 super("Pizza List");
19 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
20 setLayout(flow);
21 pizzaBox.addItemListener();
22 add(toppingList);
23 pizzaBox.addItem(cheese);
24 pizzaBox.addItem(sausage);
25 pizzaBox.addItem("pepperoni");
26 pizzaBox.addItem("onion");
27 pizzaBox.addItem("green pepper");
28 pizzaBox.addItem("green olive");
29 pizzaBox.addItem("black olive");
30 add(pizzabox);
31 add(aLabel);
32 }
33 public static void main(String[] arguments)
34 {
35 JFrame frame = new DebugFourteen3();
36 frame.setSize(200, 150);
37 frame.setVisible();
38 }
39
40 @Override
41 public void itemStatechanged(ItemEvent[] list)
42 {
43 Object source = list.getSource;
44 if(source = pizzaBox)
45 {
46 int pizzaNum = pizzaBox.getSelectedIndex();
47 totalPrice = pizzaPrice[pizzaNum];
48 output = "Pizza Price $" + totalPrice;
49 totPrice.setText(output);
50 }
51 }
52 }
=====================
1 // Beverage selector
2 // Milk and cola are $3
3 // all other drinks are $2
4 import javax.swing.*;
5 import java.awt.*;
6 import java.awt.event.*;
7
8 public class DebugFourteen4 extends JFrame implements ItemListener
9 {
10 FlowLayout flow = new FlowLayout();
11 String title = new String("Beverage Selector");
12 Font bigFont = new Font("Arial", Font.PLAIN, 24);
13 ButtonGroup drinkGrp = new ButtonGroup();
14 JCheckBox cola = new JCheckBox("Cola",false);
15 JCheckBox lemon = new JCheckBox("Lemonade",false);
16 JCheckBox tea = new JCheckBox("Iced tea");
17 JCheckBox milk = new JCheckBox("Milk");
18 JCheckBox coffee = new JCheckBox("Coffee");
19 JTextField totPrice = new JTextField(10);
20 String output;
21 int totalPrice = 0;
22 final int HIGH_PRICE = 3;
23 final int LOW_PRICE = 2;
24 public FixDebugFourteen4()
25 {
26 super("Beverage Selector");
27 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
28 setLayout(flow);
29 add(cola);
30 cola.addItemListener(this);
31 add(lemon);
32 add(tea);
33 tea.addItemListener(this);
34 add(milk);
35 milk.addItemListener(this);
36 add(totPrice);
37
38 drinkGrp.add(cola);
39 drinkGrp.add(lemon);
40 drinkGrp.add(milk);
41 drinkGrp.add(coffee);
42 totPrice.setText("0");
43 }
44 public static void main(String[] arguments)
45 {
46 JFrame bFrame = new DebugFourteen4();
47 bFrame.setSize(350, 100);
48 bFrame.setVisible(true);
49 }
50 @Override
51 public void itemStateChanged(ItemEvent check)
52 {
53 Object source = check.getItem();
54 if(source == cola && source == milk)
55 {
56 totPrice.setText("$" + HIGH_PRICE);
57 }
58 else
59 {
60 totPrice.setText("$" + LOW_PRICE);
61 }
62 }
63 }
Explanation / Answer
Please find the required solutions:
---------------------------------------------------------
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class DebugFourteen1 extends JFrame {
public DebugFourteen1( int size )
{
super("This is my frame");
setSize(size, size * 2);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main( String[] args )
{
DebugFourteen1 frame = new DebugFourteen1(200);
}
}
----------------------------------------------------------
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class DebugFourteen2 extends JFrame implements ItemListener
{
FlowLayout flow = new FlowLayout();
JComboBox payMethod = new JComboBox();
JLabel payList = new JLabel("Pay List");
JTextField totFees = new JTextField(25);
String pctMsg = new String("per cent will be added to your bill");
int[] fees = { 5, 2, 0 };
int feePct = 0;
String output;
int fee = 0;
public DebugFourteen2()
{
super("Pay List");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(flow);
payMethod.addItemListener(this);
add(payList);
add(payMethod);
payMethod.addItem("Credit card");
payMethod.addItem("Check");
payMethod.addItem("Cash");
add(totFees);
}
public static void main( String[] arguments )
{
JFrame cframe = new DebugFourteen2();
cframe.setSize(350, 150);
cframe.setVisible(true);
}
@Override
public void itemStateChanged( ItemEvent e )
{
Object source = e.getSource();
if( source == payMethod )
{
int fee = payMethod.getSelectedIndex();
feePct = fees[fee];
output = feePct + " " + pctMsg;
totFees.setText(output);
}
}
} // User selects pizza topping and sees price
----------------------------------------------------------------
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class DebugFourten3 extends JFrame implements ItemListener
{
FlowLayout flow = new FlowLayout();
JComboBox pizzaBox = new JComboBox();
JLabel toppingList = new JLabel("Topping List");
JLabel aLabel = new JLabel("Paulos's American Pie");
JTextField totPrice = new JTextField(10);
int[] pizzaPrice = { 7, 10, 10, 8, 8, 8, 8 };
int totalPrice = 0;
String output;
int pizzaNum;
public DebugFourten3()
{
super("Pizza List");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(flow);
pizzaBox.addItemListener(this);
add(toppingList);
pizzaBox.addItem("cheese");
pizzaBox.addItem("sausage");
pizzaBox.addItem("pepperoni");
pizzaBox.addItem("onion");
pizzaBox.addItem("green pepper");
pizzaBox.addItem("green olive");
pizzaBox.addItem("black olive");
add(pizzaBox);
add(aLabel);
}
public static void main( String[] arguments )
{
JFrame frame = new DebugFourten3();
frame.setSize(200, 150);
frame.setVisible(true);
}
@Override
public void itemStateChanged( ItemEvent e )
{
Object source = e.getSource();
if( source == pizzaBox )
{
int pizzaNum = pizzaBox.getSelectedIndex();
totalPrice = pizzaPrice[pizzaNum];
output = "Pizza Price $" + totalPrice;
totPrice.setText(output);
}
}
}
-----------------------------------------------------------------------
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class DebugFourteen4 extends JFrame implements ItemListener
{
FlowLayout flow = new FlowLayout();
String title = new String("Beverage Selector");
Font bigFont = new Font("Arial", Font.PLAIN, 24);
ButtonGroup drinkGrp = new ButtonGroup();
JCheckBox cola = new JCheckBox("Cola", false);
JCheckBox lemon = new JCheckBox("Lemonade", false);
JCheckBox tea = new JCheckBox("Iced tea");
JCheckBox milk = new JCheckBox("Milk");
JCheckBox coffee = new JCheckBox("Coffee");
JTextField totPrice = new JTextField(10);
String output;
int totalPrice = 0;
final int HIGH_PRICE = 3;
final int LOW_PRICE = 2;
public DebugFourteen4()
{
super("Beverage Selector");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(flow);
add(cola);
cola.addItemListener(this);
add(lemon);
add(tea);
tea.addItemListener(this);
add(milk);
milk.addItemListener(this);
add(totPrice);
drinkGrp.add(cola);
drinkGrp.add(lemon);
drinkGrp.add(milk);
drinkGrp.add(coffee);
totPrice.setText("0");
}
public static void main( String[] arguments )
{
JFrame bFrame = new DebugFourteen4();
bFrame.setSize(350, 100);
bFrame.setVisible(true);
}
@Override
public void itemStateChanged( ItemEvent check )
{
Object source = check.getItem();
if( source == cola && source == milk )
{
totPrice.setText("$" + HIGH_PRICE);
}
else
{
totPrice.setText("$" + LOW_PRICE);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.