I first touched Java ②

I touched Java.

Please forgive it as it is a self-sufficient memorandum.

Basic structure of the program

There are only three movements in the program. ① Progress ② Conditional branch ③ Repeat

Sequential progress

Greeting.java


class Greeting {
  public static void main(String args[]) {
    System.out.println("Good morning");
    System.out.println("Good afternoon");
    System.out.println("Good evening");
  }
}

Terminal


$ javac Greeting.java
$ java Greeting

Good morning
Good afternoon
Good evening

It seems that javac Greeting.java creates a folder called Greeting.class.

Array

Array.java


class Array {
  public static void main(String[] args) {
    String[] arr = {"sato", "suzuki", "takahashi"};    

    System.out.println(arr[0]);
    System.out.println(arr[1]);
    System.out.println(arr[2]);
  }
}

Terminal


sato
suzuki
takahashi

repetition

For.java


class For {
  public static void main(String[] args) {

    for (int i = 0; i <= 2; i++) {
      for (int j = 0; j <= 2; j++) {
        System.out.println(i + "-" + j);
      }
    }
  }
}

Terminal


0-0
0-1
0-2
1-0
1-1
1-2
2-0
2-1
2-2

Since it is nested, it will be processed like this.

Practice

Let's create a function to calculate the average salary of a salaryman for 5 months and judge whether it is amazing or not.

Lesson.java


class Office_worker {
  String name;

  public int calculateAVG(int[] data) {
    int sum = 0;
    for (int i = 0; i < data.length; i++) {
      sum += data[i];
    }
    int avg = sum / data.length;
    return avg;
  }

  public String judge(double avg) {
    String result;
    if (avg >= 250000) {
      result = "Wow!";
    } else {
      result = "not yet!";
    }
    return result;
  }
}

public class Lesson {
  public static void main(String[] args) {
    Office_worker a001 = new Office_worker();
    a001.name = "sato";
    int[] data = {270000, 265000, 250000, 290000, 230000};

    int avg = a001.calculateAVG(data);
    String result = a001.judge(avg);

    System.out.println(avg);
    System.out.println(a001.name + "The average salary is" + result);
  }
}

Terminal


261000
The average salary of sato is amazing!

Recommended Posts

I first touched Java ②
I first touched Java ③
I first touched Java ④
I first touched Java
I touched Scala
First gradle build (Java)
I touched Scala ~ [Class] ~
I touched on the new features of Java 15
I touched Scala ~ [Object] ~
I touched Scala ~ [Trait] ~
After all, if you learn first, I think Java
What I researched about Java 8
I started Java Gold (Chapter 1-1)
What I researched about Java 6
I made roulette in Java.
What I researched about Java 9
I investigated Java primitive types
I touched Scala ~ [Control syntax] ~
I took Java SE8 Gold.
I tried Drools (Java, InputStream)
What I researched about Java 7
I tried using Java REPL
[Java] I implemented the combination.
First Java development in Eclipse
Java concurrency I don't understand
I studied the constructor (java)
I tried metaprogramming in Java
What I researched about Java 5
JAVA (First step: Git Bush edition)
I sent an email in Java
I compared PHP and Java constructors
I created a PDF in Java.
I made a shopify app @java
I checked Java Flight Recorder (JFR)
I fell into Java Silver (crying)
I tried to interact with Java
I tried UDP communication with Java
I wrote Goldbach's theorem in java
I tried the Java framework "Quarkus"
Java
I tried using Java8 Stream API
What I learned with Java Gold
I made an annotation in Java.
I tried using JWT in Java
I tried to summarize Java learning (1)
Java
What I learned with Java Silver
What I researched about Java learning
I tried to summarize Java 8 now
<First post> I started studying Ruby
I tried using Java memo LocalDate
I compared Java and Ruby's FizzBuzz.
I tried using GoogleHttpClient of Java
I touched Tribuo published by Oracle. Document Tribuo --A Java prediction library (v4.0)
[Java] I participated in ABC-188 of Atcorder.
Introduction to java for the first time # 2
First steps for deep learning in Java
Java for All! I read everyone's Java #minjava
I tried Cassandra's Object Mapper for Java
I tried to summarize Java lambda expressions
Java9 was included, so I tried jshell.