Introduction to Java basics javaoop (day 4)

Posted by hank__22 on Thu, 09 Dec 2021 13:39:18 +0100

Today's course is changed to javaoop (object-oriented), and the basic course of Java has not been available until yesterday (the process is quite fast). Go directly to today's notes:

method:
1. The method is behavior
2. Return value:
If there is a return value, the data returned by return is required;
void: no return value
Each method can only return one return value
3. If the method name is the same and the data type and quantity of the formal parameters of the method are different, it is called method overloading.
4. Static cannot access non static
When static methods are called, they can be called directly, using the class name Method name
When a non static method is called, its object is instantiated, and then it is called by instantiating the object's variable
5. Methods can call each other
6. Local variable (available in its scope) global variable (available in the whole class)

Java object orientation:
An object is an instance of a class, and a class is a template for an object
class: class
Class has properties, behaviors, and then becomes an object
A parameterless constructor will be created by default in the initial class. After we have created a parameterless constructor, the current default parameterless constructor will be overwritten.
Class is a template. It cannot be operated directly after its properties are. It can provide set and get methods to call.
After instantiating the created class, a new object will be generated. The properties and behavior of the object can be manipulated through the object name
Construction method: the method name is the same as the class name, and there is no method that returns content.

Today's exercise:

1. Generate different triangles according to different lines and characters specified

package java_1209;

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("Please enter row height:");int high=sc.nextInt();
        System.out.print("Please enter print characters:");char fu=sc.next().charAt(0);
        for (int i = 1; i <=high ; i++) {
            for (int j = 1; j <=i ; j++) {
                System.out.print(fu);
            }
            System.out.println();
        }
    }
}

2. Specify the search interval, find the student name and display whether the search is successful

package java_1209;

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String[] stus=new String[5];
        for (int i = 0; i <stus.length ; i++) {
            System.out.print("Please enter student name:"); String stu=sc.next();
            stus[i]=stu;
        }
        System.out.print("Name of students in this class:");
        for (int i = 0; i <stus.length ; i++) {
            System.out.print(stus[i]+"\t");
        }
        System.out.println();
        System.out.print("Please enter the location to start searching:"); int begin=sc.nextInt();
        System.out.print("Please enter a location to end the search:"); int end=sc.nextInt();
        System.out.print("Please enter the name of the person you are looking for:"); String name=sc.next();
        System.out.println();
        String result="Can't find";
        for (int i = begin; i <=end ; i++) {
            if (name.equals(stus[i])) result="Found this man";
        }
        System.out.println(result);
    }
}

3. Modify the student name, enter the new and old name, modify it and display whether the modification is successful.

package java_1209;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String[] stus=new String[5];
        for (int i = 0; i <stus.length ; i++) {
            System.out.print("Please enter student name:"); String stu=sc.next();
            stus[i]=stu;
        }
        System.out.print("Name of students in this class:");
        for (int i = 0; i <stus.length ; i++) {
            System.out.print(stus[i]+"\t");
        }
        System.out.println();
        System.out.print("Please enter the name of the student to modify:"); String name=sc.next();
        System.out.print("Please enter the name of the student to be new:"); String nameNew=sc.next();

        System.out.println();
        String result="Can't find";
        for (int i = 0; i <stus.length ; i++) {
            if (name.equals(stus[i])){
                result="Found this man,And the modification is successful";
                stus[i]=nameNew;
            }
        }
        System.out.println(result);
        System.out.print("Name of students in this class:");
        for (int i = 0; i <stus.length ; i++) {
            System.out.print(stus[i]+"\t");
        }
    }
}

4. Select whether to enter data as required, display the data after termination, and let the customer search the name and return the query results

package java_1209;

import java.util.ArrayList;
import java.util.Scanner;

public class Demo04 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        ArrayList name=new ArrayList();
        String yn="";
       do {
           System.out.print("Please enter the customer's name:");String str=sc.next();
           name.add(str);
           System.out.print("Continue to enter?(y/n):");yn=sc.next();
       }while("y".equals(yn));
        System.out.println("*******************");
        System.out.println("Customer name list:");
        System.out.println("*******************");
        for (int i = 0; i <name.size() ; i++) {
            System.out.print(name.get(i)+"\t");
        }
        System.out.println();
        System.out.println("Please enter the name of the customer you want to find:");String nameOne=sc.next();
        String result="Can't find";
        System.out.println("*******Find results*******");
        for (int i = 0; i <name.size() ; i++) {
            if (nameOne.equals(name.get(i))) result="Found this man";
        }
        System.out.println(result);



    }
}

Today's practice questions are few, but they are highly repetitive and diverse, which is still very challenging.
       

Today's harvest is as rich as usual. Today is a happy end.

Topics: Java Back-end