Project #47047 - java HW

Hip to be Square

 

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 Lab18Driver 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

 

 

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

Finally:

Upload DimensionException and Sqaure file to the dropbox

 

Lab18 Driver

 

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

Subject Computer
Due By (Pacific Time) 11/10/2014 11:30 pm
Report DMCA
TutorRating
pallavi

Chat Now!

out of 1971 reviews
More..
amosmm

Chat Now!

out of 766 reviews
More..
PhyzKyd

Chat Now!

out of 1164 reviews
More..
rajdeep77

Chat Now!

out of 721 reviews
More..
sctys

Chat Now!

out of 1600 reviews
More..
sharadgreen

Chat Now!

out of 770 reviews
More..
topnotcher

Chat Now!

out of 766 reviews
More..
XXXIAO

Chat Now!

out of 680 reviews
More..
All Rights Reserved. Copyright by AceMyHW.com - Copyright Policy