Java to be involved from today

Summary of what you need to study Java

Eclipse shortcut keys

■ Completion (Ctrl + Space) (sysout) System.out.println(); (syso) System.out.println(); (main) public static void main(String[] args) {}

■ Declare a local variable (Ctrl + 2 → L) new ArrayList(); → ArrayList arrayList = new ArrayList();

■ Other shortcuts Source code format (Ctrl + Shift + F) Comment out (Ctrl + /) Quick fix (Ctrl + 1) Move 1 line, copy 1 line (Alt + ↓ ↑, Ctrl + Alt + ↓ ↑) Delete one line (Ctrl + D) Method extraction (Alt + Shift + M) Open call hierarchy (Ctrl + Alt + H) Open Declaration (Ctrl + Click) Open type (Ctrl + Shift + T)

How to write a program

A refreshing summary for myself after reading an introduction to Java https://sukkiri.jp/books/sukkiri_java3

▼ Class block + main block

Main.java


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

	}
}

Description in the main block

Main.java


System.out.println("Hello");「Hello」と表示させる
System.out.println("Double quotes (\")");"double quotes(") ”Is displayed

int age;//Variable declaration (prepare a box called age)
age = 20;//Variable age assignment 20
int age = 20;//Initialize variable age with 20
age = 32;//Reassign to variable age
final double Pi = 3.14;//Declare pi as a constant

//: Data type (integer)
byte glasses = 2;//Number of glasses you have
short age = 20;//age
int salary = 180000;//Salary amount
long worldPeople = 6900000000L;//World population
//Data type (decimal)
float weight = 56;//body weight
double pi = 3.14;//Pi
//Data type (true / false value)
boolean isError = true;//true or false
//Data type (character, string)
char initial = F;//1 initial letter
String name = Haru;//name

int m = Math.max(①,②);//Compare the numbers and substitute the larger one
int n = Interger.parseInt(①);Convert strings to numbers
int r = new java.util.Random().nextInt(①);//Generate random numbers (up to ①)
String s = new java.util.Scanner(System.in).nextLine();Enter characters from the keyboard
int i = new java.util.Scanner(System.in).nextInt();Integer input from keyboard

Branch

Main.java


//if
if (tenki == true) {
	System.out.println("It's sunny");
}else {
	System.out.println("It's rain");
}

//switch
int lucky = new java.util.Random().nextInt(3)+1;
System.out.println(lucky);
switch (lucky) {
case 1:
	System.out.println("I'm Daikichi");
	break;
case 2:
	System.out.println("I'm good");
	break;
case 3:
	System.out.println("It ’s bad.");
}

//Break statement for iterative processing
//Suspend only this time and continue to the next week

//Logical operator&&(And),||(Or)

repetition

Main.java


for (int i = 0; i < 10; i++) {
block
}

while (i < 100) {
block
}

do {
	if (i % 3 == 0) {
		System.out.println(i);
	}
	i++;
} while (i < 100);

Array

Main.java


int[] score;//Prepare an array variable score to which int type elements can be assigned
score = new int[5];Create 5 int type elements and assign them to score

score.length//Get the number of elements in an array

int[] scores1 = new int[] {1,2,3};//Abbreviation 1
int[] scores2 = {1,2,3};//Abbreviation 2

for (Element type variable name:Array variable name){//Extended for statement
}

//Example
int[] moneyList = {1,2,3};
for(int i=0; i < moneyList.length; i++) {
	System.out.println(moneyList[i]);
}
for(int m : moneyList) {
	System.out.println(m);
}

Method

Main.java


//Method definition
public static return type method name(Argument list) {
Specific action to be taken when the method is called
}

public static void hello() {//hello method, void is used when there is no return value
	System.out.println("Hello");
}
hello();//Call the hello method

//Example
public static void main(String[] args) {
	introduceOneself();
}
public static void introduceOneself() {
	String name = "Haru";
	int age = 6;
	double height = 110.5;
	char zodiac = 'Horse';
	System.out.println(name);
	System.out.println(age);
	System.out.println(height);
	System.out.println(zodiac);
}

//Example (overload)
public static void main(String[] args) {
	String address = "[email protected]";
	String text = "Email body";
	email(address,text);
}
public static void email(String address, String text) {
	System.out.println(address + "I sent the following email to");
	System.out.println("Subject: Untitled");
	System.out.println("Text:" + text);
}
public static void email(String title, String address, String text) {
	System.out.println(address + "I sent the following email to");
	System.out.println("subject:" + title);
	System.out.println("Text:" + text);
}

//Example (triangle area)
public static void main(String[] args) {
	double triangleArea = calcTriangleArea(10.0,5.0);
	System.out.println(triangleArea);
}
public static double calcTriangleArea(double bottom, double height) {
	double area = bottom * height / 2;
	return area;
}

▼ Reference: How to use the method https://www.javadrive.jp/start/method/index1.html

Development using multiple classes

Calc.java


public class Calc {
	public static void main(String[] args) {
		int a = 10; int b = 2;
		int total = CalcLogic.tasu(a, b);
		int delta = CalcLogic.hiku(a, b);
		System.out.println("When added" + total + ", Pull" + delta);
	}
}

CalcLogic.java


public class CalcLogic {
	public static void main(String[] args) {
		public static int tasu(int a, int b) {
			return (a + b);
		}
		public static int hiku(int a, int b) {
			return (a - b);
		}
	}
}

Instances and classes

Object-orientation

Main.java


public class Main {
	public static void main(String[] args) {
		Cleric c = new Cleric();
		c.name = "clergyman";
		c.SelfAid();
		c.pray(3);
	}
}

Cleric.java


import java.util.Random;

public class Cleric {
	String name;
	int hp = 50;
	final int MAX_HP = 50;
	int mp = 10;
	final int MAX_MP = 10;

	public void SelfAid() {
		System.out.println(this.name + "Chanted self-aid");
		this.hp = this.MAX_HP;
		this.mp -= 5;
		System.out.println("HP recovered to maximum");
	}

	public int pray(int sec) {
		System.out.println(this.name + "Is" + sec + "I prayed to heaven for a second!");

		int recover = new Random().nextInt(3) + sec;

		int recoverActual = Math.min(this.MAX_MP - this.mp, recover);

		this.mp += recoverActual;
		System.out.println("MP recovered" + recoverActual);
		return recoverActual;
	}
}

Various class mechanisms

Constructor conditions -Method name is exactly equal to class name -The return value is not described in the method declaration (void is also useless)

public class class name{
name of the class() {
Process to be executed automatically
	}
}

Special case for constructor A "no arguments, no processing" constructor is automatically added at compile time only if no constructor is defined in the class.

Thief.java


public class Thief {
	String name;
	int hp;
	int mp;

	//new Thief("Haruta", 40, 5)Description when instantiating with
	public Thief(String name, int hp, int mp) {
		this.name = name;
		this.hp = hp;
		this.mp = mp;
	}
	//new Thief("Haruta", 40)Description when instantiating with (MP is initialized with 5)
	public Thief(String name, int hp) {
		this(name, hp, 5);
	}
	//new Thief("Haruta")Description when instantiating with (HP is 40,MP is initialized with 5)
	public Thief(String name) {
		this(name, 40);
	}

	//new Thief()Make it impossible to instantiate (no unnamed Thief exists)
}

A program that uses the above Thief class

Main.java


public class Main {
	public static void heal(int hp) {
		hp += 10;
	}
	public static void heal(Thief thief) {
		thief.hp += 10;
	}
	public static void main(String[] args) {
		int baseHp  = 25;
		Thief t = new Thief("Haruta", baseHp);
		System.out.println(baseHp + ":" + t.hp);
		heal(baseHp);
		heal(t);
		System.out.println(baseHp + ":" + t.hp);
	}

	//Execution result
//25:25 (Because if the argument is an int type, the value of the variable baseHp is copied to the argument hp
//25:35 (If the argument is a class type, the address indicated by the variable t is copied to the argument thief by reference..hp and thief.Because hp will point to the same location in memory)
}

Inheritance

Matango.java


//Create a class of mushroom monsters
public class Matango {
	int hp = 50;
	char suffix;
	public Matango(char suffix) {
		this.suffix = suffix;
	}
	public void attack(Hero h) {
		System.out.println("Mushroom attack" + this.suffix);
		System.out.println("10 damage");
		h.hp -= 10;
	}
}

PoisonMatango.java


public class PoisonMatango extends Matango {
	int poisonCount = 5;
	public PoisonMatango(char suffix) {
		super(suffix);
	}
	public void attack(Hero h) {
		super.attack(h);
		if (this.poisonCount > 0) {
			System.out.println("Further scattered spores");
			int dmg = h.hp / 5;
			h.hp -= dmg;
			System.out.println(dmg + "Point damage");
			this.poisonCount--;
		}
	}
}

Hero.java


public class Hero {
	String name;
	int hp = 150;
	public Hero(String name) {
		this.name = name;
		System.out.println("Name is" + this.name + ", Physical strength" + this.hp);
	}
}

Main.java


public class Main {
	public static void main(String[] args) {
		Hero h = new Hero("Hirota");
		Hero h2 = new Hero("Hirota 2");

		Matango m1 = new Matango('A');
		m1.appear();
		m1.attack(h);

		PoisonMatango pm1 = new PoisonMatango('A');
		pm1.appear();
		pm1.attack(h2);
	}
}

Recommended Posts

Java to be involved from today
Changes from Java 8 to Java 11
Sum from Java_1 to 100
From Java to Ruby !!
Minecraft BE server development from PHP to Java
Migration from Cobol to JAVA
New features from Java7 to Java8
Connect from Java to PostgreSQL
From Ineffective Java to Effective Java
From Java to VB.NET-Writing Contrast Memo-
Java, interface to start from beginner
The road from JavaScript to Java
[Java] Conversion from array to List
Convert from java UTC time to JST time
Connect from Java to MySQL using Eclipse
From installing Eclipse to executing Java (PHP)
Post to Slack from Play Framework 2.8 (Java)
Java: How to send values from Servlet to Servlet
[Java] Flow from source code to execution
Introduction to monitoring from Java Touching Prometheus
Precautions when migrating from VB6.0 to JAVA
Memo for migration from java to kotlin
Type conversion from java BigDecimal type to String type
[Java] Introduction to Java
Introduction to java
[Java] From two Lists to one array list
Upsert from Java SDK to Azure Cosmos DB
Run R from Java I want to run rJava
Connect to Aurora (MySQL) from a Java application
To become a VB.net programmer from a Java shop
CORBA seems to be removed in Java SE 11. .. ..
Migrate from Java to Server Side Kotlin + Spring-boot
How to get Class from Element in Java
There seems to be no else-if in java
[Java] How to switch from open jdk to oracle jdk
I want to write quickly from java to sqlite
[Java] char type can be cast to int type
Kotlin's reified function cannot be called from java.
Select * from Java SDK to Azure Cosmos DB
Launch Docker from Java to convert Office documents to PDF
Call Java from JRuby
Convert Java enum enums and JSON to and from Jackson
Tomcat cannot be started due to java version change
[Java] I want to calculate the difference from the date
How to jump from Eclipse Java to a SQL file
How to write Scala from the perspective of Java
Migrate from JUnit 4 to JUnit 5
[Java] How to extract the file name from the path
Eval Java source from Java
[Java] Connect to MySQL
Access API.AI from Java
Kotlin's improvements to Java
Java development for beginners to start from 1-Vol.1-eclipse setup
How to change from Oracle Java 8 to Adopt Open JDK 9
[Java] How to erase a specific character from a character string
Introduction to java command
Generate models from JSON to Swift, PHP, C #, JAVA
[Java] Platforms to choose from for Java development starting now (2020)
[Java] Things to be aware of when outputting FizzBuzz
Java to fly data from Android to ROS of Jetson Nano
To you who suffer from unexpected decimal points in Java