LoginSignup
4
11

More than 5 years have passed since last update.

[Swift/Ruby/Python/Java] オブジェクト指向に基づくプログラミング

Last updated at Posted at 2018-08-13

この記事で書くこと

下記「出力したい結果」を出力するプログラミングをSwift/Ruby/Python/Javaの各言語で、オブジェクト指向に基づいて実装

※BMIの小数点以下桁数は、今回は特に考慮しない。

この記事を書いた理由

・忘れたときに思い出すため
・各言語の比較

処理内容

① 身長・体重からBMIを計算
② BMIの値から、やせ・普通・肥満を判別
③ 上記①②を表示

出力したい結果

---1人目---
身長:178.0cm
体重:61.8kg
BMI: 19.505112990784
普通
---2人目---
身長:153.0cm
体重:74.0kg
BMI: 31.6117732496049
肥満
---3人目---
身長:185.0cm
体重:59.0kg
BMI: 17.2388604821037
やせ

Swift

sample.swift
import Foundation 

class Person{
    var height:Double
    var weight:Double

    init(_ height:Double,_ weight:Double){
        self.height = height
        self.weight = weight
    }

    func getHeight() -> Double{
        return self.height
    }

    func getWeight() -> Double{
        return self.weight
    }

    func bmi() -> Double{
        return self.weight / self.height / self.height * 10000
    }

    func isHealthy() -> String{
        switch self.bmi(){
            case ..<18.5:
                return "やせ"
                break
            case 18.5..<25:
                return "普通"
                break
            default:
                return "肥満"
                break
        }
    }

     func info() {
         print("身長:\(self.getHeight())cm")
         print("体重:\(self.getWeight())kg")
         print("BMI: \(self.bmi())")
         print("\(self.isHealthy())")
     }
}

var person1:Person = Person(178,61.8)
var person2:Person = Person(153,74)
var person3:Person = Person(185,59)

var persons:[Person] = [person1,person2,person3]
 var index:Int = 1

for person in persons{
    print("---\(index)人目---")
    person.info()
    index+=1
}

Ruby

sample.rb
class Person
    attr_accessor :height,:weight

    def initialize(height,weight)
        @height = height
        @weight = weight
    end

    def getHeight()
        return self.height
    end

    def getWeight()
        return self.weight
    end

    def bmi()
        return self.weight / self.height / self.height * 10000
    end

    def isHealthy()
        case self.bmi()
            when 0..18.49
                return "やせ"
            when 18.5..24.99
                return "普通"
            else
                return "肥満"
        end
    end

    def info()
        puts "#{self.getHeight()} cm"
        puts "#{self.getWeight()} kg"
        puts "BMI: #{self.bmi()}"
        puts "#{self.isHealthy()}"
    end
end

person1 = Person.new(178.0,61.8)
person2 = Person.new(153.0,74.0)
person3 = Person.new(185.0,59.0)
persons = [person1,person2,person3]

index = 1

persons.each do |person|
    puts "---#{index}人目---"
    person.info()
    index += 1
end

Python

sample.py
class Person:
    def __init__(self,height,weight):
        self.height = height
        self.weight = weight

    def getHeight(self):
        return self.height

    def getWeight(self):
        return self.weight

    def bmi(self):
        return self.weight / self.height / self.height * 10000

    def isHealthy(self):
        if self.bmi() < 18.5:
            return "やせ"
        elif 18.5 <= self.bmi() < 25.0:
            return "普通"
        else:
            return "肥満"

    def info(self):
        print("身長:" + str(self.height) + "cm")
        print("体重:" + str(self.weight) + "kg")
        print("BMI:" + str(self.bmi()))
        print(self.isHealthy())

person1 = Person(178.0,61.8)
person2 = Person(153.0,74.0)
person3 = Person(185.0,59.0)
persons = [person1,person2,person3]

index = 1

for person in persons:
    print("---" + str(index) + "人目---")
    person.info()
    index += 1

Java

sample.java
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {

    Person person1 = new Person(178.0,61.8);
    Person person2 = new Person(153.0,74.0);
    Person person3 = new Person(185.0,59.0);

    Person[] persons = {person1,person2,person3};
    int index = 1;

    for(int i=0;i<persons.length;i++){
        System.out.println("---" + index + "人目---");
        persons[i].info();
        index += 1;
    }
}

public static class Person{
    private double height;
    private double weight;

    Person(double height,double weight){
        this.height = height;
        this.weight = weight;
    }

    public double getHeight() {
        return this.height;
    }

    public double getWeight(){
        return this.weight;
    }

    public double bmi(){
        return this.weight / this.height / this.height * 10000;
    }

    public String isHealthy(){
        if(this.bmi()< 18.5){
            return "やせ";
        } else if (this.bmi()>=18.5 && this.bmi()<25){
            return "普通";
        } else {
            return "肥満";
        }
    }

    public void info(){
        System.out.println("身長:" + this.getHeight() + "cm");
        System.out.println("体重:" + this.getWeight() + "kg");
        System.out.println("BMI:" + this.bmi());
        System.out.println(this.isHealthy());
    }
}
}

個人的な感想

① Swiftがいちばん書きやすい
② 案外Rubyがコード量が多い
③ Javaを習得するとSwiftが書きやすい
④ 初めてプログラミングを学ぶ人は、RubyよりもPythonのほうが取っつきやすいかも

4
11
2

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
4
11