LoginSignup
2
0
記事投稿キャンペーン 「2024年!初アウトプットをしよう」

【初めてのJava】まずはコーディングしてみた(クラス)

Last updated at Posted at 2024-01-05

はじめに

HelloWorldの次として、BMI計算機を書いてみることにしました。

機能

 標準入力で身長、体重を入力すると、BMIの計算結果を出力するだけのツールです。
メインメソッドで終わってしまう内容ですが、目的はJavaを理解するためのコーディングなので BMIを計算するクラスを別に立てています。

ソースコード(初回投稿時)

```java:App.java import java.util.Scanner;

public class App{
public static void main(String[] args){

    calcBmi bmi = new calcBmi();
    Scanner scanner = new Scanner(System.in);
    double height = 0.0;
    double weight = 0.0;

    try{
        System.out.println("身長を入力してください");
        height = scanner.nextDouble();

        System.out.println("体重を入力してください");
        weight = scanner.nextDouble();
    }catch(Exception e){
        System.out.println("数値を入力してください");
        return;
    }finally{
        scanner.close();
    }
    if(!bmi.setBmiParameter(height,weight)){
        return;
    };
    if(!bmi.calculationBmi()){
        return;
    };
    if(!bmi.bmiResultDisplay()){
        return;
    };
}

}


```java:calcBmi.java
public class calcBmi {
    protected double height;
    protected double weight;
    protected double bmi;

    protected static double HEIGHT_MAX;
    protected static double WEIGHT_MAX;

    calcBmi(){
        initHeightAndWeight();
        HEIGHT_MAX = 3.0;
        WEIGHT_MAX = 700.0;
    }

    protected void initHeightAndWeight(){
        this.height = 0.0;
        this.weight = 0.0;
    }

    boolean setBmiParameter(double height, double weight){

        if(!checkHeight(height) || !checkWeight(weight)){
            initHeightAndWeight();
            return false;
        }
        this.height = height;
        this.weight = weight;
        System.out.println("正しくセットされました");
        return true;
    }

    boolean calculationBmi(){
        this.bmi = (this.weight / this.height / this.height);
    
        return true;
    }

    boolean bmiResultDisplay(){
        System.out.println("身長" + this.height);
        System.out.println("体重" + this.weight);
        System.out.println("BMI" + this.bmi);

        return true;
    }

    protected boolean checkHeight(double h){
        if((0 >= h) || (h >= HEIGHT_MAX)){
            System.out.println("身長の値を間違えています。身長の単位はmでセットしてください");
            return false;
        }
        return true;
    }
    protected boolean checkWeight(double w){
        if((0 >= w) || (w >= WEIGHT_MAX)){
            System.out.println("体重の値を間違えています。体重の単位はkgでセットしてください");     
            return false;
        }
        return true;
    }
}

ソースコード(修正)

※コメントを参考にしたり、後から気づいた改善点を反映したものです

Body.java
/*
初回からの主な変更点
・メソッドの戻り値 Booleanをboolean型に変更
・クラス名calcBmiをBodyに変更
・メソッド名check〜をisValid〜に変更
・クラスBodyの変数(height,weight,bmi)や一部のメソッドの戻り値について、protectedをprivateに変更
 (カプセル化したい目的に対して実装が間違えていた)
・HEIGHT_MAX、WEIGHT_MAXを定数とするため、finalを設定
*/

public class Body {
    private double height;
    private double weight;
    private double bmi;

    protected static final double HEIGHT_MAX = 3.0;
    protected static final double WEIGHT_MAX = 700.0;

    Body(){
        this.height = 0.0;
        this.weight = 0.0;
    }

    boolean setParameter(double height, double weight){

        if(!isValidHeight(height) || !isValidWeight(weight)){
            this.height = 0.0;
            this.weight = 0.0;
            return false;
        }
        this.height = height;
        this.weight = weight;
        System.out.println("正しくセットされました");
        return true;
    }

    void calculation(){
        bmi = weight / height / height;    
    }

    void display(){
        System.out.println("身長" + this.height);
        System.out.println("体重" + this.weight);
        System.out.println("BMI" + this.bmi);
    }

    private boolean isValidHeight(double h){
        if((0 >= h) || (h >= HEIGHT_MAX)){
            System.out.println("身長の値を間違えています。身長の単位はmでセットしてください");
            return false;
        }
        return true;
    }
    private boolean isValidWeight(double w){
        if((0 >= w) || (w >= WEIGHT_MAX)){
            System.out.println("体重の値を間違えています。体重の単位はkgでセットしてください");     
            return false;
        }
        return true;
    }
}
App.java
/*
初回からの主な変更点
・try with-resources文を使ってScannerオブジェクトを自動的にクローズするように変更
・変数やクラスBodyのオブジェクト生成場所をtryの中に入れて スコープを限定
*/

import java.util.Scanner;
public class App{
    public static void main(String[] args){
        try(Scanner scanner = new Scanner(System.in)){
            double height;
            double weight;
            Body bmi = new Body();

            System.out.println("身長を入力してください 単位[m]");
            height = scanner.nextDouble();

            System.out.println("体重を入力してください 単位[kg]");
            weight = scanner.nextDouble();

            if(!bmi.setParameter(height,weight)){
                return;
            };
            bmi.calculation();
            bmi.display();

        }catch(Exception e){
            System.out.println("数値を入力してください");
        }
    }

}

結果

正しい入力時
$ java App
身長を入力してください
1.7
体重を入力してください
66
正しくセットされました
身長1.7
体重66.0
BMI22.837370242214536
間違いのある入力時
$ java App
身長を入力してください
170
体重を入力してください
55
身長の値を間違えています。身長の単位はmでセットしてください

感想

作成に2時間くらいかかりました。
BMIの計算結果は間違えていないようです。
まずは慣れていくことが重要だと思いますので、続けていこうと思います。

2
0
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0