abstract class Teacher{
String name;
int rate;
int totalHours;
Teacher(){}
Teacher(String n,int r,int t){
name=n;
rate=r;
totalHours=t;
}
abstract double salary();
}
class FullTimeTeacher extends Teacher{
FullTimeTeacher(){}
FullTimeTeacher(String n,int r,int t){
super(n,r,t);
}
double salary(){
return rate*9+((totalHours-9)*rate*0.8);
}
}
class PartTimeTeacher extends Teacher{
PartTimeTeacher(){}
PartTimeTeacher(String n,int r,int t){
super(n,r,t);
}
double salary(){
return rate*totalHours;
}
}
public class JPA06_1 {
public static void main(String argv[]) {
PartTimeTeacher p1 = new PartTimeTeacher("John",400,2);
PartTimeTeacher p2 = new PartTimeTeacher("Mary",300,4);
FullTimeTeacher f1 = new FullTimeTeacher("Peter",400,9);
FullTimeTeacher f2 = new FullTimeTeacher("Paul",300,12);
FullTimeTeacher f3 = new FullTimeTeacher("Eric",350,15);
System.out.println("John-PartTime" + p1.salary());
System.out.println("Mary-PartTime" + p2.salary());
System.out.println("Peter-FulLTime" + f1.salary());
System.out.println("Paul-FulLTime" + f2.salary());
System.out.println("Eric-FulLTime" + f3.salary());
}
}
第二題
abstract class Teacher{
String name;
int rate;
int totalHours;
Teacher(){}
Teacher(String n,int r,int t){
name=n;
rate=r;
totalHours=t;
}
abstract double salary();
double afterTaxIns(){
return salary()*0.9-100;
}
}
class Manager extends FullTimeTeacher{
int rank;
Manager(){}
Manager(String n,int r,int t,int k){
super(n,r,t);
rank=k;
}
double salary(){
return super.salary()+rank*500;
}
double getTotalSalary(){
return salary();
}
}
public class JPA06_2 {
public static void main(String argv[]) {
PartTimeTeacher p1 = new PartTimeTeacher("John",400,2);
PartTimeTeacher p2 = new PartTimeTeacher("Mary",300,4);
FullTimeTeacher f1 = new FullTimeTeacher("Peter",400,9);
FullTimeTeacher f2 = new FullTimeTeacher("Paul",300,12);
FullTimeTeacher f3 = new FullTimeTeacher("Eric",350,15);
System.out.println("John-afterTaxIns:" + p1.afterTaxIns());
System.out.println("Mary-afterTaxIns:" + p2.afterTaxIns());
System.out.println("Peter-afterTaxIns:" + f1.afterTaxIns());
System.out.println("Paul-afterTaxIns:" + f2.afterTaxIns());
System.out.println("Eric-afterTaxIns:" + f3.afterTaxIns());
Manager am1 = new Manager("Fang", 500, 12, 3);
System.out.println("Fang-Manager:" + am1.getTotalSalary());
System.out.println("Fang-afterTaxIns:" + am1.afterTaxIns());
}
}
第三題
abstract class Teacher{
String name;
int rate;
int totalHours;
Teacher(){}
Teacher(String n,int r,int t){
name=n;
rate=r;
totalHours=t;
}
abstract double salary();
double afterTaxIns(){
return salary()*0.9-100;
}
void compare(Teacher t){
if(salary()>t.salary()){
System.out.println(name+" is higher than "+t.name);
}
else{
System.out.println(t.name+" is higher than "+name);
}
}
}
public class JPA06_3 {
public static void main(String argv[]) {
PartTimeTeacher p1 = new PartTimeTeacher("John",400,2);
PartTimeTeacher p2 = new PartTimeTeacher("Mary",300,4);
FullTimeTeacher f1 = new FullTimeTeacher("Peter",400,9);
FullTimeTeacher f2 = new FullTimeTeacher("Paul",300,12);
FullTimeTeacher f3 = new FullTimeTeacher("Eric",350,15);
Manager am1 = new Manager("Fang", 500, 12, 3);
am1.compare(f3);
p1.compare(f3);
}
}
第四題
import java.util.*;
class TeacherDB{
HashMap<String,Teacher> MultiFund;
TeacherDB(){
MultiFund = new HashMap<String,Teacher>();
}
void store(String s,Teacher t){
MultiFund.put(s,t);
}
double totalOfAll(){
double sum=0;
for(String key:MultiFund.keySet()){
sum+=MultiFund.get(key).afterTaxIns();
}
return sum;
}
}
public class JPA06_4 {
public static void main(String argv[]) {
PartTimeTeacher p1 = new PartTimeTeacher("John",400,2);
PartTimeTeacher p2 = new PartTimeTeacher("Mary",300,4);
FullTimeTeacher f1 = new FullTimeTeacher("Peter",400,9);
FullTimeTeacher f2 = new FullTimeTeacher("Paul",300,12);
FullTimeTeacher f3 = new FullTimeTeacher("Eric",350,15);
Manager am1 = new Manager("Fang", 500, 12, 3);
TeacherDB school = new TeacherDB();
school.store("John", p1);
school.store("Mary", p2);
school.store("Peter", f1);
school.store("Paul", f2);
school.store("Eric", f3);
school.store("Fang", am1);
System.out.println("Total salary: " + school.totalOfAll());
}
}
第五題
import java.util.*;
class TeacherDB{
HashMap<String,Teacher> MultiFund;
TeacherDB(){
MultiFund = new HashMap<String,Teacher>();
}
void store(String s,Teacher t){
MultiFund.put(s,t);
}
double totalOfAll(){
double sum=0;
for(String key:MultiFund.keySet()){
sum+=MultiFund.get(key).afterTaxIns();
}
return sum;
}
void printAllTeacher(){
for(String key:MultiFund.keySet())
{
try{
if(key.equals("Fang")){
continue;
}
if(MultiFund.get(key).salary()<1500){
throw new Exception("**"+key+"__Salary__"+MultiFund.get(key).salary());
}
System.out.println(key+"__Salary__"+MultiFund.get(key).salary());
}
catch(Exception s)
{
System.out.println(s.getMessage());
}
}
}
}
沒有留言:
張貼留言