In: Computer Science
Each of the following files in the Chapter15 folder of your downloadable student files has syntax and/or logic errors. In each case, determine the problem and fix the program. After you correct the errors, save each file using the same filename preceded with Fix. For example, DebugFifteen1.java will become FixDebugFifteen1.java.
a. DebugFifteen1.java
b. DebugFifteen2.java
c. DebugFifteen3.java
d. DebugFifteen4.java
Debugging
a) Code segment: DebugFifteen1.java
The syntax and logical errors are highlighted
import javax.swing.*;
import java.awt.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
public\nclass DebugFifteen1 extends JFrame implements
ActionListener
{
//code statement with error
JButton button1 = new JButton("Red")
JButton button2 = new JButton("Pink");
//code statement with error
JButton button3 = new JButton("Orange")
JButton button4 = new JButton("Yellow");
JButton button5 = new JButton("Black");
JPanel panel1 = new JPanel(new FlowLayout());
//code statement with error
JPanel panel2 = new JPanel;
public DebugFifteen1()
{
setLayout(new GridLayout(1, 2));
setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
//code statement with error
add(panel1)
add(panel2);
panel1.add(button1);
panel1.add(button2);
panel1.add(button2);
panel1.add(button4);
panel1.add(button5);
//code statement with error
button1.addActionListener();
button2.addActionListener();
button3.addActionListener();
button4.addActionListener();
button5.addActionListener();
setSize(400, 200);
setVisible(true);
}
public\nvoid actionPerformed(ActionEvent e)
{
//code statement with error
Object source = e.getSource();
if(source = button1)
panel2.setBackground(Color.RED);
//code statement with error
else\nif(source == button3)
panel2.setBackground(Color.PINK);
else\nif(source == button3)
panel2.setBackground(Color.ORANGE);
else\nif(source == button4)
panel2.setBackground(Color.YELLOW);
else
panel2.setBackground(Color.BLACK);
}
public\nstatic\nvoid main(String[] args)
{
DebugFifteen1 frame = new DebugFifteen1();
}
}
Modified Code: FixDebugFifteen1.java
import javax.swing.*;
import java.awt.*;
//The AWT package put in two times
import java.awt.event.*;
import java.awt.Color;
public\nclass FixDebugFifteen1 extends JFrame implements ActionListener
{
//Missing semicolon in the following statement
JButton button1 = new JButton("Red");
JButton button2 = new JButton("Pink");
//Missing semicolon in the following statement
JButton button3 = new JButton("Orange");
JButton button4 = new JButton("Yellow");
JButton button5 = new JButton("Black");
JPanel panel1 = new JPanel(new FlowLayout());
//Syntax error in the JPanel creation
JPanel panel2 = new JPanel();
public FixDebugFifteen1()
{
setLayout(new GridLayout(1, 2));
setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
//Missing semicolon in the following statement
add(panel1);
add(panel2);
panel1.add(button1);
panel1.add(button2);
//button3 not added
panel1.add(button3);
panel1.add(button4);
panel1.add(button5);
//Arguments missing the following statements
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
setSize(400, 200);
setVisible(true);
}
public\nvoid actionPerformed(ActionEvent e)
{
Object source = e.getSource();
//Syntax error in the comparing statement
if(source == button1)
panel2.setBackground(Color.RED);
//Syntax error in the button3 statement
else\nif(source == button2)
panel2.setBackground(Color.PINK);
else\nif(source == button3)
panel2.setBackground(Color.ORANGE);
else\nif(source == button4)
panel2.setBackground(Color.YELLOW);
else
panel2.setBackground(Color.BLACK);
}
public\nstatic\nvoid main(String[] args)
{
//create an instance of class FixDebugFifteen1
FixDebugFifteen1 frame = new FixDebugFifteen1 ();
}//end of main method
}//end of the class
b) Code segment: DebugFifteen2.java
The syntax and logical errors are highlighted
// Demonstrates layout positions using BorderLayout
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public\nclass DebugFifteen2 extends JFrame
{
JButton nb = new JButton("Center");
JButton sb = new JButton("Left ");
JButton eb = new JButton("Up ");
JButton wb = new JButton("Down ");
JButton cb = new JButton("Right");
Container con = null;
public DebugFifteen2()
{
con = this.getContentPane();
con.setLayout(new BorderLayout());
con.add(nb);
con.add(sb);
con.add(eb);
con.add(wb);
con.add(cb);
}
public\nstatic\nvoid main(String[] args)
{
DebugFifteen2 f = new DebugFifteen2();
f.setSize(300, 300);
}
}
Modified Code: FixDebugFifteen2.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public\nclass FixDebugFifteen2 extends JFrame
{
//Syntax error in access specifies of buttons and
//container and error in button declaration
private JButton cb = new JButton("Center");
private JButton wb = new JButton("Left ");
private JButton nb = new JButton("Up ");
private JButton sb = new JButton("Down ");
private JButton eb = new JButton("Right");
//Syntax error in container declaration
private Container con = getContentPane();
public FixDebugFifteen2()
{
con = this.getContentPane();
con.setLayout(new BorderLayout());
//Syntax error in declaration of layouts
con.add(nb, BorderLayout.NORTH);
con.add(sb, BorderLayout.SOUTH);
con.add(eb, BorderLayout.EAST);
con.add(wb,BorderLayout.WEST);
con.add(cb,BorderLayout.CENTER);
}
public\nstatic\nvoid main(String[] args)
{
//create an instance of class
FixDebugFifteen2 f = new FixDebugFifteen2 ();
f.setSize(300, 300);
//for display output
f.setVisible(true);
}
}
Sample Output:
c) Code segment: DebugFifteen3.java
The syntax and logical errors are highlighted
// Lets user type keys displays key typed
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public\nclass DebugFifteen3 extends JFrame implements
KeyListener
{
char key;
Container con = null;
GridLayout grid = new GridLayout(2, 1)
JLabel label = new JLabel("Key Typed:");
JTextArea textArea = new JTextArea(4, 25);
public DebugFifteen3()
{
setTitle("Debug Key Frame");
setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
con = this.getContentPane();
con.setLayout(grid);
con.add(textArea);
con.add(label);
addKeyListener();
textArea.addKeyListener(this);
}
public\nvoid keyTyped( e)
{
char c = e.getKeyChar();
label.setText("Key Typed: " + c);
}
public\nvoid keyPressed(KeyEvent e)
{
}
public\nvoid keyReleased(KeyEvent e)
{
}
public\nstatic\nvoid main(String[] args)
{
DebugFifteen2 kFrame = new DebugFifteen2();
kFrame.setSize(300, 200);
kFrame.setVisible(true);
}
}
Modified Code: FixDebugFiften3.java
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public\nclass FixDebugFifteen3 extends
JFrame implements KeyListener
{
char key;
Container con = null;
//Semi colon missing the following statement
GridLayout grid = new GridLayout(2, 1);
JLabel label = new JLabel("Key Typed:");
JTextArea textArea = new JTextArea(4, 25);
public FixDebugFifteen3()
{
setTitle("Debug Key Frame"); setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
con = this.getContentPane();
con.setLayout(grid);
con.add(textArea);
con.add(label);
//modified syntax error in the following declaration
//of addKeyListener
addKeyListener(this);
textArea.addKeyListener(this);
}
//Syntax error in the declaration of the following method
public\nvoid keyTyped(KeyEvent e)
{
char c = e.getKeyChar();
label.setText("Key Typed: " + c);
}
public\nvoid keyPressed(KeyEvent e)
{
}
public\nvoid keyReleased(KeyEvent e)
{
}
public\nstatic\nvoid main(String[] args)
{
//create an instance of class
//FixDebugFifteen3
FixDebugFifteen3 kFrame =
new FixDebugFifteen3();
kFrame.setSize(300, 200);
kFrame.setVisible(true);
}
}
d) Code segment: DebugFifteen4.java
The syntax and logical errors are highlighted
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public\nclass DebugFifteen4 extends JFrame implements
ActionListener
{
JMenuBar mainBar = new JMenuBar();
JMenu menu1 = new JMenu("File");
JMenu menu2 = new JMenu("Symbols");
JMenu menu3 = new JMenu("Cities");
JMenuItem exit = new JMenuItem("Exit");
JMenuItem animal = new JMenuItem("Animal");
JMenuItem song = new JMenuItem("Song");
JMenuItem flower = new JMenuItem("Flower");
JMenuItem milwaukee = new JMenuItem("Milwaukee");
JMenuItem madison = new JMenuItem("Madison");
JLabel label1 = new JLabel(" Wisconsin Facts ");
JLabel label2 = new JLabel();
public DebugFifteen4()
{
setTitle("Facts about Wisconsin");
setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout);
setJMenuBar(mainBar);
mainBar.add(menu1);
mainBar.add(menu2);
mainBar.add(menu3);
menu1.add(exit);
menu2.add(animal);
menu2.add(song);
menu2.add(flower);
menu3.add(milwaukee);
menu3.add(madison);
exit.addActionListener(this);
animal.addActionListener(this);
song.addActionListener(this);
flower.addActionListener(this);
milwaukee.addActionListener(this);
madison.addActionListener(this);
label1.setFont(new Font("Arial",
Font.BOLD, 14));
add(label2);
label2.setFont(new Font("Arial",
Font.PLAIN, 14));
}
public\nvoid actionPerformed(ActionEvent e)
{
Object source = e.getSource();
String text = " ";
if(source = exit)
System.exit(0);
else\nif(source == animal)
text = "The state animal is badger";
else\nif(source = song)
text = "The state song is On
Wisconsin!";
else\nif(source == flower)
text = "The state flower is wood
violet";
else\nif(source == milwaukee)
text = "Milwaukee is the largest city";
else
text = "Madison is the capitol";
label2.setText(text);
repaint();
}
public\nstatic\nvoid main(String[] args)
{
DebugFifteen4 frame = new DebugFifteen4();
final\nint WIDTH = 250;
final\nint HEIGHT = 200;
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
}
}
Modified Code: FixDebugFiften4.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public\nclass FixDebugFiften4 extends JFrame implements
ActionListener
{
JMenuBar mainBar = new JMenuBar();
JMenu menu1 = new JMenu("File");
JMenu menu2 = new JMenu("Symbols");
JMenu menu3 = new JMenu("Cities");
JMenuItem exit = new JMenuItem("Exit");
JMenuItem animal = new JMenuItem("Animal");
JMenuItem song = new JMenuItem("Song");
JMenuItem flower = new JMenuItem("Flower");
JMenuItem milwaukee = new JMenuItem("Milwaukee");
JMenuItem madison = new JMenuItem("Madison");
JLabel label1 = new JLabel(" Wisconsin Facts ");
JLabel label2 = new JLabel();
public FixDebugFiften4()
{
setTitle("Facts about Wisconsin");
setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
//Syntax error in the FlowLayout declaration
setLayout(new FlowLayout());
setJMenuBar(mainBar);
mainBar.add(menu1);
mainBar.add(menu2);
mainBar.add(menu3);
menu1.add(exit);
menu2.add(animal);
menu2.add(song);
menu2.add(flower);
menu3.add(milwaukee);
menu3.add(madison);
exit.addActionListener(this);
animal.addActionListener(this);
song.addActionListener(this);
flower.addActionListener(this);
milwaukee.addActionListener(this);
madison.addActionListener(this);
label1.setFont(new Font("Arial",
Font.BOLD, 14));
add(label2);
label2.setFont(new Font("Arial",
Font.PLAIN, 14));
}
public\nvoid actionPerformed(ActionEvent e)
{
Object source = e.getSource();
String text = " ";
//Comparsion error
if(source == exit)
System.exit(0);
else\nif(source == animal)
text = "The state animal is badger";
//Comparsion error
else\nif(source == song)
text = "The state song is On
Wisconsin!";
else\nif(source == flower)
text = "The state flower is wood
violet";
else\nif(source == milwaukee)
text = "Milwaukee is the largest city";
else
text = "Madison is the capitol";
label2.setText(text);
repaint();
}
public\nstatic\nvoid main(String[] args)
{
//create an instance of class FixDebugFiften4
FixDebugFiften4 frame = new FixDebugFiften4();
final\nint WIDTH = 250;
final\nint HEIGHT = 200;
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
}
}
Debugging
a) Code segment: DebugFifteen1.java
The syntax and logical errors are highlighted
import javax.swing.*;
import java.awt.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
public\nclass DebugFifteen1 extends JFrame implements