java learning notes 10

Keywords: Java

java learning notes 10

Knowledge points

1. Get string length

String name. length();

2. User defined shortcut input

In IDEA, click File → Settings → Editor → Live Templates
For example:

As long as you enter buff, you will directly enter BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

As long as ssrl is entered, string s $END $= reader. Readline(), And the input cursor stays at $END $.

3. Process of class creation

Classes are basically created according to the following process:

  1. The programmer determines what other objects are needed.

  2. Programmers divide objects into different types according to the operations they want to perform.

  3. Programmers write separate classes for each type.

  4. In a class, the programmer declares the required methods and variables.

  5. In each method, the programmer writes commands to make the method perform the required operations.

  6. Class completed. Now you can create objects of this class.

task

Course 1 Comparison and setting conditions

Task 1 the minimum of the two numbers

package zh.codegym.task.task04.task0418;

/* 
Minimum of two numbers
 Use the keyboard to enter two integers and display the minimum value. If the two numbers are equal, either number is displayed.
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String sA = reader.readLine();
        String sB = reader.readLine();
        int a = Integer.parseInt(sA);
        int b = Integer.parseInt(sB);
        if(a < b){
            System.out.println(a);
        }else{
            System.out.print(b);
        }
        //Write your code here
    }
}

Task 2 maximum of four numbers

package zh.codegym.task.task04.task0419;

/* 
Maximum of four numbers
 Use the keyboard to enter four numbers and display the maximum value. If the maximum value occurs more than once, it is displayed only once.
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String sA = reader.readLine();
        String sB = reader.readLine();
        String sC = reader.readLine();
        String sD = reader.readLine();
        int a = Integer.parseInt(sA);
        int b = Integer.parseInt(sB);
        int c = Integer.parseInt(sC);
        int d = Integer.parseInt(sD);
        int max = 0 ;
        if (a > b) {
            max = a;
        } else {
            max = b;
        }
        if (c > max) {
            max = c;
        }
        if (d > max) {
            max = d;
        }
        System.out.println(max);
        //Write your code here
    }
}

Task 3 sort the three numbers

Use the keyboard to enter three numbers and display them in descending order.
The numbers displayed must be separated by spaces.

package zh.codegym.task.task04.task0420;

/* 
Sort three numbers
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String sA = reader.readLine();
        String sB = reader.readLine();
        String sC = reader.readLine();
        int a = Integer.parseInt(sA);
        int b = Integer.parseInt(sB);
        int c = Integer.parseInt(sC);
        int tmp = 0;
        if (a > b) {
            if (c > a) {
                tmp = a;
                a = c;
                c = tmp;
                tmp = b;
                b = c;
                c = tmp;
            } else if (c > b) {
                tmp = b;
                b = c;
                c = tmp;
            }
        } else {
            tmp = b;
            b = a;
            a = tmp;
            if (c > a) {
                tmp = a;
                a = c;
                c = tmp;
                tmp = b;
                b = c;
                c = tmp;
            } else if (c > b) {
                tmp = b;
                b = c;
                c = tmp;
            }
        }
        System.out.println(a + " " + b + " " + c);
    }
}

Task 4 Jane or Jane?

Use the keyboard to enter two names. If the two names are the same, "same name" is displayed.
If the two names are different but they are the same length, "same name length" is displayed.
If both names and names are not the same length, nothing is displayed.

package zh.codegym.task.task04.task0421;

/* 
Jane or Jane?
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String sName1 = reader.readLine();
        String sName2 = reader.readLine();
        if (sName1.equals(sName2)) {
            System.out.println("Same name");
        } else if (sName1.length() == sName2.length()) {
            System.out.println("Same name length");
        }
        //Write your code here
    }
}

Task 5 over 18

Use the keyboard to enter your name and age. If the age is less than 18, "grow up a little more" is displayed.

package zh.codegym.task.task04.task0422;

/* 
18 Over years old
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String name = reader.readLine();
        String sAge = reader.readLine();
        int age = Integer.parseInt(sAge);
        if (age < 18) {
            System.out.println("Grow up a little more");
        }
        //Write your code here
    }
}

Task 6 Security Policy

package zh.codegym.task.task04.task0423;

/* 
Security policy
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String name = reader.readLine();
        String sAge = reader.readLine();
        int age = Integer.parseInt(sAge);
        if (age > 20) {
            System.out.println("18 Years old is enough");
            //Write your code here
        }
    }
}

Task 7 three numbers

Use the keyboard to enter three integers. One of the numbers is unique. The other two numbers are the same. Displays the sequence number different from the other two numbers.

package zh.codegym.task.task04.task0424;

/* 
Three numbers
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String sA = reader.readLine();
        String sB = reader.readLine();
        String sC = reader.readLine();
        int a = Integer.parseInt(sA);
        int b = Integer.parseInt(sB);
        int c = Integer.parseInt(sC);
        if (a == b && b != c) {
            System.out.println(3);
        } else if (a == c && b != c) {
            System.out.println(2);
        } else if (b == c && a != b) {
            System.out.println(1);
        }
        //Write your code here
    }
}

Task 8 target locked!

Use the keyboard to enter two integers to represent the coordinates of points not on the axis.

Displays the number of quadrants that contain a given point.

package zh.codegym.task.task04.task0425;

/* 
Target locked!
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String sX = reader.readLine();
        String sY = reader.readLine();
        int x = Integer.parseInt(sX);
        int y = Integer.parseInt(sY);
        if (x > 0 && y > 0) {
            System.out.println(1);
        } else if (x < 0 && y > 0) {
            System.out.println(2);
        } else if (x < 0 && y < 0) {
            System.out.println(3);
        } else if (x > 0 && y < 0){
            System.out.println(4);
        }
        //Write your code here
    }
}

Course 2 boolean type

Task 1 labels and numbers

Use the keyboard to enter an integer. The string description is displayed as follows:

"Negative even" - if the number is both negative and even,

"Negative odd" - if the number is both negative and odd,

"Zero" - if the number is 0,

"Positive even" - if the number is both positive and even,

Positive odd - if the number is both positive and odd.

package zh.codegym.task.task04.task0426;

/* 
Labels and numbers
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String sA = reader.readLine();
        int a = Integer.parseInt(sA);
        if (a == 0) {
            System.out.println("Fatal Frame");
        } else if (a > 0 && a % 2 == 0) {
            System.out.println("Positive even number");
        } else if (a > 0 && a % 2 == 1) {
            System.out.println("Positive odd number");
        } else if (a < 0 && a % 2 == 0) {
            System.out.println("Negative even number");
        } else {
            System.out.println("Negative odd number");
        }
        //Write your code here
    }
}

Task 2 description number

Enter an integer between 1 and 999 from the keyboard. The string description is displayed as follows:

"One digit even" - if the number is even and contains one digit,

"One digit odd" - if the number is odd and contains one digit,

"Two digit even" - if the number is even and contains two digits,

"Two digit odd" - if the number is odd and contains two digits,

"Three digit even" - if the number is even and contains three digits,

"Three digit odd" - if the number is odd and contains three digits.

If the number entered is not between 1 - 999, nothing is displayed.

package zh.codegym.task.task04.task0427;

/* 
Description number
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String sNum = reader.readLine();
        int num = Integer.parseInt(sNum);
        if ((num > 0 && num < 10) && num % 2 == 0) {
            System.out.println("One even number");
        } else if ((num > 0 && num < 10) && num % 2 == 1) {
            System.out.println("One odd number");
        } else if ((num >= 10 && num < 100) && num % 2 == 0) {
            System.out.println("Two digit even number");
        } else if ((num >= 10 && num < 100) && num % 2 == 1) {
            System.out.println("Two digit odd number");
        } else if ((num >= 100 && num < 1000) && num % 2 == 0) {
            System.out.println("Three digit even number");
        } else if ((num >= 100 && num < 1000) && num % 2 == 1) {
            System.out.println("Three digit odd number");
        }

    }
}

Task 3 positive

Use the keyboard to enter three integers. Displays the number of positive numbers in the original set.

package zh.codegym.task.task04.task0428;

/* 
Positive number
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String sA = reader.readLine();
        String sB = reader.readLine();
        String sC = reader.readLine();
        int a = Integer.parseInt(sA);
        int b = Integer.parseInt(sB);
        int c = Integer.parseInt(sC);
        if (a > 0 && (b > 0 && c > 0)) {
            System.out.println(3);
        } else if ((a > 0 && (b > 0 || c > 0)) || (b > 0 && c > 0)) {
            System.out.println(2);
        } else if ((a > 0 || b > 0) || c > 0) {
            System.out.println(1);
        } else {
            System.out.println(0);
        }
    }
}

Task 4 positive and negative numbers

Use the keyboard to enter three integers. Displays the number of positive and negative numbers in the original set. The format is as follows:

"Negative number: a" and "positive number: b", where a and b are unknown.

package zh.codegym.task.task04.task0429;

/* 
Positive and negative numbers
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String sA = reader.readLine();
        String sB = reader.readLine();
        String sC = reader.readLine();
        int a = Integer.parseInt(sA);
        int b = Integer.parseInt(sB);
        int c = Integer.parseInt(sC);
        int pos = 0;
        int neg = 0;
        if (a > 0) {
            pos++;
        } else if (a < 0) {
            neg++;
        }
        if (b > 0) {
            pos++;
        } else if (b < 0) {
            neg++;
        }
        if (c > 0) {
            pos++;
        } else if (c < 0) {
            neg++;
        }
        System.out.println("Number of negative numbers:" + neg);
        System.out.println("Positive number:" + pos);

    }
}

Lesson 3 while loop

Tasks 1 to 10

Use the while loop to display numbers from 1 to 10. Each row displays a value.

package zh.codegym.task.task04.task0430;

/* 
1 To 10
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        int i =1;
        while(i <= 10){
            System.out.println(i++);
        }
        //Write your code here

    }
}

Task 2 from 10 to 1

Use the while loop to display numbers from 10 to 1. Each row displays a value.

package zh.codegym.task.task04.task0431;

/* 
From 10 to 1
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        int i = 10;
        while (i > 0) {
            System.out.println(i--);

        }
    }
}

Task 3 you can't ask for good things

Use the keyboard to enter a string and a number N greater than 0.

Use the while loop to display the string N times. Each row displays a value.

package zh.codegym.task.task04.task0432;



/* 
You can't ask for good things
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String s = reader.readLine();
        String sNum = reader.readLine();
        int num = Integer.parseInt(sNum);
        int i = 1;
        while (i <= num) {
            System.out.println(s);
            i++;
        }
    }
}

Task 4 look at the dollars you will have in the future

Use the while loop to display the 10x10 square of the dollar sign.

Do not separate symbols in each line.

package zh.codegym.task.task04.task0433;


/* 
Look at the dollars you will have in the future
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        int i = 1;
        while (i <= 10) {
            System.out.println("$$$$$$$$$$");
            i++;
        }

    }

}

Task 5 multiplication table

Use the while loop to display the 10x10 multiplication table.

Use spaces to separate numbers.

package zh.codegym.task.task04.task0434;


/* 
multiplication table
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        int i = 1;
        while (i <= 10) {
            int j = 1;
            while (j <= 10) {
                System.out.print(i * j + " ");
                j++;
            }
            System.out.println();
            i++;
        }

    }
}

Lesson 4 for loop

Task 1 even

Use the for loop to display even numbers between 1 and 100 inclusive.

Each row displays a value.

package zh.codegym.task.task04.task0435;

/* 
even numbers
*/

public class Solution {
    public static void main(String[] args) throws Exception {
        for (int i = 1; i <= 100; i++) {
            if (i % 2 == 0) {
                System.out.println(i);
            }
        }

    }
}

Task 2 draw a rectangle

Use the keyboard to enter two numbers m and n.

Use the for loop to display an n x m rectangle of 8.

package zh.codegym.task.task04.task0436;


/* 
Draw rectangle
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String sA = reader.readLine();
        String sB = reader.readLine();
        int a = Integer.parseInt(sA);
        int b = Integer.parseInt(sB);
        for (int i = 0; i < a; i++) {
            for (int j = 0; j < b; j++) {
                System.out.print(8);
            }
            System.out.println();
        }
        //Write your code here

    }
}

Task 3 triangle of 8

Use the for loop to display a right triangle composed of 8, with a bottom edge of 10 and a height of 10.

package zh.codegym.task.task04.task0437;


/* 
A triangle of eight
*/

public class Solution {
    public static void main(String[] args) throws Exception {
        for (int i = 1; i <= 10; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print(8);
            }
            System.out.println();
        }
        //Write your code here

    }
}

Task 4 draw lines

Use the for loop to display:

  • A horizontal line consisting of 10 8's
  • A vertical line consisting of 10 8's (in this vertical line, no 8 on the horizontal line is counted).
package zh.codegym.task.task04.task0438;

/* 
Draw a line
*/

public class Solution {
    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 10; i++) {
            System.out.print(8);
        }
        for (int i = 0; i < 10 ; i++) {
            System.out.println(8);
        }
    }
}

Task 5 chain letter

Enter a name from the keyboard and use the for loop to display the following phrase 10 times: < name > love me.

package zh.codegym.task.task04.task0439;

/* 
Chain letter
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String sName = reader.readLine();
        for (int i = 0; i < 10; i++) {
            System.out.println(sName + "Love me.");
        }

    }
}

Lesson 5 Practice

Task 1 considerable revenue

Use a loop to display the following phrase a hundred times:
"I'll never work for a small salary. Amigo"
Each row displays a value.

package zh.codegym.task.task04.task0440;

/* 
Considerable income
*/

public class Solution {
    public static void main(String[] args) {
        for (int i = 0; i < 100 ; i++) {
            System.out.println("I will never work for a small salary. Amigo");
        }
        //Write your code here
    }
}

Task 2 try to achieve balance

Use the keyboard to enter three numbers, and then display the middle number.

In other words, it is neither the maximum nor the minimum.

If all numbers are equal, any of them is displayed.

package zh.codegym.task.task04.task0441;


/* 
Try to achieve balance
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String sA = reader.readLine();
        String sB = reader.readLine();
        String sC = reader.readLine();
        int a = Integer.parseInt(sA);
        int b = Integer.parseInt(sB);
        int c = Integer.parseInt(sC);
        if ((a >= b && b >= c) || (c >= b && b >= a)) {
            System.out.println(b);
        } else if ((b >= a && a >= c) || (c >= a && a >= b)) {
            System.out.println(a);
        } else {
            System.out.println(c);
        }
    }
}

Task 3 add

Use the keyboard to enter numbers.

If the user enters - 1, the sum of all entered numbers is displayed and the program ends.

-1 shall be included in the sum.

package zh.codegym.task.task04.task0442;


/* 
Add
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int sum = 0;
        while (true) {
            String sNum = reader.readLine();
            int num = Integer.parseInt(sNum);
            sum += num;
            if (num == -1) {
                System.out.println(sum);
                break;
            }
        }
        //Write your code here
    }
}

Task 4 add

A name is a name

Use the keyboard to enter a name.

Use the keyboard to enter the date of birth (three numbers): year, month and day.

Display the following:

"My name is name.

I was born on / day / year "
Sample output:

My name is Kevin.

I was born on February 15, 1988

package zh.codegym.task.task04.task0443;


/* 
A name is a name
*/

import java.io.*;

public class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String sName = reader.readLine();
        String sYear = reader.readLine();
        String sMonth = reader.readLine();
        String sDay = reader.readLine();
        System.out.println("My name is" + sName + ". ");
        System.out.println("I was born in " + sMonth + "/" + sDay + "/" + sYear);
    }
}

Lesson 5 exercise creating objects

Task 1 create a cat

Create Cat classes. The Cat class must contain the String variable name, the int variable age, the int variable weight, and the int variable strength.

package zh.codegym.task.task05.task0501;

/* 
Create cat
*/

public class Cat {
    String name;
    int age;
    int weight;
    int strength;

    public static void main(String[] args) {    

    }
}

Task 2 implement the fight method

How to implement Boolean fight (cat othercat):

A cat combat mechanism is realized based on the cat's weight, age and strength.

You can decide for yourself how a cat's character affects combat.

The method should determine whether we (this) win the battle, that is, if we win, return true, otherwise return false.

package zh.codegym.task.task05.task0502;

/* 
Implement the fight method
*/

public class Cat {
    public int age;
    public int weight;
    public int strength;

    public Cat() {
    }

    public boolean fight(Cat anotherCat) {
        if ((this.strength + this.age + this.weight) > (anotherCat.strength + anotherCat.weight + anotherCat.age)) {
            return true;
        } else {
            return false;
        }
        //Write your code here
    }

    public static void main(String[] args) {

    }
}

Posted by Spreegem on Wed, 13 Oct 2021 07:44:31 -0700