About method splitting (Java)

Steps to split the method

I wrote verbose code and the readability deteriorated. I would like to split the method by following the steps below.

  1. Create printData and fullName methods
  2. Describe the contents of the fullName method
  3. Describe the contents of the printData method
  4. Description for calling the printData method

Main.java


import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    
    System.out.print("name:");
    String firstName = scanner.next();
    
    System.out.print("Last name:");
    String lastName = scanner.next();
    
    String name = firstName + " " + lastName;
    
    System.out.print("age:");
    int age = scanner.nextInt();
    
    System.out.print("height(m):");
    double height = scanner.nextDouble();
    
    System.out.print("body weight(kg):");
    double weight = scanner.nextDouble();
    
    System.out.println("Name is" + name + "is");
    System.out.println("Age is" + age + "I'm old");
    if (age >= 20) {
      System.out.println("I'm an adult");
    } else {
      System.out.println("I'm a minor");
    }
    System.out.println("How tall are you"+ height + "m");
    System.out.println("Weight" + weight + "kg");
  }
}

Create printData and fullName methods

First, create the printData and fullName methods. The contents can still be empty.

Main.java


import java.util.Scanner;

class Main {
  public static void main(String[] args) {
       //Omitted because it is long
  }
  public static void printData(String firstName, String lastName, int age, double height, double weight) {
  }
  public static String fullName(String firstName, String lastName) {
  }
}

Describe the contents of the fullName method

Describes the return value of the fullName method. Concatenate firstName and lastName as the return value of the fullName method.

Main.java


import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    
    System.out.print("name:");
    String firstName = scanner.next();
    
    System.out.print("Last name:");
    String lastName = scanner.next();
    
    //It is deleted because it is no longer needed ↓
    //String name = firstName + " " + lastName;
    
    System.out.print("age:");
    int age = scanner.nextInt();
    
    System.out.print("height(m):");
    double height = scanner.nextDouble();
    
    System.out.print("body weight(kg):");
    double weight = scanner.nextDouble();
    
    System.out.println("Name is" + name + "is");
    System.out.println("Age is" + age + "I'm old");
    if (age >= 20) {
      System.out.println("I'm an adult");
    } else {
      System.out.println("I'm a minor");
    }
    System.out.println("How tall are you"+ height + "m");
    System.out.println("Weight" + weight + "kg");
  }
  public static void printData(String firstName, String lastName, int age, double height, double weight) {
  }
  public static String fullName(String firstName, String lastName) {
    //Concatenate firstName and lastName as the return value of the fullName method
    return firstName + " " + lastName;
  }
}

Describe the contents of the printData method

Output the full name using the fullName method. In addition, copy and paste the output such as age from the main method.

Main.java


import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    
    System.out.print("name:");
    String firstName = scanner.next();
    
    System.out.print("Last name:");
    String lastName = scanner.next();
    
    System.out.print("age:");
    int age = scanner.nextInt();
    
    System.out.print("height(m):");
    double height = scanner.nextDouble();
    
    System.out.print("body weight(kg):");
    double weight = scanner.nextDouble();
    
    //It is deleted because it is no longer needed ↓

    // System.out.println("Name is" + name + "is");
    // System.out.println("Age is" + age + "I'm old");
    // if (age >= 20) {
    //  System.out.println("I'm an adult");
    // } else {
    //  System.out.println("I'm a minor");
    // }
    // System.out.println("How tall are you"+ height + "m");
    // System.out.println("Weight" + weight + "kg");
  }

  public static void printData(String firstName, String lastName, int age, double height, double weight) {
    
    //Call the fullName method and output
    System.out.println("Name is" + fullName(firstName, lastName) + "is");

    //Output age(Copy and paste from the main method)
    System.out.println("Age is" + age + "I'm old");

    //Conditional branching of minors(Copy and paste from the main method)
    if (age >= 20) {
      System.out.println("I'm an adult");
    } else {
      System.out.println("I'm a minor");
    }

    //Output height and weight(Copy and paste from the main method)
    System.out.println("How tall are you" + height + "m");
    System.out.println("Weight" + weight + "kg");
  }

  public static String fullName(String firstName, String lastName) {
    return firstName + " " + lastName;
  }
}

Description for calling the printData method

Finally, add a description to call the printData method to the main method.

Main.java


import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    
    System.out.print("name:");
    String firstName = scanner.next();
    
    System.out.print("Last name:");
    String lastName = scanner.next();
    
    System.out.print("age:");
    int age = scanner.nextInt();
    
    System.out.print("height(m):");
    double height = scanner.nextDouble();
    
    System.out.print("body weight(kg):");
    double weight = scanner.nextDouble();

    //Call the printData method
    printData(firstName, lastName, age, height, weight);
  }

  public static void printData(String firstName, String lastName, int age, double height, double weight) {
    System.out.println("Name is" + fullName(firstName, lastName) + "is");
    System.out.println("Age is" + age + "I'm old");
    if (age >= 20) {
      System.out.println("I'm an adult");
    } else {
      System.out.println("I'm a minor");
    }
    System.out.println("How tall are you" + height + "m");
    System.out.println("Weight" + weight + "kg");
  }

  public static String fullName(String firstName, String lastName) {
    return firstName + " " + lastName;
  }
}

I think this has improved readability.

Recommended Posts

About method splitting (Java)
About Java method binding
java (method)
[Java Silver] About equals method
Java method
[Java] method
[Java] method
About Java interface
[Java] About Java 12 features
[Java] About arrays
Java8 method reference
Something about java
Where about java
About Java features
About the method
[Java] forEach method
About Java threads
[Java] About interface
About Java class
About Java arrays
About java inheritance
About interface, java interface
java8 method reference
[Java] Random method
[Java] split method
About List [Java]
About java var
About Java literals
About Java commands
About Java log output
Output about the method # 2
About Java functional interface
About No Method Error
Java, about 2D arrays
About class division (Java)
JAVA DB connection method
About [Java] [StreamAPI] allMatch ()
About Java StringBuilder class
Java learning 2 (learning calculation method)
[Java] About Singleton Class
Java learning memo (method)
[Java ~ Method ~] Study memo (5)
[Java] About anonymous classes
Studying Java 8 (see method)
[Java Silver] About initialization
About the length method
About Java Array List
About Java Polymorphism super ()
About inheritance (Java Silver)
Java programming (class method)
About Java String class
About Java access modifiers
About Java lambda expressions
About the authenticate method.
About Java entry points
About Java 10 Docker support
Personal summary about Java
About the map method
[Java] About enum type
About the ancestors method
All about Java programming