Java learning mind map
1. Code error correction:
package com.qf.q8.t1; interface IA{ void m1(); int a = 100; } class MyClass implements IA{ void m1() {}// public void m1() {} } public class TestInterface { public static void main(String[] args) { IA ia = new MyClass(); ia.m1(); System.out.println(IA.a); } }
2. Fill in the blank Code:
package com.qf.q8.t2; interface IA{ void m1(); void m2(); } //1/*abstract*/ class MyClassA implements IA { public void m1() {} } class MyClassB extends MyClassA{ //2 public void m2() {} }
3. There are the following codes:
package com.qf.q8.t3; interface IA { void ma(); } interface IB extends IA{ void mb(); } interface IC{ void mc(); } interface ID extends IB,IC{ void md(); }
1. If there is a class class that implements the ID interface, what methods do you need to implement if you don't want class to be abstract?
package com.qf.q8.t3; public class ClassE implements ID{ public void md() { } public void ma() { } public void mb() { } public void mc() { } }
2. Complete the following code
package com.qf.q8.t3; public class TestClassE { public static void main(String[] args) { IC ic = new ClassE(); //Call ma method (IA)ic.ma(); //Call mb method (IB)ic.mb(); //Call mc method ic.mc(); //Call md method (ID)ic.md(); } }
3. Write the output of the following code
package com.qf.q8.t3; public class TestClassE { public static void main(String[] args) { IC ic = new ClassE(); System.out.println(ic instanceof IA); System.out.println(ic instanceof IB); System.out.println(ic instanceof IC); System.out.println(ic instanceof ID); System.out.println(ic instanceof ClassE); } }
true true true true true
4. There are the following codes:
package com.qf.q8.t4; interface IA{ void ma(); } interface IB{ void mb(); } class MySuper implements IA{ public void ma() {} } class MySub extends MySuper implements IB{ public void mb() {} } public class TestMain { public static void main(String[] args) { MySuper ms = new MySub(); System.out.println(ms instanceof IA); System.out.println(ms instanceof IB); System.out.println(ms instanceof MySuper); System.out.println(ms instanceof MySub); } }
What is the output of the program? true true true true
5. For interfaces and abstract classes, the following statements are correct:
A.Abstract class can have constructor, interface has no constructor B.Abstract class can have properties, interface has no properties C.Abstract classes can have non abstract methods, and all interfaces are abstract methods D.Neither abstract classes nor interfaces can create objects E.A class can inherit at most one abstract class, but can implement multiple interfaces ACDE
6. Write the output of the following code
package com.qf.q8.t6; interface Light{ void shine(); } class RedLight implements Light{ public void shine() { System.out.println("Red Light shine in Red"); } } class YellowLight implements Light{ public void shine() { System.out.println("Yellow Light shine in Yellow"); } } class GreenLight implements Light{ public void shine() { System.out.println("Green Light shine in Green"); } } class Lamp{ private Light light; public void setLight(Light light) { this.light = light; } public void on() { light.shine(); } } public class TestLamp { public static void main(String[] args) { Light[] Is = new Light[3]; Is[0] = new RedLight(); Is[1] = new YellowLight(); Is[2] = new GreenLight(); Lamp lamp = new Lamp(); for(int i=0;i<Is.length;i++) { lamp.setLight(Is[i]); lamp.on(); } } }
Red Light shine in Red Yellow Light shine in Yellow Green Light shine in Green
7. Write the execution result of the following code
package com.qf.q8.t7; interface JavaTeacher{ void teach(); } class TeacherA implements JavaTeacher{ public void teach() { System.out.println("TeacherA teach Java"); } } class TeacherB implements JavaTeacher{ public void teach() { System.out.println("TeacherB teach Java"); } } class School{ public static JavaTeacher getTeacher(int i) { if(i==0)return new TeacherA(); else return new TeacherB(); } } public class TestSchool { public static void main(String[] args) { JavaTeacher jt = School.getTeacher(0); jt.teach(); jt = School.getTeacher(10); jt.teach(); } }
TeacherA teach Java TeacherB teach Java
8. Fill in the blank Code:
package com.qf.q8.t8; abstract class Animal{ public abstract void eat(); } interface Pet{ void play(); } class Dog extends Animal implements Pet{ public void eat() { System.out.println("Dog eat Bones"); } public void play() { System.out.println("Play with Dog"); } } class Cat extends Animal implements Pet{ public void eat() { System.out.println("Cat eat fish"); } public void play() { System.out.println("Play with Cat"); } } class Wolf extends Animal{ public void eat() { System.out.println("Wolf eat meat"); } } public class TestMain { public static void main(String[] args) { Animal as[] = new Animal[3]; as[0] = new Dog(); as[1] = new Cat(); as[2] = new Wolf(); //Call the eat method of all animals in as array //1 for(int i = 0 ;i<as.length;i++) { as[i].eat(); } //Call play method of all pets in as array //2 for(int i = 0 ;i<as.length;i++) { if(as[i]instanceof Pet) { Pet pet = (Pet)as[i]; pet.play(); } } } }
9. Modify the code based on the 17 questions in the original Chap6
The company will pay salaried employee another 2000 yuan of overtime every month and BasePlusEmployee 1000 yuan of overtime Change to the original code and add the above logic. And write a method to print out the total amount of overtime paid by the company this month.
package com.qf.q8.t9; public class TestEmplyee { public static void main(String[] args) { Employee[] e =new Employee[4]; e[0] = new SalariedEmployee("john",5,5000); e[1] = new HourlyEmployee("tom",10,25,170); e[2] = new SalesEmployee("kunyan",7,200000,0.03); e[3] = new BasePlusSalesEmployee("kunyan",8,1000000,0.02,5000); double sum=0; for(int i=0;i<e.length;i++) { if(e[i] instanceof Overtime) { Overtime ot = (Overtime)e[i]; sum+=ot.getOvertimePay(); } System.out.println(e[i].getName()+"Wages are"+e[i].getSalary(5)); } System.out.println("The company will pay overtime pay in total this month"+sum); } } interface Overtime{ double getOvertimePay(); } abstract class Employee{ private String name; private int month; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public Employee(String name, int month) { this.name = name; this.month = month; } public double getSalary(int month) { if(this.month==month) return 100; else return 0; } } class SalariedEmployee extends Employee implements Overtime{ private double salary; public SalariedEmployee(String name,int month,double salary) { super(name,month); this.salary = salary; } public void setSalary(double salary) { this.salary = salary; } public double getSalary(int month) { return salary+super.getSalary(month); } public double getOvertimePay() { return 2000; } } class HourlyEmployee extends Employee{ private int hours; private double hourlySalary; public HourlyEmployee(String name,int month,int hours,double hourlySalary) { super(name,month); this.hours=hours; this.hourlySalary = hourlySalary; } public double getSalary(int month) { if(getHours()>160) { return 160*this.hourlySalary+(this.hourlySalary-160)*1.5*this.hourlySalary; }else { return this.hourlySalary*this.hours+super.getSalary(month); } } public int getHours() { return hours; } public void setHours(int hours) { this.hours = hours; } public double getHourlySalary() { return hourlySalary; } public void setHourlySalary(double hourlySalary) { this.hourlySalary = hourlySalary; } } class SalesEmployee extends Employee{ private double sales; private double rate; public SalesEmployee(String name,int month,double sales,double rate) { super(name,month); this.sales=sales; this.rate = rate; } public double getOvertimePay() { return 0; } public double getSalary(int month) { return this.sales*this.rate+super.getSalary(month); } public double getSales() { return sales; } public void setSales(double sales) { this.sales = sales; } public double getRate() { return rate; } public void setRate(double rate) { this.rate = rate; } } class BasePlusSalesEmployee extends SalesEmployee implements Overtime{ private double baseSalary; public BasePlusSalesEmployee(String name, int month, double sales, double rate,double baseSalary) { super(name, month, sales, rate); this.baseSalary = baseSalary; } public double getSalary(int month) { return this.baseSalary+super.getSalary(month); } public double getOvertimePay() { return 1000; } }
10. There are the following codes:
package com.qf.q8.t10; interface ServiceInterface { void doService1(); void doService2(); void doService3(); } abstract class AbstractService implements ServiceInterface{ public void doService1() {} public void doService2() {} public void doService3() {} }
You need a MyService class that implements the ServiceInterface interface. 1. The first way is to enable MyService to implement the ServiceInterface interface, namely: class MyService implements ServiceInterface
class MyService implements ServiceInterface{ public void doService1() {} public void doService2() {} public void doService3(){} }
2. The second way is to let MyService inherit AbstractService class, that is, class MyService extends AstractService
class MyService extends AbstractService{ public void doService1() {} public void doService2() {} public void doService3() {} }
Excuse me: what's the difference between the two ways? What is the function of the AbstractService class? The abstract methods in the interface must be implemented Inherited classes do not need to implement all methods, and can be overridden.
11. Test Goldbach's conjecture
Enter an even number greater than 6. Please output the sum of which two prime numbers this even number can be decomposed into. For example, 10 = 3 + 7, 12 = 5 + 7 Requirement: two people work together. One person is responsible for splitting a n integer n into the sum of two integers, and the other person is responsible for writing a function to determine whether an integer a is a prime number.
package com.qf.q8.t11; import java.util.Scanner; interface MathTool{ boolean isPrime(int n); } class MathToolImpl implements MathTool{ public boolean isPrime(int n) { for(int i=2;i<=Math.sqrt(n);i++) { if(n%i==0) return false; } return true; } } public class Gedebahe { public static void main(String[] args) { System.out.println("Please enter an even number greater than 6:"); int n = new Scanner(System.in).nextInt(); if(n%2!=0 || n<=6) { System.out.println("parameter error!"); } MathTool mt = new MathToolImpl(); for(int i=2;i<=n/2;i++) { if(mt.isPrime(i)&&mt.isPrime(n-i)) { System.out.print(n+"="+i+"+"+(n-i)); System.out.println(); } } } }