1, Basic case
Topic 1
1, Analyze the following requirements and implement them in Code:
- Define the Person class
Attributes: name, age;
Methods: null parameter construction method, full parameter construction method, setXxx(), getXxx(), work. - According to human beings, a Teacher class Teacher is derived
Rewrite the working method (the teacher's job is to give a good lecture). - According to human beings, a Student class Student is derived
Rewrite working methods (students' job is to study hard). - Write test classes to test the above two types of specific characters respectively
- Required operation results:
Students should study hard in their work
Teachers should give good lectures
public class mainApp { public static void main(String[] args) { // TODO Auto-generated method stub //1.1 Teacher t = new Teacher(); Student s = new Student(); t.work(); s.work(); } } public class Person { private String name; private int age; public Person() {} public Person(String name,int age) { this.name=name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void work() { } } public class Teacher extends Person{ public Teacher() {} public Teacher(String name,int age) { super(name,age); } public void work() { System.out.println("The teacher's job is to give a good lecture"); } } public class Student extends Person{ public Student() {} public Student(String name, int age) { super(name,age); } public void work() { System.out.println("Students' job is to study hard"); } }
Topic 2
1, Analyze the following requirements and implement them in Code:
- Define mobile phone class
Method: make a phone call (the function of the mobile phone is to call someone) - Derive a new mobile phone class according to the mobile phone class
Rewrite the call function (the new function can start the video call and include the old function) - Write test classes to test the above two types of mobile phones respectively
- Call Lin Qingxia for the operation results
Turn on video function
Call Lin Qingxia
public class mainApp { public static void main(String[] args) { // TODO Auto-generated method stub Phone p = new Phone(); NewPhone np = new NewPhone(); p.call("Li Zongsheng"); np.call("Li Zongsheng"); } } public class Phone { public Phone() {} public void call(String name) { System.out.println("to"+name+"phone"); } } public class NewPhone extends Phone{ public NewPhone() {} public void call(String name) { System.out.println("Turn on video function"); System.out.println("to"+name+"phone"); } }
Topic 3
1, Analyze the following requirements and implement them in Code:
- Define Animal class
Method: eat eat. - According to the animal class, a Cat class is derived
Rewrite the method of eating (cats eat fish). - A Dog dog is derived from the animal class
Rewrite the method of eating (dogs eat bones).
Dogs also have a unique method, dog watch - According to the animal class, a Pig is derived
Rewrite the eating method (pigs eat cabbage). - According to the operating animal class AnimalOperator
Methods: call the animal's eating method - Write test classes to create objects for AnimalOperator, Cat, Dog and Pig, and call the eating function for testing
- Required operation results:
Cats eat fish
Dogs eat bones
Pigs eat cabbage
public class mainApp { public static void main(String[] args) { AnimalOperator ao = new AnimalOperator(); Cat cat = new Cat(); Dog dog = new Dog(); Pig pig = new Pig(); ao.useAnimal(cat); ao.useAnimal(dog); ao.useAnimal(pig); } } public class Animal { public Animal() {} public void eat() { System.out.println("Animals eat"); } } public class Cat extends Animal{ public Cat() {} public void eat() { System.out.println("Cats eat fish"); } } public class Pig extends Animal{ public Pig() {} public void eat() { System.out.println("Pigs eat cabbage"); } } public class AnimalOperator { public void useAnimal(Animal animal) { animal.eat(); } }
2, Extended case
Topic 1
1, Analyze the following requirements and implement them in Code:
- Define the Person class
Properties:
name, gender, age, nationality;
Methods: eat, sleep and work.
2. Derive a Student class from human beings
Add attribute:
school, student number stuNumber;
Rewrite working methods (students' work is learning).
3. According to human beings, a Worker class Worker is derived
Add attribute:
unit, length of service, workAge;
Rewrite working methods (workers' job is to build houses).
4. Derive a student cadre class StudentLeader according to the student class
Add attribute:
job title;
Add method: meeting.
5. Write a test class to test the above three categories of specific characters respectively.
6. Required operation results:
Students need to learn!
The worker's job is to build a house!
Student cadres like meetings!
public class mainApp { public static void main(String[] args) { AnimalOperator ao = new AnimalOperator(); Cat cat = new Cat(); Dog dog = new Dog(); Pig pig = new Pig(); ao.useAnimal(cat); ao.useAnimal(dog); ao.useAnimal(pig); } } public class Person { private String name; private String gender; private int age; private String nationality; public Person() {} public Person(String name,int age,String gender,String nationality) { this.name=name; this.age = age; this.gender = gender; this.nationality = nationality; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getNationality() { return nationality; } public void setNationality(String nationality) { this.nationality = nationality; } public void work() { } public void eat() {} public void sleep() {} } public class Student extends Person{ private String school; private String stuNumber; public Student() {} public Student(String name, int age,String gender,String nationality,String school,String stuNumber) { super(name,age,gender,nationality); this.school =school; this.stuNumber = stuNumber; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public String getStuNumber() { return stuNumber; } public void setStuNumber(String stuNumber) { this.stuNumber = stuNumber; } public void work() { System.out.println("Students need to learn!"); } } public class Worker extends Person{ private String unit; private int workAge; public Worker() {} public String getUnit() { return unit; } public void setUnit(String unit) { this.unit = unit; } public int getWorkAge() { return workAge; } public void setWorkAge(int workAge) { this.workAge = workAge; } public void work() { System.out.println("The worker's job is to build a house!"); } } public class StudentLeader extends Student{ private String job; public StudentLeader() {} public StudentLeader(String name, int age,String gender,String nationality,String school,String stuNumber,String job) { super(name,age,gender,nationality,school,stuNumber); this.job = job; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public void meeting() { System.out.println("Student cadres like meetings!"); } }
Topic 2
1, Analyze the following requirements and implement them in code
- Define project manager class
Properties:
Name, job number, salary and bonus
Behavior:
work - Define programmer classes
Properties:
Name job number salary
Behavior:
work - Requirements: extract a parent class upward, let both classes inherit the parent class, write the common properties in the parent class, and the child class overrides the methods in the parent class
- Write test class: complete the test of these two classes
public class mainApp { public static void main(String[] args) { Manager m = new Manager(); m.work(); Coder c = new Coder(); c.work(); } } public class Manager extends Employee{ private String bonus; public Manager() {} public Manager(String name,String id,double salary,String bonus) { super(name,id,salary); this.bonus = bonus; } public String getBonus() { return bonus; } public void setBonus(String bonus) { this.bonus = bonus; } public void work() { System.out.println("The manager is working!"); } } public class Employee { private String name; private String id; private double salary; public Employee() {} public Employee(String name,String id,double salary) { this.name=name; this.id = id; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public void work() { System.out.println("Employees at work"); } } public class Coder extends Employee{ public Coder() {} public Coder(String name,String id,double salary) { super(name,id,salary); } public void work() { System.out.println("Programmers are typing code!"); } }
Topic 3
1, Analyze the following requirements and implement them in code
-
Known students and teachers are as follows:
Properties:
Name, age
Behavior:
having dinner
Teachers have their own methods:
lecture
Students have unique methods:
study -
Requirements: extract a parent class upward, let both classes inherit the parent class, write the common properties in the parent class, and the child class overrides the methods in the parent class
-
Write test class: complete the test of these two classes
public class mainApp { public static void main(String[] args) { Teacher t = new Teacher("Guo Degang",40); Student s = new Student("Guo Qilin",22); t.eat(); t.Lecture(); s.eat(); s.learn(); } } public class Person { private String name; private int age; public Person(){} public Person(String name, int age){ this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void eat() { System.out.println("having dinner"); } } public class Teacher extends Person { public Teacher() {} public Teacher(String name, int age) { super(name,age); } public void eat() { System.out.println(this.getAge()+"Year old"+this.getName()+"The teacher is eating."); } public void Lecture() { System.out.println(this.getAge()+"Year old"+this.getName()+"The teacher is in class"); } } public class Student extends Person{ public Student() {} public Student(String name, int age) { super(name,age); } public void eat() { System.out.println(this.getAge()+"Year old"+this.getName()+"The students are eating."); } public void learn() { System.out.println(this.getAge()+"Year old"+this.getName()+"Students are studying"); } }
Topic 4
1, Analyze the following requirements and implement them in Code:
1. Define employee category:
Properties:
Job number, name
Behavior:
Working method (simulated with simple output statement)
2. Define manager class:
Properties:
Job number, name, bonus
Behavior:
Working methods (managing employees, simple output statement simulation)
3. Define waiter class:
Properties:
Job number, name
Behavior:
Working method (customer service, simple output statement simulation)
4. Define test class:
Create manager class object and waiter class object respectively
Call the respective working methods
requirement:
Analyze the inheritance relationship between various things and extract relevant classes
public class MainApp { public static void main(String[] args) { Manager m = new Manager(); m.work(); Waiter w = new Waiter(); w.work(); } } public class Employee { private String name; private String id; public Employee() {} public Employee(String name,String id) { this.name=name; this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public void work() { System.out.println("Employees at work"); } } public class Manager extends Employee{ private String bonus; public Manager() {} public Manager(String name,String id,String bonus) { super(name,id); this.bonus = bonus; } public String getBonus() { return bonus; } public void setBonus(String bonus) { this.bonus = bonus; } public void work() { System.out.println("The manager is managing employees!"); } } public class Waiter extends Employee{ public Waiter() {} public Waiter(String name,String id) { super(name,id); } public void work() { System.out.println("Serving customers"); } }