Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is wrong with these codes A and B? Please help figure out and fix it. There

ID: 3599642 • Letter: W

Question

What is wrong with these codes A and B? Please help figure out and fix it. There may be more than 1 thing wrong. Also, document the process you use to figure it out using comments, and make use of as many tools as you can: (This is a debugging problem)

This is CODE A:

import java.awt.BorderLayout;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

@SuppressWarnings("serial")

public class Dab extends JFrame implements ActionListener {

JPanel p = new JPanel();

JButton b = new JButton("Here");

Graphics g;

public static void main(String[] args) {

Dab app = new Dab();

app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

app.setSize(300, 185);

app.setVisible(true);

}

Dab() {

add(p, BorderLayout.CENTER);

add(b, BorderLayout.SOUTH);

b.addActionListener(this);

g = p.getGraphics();

g.drawString("Hello from the other side!", 50, 50);

}

@Override

public void actionPerformed(ActionEvent arg0) {

g.drawString("Hello from this side!", 50, 70);

}

}

THIS IS CODE B:

import java.awt.BorderLayout;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

@SuppressWarnings("serial")

public class Lol extends JFrame implements ActionListener {

JPanel p = new JPanel();

JButton b = new JButton("Press Here");

Graphics g;

String[] ints = new String[5];

public static void main(String[] args) {

Lol app = new Lol();

app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

app.setSize(300, 185);

}

Lol() {

add(p, BorderLayout.CENTER);

add(b, BorderLayout.SOUTH);

b.addActionListener(this);

g = p.getGraphics();

ints[0] = "one";

ints[1] = "two";

ints[3] = "three";

ints[4] = "five";

}

@Override

public void actionPerformed(ActionEvent arg0) {

// say "hi five!"

g.drawString("Hi", 50, 50);

g.drawString(ints[4].toString() + "!", 50, 66);

// say "hi 5!"

g.drawString("Hi " + Integer.parseInt(ints[4]) + "!", 50, 80);

}

}

Explanation / Answer


Modified codes:
Below are the modified code values which are working fine along with comments.
Code A:
//import statements necessary for code
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.*;

// Dab Class
@SuppressWarnings("serial")
public class Dab extends JFrame {
JPanel p;
Graphics g;
//Init method which creates a button and adds it to the BorderLayout
public Dab(){
p = new JPanel(new BorderLayout());

JButton b = new JButton("Change");
  
p.add(b, BorderLayout.CENTER);
p.add(b, BorderLayout.SOUTH);
// calls paint method which displays the string
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
repaint();
}
});
setContentPane(p);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 185);
setVisible(true);
}
// paint method draws the string
public void paint(Graphics g) {
g.drawString("Hello from this side!", 50, 70);
g.drawString("Hello from the other side!", 50, 50);
}
// main method which creates an object for the Dab()
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Dab();
}
});
}
}

output on button click:
Hello from the other side!
Hello from this side!

Code B
//import statement necessary for codes.
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
//Lol class for creating a button and adding functionality to it.
public class Lol extends JFrame {

JPanel p;
Graphics g;
//Init method()
Lol() {
p = new JPanel(new BorderLayout());
JButton b = new JButton("Press here");
add(p, BorderLayout.CENTER);
add(b, BorderLayout.SOUTH);
//containd paint method to draw the string
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
repaint();
}
});
setContentPane(p);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 185);
setVisible(true);
}
//paint method which draws the string required
public void paint(Graphics g) {
String[] ints = new String[5];
ints[0] = "one";
ints[1] = "two";
ints[3] = "three";
ints[4] = "four";
g.drawString("Hi", 50, 50);
g.drawString(ints[4].toString() + "!", 50, 66);
g.drawString("Hi " + ints[4].toString() + "!", 50, 80);
}
//main method which calls the init method
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Lol();
}
});
}
}
output on button click:
Hi
Four!
Hi Four!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote