1903021137 - Zhao Wuyan - Java Week 11 assignment - inheritance, polymorphism and abstract classes of Java

Project

content

Course class blog link

Level 19 information and accounting class (this class)
This job requires a link Java Week 11 homework
Blog name 1903021137 - Zhao Wuyan - Java Week 11 assignment - inheritance, polymorphism and abstract classes of Java
requirement Each question should have a question and code (use the inserted code. If you can't insert the code, check the data and solve it by yourself. Don't directly screenshot the code!!), Screenshot (only the running results are taken).

Topic 1:

  • Class Person defines name, birth year, and its subclass Graduate defines gpa and graduation year on the basis of inheriting the parent class. Write the test class and output the name, age, graduation year and achievement of the graduates.

  • Tip: the parent class must have a constructor, and the child class calls the constructor of the parent class through super.

Code:

Parent class:
package edu.xj.zwy.week11;
public class Person {
     String name;
        int birthYear;
        public Person(String name,int birthYear ) {
            super();
            this.name = name;
            this.birthYear = birthYear;
        }
        public String getName() {
            return name;
        }
        

        public int birthYear(){
            return birthYear;
        }
            
            }
 1 Subclass:
 2 package edu.xj.zwy.week11;
 3 
 4 public class Graduate extends Person {
 5     int gpa;
 6     int graguateYear;
 7     int  nowYear;
 8     public Graduate(String name, int birthYear, int gpa, int graguateYear,
 9             int nowYear) {
10         super(name, birthYear);
11         this.gpa = gpa;
12         this.graguateYear = graguateYear;
13         this.nowYear = nowYear;
14     }
15     public int getGpa() {
16         return gpa;
17     }
18     
19     public int getGraguateYear() {
20         return graguateYear;
21     }
22     
23     public int getNowYear() {
24         return nowYear;
25         
26             
27     }
28     public int age(){
29         return nowYear-birthYear;
30         
31     }
32 }
 1 Test class:
 2 package edu.xj.zwy.week11;
 3 
 4 public class Test {
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Graduate s=new Graduate("so-and-so",2000,100,2023,2019);
 8          System.out.println("full name:"+s.name+"\n Age:"+s.age()+"\n Year of graduation:"+s.getGraguateYear()+"\n Achievements:"+s.gpa);
 9     }
10 }

Screenshot:

 

  Topic 2:

  • Define a basic class shape. There is a draw method. Define three classes: Circle, Triangle and Square. All of them inherit from shape. Define a method doStuff in the test class, pass in the basic class shape as the parameter, and call the draw method. Use the parent class shape to create three instance objects of Circle, Triangle and Square, which are respectively passed into the draw method as parameters.
  • Tip: polymorphism

Code:

 

Parent class:
1
package edu.xj.zwy.week11; 2 3 public class Shape { 4 5 void draw() { 6 7 } 8 9 }

 

 Subclass:
1
Circle Class: 2 package edu.xj.zwy.week11; 3 4 public class Circle extends Shape { 5 6 void draw() { 7 8 System.out.println("Circle.draw()"); 9 } 10 11 } 12 13 Square Class: 14 package edu.xj.zwy.week11; 15 16 public class Square extends Shape { 17 void draw(){ 18 19 System.out.println("Square.draw()"); 20 } 21 22 } 23 24 Triangle Class: 25 package edu.xj.zwy.week11; 26 27 public class Triangle extends Shape { 28 void draw() { 29 30 System.out.println("Triangle.draw()"); 31 } 32 }
Test class:
1
package edu.xj.zwy.week11; 2 3 public class Test2 { 4 static void doStuff(Shape s){ 5 s.draw(); 6 } 7 8 public static void main(String[] args) { 9 Shape c=new Circle(); 10 Shape s=new Square(); 11 Shape t=new Triangle(); 12 doStuff(c); 13 doStuff(s); 14 doStuff(t); 15 // TODO Auto-generated method stub 16 17 } 18 }

 

Screenshot:

 

 

Topic 3:

  • All animals have a parent class Animal, and then define two subclasses Bird and Dog, which inherit from Animal and implement the bark method in the parent class.

  • Tip: Abstract

Code:

Parent class:
package edu.xj.zwy.week11;
public class Animal { void bark() { } }
Subclass:
Bird Class: package edu.xj.zwy.week11;
public class Bird extends Animal { void bark() { System.out.println("The bird's cry is jomi jomi~"); } } Dog Class: package edu.xj.zwy.week11; public class Dog extends Animal { void bark() { System.out.println("The dog's bark is barking~"); } }
Test class:
package edu.xj.zwy.week11;

public class Test1 {
    public static void main(String[] args) {
        Dog d=new Dog();
        Bird b=new Bird();
        d.bark();
        b.bark();
        // TODO Auto-generated method stub

    }

}

Screenshot:

 

 

Topic 4:

  • The area calculation formulas of different geometric figures are different, but they have the same characteristics. They both have the two attributes of length and width, and also have the method of area calculation. Calculate the area of rectangle and triangle according to the abstract concept.

  • Tip: Abstract

Code:

 

 1 package edu.xj.zwy.week11;
 2 
 3 public abstract class Area {
 4     int width;
 5     int height;
 6     public Area(int width, int height) {
 7         super();
 8         this.width = width;
 9         this.height = height;
10     }
11     public  abstract double area();
12 
13 }

 

 Trigon Class:
1
package edu.xj.zwy.week11; 2 3 public class Trigon extends Area { 4 public Trigon(int width, int height) { 5 super(width, height); 6 } 7 8 @Override 9 public double area() { 10 // TODO Auto-generated method stub 11 return width*height; 12 } 13 14 }
Rectangle Class:
package edu.xj.zwy.week11;
public class Rectangle extends Area { public Rectangle(int width, int height) { super(width, height); } @Override public double area() { // TODO Auto-generated method stub return width*height; } }
Test class:
package edu.xj.zwy.week11;
public class Test3 { public static void main(String[] args) { Rectangle r=new Rectangle(6,7); System.out.println("The area of the rectangle is:"+r.area()); Trigon t=new Trigon(3,5); System.out.println("The area of the triangle is:"+t.area()); // TODO Auto-generated method stub } }

 

Screenshot:

 

 

experience:

The concept of inheritance: expand and transform existing classes to form a new class. The relationship of "is # a" is inheritance.

Concept of polymorphism: polymorphism is a kind of transaction with multiple forms.

Classification of polymorphism:

                             1. Polymorphism of object (parent object reference points to subclass instance)

                             2. Polymorphism of methods (including method overloading, rewriting or overwriting)

                             3. Polymorphism of the object (if the method of the parent class is overridden in the subclass, the method overridden in the subclass is called through the polymorphism of the object)

Method overloading: overloading occurs in the same class. The name of the function is the same, but the number of parameters and parameter types of the function are different.  

Function override:

                        1. Rewriting occurs in two classes. The method of the parent class is rewritten in the child class.

                        2. The rewritten function name is exactly the same as the parameter list.

                        3. The rewritten function can only throw smaller exceptions than before, and the access permission can only be greater than before.

                        4. In the polymorphism of objects mentioned earlier, the parent object points to the subclass instance, and can only call the same methods in the parent and subclass. The newly added methods in the subclass cannot be called. If you want to call, there must be forced type conversion.

 

Posted by faizanno1 on Sat, 14 May 2022 20:14:13 +0300