If you write redundant code like the following, readability will deteriorate. This is because the execution part and the logic part are described in the same file. In this article, the procedure to divide the execution part into the Main class and the logic part into the person class is described in the following order.
Main.java
public class Main {
public static void main(String[] args) {
printData(fullName("Kate", "Jones"), 27, 1.6, 50.0);
printData(fullName("John", "Christopher", "Smith"), 65, 1.75, 80.0);
}
public static void printData(String name, int age, double height, double weight) {
System.out.println("my name is" + name + "is");
System.out.println("Age is" + age + "I'm old");
System.out.println("How tall are you" + height + "m");
System.out.println("Weight" + weight + "kg");
double bmi = bmi(height, weight);
System.out.println("BMI" + bmi + "is");
if (isHealthy(bmi)) {
System.out.println("Standard value");
} else {
System.out.println("Out of standard value range");
}
}
public static String fullName(String firstName, String lastName) {
return firstName + " " + lastName;
}
public static String fullName(String firstName, String middleName, String lastName) {
return firstName + " " + middleName + " " + lastName;
}
public static double bmi(double height, double weight) {
return weight / height / height;
}
public static boolean isHealthy(double bmi) {
return bmi >= 18.5 && bmi < 25.0;
}
}
The above Main.java is described with the following configuration.
--Execution part --main method --Logic part --printData method --fullName method --bmi method --isHealthy method
First, create Person.java and define the Person class
Person.java
class Person {
}
Next, move the logic part of the main class to the Person class.
--Logic part --printData method --fullName method --bmi method --isHealthy method
Person.java
class Person {
public static void printData(String name, int age, double height, double weight) {
System.out.println("my name is" + name + "is");
System.out.println("Age is" + age + "I'm old");
System.out.println("How tall are you" + height + "m");
System.out.println("Weight" + weight + "kg");
double bmi = bmi(height, weight);
System.out.println("BMI" + bmi + "is");
if (isHealthy(bmi)) {
System.out.println("Standard value");
} else {
System.out.println("Out of standard value range");
}
}
public static String fullName(String firstName, String lastName) {
return firstName + " " + lastName;
}
public static String fullName(String firstName, String middleName, String lastName) {
return firstName + " " + middleName + " " + lastName;
}
public static double bmi(double height, double weight) {
return weight / height / height;
}
public static boolean isHealthy(double bmi) {
return bmi >= 18.5 && bmi < 25.0;
}
}
Main.java
public class Main {
public static void main(String[] args) {
printData(fullName("Kate", "Jones"), 27, 1.6, 50.0);
printData(fullName("John", "Christopher", "Smith"), 65, 1.75, 80.0);
}
}
I was able to divide it, but if I execute the Main class as it is, an error will occur. Finally, add the following description to the Main class to call the method of the Person class.
Main.java
public class Main {
public static void main(String[] args) {
Person.printData(Person.fullName("Kate", "Jones"), 27, 1.6, 50.0);
Person.printData(Person.fullName("John", "Christopher", "Smith"), 65, 1.75, 80.0);
}
}
Now you can run it without any problems.
Recommended Posts