第一題
abstract class MIS {
String name;
double chi=0;
double eng=0;
MIS(){}
MIS(String s,double ch,double en){
name=s;
chi=ch;
eng=en;
}
abstract double averageElect();
double averageMust(){
return (chi+eng)/2.0;
}
double averageAll(){
return averageElect()*.6+averageMust()*0.4;
}
}
class IT extends MIS{
double pl=0;
double db=0;
double ds=0;
IT(){}
IT(String n,double ch,double en,double p,double b,double s){
super(n,ch,en);
pl=p;
db=b;
ds=s;
}
double averageElect(){
return (pl+db+ds)/3;
}
}
class IM extends MIS{
double econ=0;
double acct=0;
IM(){}
IM(String n,double ch,double en,double e,double a){
super(n,ch,en);
econ=e;
acct=a;
}
double averageElect(){
return (econ+acct)/2;
}
}
public class JPA06_1 {
public static void main(String argv[]) {
MIS s1 = new IT("John", 88, 90, 76, 68, 84);
MIS s2 = new IM("Paul", 92, 80, 76, 68);
System.out.printf("John's elect score: %.2f all score %.2f\n", s1.averageElect(), s1.averageAll());
System.out.printf("Paul's elect score: %.2f all score %.2f\n", s2.averageElect(), s2.averageAll());
}
}
第二題
class ITM extends IT{
double acct=0;
ITM(){}
ITM(String n,double ch,double en,double p,double b,double s,double a){
super(n,ch,en,p,b,s);
acct=a;
}
double averageElect(){
return (super.averageElect()+acct)/2;
}
double averageAll(){
return super.averageElect()*0.4+averageMust()*0.4+acct*0.2;
}
}
public class JPA06_2 {
public static void main(String argv[]) {
MIS s3 = new ITM("Mary", 79, 88, 94, 92, 83, 69);
System.out.printf("Mary's elect score: %.2f all score %.2f\n", s3.averageElect(), s3.averageAll());
}
}
第三題
abstract class MIS {
String name;
Q
double chi=0;
double eng=0;
static int TotalCount=0;
MIS(){}
MIS(String s,double ch,double en){
name=s;
chi=ch;
eng=en;
TotalCount++;
}
abstract double averageElect();
double averageMust(){
return (chi+eng)/2.0;
}
double averageAll(){
return averageElect()*.6+averageMust()*0.4;
}
}
public class JPA06_3 {
public static void main(String argv[]) {
MIS s1 = new IT("John", 88, 90, 76, 68, 84);
MIS s2 = new IM("Paul", 92, 80, 76, 68);
MIS s3 = new ITM("Mary", 79, 88, 94, 92, 83, 69);
System.out.printf("John's elect score: %.2f all score %.2f\n", s1.averageElect(), s1.averageAll());
System.out.printf("Paul's elect score: %.2f all score %.2f\n", s2.averageElect(), s2.averageAll());
System.out.printf("Mary's elect score: %.2f all score %.2f\n", s3.averageElect(), s3.averageAll());
System.out.println("Total students: " + MIS.TotalCount);
}
}
第四題
import java.util.*;
class MISClass{
HashMap<String,MIS> List;
MISClass(){
List = new HashMap<String,MIS>();
}
void put(String a,MIS m){
List.put(a,m);
}
void list(){
for(String key:List.keySet()){
System.out.printf("%s: %.2f\n",key,List.get(key).averageAll());
}
}
}
public class JPA06_4 {
public static void main(String argv[]) {
MIS s1 = new IT("John", 88, 90, 76, 68, 84);
MIS s2 = new IM("Paul", 92, 80, 76, 68);
MIS s3 = new ITM("Mary", 79, 88, 94, 92, 83, 69);
//System.out.printf("John's elect score: %.2f all score %.2f\n", s1.averageElect(), s1.averageAll());
//System.out.printf("Paul's elect score: %.2f all score %.2f\n", s2.averageElect(), s2.averageAll());
//System.out.printf("Mary's elect score: %.2f all score %.2f\n", s3.averageElect(), s3.averageAll());
MISClass c1 = new MISClass();
c1.put("John", s1);
c1.put("Paul", s2);
c1.put("Mary", s3);
c1.list();
}
}
第五題
import java.util.*;
class MISClass{
HashMap<String,MIS> List;
MISClass(){
List = new HashMap<String,MIS>();
}
void put(String a,MIS m){
List.put(a,m);
}
void list()throws Exception{
for(String key:List.keySet()){
if(List.get(key).averageAll()>100){
throw new Exception("**"+key+": "+List.get(key).averageAll());
}
System.out.printf("%s: %.2f\n",key,List.get(key).averageAll());
}
}
}
public class JPA06_5 {
public static void main(String argv[]) {
MISClass c1 = new MISClass();
c1.put("Peter", new IM("Peter", 89, 980, 77, 69));
try{
c1.list();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
沒有留言:
張貼留言