Getting Started with Java_Chapter 5_Practice Exercises 5_4

Creation of "calc Triangle Area" and "calc Circle Area" according to the specifications

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.|

answer

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

Programming order

① 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.

Creating a class

class Main

Main.java


1  public class Main {

Method definition

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)] **.

⑤ 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)] **.

⑧ In Sytem.out.println () ;, if you write ** "area of a circle:" + circleArea + "square cm" ** in (), Output to the screen.

When compiled (screen output)

Main.java_console


Triangle area:25.0 square cm
Area of a circle:78.5 square cm

-Becomes.

that's all.

Summary

I tried to summarize how to create "calcTriangleArea" and "calcCircleArea" according to the specifications of Java Introduction-Chapter 5-Practice Exercises 5-4.

References

A refreshing introduction to Java-Second Edition-Impress Publishing, Inc. Kiyotaka Nakayama / Daigo Kunimoto

Recommended Posts

Getting Started with Java_Chapter 5_Practice Exercises 5_4
Getting Started with DBUnit
Getting Started with Ruby
Getting Started with Swift
Getting Started with Docker
Getting Started with Doma-Transactions
Getting Started with Java_Chapter 8_About Instances and Classes
Getting Started with Doma-Annotation Processing
Getting Started with Java Collection
Getting Started with JSP & Servlet
Getting Started with Java Basics
Getting Started with Spring Boot
Getting Started with Ruby Modules
[Google Cloud] Getting Started with Docker
Getting started with Java lambda expressions
Getting Started with Docker with VS Code
Getting Started with Doma-Criteria API Cheat Sheet
Getting Started with Ruby for Java Engineers
Getting Started with Docker for Mac (Installation)
Getting Started with Parameterization Testing in JUnit
Getting Started with Java Starting from 0 Part 1
Getting Started with Ratpack (4)-Routing & Static Content
Getting started with the JVM's GC mechanism
Getting Started with Language Server Protocol with LSP4J
Getting Started with Creating Resource Bundles with ListResoueceBundle
Links & memos for getting started with Java (for myself)
Getting Started with Doma-Using Projection with the Criteira API
Getting Started with Doma-Using Subqueries with the Criteria API
Getting Started with Java 1 Putting together similar things
Getting started with Kotlin to send to Java developers
Getting Started with Doma-Using Joins with the Criteira API
Getting Started with Doma-Introduction to the Criteria API
I tried Getting Started with Gradle on Heroku
Getting started with Java programs using Visual Studio Code
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Get started with Gradle
Proceed with Rust official documentation on Docker container (1. Getting started)
Getting started with Java and creating an AsciiDoc editor with JavaFX
Getting Started with Doma-Dynamicly construct WHERE clauses with the Criteria API
Getting Started with Reactive Streams and the JDK 9 Flow API
Getting Started with GitHub Container Registry instead of Docker Hub
Get started with Spring boot
Get started with DynamoDB with docker
Completable Future Getting Started (First Future)
Returning to the beginning, getting started with Java ② Control statements, loop statements
Summarize the main points of getting started with JPA learned with Hibernate
Getting started with Swift / C bridges with porting Echo Server using libuv