# Java 2 ## demo ```java= // Main.java class Main { public static void main(String[] args) { Person person1 = new Person(Type.CAR.getValue()); // Person person2 = new Person(Type.BICYCLE.getValue()); Person person2 = new Person(Type.getValue(Type.BICYCLE.getCode())); Car car = new Car("フェラーリ", "赤"); Bicycle bicycle = new Bicycle("ビアンキ", "緑"); person1.buy(car); person2.buy(bicycle); System.out.println("【車の情報】"); car.printData(); System.out.println("-----------------"); System.out.println("【車の所有者の情報】"); car.getOwner().printData(); System.out.println("================="); System.out.println("【自転車の情報】"); bicycle.printData(); System.out.println("-----------------"); System.out.println("【自転車の所有者の情報】"); bicycle.getOwner().printData(); } } ``` ```java= // Person.java // import java.util.Scanner; import java.util.*; class Person { private String firstName; private String middleName; private String lastName; private int age; private double height; private double weight; Scanner ipt = new Scanner(System.in); Person(String type) { System.out.println(type + "の所有者の情報を入力してください"); System.out.print("ファーストネーム: "); this.firstName = ipt.next(); System.out.print("ミドルネーム: "); this.middleName = ipt.next(); System.out.print("ラストネーム: "); this.lastName = ipt.next(); System.out.print("年齢: "); this.age = ipt.nextInt(); System.out.print("身長: "); this.height = ipt.nextDouble(); System.out.print("体重: "); this.weight = ipt.nextDouble(); System.out.println("-----------------"); } // Person(String middleName, int age, double height, double weight) { // this(age, height, weight); // System.out.print("please enter middleName name : "); // this.middleName = ipt.next(); // } public String fullName() { if (this.middleName == null || this.middleName == "" ) { return this.firstName + " " + this.lastName; } else { return this.firstName + " " + this.middleName + " " + this.lastName; } } public void printData() { System.out.println("名前は" + this.fullName() + "です"); System.out.println("年齢は" + this.age + "歳です"); System.out.println("身長は" + this.height + "mです"); System.out.println("体重は" + this.weight + "kgです"); System.out.println("BMIは" + Math.round(this.bmi()) + "です"); } public double bmi() { return this.weight / this.height / this.height; } // 以下2つを一つのメソッドで書き換えてください public void buy(Vehicle vehicle) { vehicle.setOwner(this); } } ``` ```java= // Vehivle.java abstract class Vehicle { private String name; private String color; protected int distance = 0; private Person owner; Vehicle(String name, String color) { this.name = name; this.color = color; } public String getName() { return this.name; } public String getColor() { return this.color; } public int getDistance() { return this.distance; } public Person getOwner() { return this.owner; } public void setName(String name) { this.name = name; } public void setColor(String color) { this.color = color; } public void setOwner(Person person) { this.owner = person; } public void printData() { System.out.println("名前:" + this.name); System.out.println("色:" + this.color); System.out.println("走行距離:" + this.distance + "km"); } abstract public void run(int distance); } ``` ```java= // Car.java class Car extends Vehicle { private int fuel = 50; Car(String name, String color) { super(name, color); } public int getFuel() { return this.fuel; } public void printData() { super.printData(); System.out.println("ガソリン量:" + this.fuel + "L"); } public void run(int distance) { System.out.println(distance + "km走ります"); if (distance <= this.fuel) { this.distance += distance; this.fuel -= distance; } else { System.out.println("ガソリンが足りません"); } System.out.println("走行距離:" + this.distance + "km"); System.out.println("ガソリン量:" + this.fuel + "L"); } public void charge(int litre) { System.out.println(litre + "L給油します"); if (litre <= 0) { System.out.println("給油できません"); } else if (litre + this.fuel >= 100) { System.out.println("満タンまで給油します"); this.fuel = 100; } else { this.fuel += litre; } System.out.println("ガソリン量:" + this.fuel + "L"); } } ``` ```java= // Bicycle.java class Bicycle extends Vehicle { Bicycle(String name, String color) { super(name, color); } public void run(int distance) { System.out.println(distance + "km走ります"); this.distance += distance; System.out.println("走行距離:" + this.distance + "km"); } } ``` ```java= // Type.java public enum Type { CAR(0, "車"), BICYCLE(1, "自転車"); private int code; private String value; private Type(int code, String value) { this.code = code; this.value = value; } public int getCode() { return code; } public String getValue() { return value; } public static String getValue(Integer code){ for (Type type : Type.values()) { if (type.code == code) { return type.value; } } return null; } } ``` ```javascript= // 输出内容 車の所有者の情報を入力してください ファーストネーム: Charlotte ミドルネーム: Elizabeth ラストネーム: Diana 年齢: 17 身長: 1.66 体重: 40 ----------------- 自転車の所有者の情報を入力してください ファーストネーム: John ミドルネーム: Christopher ラストネーム: Smith 年齢: 21 身長: 1.88 体重: 45 ----------------- 【車の情報】 名前:フェラーリ 色:赤 走行距離:0km ガソリン量:50L ----------------- 【車の所有者の情報】 名前はCharlotte・ Elizabeth・Dianaです 年齢は17歳です 身長は1.66mです 体重は40.0kgです BMIは15です ================= 【自転車の情報】 名前:ビアンキ 色:緑 走行距離:0km ----------------- 【自転車の所有者の情報】 名前はJohn・ Christopher・Smithです 年齢は21歳です 身長は1.88mです 体重は45.0kgです BMIは13です ```
{"metaMigratedAt":"2023-06-15T00:52:17.940Z","metaMigratedFrom":"Content","title":"Java 2","breaks":true,"contributors":"[{\"id\":\"23ed29e7-45bc-4b5c-adb3-451e2ae4f0af\",\"add\":10513,\"del\":4797}]"}
Expand menu