Educoder training project - Java object oriented - classes and objects

Posted by markmusicman on Fri, 04 Mar 2022 00:36:13 +0100

Level 1: what is a class and how to create it

Task description:

This related task: create a class and an object, and call the properties and methods of the object.

Programming requirements:

According to the prompt, supplement the code at the beginning end of the editor on the right:

  • Add two classes, namely, run name, String, and color, to define the two classes.

  • Create a Dog object named wuhuarou in the main method, and set the name to streaky pork, color to brown and variety to Alaska.

  • So that the eat method of calling wuhuarou object can output gnawing bone, and the run method can output running with bone in its mouth.

See the test instructions for specific output requirements.

package step1;

public class Test {
	public static void main(String[] args) {
		/********** Begin **********/
		//Create a Dog object
		//Set the properties of the Dog object
		//Create a Dog object named wuhuarou in the main method,
        //Set name to streaky pork, color to brown and variety to Alaska.
		Dog wuhuarou = new Dog();
        wuhuarou.name = "streaky pork";
        wuhuarou.color = "brown";
        wuhuarou.variety = "Alaska";
		//Output properties
		System.out.println("name:" +wuhuarou.name+ ",Coat color:" +wuhuarou.color+ ",varieties:" +wuhuarou.variety);
		
		//Call method
        wuhuarou.eat();
        wuhuarou.run();

		
		/********** End **********/
		
	}
}

//The Dog class is defined here
/********** Begin **********/
class Dog{
    String name;
    String color;
    String variety;
    void eat(){
        System.out.println("Gnaw a bone");
    }
    void run(){
        System.out.println("Run with a bone in your mouth");
    }
}

Level 2: construction method

Task description:

This related task: create objects and define parameterless construction methods and parameterless construction methods for objects.

Programming requirements:

According to the prompt, supplement the code at the beginning end of the editor on the right:

  • Create a Person class, define a non parametric construction method and a parametric construction method for the Person class. There are two String type parameters name and sex in the main method.

  • Call the parameterless construction method output: a person is created;

  • When calling the parametric construction method, the corresponding result shall be output according to the input data. For example, if the input is Zhang San, male, the output is: Name: Zhang San, gender: male, which is created.

package step2;

import java.util.Scanner;

public class Test {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String name = sc.next();
		String sex = sc.next();
		/********** Begin **********/
		//Two constructors are used to create the Person object 
                Person person1 = new Person();

		Person person2 = new Person(name,sex);

		/********** End **********/
		
	}
}

//Create a Person object and create two construction methods
/********** Begin **********/
class Person{
    String name;
    String sex;

    public Person(){
        System.out.println("A man was created");
    }



    public Person(String name, String sex){
        this.name = name;
        this.sex = sex;
        System.out.println("full name:"+name+','+"Gender:"+sex+','+"Was created");
    }
}
    

/********** End **********/

Level 3: multiple choice questions

  • 1. The following statement about the construction method is incorrect (C)

    A,

    The Java language stipulates that the constructor name must be the same as the class name

    B,

    The Java language specifies that the constructor has no return value and is not declared with the void keyword

    C,

    Constructor cannot be overloaded

    D,

    Constructors can only be created with the new keyword

  • 2. Class Test is defined as follows:

     
    1. public class Test{
    2. public float method1(){
    3. System.out.println("method 1");
    4. return 1.5;
    5. }
    6.  
    7.  
    8. }

    Which of the following methods is legal to insert into line 7 (CD)

    A,

    public float method1(){
    System.out.println("method 2"); return 1.1; }

    B,

    public Test1(){ System.out.println("hello"); }

    C,

    Public float method1 (int a) {system. Out. Println ("method 2"); return a+0.5;}

    D,

    public Test(){ System.out.println("hello"); }

Level 4: This keyword

Task description:

This related task: use this keyword to set the member variable of the object.

Programming requirements

The code has been written in the editor on the right, but the output result is not satisfactory. Please modify the code according to your knowledge so that it can output the following results: name: baby age: 45 sex: female

package step3;

import java.util.Scanner;

public class Test {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String name = sc.next();
		int age = sc.nextInt();
		String sex = sc.next();
		Person p = new Person(name,age,sex);
		p.display();
	}
}

class Person{
	String name = "Zhang San";
	int age = 18; 
	String sex = "male";
	/********** Begin **********/

	public Person(String name,int age,String sex){
		this(age);
		this.name = name;
		this.sex = sex;
	}
	
	public Person(int age){
		this.age = age;
	}
	
	public void display(){
		String name = "baby";
		int age = 45;
		String sex = "female";
		System.out.println("name: " + this.name);
		System.out.println("age: " + this.age);
		System.out.println("sex: " + this.sex);
	}


	/********** End **********/
}

Level 5: class and object practice

Task description:

This off task: write a class for "nameless powder", and write the construction method and member variables of this class.

Programming requirements:

I have created two files for you. One is wumingfen Java, the other is test Java, you need to switch files and write code in two files to complete this task.

Write a class for "nameless powder": class WuMingFen requirements:

  • There are three attributes: face code: String theMa powder component (two): int quantity whether there is soup: boolean likeSoup;

  • Write a construction method to simplify the initialization process, such as: WuMingFen f1 = new WuMingFen("beef", 3,true);;

  • Overload the construction method so that the initialization process can be diversified: WuMingFen f2 = new WuMingFen("beef", 2);;

  • How to make the powder object constructed by the following statements be sour and spicy noodles, 2 Liang and soup? WuMingFen f3 = new WuMingFen();;

  • Write a common method: check(), which is used to check whether the powder meets the requirements. That is, print the three attributes of the object on the console.

Test.java file

package step4;

import java.util.Scanner;

public class Test {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String theMa = sc.next();
		int quantity = sc.nextInt();
		boolean likeSoup = sc.nextBoolean();
		/********** Begin **********/
		//Use the construction method of three parameters to create a WuMingFen object named f1
        WuMingFen f1 = new WuMingFen(theMa, quantity, likeSoup);
		//The two parameter construction method is used to create the WuMingFen object named f2
        WuMingFen f2 = new WuMingFen(theMa, quantity);
		//Use the parameterless construction method to create a WuMingFen object named f3
        WuMingFen f3 = new WuMingFen();
            f3.theMa = "sour and hot";
            f3.quantity = 2;
            f3.likeSoup = true;
		//Call the check methods of the three classes respectively
        f1.check();
        f2.check();
        f3.check();

		
		/********** End **********/	
	}
}
WuMingFen.java file

/********** Begin **********/
//Add the package name step4 here
package step4;
//Create classes and add properties and methods
public class WuMingFen{
    String theMa;
    int quantity;
    boolean likeSoup;
    public WuMingFen(){

    }
public WuMingFen(String theMa, int quantity, boolean likeSoup){
    this.theMa = theMa;
    this.quantity = quantity;
    this.likeSoup = likeSoup;
}
public WuMingFen(String theMa, int quantity){
    this.theMa = theMa;
    this.quantity = quantity;
}
public void check(){
    System.out.println("Face code:" + theMa + ",Amount of powder:" + quantity + "Two, whether to bring soup:" + likeSoup);
}
}

Level 6: static keyword

Programming requirements:

Now there is an exception in the code in the editor. Please use the knowledge learned in this level to modify the code on the right so that it can output the following results:

Hello educator my name is Chu Liuxiang. I like learning java on educator

package step5;

public class Test {
	/********** Begin **********/
	static String name = "Chu Liuxiang";
	static
	{
		System.out.println("hello educoder");
	}
	public static void main(String[] args) {
		System.out.println("My name is" + name);
		study();
	}
	
	public static void study(){
		System.out.println("I like to be in educoder Study on java");
	}
	/********** End **********/
}

Off 7: multiple choice questions

  • 1. There are the following codes:

     
    1. public class TestMain{
    2. public static void main(String args[]){
    3. MyClass mc1 = new MyClass();
    4. MyClass mc2 = new MyClass();
    5. mc1.a = 100;
    6. mc1.b = 200;
    7. mc2.a = 300;
    8. mc2.b = 400;
    9. System.out.println(mc1.a);
    10. System.out.println(mc1.b);
    11. System.out.println(mc2.a);
    12. System.out.println(mc2.b);
    13. }
    14. }
    15. class MyClass{
    16. static int a;
    17. int b;
    18. }

    What is the output result

    A,                                   B,                                    C,                               D,

    100 100 100 100              100 200 300 400               400 400 400 400          300 200 300 400

     

  • 2,```java class MyClass { int a; static int b; void fa(){

    1. }
    2. static void fb(){
    3.  
    4. }
    5. public void m1(){
    6. System.out.println(a); // Position 1
    7. System.out.println(b); // Position 2
    8. fa(); // Position 3
    9. fb(); // Position 4
    10. }
    11. public static void m2(){
    12. System.out.println(a); // Position 5
    13. System.out.println(b); // Position 6
    14. fa(); // Position 7
    15. fb(); // Position 8
    16. }

    }

    ```The error locations of the above codes are: (EG)

    A,          B,         C,         D,         E,         F,          G,

    Position 1 , position 2 , position 3 , position 4 , position 5 , position 6 , position 7

  • 3,```java class MyClass { static int i = 10; static { i = 20; System.out.println("In Static"); }

    1. public MyClass() {
    2. System.out.println("MyClass()");
    3. }
    4.  
    5. public MyClass(int i) {
    6. System.out.println("MyClass(int)");
    7. this.i = i;
    8. }

    }

    public class TestMain { public static void main(String args[]) { MyClass mc1 = new MyClass(); System.out.println(mc1.i); MyClass mc2 = new MyClass(10); System.out.println(mc2.i); } }

    ```The running result of the above code is: (B)

    A,                                                                      B,

    MyClass() 20 MyClass(int) 10                             In Static MyClass() 20 MyClass(int) 10

    C,                                                                       D,

    In Static MyClass(int) 20 MyClass() 10               In Static MyClass() 10 MyClass(int) 20

     

Topics: Java