0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【15章】Javaを学ぼう

Last updated at Posted at 2021-09-07

前章ではオブジェクト指向を中心に説明をさせていただきました。
今回は前章の勉強した内容を活用し、自己紹介プログラムを作ってみたいと思います。
私自身のアウトプットの場となりますので、よろしくお願いいたします!

自己紹介プログラム

自己紹介プログラムを、オブジェクト指向を使って実装していきたいと思います。

インスタンスフィールドを定義

まずはインスタンスフィールドを定義しましょう。
インスタンスにどんな情報を持たせるかを考えていきます。

Person オブジェクト

インスタンスフィールド
・firstName
・lastName
・age
・height
・weight

インスタンスメソッド
・fullName
・bmi
・printData

上記の情報を持たせていきます。

Person.java
//インスタンスフィールドを定義
class Person {
 public String firstName;
 public String lastName;
 public int age;
 public double height;
 public double weight;
 
//コンストラクタを定義し、インスタンスフィールドに値をセット
 Person(String firstName, String lastName, int age, double height, double weight) { 
  this.firstName = firstName;
  this.lastName = lastName;
  this.age = age;
  this.height = height;
  this.weight = weight;
 }
}

インスタンスの生成と、各フィールドの値の出力を用意

Main.java
class Main {
 public static void main(String[] args) {
  Person person1 = new Person("John", "Connor", 28, 1.7, 65.0);

  System.out.println(person1.firstName);
  System.out.println(person1.lastName);
  System.out.println(person1.age);
  System.out.println(person1.height);
  System.out.println(person1.weight);
 }
}

インスタンスメソッドを定義

インスタンスメソッドを定義します。まずはfullNameメソッドとbmiメソッドをインスタンスメソッドにします。
printDataメソッドは後ほど作ります。

Person.java
//インスタンスフィールドを定義
class Person {
 public String firstName;
 public String lastName;
 public int age;
 public double height;
 public double weight;
 
//コンストラクタを定義し、インスタンスフィールドに値をセット
 Person(String firstName, String lastName, int age, double height, double weight) { 
  this.firstName = firstName;
  this.lastName = lastName;
  this.age = age;
  this.height = height;
  this.weight = weight;
 }

 //fullNameメソッドを定義
 public String fullName() {
  return this.firstName + " " + lastName;
 }
 
 //bmiメソッドを定義
 public double bmi() {
  return this.weight / height / height;
 }
}
Main.java
class Main {
 public static void main(String[] args) {
  Person person1 = new Person("John", "Connor", 28, 1.7, 65.0);

  System.out.println(person1.fullName()); 
  System.out.println(person1.age);
  System.out.println(person1.bmi());
 }
}

次はインスタンスが自分の情報をコンソールに出力するprintDataメソッドを定義します。
printDataメソッドは、fullNameメソッドやbmiメソッドといった他のメソッドを呼び出します。

Person.java
//インスタンスフィールドを定義
class Person {
 public String firstName;
 public String lastName;
 public int age;
 public double height;
 public double weight;
 
//コンストラクタを定義し、インスタンスフィールドに値をセット
 Person(String firstName, String lastName, int age, double height, double weight) { 
  this.firstName = firstName;
  this.lastName = lastName;
  this.age = age;
  this.height = height;
  this.weight = weight;
 }

 //fullNameメソッドを定義
 public String fullName() {
  return this.firstName + " " + lastName;
 }
 
 //bmiメソッドを定義
 public double bmi() {
  return this.weight / height / height;
 }

 // printDataメソッドを定義
 public void printData() {
  System.out.println("私の名前は" + this.fullName() + "です");
  System.out.println("年齢は" + this.age + "歳です");
  System.out.println("BMIは" + Math.round(this.bmi()) + "です");
 }
}
Main.java
class Main {
 public static void main(String[] args) {
  Person person1 = new Person("John", "Connor", 28, 1.7, 65.0);

  person1.printData();
  //printDataメソッドを定義したため不要
  //System.out.println(person1.fullName()); 
  //System.out.println(person1.age);
  //System.out.println(person1.bmi());
 }
}

出力結果↓

コンソール
私の名前はJohn Connorです
年齢は28歳です
BMIは23です

今回はここまでにしたいと思います。次章では今回作成した自己紹介プログラムをさらに改善していきたいと思います。
よろしくお願いいたします!
最後までご覧いただきまして、ありがとうございました!

0
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?