[Swift / Ruby / Python / Java] Object-oriented programming

What to write in this article

Implementation of programming to output the following "results you want to output" in each language of Swift / Ruby / Python / Java based on object orientation

Why I wrote this article

・ To remember when you forget ・ Comparison of each language

Processing content

① Calculate BMI from height and weight (2) Discriminate between thin, normal, and obese from the BMI value ③ Display ①② above

The result you want to output

---1st person---
Height: 178.0cm
Weight: 61.8kg
BMI: 19.505112990784
usually
---Second person---
Height: 153.0cm
Weight: 74.0kg
BMI: 31.6117732496049
obesity
---3rd person---
Height: 185.0cm
Weight: 59.0kg
BMI: 17.2388604821037
Skinny

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 "Skinny"
                break
            case 18.5..<25:
                return "usually"
                break
            default:
                return "obesity"
                break
        }
    }

     func info() {
         print("height:\(self.getHeight())cm")
         print("body weight:\(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)Eye---")
    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 "Skinny"
            when 18.5..24.99
                return "usually"
            else
                return "obesity"
        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}Eye---"
    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 "Skinny"
        elif 18.5 <= self.bmi() < 25.0:
            return "usually"
        else:
            return "obesity"
    
    def info(self):
        print("height:" + str(self.height) + "cm")
        print("body weight:" + 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) + "Eye---")
    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 + "Eye---");
        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 "Skinny";
        } else if (this.bmi()>=18.5 && this.bmi()<25){
            return "usually";
        } else {
            return "obesity";
        }
    }
    
    public void info(){
        System.out.println("height:" + this.getHeight() + "cm");
        System.out.println("body weight:" + this.getWeight() + "kg");
        System.out.println("BMI:" + this.bmi());
        System.out.println(this.isHealthy());
    }
}
}

Personal impression

① Swift is the easiest to write (2) Unexpectedly, Ruby has a large amount of code ③ Swift is easy to write if you learn Java ④ For those who are learning programming for the first time, Python may be easier to get used to than Ruby.

Recommended Posts

[Swift / Ruby / Python / Java] Object-oriented programming
Java VS PHP VS Python VS Ruby
[Ruby / Python / Java / Swift / JS] What is an algorithm?
[Python] Object-oriented programming learned with Pokemon
Ruby Python Java Case insensitive sorting
Eating and comparing programming languages: Python and Ruby
Python programming note
Programming in python
How to enjoy programming with Minecraft (Ruby, Python)
What is "functional programming" and "object-oriented" in Python?
paiza POH ec-campaign (C # / Java / Python / Ruby) # paizahack_01
2014 Web Application Framework Trends (PHP / Java / Ruby / Python / Perl)
[Beginner / Saved version] What you can do by programming language (12 selections such as Ruby / Python / Java / Swift / PHP / Go)
"Object-oriented" learning with python
3. 3. AI programming with Python
Ruby, Python and map
Competitive programming diary python 20201213
Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation
Python programming with Atom
Python and Ruby split
Competitive programming diary python 20201220
Competitive programming with python
Python programming in Excel
LEGO Mindstorms 51515 Python Programming
[Python] Dynamic programming ABC015D
Competitive programming diary python
Let's write Python, Ruby, PHP, Java, JavaScript side respectively
Programming with Python Flask
[Python version] Why can't you do object-oriented development in Java?
Five languages basic grammar comparison (C #, Java, Python, Ruby, Kotlin)
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
I tried programming the chi-square test in Python and Java.
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Hello World in various languages [Python / PHP / Java / Perl / Ruby]
Programming with Python and Tkinter
Python Programming Workshop-Super Introductory Vol.3
Python3 programming functions personal summary
Python on Ruby and angry Ruby on Python
Python, Java, C ++ speed comparison
Atcoder Acing Programming Contest Python
[Python] Dynamic programming knapsack problem
[Python] Dynamic programming TDPC D
Python and ruby slice memo
Study from Python Hour4: Object-oriented ②
Standard input / summary / python, ruby
I compared Java and Python!
Python web programming article summary
Paiza Python Primer 1 Learn Programming
Zundokokiyoshi with python / ruby / Lua
About Perl, Python, PHP, Ruby
Ruby and Python syntax ~ branch ~
Python Competitive Programming Site Summary
Python Machine Learning Programming> Keywords
Ruby, Python Module Installation Guide
Python Programming Workshop-Super Introductory Vol.4
Study from Python Hour4: Object-oriented ①
An introduction to Python Programming
[Python] Dynamic programming TDPC A