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

Problem 1: PARTI: Define a Java class named \"Square\" in a package named \"SQUA

ID: 3910238 • Letter: P

Question

Problem 1: PARTI: Define a Java class named "Square" in a package named "SQUARE". This class has only one attribute: side-length that is defined as private. Class Square definition provides a default constructor, get-method and set-method of the attribute side-length, and the methods to calculate the perimeter and the area of the square. PART II: Write a Java program that can calculate and print out the perimeter and area of a square. The user enters data of the side-length and its measurement unit "in" for inch, "ft" for feet, "cm" for centimeter, and "m" for meter) from the console. The program should check to be sure that the side-length is not negative and the measurement unit must be one among the listed above units. The Java program should be another Java class named "SquareCalculator" in the same package, i.e. SQUARE. The program creates an object of class Square. After reading the input of side-length, the program sets the value of side-length of the Square object and invokes the public methods of class Square to calculate the perimeter and the area of the square. Important Notes: • To submit PART I, students copy all the code of the class Square into a Notepad file named "hw2_Square" (the file should have the suffix .txt) • To submit PART II, students copy all the code of the program Square Calculator into a Notepad file named "hw2_Square Calculator" (the file should have the suffix .txt) Example Output in the Console: Enter the side-length of the square: Enter measurement unit - in, ft, cm, or m: in Perimeter of the square: Area of the square: 12.00 in 9.00 square in

Explanation / Answer

SquareCalculator.java

import java.util.Scanner;

public class SquareCalculator {

public static void main(String[] args) {

Scanner scan =new Scanner(System.in);

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

int side = scan.nextInt();

System.out.println("Enter measurement unit - in, ft, cm, or m:");

String measurement = scan.next();

Square obj = new Square();

obj.setSide_length(side);

double permeter=0;

double area = 0;

if(measurement.equals("in")) {

permeter = obj.perimeter();

area = obj.area();

} else if(measurement.equals("ft")) {

permeter = obj.perimeter() * 0.08333333;

area = obj.area() * 0.08333333;

} else if(measurement.equals("cm")) {

permeter = obj.perimeter() * 2.54;

area = obj.area() * 2.54;

} else if(measurement.equals("m")) {

permeter = obj.perimeter() * 0.0254;

area = obj.area() * 0.0254;

}

System.out.println("Perimeter of the square: "+permeter);

System.out.println("Area of the square: "+area);

}

}

Square.java

public class Square {

private int side_length;

public Square() {

}

public int getSide_length() {

return side_length;

}

public void setSide_length(int sideLength) {

side_length = sideLength;

}

public double area() {

return side_length*side_length;

}

public double perimeter() {

return 4 * side_length;

}

}

Output:

Enter the side-length of the square:
3
Enter measurement unit - in, ft, cm, or m:
in
Perimeter of the square: 12.0
Area of the square: 9.0