Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need it in Java DrJava please. **Objective: Write two classes: One class draws a square, and an exception that occurs when the user puts

I need it in Java DrJava please.

**Objective:

Write two classes: One class draws a square, and an exception that occurs when the user puts in an invalid dimension.

  • Put the driver in your project
    • DO NOT ALTER THE DRIVER!
  • Write a class file called DimensionException that inherits from Exception
  • Create the following constructors
    • Default calls the parents constructor and pass a message that it is an invalid dimension
    • A constructor that takes one string parameters that is passed to the parents constructor
  • Write a class file called Square that DOES NOT HAVE a main method
  • An attribute of this class is
    • Length corresponding to the length of each side
  • Create the following constructors
    • Default creates a 1x1 square (or a single star)
    • One that has a integer parameter that corresponds to its length and width
  • Accessors and Mutators for each variable
    • MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
    • If the dimension is set improperly throw the DimensionException
  • Create the following Methods
    • draw this method will draw a square of asterisks (*) by the given dimensions.
    • getArea returns the area of the square
    • getPerimeter returns the perimeter of the square

**The driver code is:

import java.util.*;

public class SquareDriver {

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

String input ="";

System.out.println("Welcome to the easy square program");

while(true)

{

System.out.println("Enter the length of the side of a square or enter QUIT to quit");

try

{

input = keyboard.nextLine();

if(input.equalsIgnoreCase("quit"))

break;

int length = Integer.parseInt(input);

Square s = new Square();

s.setLength(length);

s.draw();

System.out.println("The area is "+s.getArea());

System.out.println("The perimeter is "+s.getPerimeter());

}

catch(DimensionException e)

{

System.out.println(e.getMessage());

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

}

}

}

**Example Dialog:

Welcome to the easy square program

Enter the length of the side of a square or enter QUIT to quit

5

*****

*****

*****

*****

*****

The area is 25

The perimeter is 20

Enter the length of the side of a square or enter QUIT to quit

0

Dimensions must be of length one at least

Enter the length of the side of a square or enter QUIT to quit

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions