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

This is what I have. Can\'t get it to compile. Below the code are the instructio

ID: 3532969 • Letter: T

Question

This is what I have. Can't get it to compile. Below the code are the instructions.

NOTE: This is NOT a gui program, that will be the next assignment.

class Square {

public float perimeter(float side) { return (4 * side);

String length = JOptionPane.showInputDialog("Enter length: ");

float len = Float.parseFloat(length);

Square sq = new Square();

float circum = sq.perimeter(len);

}

}

class Circle extends Square {

public float perimeter(float radius) {

// set PI constant

final float PI = 3.14159;

return (2 * PI * radius);

}

}

class Rectangle extends Square {

public float perimeter (float side) { return ((2.0 * l) + (2.0 * w))

}

class Triangle extends Square {

public float perimeter (float side) ( return (3.0 * t)

}

public class Main

{

publicstaticvoidmain (String args[])

{

doubles, l, w, d, t;

Scanner opt = newScanner(System.in);

Scanner keyboard = newScanner(System.in);

String optionTxt = JOptionPane.showInputDialog("This program will calculate the perimeter of different shapes. Select 1 for square: Select 2 for rectangle: Select 3 for circle: Select 4 for triangle: Select or 5 to exit:");

intoptionNum = Integer.parseInt(optionTxt);


if(optionNum==1)

{

String squTxt = JOptionPane.showInputDialog("You selected square. Please enter the length of a side in centimeters:") ;

s=Float.parseFloat(squTxt);

JOptionPane.showMessageDialog(null,"The perimeter is: "+ (4* s) + " cm.");

System.exit(0);

}

if(optionNum==2)

{

String rectTxt = JOptionPane.showInputDialog("You selected rectangle. Please enter the length of a side in centimeters:") ;

l=Float.parseFloat(rectTxt);

String rectTxt2 = JOptionPane.showInputDialog("Now enter the width in centimeters:") ;

w=Float.parseFloat(rectTxt2);

JOptionPane.showMessageDialog(null,"The perimeter is: "+ ((2*l) + (2* w)) + " cm.");

System.exit(0);

}

if(optionNum==3)

{

String circTxt = JOptionPane.showInputDialog("You selected circle. Please enter the diameter in centimeters:") ;

d=Float.parseFloat(circTxt);

JOptionPane.showMessageDialog(null,"The perimeter is: "+ (Math.PI * d) + " cm.");

System.exit(0);

}

if(optionNum==4)

{

String triTxt = JOptionPane.showInputDialog( "You selected triangle. Please enter the length of a side in centimeters.") ;

t=Float.parseFloat(triTxt);

JOptionPane.showMessageDialog(null, "The perimeter is: "+ (3* t) + " cm.");

System.exit(0);

}

if(optionNum==5)

{

JOptionPane.showMessageDialog(null, "You have chosen to exit the program. Goodbye.");

System.exit(0);

}

else

{

JOptionPane.showMessageDialog(null, "Hey! We asked you to select from options 1-5, bub!");

}

}


Create a Java program based on the geometric shapes example described at the beginning

of this lesson using Classes Square, Triangle, Rectangle, and Circle to help describe

inheritance and polymorphism using the circumference() method. The program will

calculate the circumference of a designated shape.


The program should begin by prompting you for the shape you want to calculate the

circumference for

Explanation / Answer

// Corrected program given below

// You needed to import JOptionPane, and few other errors

// make sure to put the entire code in a file called "Main.java" before compiling

// because filename neeeds to be same as public class name




import javax.swing.JOptionPane;

class Square {

public double perimeter(float side) { return (4 * side);


}

}



class Circle extends Square {

public double perimeter(float diameter) {

// set PI constant

final double PI = 3.14159;

return (PI * diameter);

}

}



class Rectangle extends Square {

public double perimeter (float l,float w) { return ((2.0 * l) + (2.0 * w)); }

}



class Triangle extends Square {

public double perimeter (float side1,float side2,float side3) { return ((side1 + side2 + side3)); }

}



public class Main

{

public static void main (String args[])

{

float l=0, w=0, d=0, l1=0,l2=0,l3=0,s=0;

String optionTxt = JOptionPane.showInputDialog("This program will calculate the perimeter of different shapes. Select 1 for square: Select 2 for rectangle: Select 3 for circle: Select 4 for triangle: Select or 5 to exit:");

int optionNum = Integer.parseInt(optionTxt);


if(optionNum==1)

{

String squTxt = JOptionPane.showInputDialog("You selected square. Please enter the length of a side in centimeters:") ;

s = Float.parseFloat(squTxt);

JOptionPane.showMessageDialog(null,"The perimeter is: "+ new Square().perimeter(s) + " cm.");

System.exit(0);

}

if(optionNum==2)

{

String rectTxt = JOptionPane.showInputDialog("You selected rectangle. Please enter the length of a side in centimeters:") ;

l=Float.parseFloat(rectTxt);

String rectTxt2 = JOptionPane.showInputDialog("Now enter the width in centimeters:") ;

w=Float.parseFloat(rectTxt2);

JOptionPane.showMessageDialog(null,"The perimeter is: "+ new Rectangle().perimeter(l,w) + " cm.");

System.exit(0);

}

if(optionNum==3)

{

String circTxt = JOptionPane.showInputDialog("You selected circle. Please enter the diameter in centimeters:") ;

d=Float.parseFloat(circTxt);

JOptionPane.showMessageDialog(null,"The perimeter is: "+ new Circle().perimeter(d) + " cm.");

System.exit(0);

}

if(optionNum==4)

{

String triTxt = JOptionPane.showInputDialog( "You selected triangle. Please enter the length of a first side in centimeters.") ;

l1=Float.parseFloat(triTxt);

String triTxt2 = JOptionPane.showInputDialog( "Please enter the length of a second side in centimeters.") ;

String triTxt3 = JOptionPane.showInputDialog( "Please enter the length of a third side in centimeters.") ;

l2=Float.parseFloat(triTxt2);

l3=Float.parseFloat(triTxt3);

JOptionPane.showMessageDialog(null, "The perimeter is: "+ new Triangle().perimeter(l1,l2,l3) + " cm.");

System.exit(0);

}

if(optionNum==5)

{

JOptionPane.showMessageDialog(null, "You have chosen to exit the program. Goodbye.");

System.exit(0);

}

else

{

JOptionPane.showMessageDialog(null, "Hey! We asked you to select from options 1-5, bub!");

}

}

}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote