I need some help, would you please use the java to write the code for me Objecti
ID: 3705877 • Letter: I
Question
I need some help, would you please use the java to write the code for me
Objective:
Write two classes: One class draws a square, and an exception that occurs when the user puts in an invalid dimension.
First download the driver and put it 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 parent’s constructor and pass a message that it is an invalid dimension
A constructor that takes one string parameters that is passed to the parent’s 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:
Explanation / Answer
SquareDriver.java
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());
}
}
}
}
Square.java
public class Square {
private int length;
public Square() {
length=1;
}
public Square(int lengthh) throws DimensionException {
setLength(length);
}
public int getLength() {
return length;
}
public void setLength(int length) throws DimensionException {
if(length<=0) {
throw new DimensionException("Invalid length");
}
this.length = length;
}
public void draw() throws DimensionException {
for(int i=1;i<=length;i++) {
for(int j=1;j<=length;j++) {
System.out.print("*");
}
System.out.println();
}
}
public int getArea(){
return length* length;
}
public int getPerimeter() {
return 4 * length;
}
}
DimensionException.java
public class DimensionException extends Exception{
String errorMsg ;
public DimensionException(String s){
super(s);
}
public DimensionException() {
super("it is an invalid dimension");
}
}
Output:
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
-1
Invalid length
Enter the length of the side of a square or enter QUIT to quit
3
***
***
***
The area is 9
The perimeter is 12
Enter the length of the side of a square or enter QUIT to quit
quit