[Java ~ Creating a self-introduction program while reviewing the past) ~] Study memo (7)

Learning Java. A comprehensive review of what you have studied so far. It's almost my study memo. Please do not excessive expectations.

Past posts

[Java ~ Variable definition, type conversion ~] Study memo [Java ~ False Value ~] Study Memo (2) [Java ~ Conditional branching / Iterative processing ~] Study memo (3) [Java ~ Array ~] Study memo (4) [Java ~ Method ~] Study Memo (5) [Java ~ Class ~] Study Memo (6)

Creating a self-introduction program

--Simply display a character string

class Main {
  public static void main(String[] args) {
    System.out.println("The name is Kate Jones");
  }
}

-Output the contents of () by writing System.out.println (). -Enclose the character string in double quotation marks (").

--Receive input from console

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.println("Name is"+ lastName + firstName +"is");
  }
}

-Import an external library called Scanner to receive input to the console. Also, Scanner is read by "import java.util.Scanner".

--Conditional branching and receiving numbers from the console

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.println("age:");
    int age = scanner.nextInt();
    
    System.out.println("Name is"+ lastName + firstName +"is");
    System.out.println("Age is"+ age  +"I'm old");
    if(age >= 20){
      System.out.print("I'm an adult");
    }else{
      System.out.print("I'm a minor");
    }
  }
}

・ Receive the numerical value entered in the console Receive the entered value with scanner.nextInt (). Other usage of Scanner is the same as the method of receiving a character string.

・ How to write conditional branch

if(Conditional expression){
  //Processing content ①;
}else{
  //Processing content ②;
}

If the conditional expression is true, the processing content (1) in {} is performed, and if the conditional expression is false, the processing content (2) is performed.

--Method

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

· Receive decimals entered in the console Reading minority values ​​⇨ double d = scanner.nextDouble (); Receive the input decimal with scanner.nextDouble (). Other usage of Scanner is the same as the method of receiving character strings and numerical values. double is a data type that represents a decimal.

-Method definition and call Method definition is ⇨ public static void Method name () {Process to be executed} To call the method ⇨ Method name ();

-Method definition and call using arguments Method definition is ⇨ public static void method name (data type variable name) {process to be executed} To call a method ⇨ Method name (argument); Also, in order to receive multiple arguments, define them separated by commas (,).

-Method with return value Method definition is ⇨ public static Return data type Method name (argument) {return Return value}

--Class

.java: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 a method of another class
    Person.printData(firstName, lastName, age, height, weight);
  }
}

.java:Another.java


//Import external library
import java.lang.Math;

class Another{
  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");
    //Call the bmi method and assign it to a variable
    double bmi = bmi(height, weight);
    System.out.println("BMI"+ Math.round(bmi) +"is");
  }
  public static String fullName(String firstName, String lastName) {
    return firstName + " " + lastName;
  }
  //Define bmi method
  public static double bmi(double height, double weight) {
    return weight/height/height;
  }
}

・ Read the class (library) Described as "import java.lang. Class name" above the class definition.

・ Class definition class class name {define method here} The file name must be "class name.java".

-Call a method of another class You can call a method of another class by using class name.method name ().

-Method for rounding Math.round (number);

--Return value of boolean value

.java:Another.java


import java.lang.Math;

class Another{
  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");
    double bmi = bmi(height, weight);
    System.out.println("BMI"+ Math.round(bmi) +"is");
    //Create a conditional branch to determine if you are healthy
    if(isHealthy(bmi)){
      System.out.println("I'm healthy");
    }else{
      System.out.println("Not healthy");
    }
  }
  public static String fullName(String firstName, String lastName) {
    return firstName + " " + lastName;
  }
  public static double bmi(double height, double weight) {
    return weight/height/height;
  }
  //Define isHealthy method
  public static boolean isHealthy(double bmi) {
    return bmi>=18.5 && bmi < 25;
  }
}

-The boolean data type is boolean type. ・ Logical operators "Katsu" of 18.5 or more and less than 25.0 is expressed by &&, and "Condition 1 && Condition 2" is If "Condition 1 is true and Condition 2 is also true", the result will be true, and either one may be false. The result will be false.

By the way, "or" is| |Expressed as "Condition 1| |Condition 2 "is If "either condition 1 or condition 2 is true", the result is true.

Recommended Posts

[Java ~ Creating a self-introduction program while reviewing the past) ~] Study memo (7)
[Java ~ Method ~] Study memo (5)
[Java ~ Array ~] Study memo 4
Using the database (SQL Server 2014) from a Java program 2018/01/04
[Java ~ Boolean value ~] Study memo (2)
Create a java method [Memo] [java11]
Java study memo 2 with Progate
Java learning memo (creating an array)
[Study session memo] Java Day Tokyo 2017
Java learning memo (while statement, do-while statement)
Memo: [Java] If a file is in the monitored directory, process it.
Sample program that returns the hash value of a file in Java