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

I haviing problems running thsi program in my programing lab, the program has to

ID: 3695518 • Letter: I

Question

I haviing problems running thsi program in my programing lab, the program has to be able to produce the same example output. Thank you.

Define the class InvalidSideException, which inherits from the Exception class . Also define a Square class , which has one method  variable -- an int describing the side length. Theconstructor of the Square class should take one argument , an int meant to initialize the side length; however, if the argument is not greater than 0, the constructor should throw an InvalidSideError. The Square class should also have a method getArea(), which returns the area of the square.

Create a Driver class with a main method to test your classes. Your program should prompt the user to enter a value for the side length, and then create a Square object with that side length. If the side length is valid, the program should print the area of the square. Otherwise, it should catch the InvalidExceptionError, print "Side length must be greater than 0.", and terminate the program .

SAMPLE RUN #1: java Driver

Interactive SessionStandard InputStandard Error (empty)Standard OutputHide Invisibles

Highlight: NoneStandard Input OnlyPrompts OnlyStandard Output w/o PromptsFull Standard OutputAll Show Highlighted Only

import java.util.Scanner;

import java.util.logging.Level;

import java.util.logging.Logger;

class InvalidSideException extends Exception {

InvalidSideException(String s) {

super(s);

}

}

/**

*

* @author prmsh

*/

public class Driver {

int side;

//constructor declared

public Driver(int side) throws InvalidSideException {

if(side < 0){ //checking for negative parameter

throw new InvalidSideException("side length must be greater than 0");

}

else{//if positive parameter

this.side = side;

}

}

//calculating square area

public int getArea(){

return this.side*this.side;

}

//main method

public static void main(String args[]){

//scanner object to read inputs from user

Scanner sc = new Scanner(System.in);

//reading length from user

System.out.println("Enter side length of square: ");

int length = sc.nextInt();

try {

//creating object

Driver obj = new Driver(length);

} catch (InvalidSideException ex) {

Logger.getLogger(Driver.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

Problems Detected:
          The contents of your standard output is incorrect.
          Failed 5 out of 5 test runs.

Failed Test Run #1

          The contents of your standard output is incorrect.

Expected Result:

Enter·side·length·of·square:-7

Side·length·must·be·greater·than·0.

Your Code's Actual Result:

Enter·side·length·of·square:-7

·


Failed Test Run #2

          The contents of your standard output is incorrect.

Expected Result:

Enter·side·length·of·square:-13

Side·length·must·be·greater·than·0.

Your Code's Actual Result:

Enter·side·length·of·square:-13

·

Failed Test Run #3

          The contents of your standard output is incorrect.

Expected Result:

Enter·side·length·of·square:5

25

Your Code's Actual Result:

Enter·side·length·of·square:5

·

Failed Test Run #4

          The contents of your standard output is incorrect.

Expected Result:

Enter·side·length·of·square:0

Side·length·must·be·greater·than·0.

Your Code's Actual Result:

Enter·side·length·of·square:0

·

Failed Test Run #5

          The contents of your standard output is incorrect.

Expected Result:

Enter·side·length·of·square:16

256

Your Code's Actual Result:

Enter·side·length·of·square:16

·

Explanation / Answer

import java.util.Scanner;

import java.util.logging.Level;
import java.util.logging.Logger;

class InvalidSideException extends Exception {
   /**
   *
   */
   private static final long serialVersionUID = 1L;

   InvalidSideException(String s) {
       super(s);
   }
}

class Square {
   int sideLength;

   /**
   * @param sideLength
   * @throws InvalidSideException
   */
   public Square(int sideLength) throws InvalidSideException {

       if (sideLength <= 0) { // checking for negative parameter
           throw new InvalidSideException("side length must be greater than 0");
       } else {// if positive parameter
           this.sideLength = sideLength;
       }

   }

   /**
   * method returns the area of the square
   *
   * @return
   */
   // calculating square area
   public int getArea() {
       return this.sideLength * this.sideLength;
   }

}

/**
*
* @author prmsh
*/
public class Driver {

   public static void main(String args[]) {
       // scanner object to read inputs from user
       Scanner sc = new Scanner(System.in);
       // reading length from user
       System.out.print("Enter side length of square: ");
       int length = sc.nextInt();

       try {
           // creating object
           Square obj = new Square(length);
           System.out.println("Area of Square is " + obj.getArea());
       } catch (InvalidSideException ex) {
           Logger.getLogger(Driver.class.getName())
                   .log(Level.SEVERE, null, ex);
           ex.printStackTrace();
       }
   }
}

OUTPUT:

Test 1 :
Enter side length of square: 3
Area of Square is 9

Test 2 :
Enter side length of square: -2
Apr 28, 2016 10:55:22 PM Driver main
SEVERE: null
InvalidSideException: side length must be greater than 0
   at Square.<init>(Driver.java:27)
   at Driver.main(Driver.java:61)
InvalidSideException: side length must be greater than 0
   at Square.<init>(Driver.java:27)
   at Driver.main(Driver.java:61)


Test 3:
Enter side length of square: -3
Apr 28, 2016 10:56:42 PM Driver main
SEVERE: null
InvalidSideException: side length must be greater than 0
   at Square.<init>(Driver.java:27)
   at Driver.main(Driver.java:61)
InvalidSideException: side length must be greater than 0
   at Square.<init>(Driver.java:27)
   at Driver.main(Driver.java:61)

Test 4:
Enter side length of square: 16
Area of Square is 256

Test 5 :
Enter side length of square: 0
Apr 28, 2016 10:59:49 PM Driver main
SEVERE: null
InvalidSideException: side length must be greater than 0
   at Square.<init>(Driver.java:27)
   at Driver.main(Driver.java:61)
InvalidSideException: side length must be greater than 0
   at Square.<init>(Driver.java:27)
   at Driver.main(Driver.java:61)