I wrote verbose code and the readability deteriorated. I would like to split the method by following the steps below.
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");
}
}
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) {
}
}
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;
}
}
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;
}
}
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