About class division (Java)

Need for class division

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.

  1. Definition of Person class
  2. Move the method of the logic part to the Person class
  3. Changed to call the method of Person class

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;
  }
}

Definition of Person class

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 {
}

Move logic part method to Person class

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);
  }
}

Changed to call method of Person class

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

About class division (Java)
About Java class
About Java StringBuilder class
[Java] About Singleton Class
About Java String class
About java abstract class
About Java class loader types
About Java class variables class methods
About Java interface
Java class methods
[Java] About Java 12 features
[Java] Class inheritance
java Scanner class
Java HashMap class
[Java] About arrays
About class inheritance.
java (abstract class)
Something about java
Where about java
About Java features
[Java] Nested class
Java anonymous class
About Java threads
[Java] About interface
About Java arrays
About java inheritance
About interface, java interface
[java] abstract class
[Java] Object class
Java local class
About List [Java]
About java var
About Java literals
About Java commands
About Java log output
About Java functional interface
Java, about 2D arrays
About the StringBuilder class
About [Java] [StreamAPI] allMatch ()
About Java method binding
[Java] About anonymous classes
About method splitting (Java)
Java inner class review
[Java Silver] About initialization
About Java Array List
About Java Polymorphism super ()
[Swing] About JFrame class
Java class type field
About inheritance (Java Silver)
Java programming (class method)
About Java access modifiers
About Java lambda expressions
About Java entry points
About Java 10 Docker support
Personal summary about Java
About sorting java.util.Arrays class
[Java] About enum type
Java programming (class structure)
All about Java programming
Find out about class methods
[Java] Integer wrapper class reference