Specification:
| | | |:-----------------|:------------------| |Method name|calcTriangleArea| |Return type|Triangle area(double)| |1st argument|The length of the base of the triangle, the unit is cm(double bottom)| |2nd argument|The height of the triangle, the unit is cm(double height)| |Processing content|Use the argument to find the area and return it.|
| | | |:-----------------|:------------------| |Method name|calcCircleArea| |Return type|Area of a circle(double)| |1st argument|The radius of the circle, the unit is cm(double bottom)| |Processing content|Use the argument to find the area and return it.|
Main.java
1 public class Main {
2 public static double calcTriangleArea(double bottom, double height){
3 double area = (bottom * height) / 2;
4 return area;
5 }
6 public static double calcCircleArea(double radius){
7 double area = radius * radius * 3.14;
8 return area;
9 }
10 public static void main(String[] args){
11 double triangleArea = calcTriangleArea(10.0, 5.0);
12 System.out.println("Triangle area:" + triangleArea + "Square cm");
13 double circleArea = calcCircleArea(5.0);
14 System.out.println("Area of a circle:" + circleArea + "Square cm");
15 }
16 }
Main.java_console
Triangle area:25.0 square cm
Area of a circle:78.5 square cm
① Make sure to check the specifications. ② "class" is created in Main. ③ Create a method of "calcTriangleArea". ④ Create a method of "calcCircleArea". ⑤ Create a "call method" in main. ⑥ Compile. ⑦ Confirmation, end.
class Main
Main.java
1 public class Main {
calcTriangleArea
Main.java
2 public static double calcTriangleArea(double bottom, double height){
3 double area = (bottom * height) / 2;
4 return area;
5 }
(1) Create an equation to find the area of the triangle.
② public static ``` return value
is ** double (because floating point is used) **,
method name` `` is specified ** calcTriangle
Describe Area **.
③ In (), pass the `` `argument ``` ** double bottom, double height **.
④ Next, describe the calculation formula.
double area = (bottom * height) / 2;
** [Variable type Arbitrary variable name = (base x height) ÷ 2;] **.
⑤
, return is used to pass the assigned double area to the return value double.
Write ** return area; **.
calcCircleArea
Main.java
6 public static double calcCircleArea(double radius){
7 double area = radius * radius * 3.14 ;
8 return area;
9 }
(1) Create a formula to calculate the area of a circle.
② public static `return value ``` is ** double (because floating point is used) **,
method name `` is specified ** calcCircle
Describe Area **.
③ In (), pass the ``` argument
** double radius **.
④ Next, describe the calculation formula.
double area = radius * radius * 3.14;
** [Variable type Arbitrary variable name = radius x radius x pi;] **.
⑤ `return is used to pass the assigned double area to the return value double.
Write ** return area; **.
# Call and output to screen
### Main method
#### **`Main.java`**
```java
10 public static void main(String[] args){
11 double triangleArea = calcTriangleArea(10.0, 5.0);
12 System.out.println("Triangle area:" + triangleArea + "Square cm");
13 double circleArea = calcCircleArea(5.0);
14 System.out.println("Area of a circle:" + circleArea + "Square cm");
15 }
16 }
(1) Create a Main method to call and output to the screen.
(2) public static return value
is ** void (no return value) **, method name
is ** Main **, and () is
Write ** String [] args **.
③ Next, call the method, pass the value, and assign it to the left side
. From calcTriangleArea, in order
Call.
④double triangleArea = calcTriangleArea(10.0, 5.0);
** [Variable type Arbitrary variable name = Method name (base, height)] **.
argument ()
, and double
Substitution to triangleArea ** is done collectively.⑤ In Sytem.out.println () ;, if you write ** "triangle area:" + triangleArea + "square cm" ** in (), Output to the screen.
⑥ Next, call calcCircleArea
, pass the ** value, and assign it to the left side **.
⑦double circleArea = calcCircleArea(5.0);
** [Variable type arbitrary variable name = method name (radius)] **.
argument ()
, and it is assigned to double circleArea **.⑧ In Sytem.out.println () ;, if you write ** "area of a circle:" + circleArea + "square cm" ** in (), Output to the screen.
Main.java_console
Triangle area:25.0 square cm
Area of a circle:78.5 square cm
-Becomes.
that's all.
I tried to summarize how to create "calcTriangleArea" and "calcCircleArea" according to the specifications of Java Introduction-Chapter 5-Practice Exercises 5-4.
A refreshing introduction to Java-Second Edition-Impress Publishing, Inc. Kiyotaka Nakayama / Daigo Kunimoto
Recommended Posts