Implementation of programming to output the following "results you want to output" in each language of Swift / Ruby / Python / Java based on object orientation
・ To remember when you forget ・ Comparison of each language
① Calculate BMI from height and weight (2) Discriminate between thin, normal, and obese from the BMI value ③ Display ①② above
---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());
}
}
}
① 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