Question

In: Computer Science

Each of the following files in the Chapter15 folder of your downloadable student files has syntax and/or logic errors.

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

 

 

Solutions

Expert Solution

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

 

Sample Output:

 

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);

}

}

 

Sample Output:

 

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);

}

}

 

Sample Output:


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

 

Related Solutions

The files provided in the code editor to the right contain syntax and/or logic errors. In...
The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. DebugBox.java public class DebugBox { private int width; private int length; private int height; public DebugBox() { length = 1; width = 1; height = 1; } public DebugBox(int width, int length, height) { width = width; length = length; height =...
The files provided in the code editor to the right contain syntax and/or logic errors. In...
The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. Please Fix code and make Copy/Paste avaliable // Application allows user to enter a series of words // and displays them in reverse order import java.util.*; public class DebugEight4 {    public static void main(String[] args)    {       Scanner input = new Scanner(System.in);       int...
The following code segment which uses pointers may contain logic and syntax errors. Find and correct...
The following code segment which uses pointers may contain logic and syntax errors. Find and correct them. a) Returns the sum of all the elements in summands int sum(int* values) { int sum = 0; for (int i = 0; i < sizeof(values); i++) sum += *(values + i); return sum; } b) Overwrites an input string src with "61C is awesome!" if there's room. Does nothing if there is not. Assume that length correctly represents the length of src....
The number of errors in each of 300 files has a Poisson distribution with 1.4 errors...
The number of errors in each of 300 files has a Poisson distribution with 1.4 errors per file on average. Assume the errors in different files are independent. Use the Central Limit Theorem to approximate the probability that the total number of errors is at least 400. (Use a calculator.)
Create a class Student (in the separate c# file but in the project’s source files folder)...
Create a class Student (in the separate c# file but in the project’s source files folder) with those attributes (i.e. instance variables): Student Attribute Data type (student) id String firstName String lastName String courses Array of Course objects Student class must have 2 constructors: one default (without parameters), another with 4 parameters (for setting the instance variables listed in the above table) In addition Student class must have setter and getter methods for the 4 instance variables, and getGPA method...
To earn full credit, program must be free of syntax, run-time, and logic errors; include program...
To earn full credit, program must be free of syntax, run-time, and logic errors; include program comments; use reasonable readable variable names and prompts. To include variables in the input prompt, you must use concatenation character (+). For example: assume you already asked for input of employee's first name and last name (they are stored into variables FirstName and LastName, then use this prompt to ask for employee's weekly hours which includes the employee's full name in the input statement....
identify the syntax errors in the following code:             public class Hello {                    &
identify the syntax errors in the following code:             public class Hello {                         private static int main(String [] args) {                                     string Msg=”Hello, Wrld!;                                     Sytem.out.println(msg+ “Ken")
In JAVA (eclipse).... Extract both files and place them in your program’s folder. You will be...
In JAVA (eclipse).... Extract both files and place them in your program’s folder. You will be using them. In your driver class: • Create an array called “boyNames” and store all names from the BoyNames.txt file • Similarly, create an array called “girlsNames” and store all names from the GirlNames.txt file • Create a text menu that allowing users to: 1. Enter a name and the application must display a message indicating if the name is among the most popular...
Analyze following program and Find Syntax errors. #include<iostream> int show(int x) int main() {     int A,B;...
Analyze following program and Find Syntax errors. #include<iostream> int show(int x) int main() {     int A,B;       char B=10; cout<<”Enter Value of A”; cin<<A; show(A) show(50)          }       cin.get(); } int show(int x); { cout<<”Value is =” <<X cout<<endl; }
4. Translate each of the following statements into a symbolic logic and tell if each of...
4. Translate each of the following statements into a symbolic logic and tell if each of the following is true or false, with a full justification (you do not have to justify your answer to (ii), which was done before) : (i) Every integer has an additive inverse. (ii) If a and b are any integers such that b > 0, then there exist integers q and r such that a = bq + r, where 0 ≤ r <...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT