Exercise (object oriented 1)

Posted by ignite on Tue, 18 Jan 2022 04:46:57 +0100

The running result of the following Java code is
public class Dog {
String name=null;
int age = 10;
public Dog() {
this.name = "Wangcai";
this.age = 20;
}
public void show(){
System.out.println("Name:" + name + "age:" + age);
}
public static void main(String[] args) {
new Dog().show();
}
}
//Name: Wangcai age: 20
In Java, the following programs compile and run the output results
public class Test {
int x = 2;
int y = 4;
Test (int x, int y) {
this.x = x;
this.y = y;
}
public static void main(string[] args) {
Test ptl = new Test (3, 3);
Test pt2 = new Test (4, 4);
System. out. print (pt1.x + pt2.x);
}
}
//7
Write the results
public class Test {
int i = 0;
void change(int i){
i++;
System.out.println(i);//1
}
void change1(Test t){
t.i++;
System.out.println(t.i);//1
}

public static void main(String[] args) {
    Test ta = new Test();  
    System.out.println(ta.i);//0
    ta.change(0);
    System.out.println(ta.i); //0
    ta.change1(ta);  
    System.out.println(ta.i);//1
}

}
//01011

Write the results
public class TestArgsValue {

public static void main(String[] args) {
	int i = 10;
	TestArgsValue tv = new TestArgsValue();
	tv.method1(i);
	System.out.println("i=" + i);//i=10
	
	System.out.println();// 
	Demo d = new Demo();
	System.out.println(d);//address
	tv.method2(d);
	System.out.println("d.i = " + d.i);//d.i=6
}

public void method1(int i){
	System.out.println("i=" + i++);//i=10
}

public void method2(Demo d){
	System.out.println(d);//address
	System.out.println("d.i : " + d.i++);//d.i:5
}

}

class Demo{
int i = 5;
}

Write the results
/*
Load an instance block and a static code block
Static code blocks take precedence over objects, so output first
*/
public class Test {
Person person = new Person(“Test”);// create object
int age;
static{
System.out.println("test static");
}
{
System.out.println("test");
}

public Test() { 
    System.out.println("test constructor");
}
 
public static void main(String[] args) {
      new Test();//create object
}

}
class Person{
static{
System.out.println("person static");
}
public Person(String str) {
System.out.println("person "+str);
}
}
//test static
//person static
//person Test
//test
//test constructor
public class Test{
public static void leftshift(int i, int j){
i+=j;
}
public static void main(String args[]){
int i = 4;
int j = 2;
leftshift(i, j);
System.out.println(i); 4
}
}
//4
Which option overloads A with show()
class Demo{
void show(int a,int b,float c){ }
}

A.void show(int a,float c,int b){ }
B,void show(int a,int b,float c){ }
C.int show(int a,float c,int b){ return a; }
D.int show(int a,float c){ return a; }

Programming size comparison: respectively use overloading to compare the size of two ints, two double, three ints and three double types, and return the larger one

package Day04.HM;
/*
Compare size: respectively use overloading to compare the size of two ints, two double, three ints and three double types, and return the larger one
 */
public class Compare {
    void Com(int a,int b){
        System.out.println(a>b?a:b);

    }
    void Com(int a,int b,int c){
        if(a>b){
            if(c>a){
                System.out.println(c);
            }
            else {
                System.out.println(a);
            }
        }else {
            if(b>c){
                System.out.println(b);
            }else {
                System.out.println(c);
            }
        }

    }
    void Com(double a,double b){
        System.out.println(a>b?a:b);

    }
    void Com(double a,double b,double c){
        if(a>b){
            if(c>a){
                System.out.println(c);
            }
            else {
                System.out.println(a);
            }
        }else {
            if(b>c){
                System.out.println(b);
            }else {
                System.out.println(c);
            }
        }

    }

}

programming
Define a China Postal Savings Bank class,
Attributes: account number, password, deposit balance, bank name.
Method: register account number, deposit, withdraw and query.
Create this kind of object and test the registered account, deposit and withdrawal query operations
Requirements: use the object-oriented idea to design the program

package Day04.HM;

import java.util.Scanner;

/*
Define a China Postal Savings Bank class,
      Attributes: account number, password, deposit balance, bank name.
      Methods: register account number, deposit, withdraw and query.
      Create this kind of object and test the registered account, deposit and withdrawal query operations
	  Requirements: use the object-oriented idea to design the program
 */
public class Bank {
    int account;
    int password;
    int yue;
    String name = "Postal Savings Bank of China";
    Scanner scanner = new Scanner(System.in);
    public void register(){

        System.out.println("Please enter your account number");
        this.account = scanner.nextInt();
        System.out.println("Please enter your password");
        this.password = scanner.nextInt();
        System.out.println("login was successful");
    }
    public void deposit(){
        System.out.println("Please enter the amount you want to deposit:");
        yue += scanner.nextInt();
        System.out.println("Deposit succeeded. Your current balance is:"+yue);
    }
    public void withdrawal(){
        System.out.println("Please enter the amount you want to withdraw:");
        int money = scanner.nextInt();
        if(money<=yue){
            yue -= money;
            System.out.println("Withdrawal succeeded. Your current balance is:"+yue);
        }
        System.out.println("Sorry, your credit is running low!");
    }
    public void search(){
        System.out.println(yue);
    }
}

package Day04.HM;

public class BankTest {
    public static void main(String[] args) {
        Bank zs = new Bank();
       out: while (true) {
        System.out.println("Please select your operation: 1. Register 2. Withdraw 3. Deposit 4. Query");
        if(zs.account==0&&zs.password==0){
            System.out.println("Please register your account first");
            zs.register();
        }
        int choice = 0;

            switch (choice){
                case 1:
                    zs.register();
                case 2:
                    zs.withdrawal();
                case 3:
                    zs.deposit();
                case 4:
                    zs.search();
                default:
                    break out;

            }

        }
    }


}

Brief answer
1. The idea of object-oriented programming
Object oriented programming languages think and solve problems in a classified way.
Object oriented first classifies the overall relationship, and then processes the details according to different classes.

2. Describe the relationship between class and object
Class contains objects. Class is a template. It is a collection of attributes and behaviors that describe a class of things (concrete objects, concrete existence)
Object is a concrete instance created in memory with a class as a template

3. Briefly describe the function, characteristics and method of structural method
Construction method: a method for creating an object. It has no return value and does not need void modification. Each class has a construction method. If you do not explicitly define a constructor for a class, Java will provide a constructor for that class
A default constructor, but the default parameterless constructor will become invalid as long as a constructor is defined in a Java class. In general, if you define a construction method with parameters, you'd better write out the default non parameter construction method
Method overloading: method overloading refers to multiple methods with the same name but different parameters in the same class.
Different parameters (there can be three differences)
The quantity, type and order are different
When called, the corresponding method will be selected according to different parameter tables.
Both constructor and member methods can be overloaded
Note: method overloading has nothing to do with the return value type of the method

4. Briefly describe the difference between static variables and non-static variables?
1. Memory allocation
Static variables exist in memory when the application program is initialized, and do not die until the program of its class runs; Non static variables need to be instantiated before allocating memory.
2. Life cycle
The static variable life cycle is the existence cycle of the application; The existence period of a non static variable depends on the existence period of the instantiated class.
3. Calling method
Static variables can only be called through "class. Static variable name", and class instances cannot be called; A non static variable can be accessed directly through the instantiated class name after the class of the variable is instantiated.
4. Sharing mode
Static variables are global variables that are shared by instance objects of all classes, that is, the value of a static variable is changed by an instance, and the changed value is read by other instances of the same kind; Non static variables are local variables and are not shared.
5. Access method
Static members cannot access non static members; Non static members can access static members.

Topics: Java JavaEE intellij-idea