Products
Study Documents
AI Grader
AI Answer
AI Code Checker
Plagiarism Checker
AI Paraphraser
AI Quiz
AI Detector
Pricing
Blog
About Us
Get 7 Days Free Trial
Login
import java.util.Scanner; public class rectangle { private
Verified
Added on  2023/04/08
|
2
|
241
|
184
AI Summary
Contribute Materials
Your contribution can guide someone’s learning journey. Share your documents today.
Contribute Now
import
java.util.Scanner;
public
class
rectangle
{
private
static
Scanner
sc
;
public
static
void
main(String[]
args
) {
sc
=
new
Scanner(System.
in
);
String
figure
=
getFigure
();
System.
out
.print(
"\nEnter
the
height
for
the "
+
figure
+
": "
);
double
height
=
readDouble
();
if
(
figure
.equals(
"rectangle"
))
{
System.
out
.print(
"Enter
the
width
for the
"
+
figure
+
": "
);
double
width
=
readDouble
();
System.
out
.println(
"\n\nArea
of
the
Rectangle
is
"
+
height
*
width
);
}
else
{
System.
out
.print(
"Enter
the
base width
for
the
"
+
figure
+
": "
);
double
width
=
readDouble
();
System.
out
.println(
"\n\nArea
of
the
Triangle
is
"
+String.
format
(
"%.2f"
,0.5*
height
*
width
));
}
}
/**
* Read
a positive
decimal
value
and
it should
be greater
than
0
*
@return
the
read decimal
value
*/
private
static
double
readDouble()
{
try
{
double
val
=Double.
parseDouble
(
sc
.nextLine());
if
(
val
<=0)
throw
new
Exception();
return
val
;
}
catch
(Exception
e
)
{
System.
out
.print(
"\nPlease
enter
a
positive
decimal
value
(Greater
than
0)
: "
);
return
readDouble
();
}
}
/**
* Read
a positive
integer
and
it should
be greater
than
0
*
@return
the
read integer
*/
private
static
int
getInt()
{
try
{
int
val
=Integer.
parseInt
(
sc
.nextLine());
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
💎 Get Pro
if
(
val
<=0)
throw
new
Exception();
return
val
;
}
catch
(Exception
e
)
{
System.
out
.print(
"\nPlease
enter
a
positive
int
(Greater
than
0) : "
);
return
getInt
();
}
}
/**
* Give
the
user
a choice
and
return
the
selected
figure
by
the user
*
@return
*/
private
static
String
getFigure()
{
System.
out
.println(
"Select
the
required
option."
);
System.
out
.println(
"1. Calculate
area
of a
triangle"
);
System.
out
.println(
"2. Calculate
area
of a
rectangle"
);
System.
out
.print(
"Please
select
one
option
:"
);
int
val
=
getInt
();
if
(
val
==1)
return
"triangle"
;
else
if
(
val
== 2)
return
"rectangle"
;
else
{
System.
out
.println(
"\n\nPlease
select
a valid
option."
);
return
getFigure
();
}
}
}
1 out of 2
Download
Related Documents
Java Code import java.util.Scanner; /** * * File name
|
2
|
257
|
314
View Document
House Deposit Calculator Project
|
13
|
1512
|
68
View Document
JAVA Program Using Array of Objects
|
28
|
2144
|
15
View Document
Analyze and visualize the ratings of users for specific items
|
13
|
1219
|
353
View Document
Vehicle Data Inventory System
|
18
|
2151
|
53
View Document
CarYardMIT162011 Class Implementation in Java
|
6
|
1107
|
393
View Document