Experiment 3 object oriented (preview report)

Posted by adrive on Mon, 17 Jan 2022 20:44:08 +0100

Object oriented -- Java experiment report

Experiment 3: Java object oriented (2)

Experiment 3 object oriented (2) (preview report)

<center> 
<strong>full name:</strong> 
<u>XXX</u> 
&emsp;&emsp;
<strong>Class:</strong> 
<u>XXXXX</u> 
&emsp;&emsp;
<strong>Student No.:</strong>
<u>XXXXXXXXXXXX</u></center>

1, Experimental objectives

   (1) be able to use Java language to realize class inheritance and polymorphism, skillfully use packages to expand program functions, and be able to establish their own packages;

  (2) be able to flexibly use the abstract methods, interfaces and abstract classes of Java language to write applications to solve practical problems.

2, Experimental environment

IntelliJ IDEA Community Edition 2021.1 x64 + openjdk-16.0.1

3, Experimental preparation

  1. Design an abstract class graphics class, which contains at least three abstract methods, which are respectively used to calculate perimeter, area and describe graphics (for example, "this is * * shape, perimeter is * *, area is * *)). Define circle class, rectangle class and equilateral triangle class to inherit graphics class, implement the above three methods, and create an example for verification.

Problem analysis: the following structure can be obtained from the question

abstract class Shape { //Abstract class graphics class
    public abstract double calculatePerimeter (); //Perimeter
    public abstract double calculateSquare ();    //the measure of area
    public abstract void describe ();}            //describe  
class Round extends Shape {  //Circular class
 @Override
    public double calculatePerimeter() ;    
    @Override
    public double calculateSquare() ;
    @Override
    public void describe() ;}
   // The operations of rectangle and equilateral triangle are the same as above
  1. Define an abstract interface Lecture. There are at least two abstract methods talk() and homework() in the interface, which define Student class and Teacher class to implement Lecture interface respectively. The talk() method of Student class is used to simulate students to answer questions, the talk() method of Teacher class is used to simulate teachers to ask questions, and the homework() method of Student class is used to simulate students to write homework, The homework () method of the Teacher class is used to simulate the Teacher's assignment.

Problem analysis: the following structure can be obtained from the question

interface Lecture {  //Interface
    public abstract void talk (); //talk() method
    public abstract void homework ();}//homework() method
class Student implements Lecture { //Student class
    @Override
    public void talked();
    @Override
    public void homework();
        //Teacher class is the same as above
  1. Define an abstract interface for summing and counting quantities. Define two odd number classes and even number classes to realize the above interfaces, which can respectively calculate the sum of odd numbers within 100, count the odd numbers within 100, and calculate the sum of even numbers within 100, and count the even numbers within 100.

Problem analysis: the following structure can be obtained from the question

interface Countable {               //Interface
    public abstract int sum ();     //Sum
    public abstract int count ();}  //Statistics
// even numbers
class Even implements Countable {
    @Override
    public int sum() ;  //Summation operation
 @Override
    public int count() ;//Statistical operation
  1. Define an abstract animal class and abstract interfaces: eating interface, sleeping interface, thinking interface, speaking interface, walking interface, swimming interface and flying interface. Cats, humans, birds and fish inherit animals respectively. At the same time, selectively implement the above interfaces according to the actual situation, and create an example for verification.

Problem analysis: the following structure can be obtained from the question

abstract class Animal {public abstract void breathe ();} //Abstract animals

interface AbleToEat {public abstract void eat ();}    //Feeding interface
interface AbleToSleep {public abstract void sleep ();}//Sleep interface
interface Thinkable {public abstract void think ();}  //Thinking interface
interface Speakable {public abstract void speak ();}  //Speech interface
interface Walkable {public abstract void walk ();}    //Traveling interface
interface AbleToSwim {public abstract void swim ();}  //Swimming interface
interface Flyable {public abstract void fly ();}      //Flying interface

class Cat extends Animal implements AbleToEat, AbleToSleep, Thinkable, Speakable, Walkable { //Cats
    @Override
    public void breathe() ;
    @Override
    public void eat() ;
    @Override
    public void sleep();
    @Override
    public void think() ;
    @Override
    public void speak();
    @Override
    public void walk();
//Fish, humans and birds operate as above

4, Problems encountered in Preview

The familiarity with relevant knowledge is not enough, and the relevant knowledge is insufficient. We should try our best to train and be fully familiar with relevant content. There are some questions about the understanding of interfaces and abstract classes. I hope to learn and master relevant knowledge through various channels in the experiment.

Topics: Java Back-end