import javax.swing.JOptionPane; // Dialog box methods public

Verified

Added on  2023/04/08

|2
|267
|54
AI Summary
tabler-icon-diamond-filled.svg

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
import javax.swing.JOptionPane; // Dialog box methods
public class rectangle {
public static void main(String[] args)
{
double shape = InputDouble("Area of \n 1. Triangle \n 2.
Retangle"); /* User choice for shape*/
double width = InputDouble("Enter Width"); /* user value for
width */
double height = InputDouble("Enter Height"); /* user value for
height */
double shapearea = calculatearea(width, height, shape); /*
variable to store the value of the calculated area */
if (shapearea == 0) /* value returned if width/height was entered
as 0 or triangle/rectangle was not selected 8 */
{
JOptionPane.showMessageDialog(null, "The value entered was
incorrect \n Please re assess the entered criteria"); /* Display warning
message */
}
else if (shapearea < 0) /* value is a negative number was entered
for dimension */
{
JOptionPane.showMessageDialog(null, "A negative value has
been entered. An area can never be negative. Please try again"); /* Display
warning message */
}
else {JOptionPane.showMessageDialog(null, "The area is"
+ shapearea);} /* Return the area of the shape*/
System.exit(0); // Terminate program
}
public static double InputDouble(String prompt)
{
/* Get data in string form: convert to double */
String s = JOptionPane.showInputDialog(prompt);
return Double.parseDouble(s);
}
public static double calculatearea(double Basewidth, double tall,
double Typeshape )
{
/* Calculates the area */
double test;
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
if (Typeshape == 1.0)
{test = ((Basewidth * .5) * tall); /* Calculate the area if the object
is a triangle*/
}
else if (Typeshape == 2.0) {
test = (Basewidth * tall); /* Calculate the area if the object is
a rectangle*/
}
else {
test = 0; /*error handling */
}
return test;
}
}
chevron_up_icon
1 out of 2
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]