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

I need to add Exception on this program.. This is what professor want : If the u

ID: 3778664 • Letter: I

Question

I need to add Exception on this program..

This is what professor want :

If the user enters a negative number for side of a number greater than 50, throw an Exception(validate the data).

thank you

//////////////////////////////////////////////////////
import javax.swing.*;
import java.awt.*;


public class Lab1 extends JApplet {

int size;

public void init() {
String input = JOptionPane.showInputDialog(
"Enter square size:");

size = Integer.parseInt(input);
}

public void squareAsterisks(Graphics g) {
int y = 50, x = 5;

for (int a = 1; a <= size * size; a++) {
g.drawString("*", x += 5, y);

if (a % size == 0) {
y += 10;
x = 5;
}
  
}
}

public void paint(Graphics g) {
squareAsterisks(g);
}
}

Explanation / Answer

Please find the required program along with its output. Please see the comments against each line to understand the step.

-----------------------------

OUTPUT:

if we input a size as negative number it will give:

Exception in thread "main" java.lang.RuntimeException: validate the data

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