Summary of what you need to study Java
■ 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)
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) {
}
}
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
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)
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);
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);
}
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
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);
}
}
}
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;
}
}
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)
}
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