610 員工薪資制度

第一題

class Employee{
String ID;
double Salary;
Employee(){}
Employee(String i){
ID=i;
}
double monthPay(){
return Salary;
}
}

class SalaryWorker extends Employee{
SalaryWorker(){}
SalaryWorker(String id,double yearSalary){
super(id);
Salary=yearSalary/12.0;
}

}

class HourlyWorker extends Employee{
double Hours;
HourlyWorker(){}
HourlyWorker(String id,double hourSalary,double hours){
super(id);
Salary=hourSalary*hours;
}
}

class Manager extends SalaryWorker{
Manager(){}
Manager(String id,double yearSalary,double bonus){
super(id,yearSalary);
Salary=monthPay()+bonus;
}
}

public class JPA06_1 {
   public static void main(String argv[]) {
        SalaryWorker sw1 = new SalaryWorker("96001",180000);
        System.out.println("SalaryWorker:" + sw1.monthPay());
        HourlyWorker hw1 = new HourlyWorker("96002", 100, 160);
        System.out.println("HourlyWorker:" + hw1.monthPay());
        Manager ma1 = new Manager("97001", 240000, 5000);
        System.out.println("Manager:" + ma1.monthPay());
    }
}


第二題

class Employee{
String ID;
double Salary;
Employee(){}
Employee(String i){
ID=i;
}
double monthPay(){
return Salary;
}

String compare(Employee w){
if(monthPay()>w.monthPay()){
return ID;
}
else{
return w.ID;
}
}
double monthTaxes(){
return monthPay()*0.15;
}
}

public class JPA06_2 {
    public static void main(String argv[]) {

        SalaryWorker sw1 = new SalaryWorker("96001",180000);
        System.out.println("SalaryWorker:" + sw1.monthPay());
        HourlyWorker hw1 = new HourlyWorker("96002", 100, 160);
        System.out.println("HourlyWorker:" + hw1.monthPay());
        Manager ma1 = new Manager("97001", 240000, 5000);
        System.out.println("Manager:" + ma1.monthPay());

        System.out.println(sw1.compare(hw1)+"較高");
System.out.println(hw1.compare(ma1)+"較高");

        System.out.println("SalaryWorker稅額:" + sw1.monthTaxes());
        System.out.println("HourlyWorker稅額:" + hw1.monthTaxes());
        System.out.println("Manager稅額:" + ma1.monthTaxes());
    }
}

class HourlyWorker extends Employee{
double Hours;
HourlyWorker(){}
HourlyWorker(String id,double hourSalary,double hours){
super(id);
Salary=hourSalary*hours;
}
}

class Manager extends SalaryWorker{
Manager(){}
Manager(String id,double yearSalary,double bonus){
super(id,yearSalary);
Salary=monthPay()+bonus;
}
}

public class JPA06_1 {
   public static void main(String argv[]) {
        SalaryWorker sw1 = new SalaryWorker("96001",180000);
        System.out.println("SalaryWorker:" + sw1.monthPay());
        HourlyWorker hw1 = new HourlyWorker("96002", 100, 160);
        System.out.println("HourlyWorker:" + hw1.monthPay());
        Manager ma1 = new Manager("97001", 240000, 5000);
        System.out.println("Manager:" + ma1.monthPay());
    }
}


第三題

class Employee{
String ID;
double Salary;
static double count=0; //累計人數
static double sumTaxes=0;//累計稅
Employee(){}
Employee(String i){
ID=i;
count++;


}
double monthPay(){
return Salary;
}

String compare(Worker w){
if(monthPay()>w.monthPay()){
return ID;
}
else{
return w.ID;
}
}
double monthTaxes(){
double Taxs=monthPay()*0.15;
sumTaxes+=Taxs;
return Taxs;
}
static double getAverageTax(){
return sumTaxes/count;
}

}

public class JPA06_3 {
    public static void main(String argv[]) {
      SalaryWorker sw1 = new SalaryWorker("96001",180000);
        HourlyWorker hw1 = new HourlyWorker("96002", 100, 160);
        Manager ma1 = new Manager("97001", 240000, 5000);

        System.out.println("SalaryWorker稅額:" + sw1.monthTaxes());
        System.out.println("HourlyWorker稅額:" + hw1.monthTaxes());
        System.out.println("Manager稅額:" + ma1.monthTaxes());
        System.out.println("平均稅額:" + Employee.getAverageTax());
    }
}


第四題

import java.util.*;
class Management{
HashMap<String,Employee> list;
Management(){
list = new HashMap<String,Employee>();
}
void put(String s,Employee e){
list.put(s,e);
}
double afterTax(String s){
return list.get(s).monthPay()*0.85;
}
}

public class JPA06_4 {
    public static void main(String argv[]) {
        SalaryWorker sw1 = new SalaryWorker("96001",180000);
        HourlyWorker hw1 = new HourlyWorker("96002", 100, 160);
        Manager ma1 = new Manager("97001", 240000, 5000);
        Management m = new Management();
        m.put("96001", sw1);
        m.put("96002", hw1);
        m.put("97001", ma1);
        System.out.println("97001 的稅後薪資: " + m.afterTax("97001"));
    }
}


第五題

import java.util.*;
class Management{
HashMap<String,Employee> list;
Management(){
list = new HashMap<String,Employee>();
}
void put(String s,Employee e){
list.put(s,e);
}
double afterTax(String s){
return list.get(s).monthPay()*0.85;
}
double totalSalary()throws Exception{
double total=0;
for(String s:list.keySet()){
total+=list.get(s).monthPay();
if(total>50000){
throw new Exception("Total salary exceed limit: "+total);
}
}
return total;
}
}

public class JPA06_5 {
    public static void main(String argv[]) {
        SalaryWorker sw1 = new SalaryWorker("96001",180000);
        HourlyWorker hw1 = new HourlyWorker("96002", 100, 160);
        Manager ma1 = new Manager("97001", 240000, 5000);
        Management m = new Management();
        m.put("96001", sw1);
        m.put("96002", hw1);
        m.put("97001", ma1);
        try{
        m.totalSalary();
        }
        catch(Exception e){
        System.out.println(e.getMessage());
        }

    }
}

609 堆疊擴充

第一題

class BoundedStack{
int count=0;
String []stack;
BoundedStack(){}
BoundedStack(int i){
stack= new String[i];
}
void push(String s){
if(count>=stack.length){
System.out.println("stack-overflow");
count--;
}else{
stack[count]=s;
count++;
}
}
String pop(){
if(count>=0){
String s=stack[count];
count--;
return s;
}else{
return "stack-is-empty";
}
}
boolean empty(){

return count==-1;
}

}

public class JPA06_1 {
    public static void main(String args[]) {
        BoundedStack s = new BoundedStack(3);
        s.push("abc");
        s.push("def");
        s.push("ghi");
        s.push("jkl");

        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.empty());
    }
}


第二題

import java.util.*;
class UnboundedStack{
LinkedList <String> list;
UnboundedStack(){
list = new LinkedList <String>();
}
void push(String s){
list.addFirst(s);
}
String pop(){

if(list.isEmpty()){
return "stack-is-empty";
}else{

String s=list.getFirst();
  list.removeFirst();
    return s;
}
}
boolean empty(){
return list.isEmpty();

}
}

public class JPA06_2 {
    public static void main(String args[]) {
        UnboundedStack s = new UnboundedStack();
        s.push("abc");
        s.push("def");
        s.push("ghi");
        s.push("jkl");

        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.empty());
        System.out.println(s.pop());
        System.out.println(s.empty());
    }
}


第三題

import java.util.*;
abstract class Stack{
abstract String pop();
abstract void push(String s);
String top(){
String s=pop();
push(s);
return s;
}
}
class BoundedStack extends Stack{
int count=0;
String []stack;
BoundedStack(){}
BoundedStack(int i){
stack= new String[i];
}
void push(String s){
if(count>=stack.length){
System.out.println("stack-overflow");
count--;
}else{
stack[count]=s;
count++;
}
}
String pop(){
if(count>=0){
String s=stack[count];
count--;
return s;
}else{
return "stack-is-empty";
}
}
boolean empty(){

return count==-1;
}

}
class UnboundedStack extends Stack{
LinkedList <String> list;
UnboundedStack(){
list = new LinkedList <String>();
}
void push(String s){
list.addFirst(s);
System.out.println("Pushing:"+s);
}
String pop(){

if(list.isEmpty()){
return "stack-is-empty";
}else{
String s=list.getFirst();
  list.removeFirst();
  System.out.println("Poping:"+s);
    return s;
}
}
boolean empty(){
return list.isEmpty();

}
}
class TraceUnboundedStack extends UnboundedStack{
int getSize(){
return list.size();
}
}
public class JPA06_3 {
    public static void main(String args[]) {
        TraceUnboundedStack s2 = new TraceUnboundedStack();
        s2.push("abc");
        s2.push("def");
        s2.push("ghi");
        s2.push("jkl");
        System.out.println(s2.getSize());
        System.out.println(s2.top());
        System.out.println(s2.pop());
        System.out.println(s2.pop());
        System.out.println(s2.pop());
        System.out.println(s2.empty());
        System.out.println(s2.pop());
        System.out.println(s2.empty());
        System.out.println(s2.getSize());
    }
}


第四題

import java.util.*;

abstract class Stack{
abstract Object pop();
abstract void push(Object s);
Object top(){
Object s=pop();
push(s);
return s;
}
}

class UnboundedStack extends Stack{
LinkedList <Object> list;
UnboundedStack(){
list = new LinkedList <Object>();
}
void push(Object s){
list.addFirst(s);
//System.out.println("Pushing:"+s);
}
Object pop(){

if(list.isEmpty()){
return "null";
}else{
Object s=list.getFirst();
  list.removeFirst();
  //System.out.println("Poping:"+s);
    return s;
}
}
boolean empty(){
return list.isEmpty();

}
}
class TraceUnboundedStack extends UnboundedStack{
int getSize(){
return list.size();
}
}

public class JPA06_4 {
    public static void main(String args[]) {
        UnboundedStack s = new UnboundedStack();
        s.push("abc");
s.push(2);
s.push("ghi");
        System.out.println(s.top());
        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.pop());
        System.out.println(s.pop());
    }
}


第五題

import java.util.*;
class UnboundedStack extends Stack{
LinkedList <Object> list;
UnboundedStack(){

list = new LinkedList <Object>();
}
void push(Object s){
list.addFirst(s);

}

Object pop() throws IndexOutOfBoundsException{

if(list.isEmpty()){
throw new IndexOutOfBoundsException("stack is empty!");
}else{
Object s=list.getFirst();
  list.removeFirst();
    return s;
}

}

boolean empty(){
return list.isEmpty();

}

}
public class JPA06_5 {
    public static void main(String args[]) {
        try {
            UnboundedStack s = new UnboundedStack();
            s.push("abc");
            s.push(2);
            s.push("ghi");
            System.out.println(s.top());
            System.out.println(s.pop());
            System.out.println(s.pop());
            System.out.println(s.pop());
            System.out.println(s.pop());
        } catch(IndexOutOfBoundsException e){
        System.out.println(e.getMessage());
        }

    }
}

608 食物熱量計算

第一題

abstract class Food{
int  amount=0;
int calorie=0;
Food(){}
Food(int x){ amount=x; }
void setCaloriePerGram(int x){
calorie = x;
}
int getAmount(){
return amount;
}
int getCalorie(){
return amount*calorie;
}
}
class Rice extends Food{
Rice(){}
Rice(int i){
super(i);
setCaloriePerGram(1);
}
}
class Egg extends Food{
Egg(){}
Egg(int i){
super(i);
setCaloriePerGram(2);
}
}
class Cabbage extends Food{
Cabbage(){}
Cabbage(int i){
super(i);
setCaloriePerGram(1);
}
}
class PorkRib extends Food{
PorkRib(){}
PorkRib(int i){
super(i);
setCaloriePerGram(10);
}
}
class Carrot extends Food{
Carrot(){}
Carrot(int i){
super(i);
setCaloriePerGram(1);
}
}

class JPD06_1
{
    public static void main(String args[])
    {
        Rice rice = new Rice(100);
        System.out.println( rice.getAmount() + " grams of rice has " + rice.getCalorie() + " calories.");

        Egg egg = new Egg(30);
        System.out.println( egg.getAmount() + " grams of egg has " + egg.getCalorie() + " calories.");

        Cabbage cabbage = new Cabbage(50);
        System.out.println( cabbage.getAmount() + " grams of cabbage has " + cabbage.getCalorie() + " calories.");

        PorkRib porkRib = new PorkRib(300);
        System.out.println( porkRib.getAmount() + " grams of pork rib has " + porkRib.getCalorie() + " calories.");

        Carrot carrot = new Carrot(100);
        System.out.println( carrot.getAmount() + " grams of carrot has " + carrot.getCalorie() + " calories.");
    }
}


第二題

import java.util.*;

class LunchBox{
int calorie=0;
Vector <Food> content;
LunchBox(){
content=new Vector<Food>();
}
void add(Food f){
content.add(f);
}
int getCalorie(){
for(Food f:content){
calorie+=f.getCalorie();
}
return calorie;
}

}

class JPA06_2
{
    public static void main(String args[])
    {
        LunchBox economy = new LunchBox();
        economy.add(new Rice(200));
        economy.add(new Cabbage(100));
        economy.add(new PorkRib(250));
        System.out.println("Total calories of an economy lunch box are " + economy.getCalorie() +".");

        LunchBox valuedChoice = new LunchBox();
        valuedChoice.add(new Rice(200));
        valuedChoice.add(new Egg(30));
        valuedChoice.add(new Carrot(100));
        valuedChoice.add(new PorkRib(300));
        System.out.println("Total calories of a valued-choice lunch box are " + valuedChoice.getCalorie()+".");

    }
}


第三題

import java.util.*;

class LunchBox{
int calorie=0;
Vector <Food> content;
LunchBox(){
content=new Vector<Food>();
}
void add(Food f){
content.add(f);
}
int getCalorie(){
for(Food f:content){
calorie+=f.getCalorie();
}
return calorie;
}

double getCost(){
double cost=0;
for(Food f:content){
cost+=f.getCost();
}
return cost;
}
double priceRatio=1;
void setPriceRatio(double x){
priceRatio=x;
}
double getPrice(){
return getCost()*priceRatio;
}

}
abstract class Food{

int amount=0;
int calorie=0;
Food(){}
Food(int x){ amount=x; }
void setCaloriePerGram(int x){
calorie = x;
}
int getAmount(){
return amount;
}
int getCalorie(){
return amount*calorie;
}

int unitCost=0;
void setCost(int x){
unitCost=x;
}
int getCost(){
return unitCost*amount;
}
}
class Rice extends Food{
Rice(){}
Rice(int i){
super(i);
setCaloriePerGram(1);
setCost(1);
}
}
class Egg extends Food{
Egg(){}
Egg(int i){
super(i);
setCaloriePerGram(2);
setCost(2);
}
}
class Cabbage extends Food{
Cabbage(){}
Cabbage(int i){
super(i);
setCaloriePerGram(1);
setCost(3);
}
}
class PorkRib extends Food{
PorkRib(){}
PorkRib(int i){
super(i);
setCaloriePerGram(10);
setCost(8);
}
}
class Carrot extends Food{
Carrot(){}
Carrot(int i){
super(i);
setCaloriePerGram(1);
setCost(3);
}
}

class JPD06_3
{
    public static void main(String args[])
    {
        LunchBox economy = new LunchBox();
        economy.add(new Rice(200));
        economy.add(new Cabbage(100));
        economy.add(new PorkRib(250));
        economy.setPriceRatio(1.2);
        System.out.println("Total calories of an economy lunch box are " + economy.getCalorie());
        System.out.println("The price of an economy lunch box is " + economy.getPrice());

        LunchBox valuedChoice = new LunchBox();
        valuedChoice.add(new Rice(200));
        valuedChoice.add(new Egg(30));
        valuedChoice.add(new Carrot(100));
        valuedChoice.add(new PorkRib(300));
        valuedChoice.setPriceRatio(1.3);
        System.out.println("Total calories of a valued-choice lunch box are " + valuedChoice.getCalorie());
        System.out.println("The price of a valued-choice lunch box is " + valuedChoice.getPrice());
    }
}


第四題

import java.util.*;

class LunchBox{
int calorie=0;
Vector <Food> content;

LunchBox(){
content=new Vector<Food>();
}

void add(Food f){
content.add(f);
}

int getCalorie(){
for(Food f:content){
calorie+=f.getCalorie();
}
return calorie;
}

double getCost(){
double cost=0;
for(Food f:content){
cost+=f.getCost();
}
return cost;
}

double priceRatio=1;

void setPriceRatio(double x){
priceRatio=x;
}

double getPrice(){
return getCost()*priceRatio;
}


String isCheaperThan(LunchBox x){

if(getPrice()<x.getPrice()){
return "YES!";
}
else{
return "NO!";
}
}

}
class JPD06_4
{
    public static void main(String args[])
    {
        LunchBox economy = new LunchBox();
        economy.add(new Rice(200));
        economy.add(new Cabbage(100));
        economy.add(new PorkRib(250));
        economy.setPriceRatio(1.2);

        LunchBox valuedChoice = new LunchBox();
        valuedChoice.add(new Rice(200));
        valuedChoice.add(new Egg(30));
        valuedChoice.add(new Carrot(100));
        valuedChoice.add(new PorkRib(300));
        valuedChoice.setPriceRatio(1.3);

        System.out.println("Is the economy lunch box cheaper than the valued-choice? " + economy.isCheaperThan(valuedChoice));

    }
}


第五題

import java.util.*;

class SaleReport{

ArrayList <LunchBox> list;

int count;

SaleReport(){
list=new ArrayList<LunchBox>();
count=0;
}

void add(LunchBox l){
System.out.println("Profit is " + l.getPrice() + ".");
System.out.println("Profit is " + l.getCost() + ".");
list.add(l);
count++;
}

int getNumberOfLunchBox(){
return count;
}

int profit;

int getProfit(){
profit=0;
for(LunchBox l : list){
System.out.println("Profit is " + l.getPrice() + ".");
System.out.println("Profit is " + l.getCost() + ".");
profit+=(int)(l.getPrice()-l.getCost());
}
return profit;
}
}

class JPA06_5
{
    public static void main(String args[])
    {
        LunchBox economy = new LunchBox();
        economy.add(new Rice(200));
        economy.add(new Cabbage(100));
        economy.add(new PorkRib(250));
        economy.setPriceRatio(1.2);

        LunchBox valuedChoice = new LunchBox();
        valuedChoice.add(new Rice(200));
        valuedChoice.add(new Egg(30));
        valuedChoice.add(new Carrot(100));
        valuedChoice.add(new PorkRib(300));
        valuedChoice.setPriceRatio(1.3);

        SaleReport sr = new SaleReport();
        sr.add(economy);
        sr.add(valuedChoice);
        System.out.println( sr.getNumberOfLunchBox() + " lunch boxes have been sold.");
        System.out.println("Profit is " + sr.getProfit() + ".");

    }
}

607 電腦銷售業績

第一題

class NB{
int cost;
NB(){}
NB(int no){
switch(no){
case 1:
cost=10000;break;
case 2:
cost=8500;break;
}
}
int getCost(){
return cost;
}
}

public class JPA06_1 {
    public static void main(String args[]){
        NB e1 = new NB(1);
System.out.println("一台17\"筆計型電腦的成本:"+e1.getCost());
        NB e2 = new NB(2);
System.out.println("一台14\"筆計型電腦的成本:"+e2.getCost());

       }
}


第二題

abstract class CNB{
HD h;
  CPU c;
  CNB(){}
  CNB(int i,String s){
  c=new CPU(s);
  h= new HD(i);
  }
abstract double cost();
double price(){
return cost()*1.5;
}

}

class BasicNB extends CNB{
BasicNB(){}
BasicNB(int i,String s){
super(i,s);
}
double cost(){
return c.getCost()+1000+h.getCost();
}
}

class LuxNB extends CNB{
LuxNB(){}
LuxNB(int i,String s){
super(i,s);
}
double cost(){
return c.getCost()+2000+h.getCost();
}
}
class CPU extends NB{

CPU(){}
CPU(String s){
if(s.equals("basic")){
cost = 1000;
}
else{
cost = 2000;
}
}
}
class HD extends NB{

HD(){}
HD(int i){
if(i==1){
cost=5000;
}
else{
cost=8500;
}
}
}

public class JPA06_2 {
    public static void main(String args[]){

        BasicNB bc = new BasicNB(1,"basic");
        System.out.println("商用電腦成本: " + bc.cost());
        System.out.println("商用電腦售價: " + bc.price());

        LuxNB lc = new LuxNB(2,"Lux");
        System.out.println("高階雙核心電腦成本: " + lc.cost());
        System.out.println("高階雙核心電腦售價: " + lc.price());
    }
}


第三題

public class JPA06_3 {
   public static void main(String[] arge){

       int [][]array={{120,420,315,250,418,818,900},
        {212,183,215,89,83,600,700},
        {215,500,430,210,300,918,880}};
       String []name={"北部","中部","南部"};

       System.out.println("\n\t  第一電腦科技公司週報表 ( 單 位 : 萬 元 ) ");
       System.out.println( "直營店 \t 一 \t 二 \t 三 \t 四 \t 五 \t六 \t 日 \t  ");
       System.out.println( "=====\t====\t====\t====\t====\t====\t====\t====");

  for(int i =0;i<3;i++){
  System.out.printf(name[i]+"\t");
  for(int j =0;j<7;j++){
  System.out.print(array[i][j]+"\t");
  }
  System.out.println();
  }


     }
}


第四題

public class JPA06_4 {
   public static void main(String[] arge){
       String[] map = { "北部" , "中部" , "南部" };
       int[][] salary = {{ 120 , 420 , 315 , 250 , 418,818,900 } ,
        { 312 , 183 , 215 , 89 , 83,600,700 } ,
        { 215 , 500 , 430 , 210 , 300,918,880 }};
       int cost[] = {0,0,0};
       int sum[] = {0,0,0,0,0,0,0};
       int data[] = {0,0,0};
       int[][]  a_box = salary ;

       int i , j , i_max , j_max, min;
       double ratio;

  int saleCost[]={1500,1515,1858};
  int openCost[]={180,200,360};
       i_max = 3;
       j_max = 7;
       for( i = 0 ; i <  i_max ; i++ ){
cost[i]= saleCost[i]+openCost[i];
           for( j=0 ; j<j_max ; j++ ){
            sum[i]+=salary[i][j];
           }
           ratio=(sum[i]-cost[i])/(double)cost[i]*100;
           System.out.print("第"+(i+1)+"間直營店銷售總成本="+cost[i]);
           System.out.println();
           System.out.print("銷售總營業額="+sum[i]);
           System.out.println();
           System.out.printf("銷售銷售毛利=%.2f",ratio);
           System.out.print("%");
           System.out.println();
           System.out.println();
        }
   }
}


第五題

  public class JPA06_5 {
   public static void main(String[] arge){

       int[][] salary = {{ 120 , 420 , 315 , 250 , 418,818,900 } ,
        { 312 , 183 , 215 , 89 , 83,600,700 } ,
        { 215 , 500 , 430 , 210 , 300,918,880 }};
       int i , j , i_max , j_max,cost,sum=0;
       double ratio;
       i_max = salary.length ;
       j_max = salary[0].length ;
       cost=1500+1515+1858+180+200+360;

      for( i = 0 ; i <  i_max ; i++ ){

           for( j=0 ; j<j_max ; j++ ){
            sum+=salary[i][j];
           }
      }


       ratio=(double)(sum-cost)/cost*100;

       System.out.print("總銷售總成本="+cost);
       System.out.println();
       System.out.print("總銷售總營業額="+sum);
       System.out.println();
       System.out.printf("總銷售銷售毛利率=%.2f",ratio);
       System.out.print("%");
       System.out.println();
       }
   }

606 薪資計算

第一題

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());
}

}
}
}

605 成績資訊系統


第一題

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());
        }
    }
}

604 銀行理財帳戶


第一題

class Account{
String name;
double rate;
int balance=0;
Account(){}
Account(String s){
name = s;
}
void setRate(double d){
rate = d;
}
void deposit(double x){
balance+=x;
}
void withdraw(double x){
balance-=x;
}
int balance(){
return balance;
}
void addInterest(){
balance=(int)(balance*(rate+1));
}

}

class DepositAccount extends Account{
DepositAccount(String s,int i){
super(s);
double r=1;
switch(i){
case 1:
r=0.03;break;
case 2:
r=0.04;break;
case 3:
r=0.05;break;
}
setRate(r);
}

}

class FreeAccount extends Account{
FreeAccount(String s){
super(s);
setRate(0.02);
}
}
class SpecialAccount extends Account{
SpecialAccount(String s){
super(s);
setRate(0.02);
}
boolean isEmpt(){
if(balance>10000){
return true;
}
else{
return false;
}
}

}

class FundAccount extends Account{
String fundname;
FreeAccount freeAccount;
  SpecialAccount specialaccount;
  double unit=0;
FundAccount(String s,String f,FreeAccount fr,SpecialAccount sp ){
super(s);
fundname=f;
freeAccount=fr;
specialaccount=sp;
}
void buy(double x ,double y){
if(specialaccount.isEmpt()){
freeAccount.withdraw(x);
}else{
freeAccount.withdraw(x*1.02);
}
unit+=(double)x/(double)y;
}
void sell(double u ,double x){
if(specialaccount.isEmpt()){
freeAccount.deposit(u*x);
}else{
freeAccount.deposit(x*u*0.98);
}
unit-=u;
}
int balance(int i){
return (int)(unit*i);
}
}

class JPD06_1 {
public static void main(String args[]) {
DepositAccount deposit = new DepositAccount("peter", 2);
deposit.deposit(5000);

FreeAccount free = new FreeAccount("peter");
free.deposit(20000);

SpecialAccount special = new SpecialAccount("peter");
special.deposit(10000);
deposit.addInterest();
free.addInterest();
special.addInterest();

        System.out.println("定期存款:" + deposit.balance());
System.out.println("活期存款:" + free.balance());
System.out.println("優惠存款:" + special.balance());

FundAccount fund = new FundAccount("peter", "A", free, special);
fund.buy(15000, 500);
System.out.println("基金現額:" + fund.balance(300));
System.out.println("活期餘額:" + fund.freeAccount.balance());
}
}

第二題

class FundAccount extends Account{
String fundname;
FreeAccount freeAccount;
  SpecialAccount specialaccount;
  double unit=0;
 
FundAccount(String s,String f,FreeAccount fr,SpecialAccount sp ){
super(s);
fundname=f;
freeAccount=fr;
specialaccount=sp;
}

void buy(double x ,double y){
if(specialaccount.isEmpt()){
freeAccount.withdraw(x);
}else{
freeAccount.withdraw(x*1.02);
}
unit+=(double)x/(double)y;
}

void sell(double u ,double x){
if(specialaccount.isEmpt()){
freeAccount.deposit(u*x);
}else{
freeAccount.deposit(x*u*0.98);
}
unit-=u;
}

int balance(int i){
return (int)(unit*i);
}

double getUnit(){
return unit;
}
}

class JPA06_2 {
public static void main(String args[]) {
DepositAccount deposit = new DepositAccount("peter", 2);
deposit.deposit(5000);
FreeAccount free = new FreeAccount("peter");

free.deposit(20000);

SpecialAccount special = new SpecialAccount("peter");

special.deposit(10000);

deposit.addInterest();

free.addInterest();

special.addInterest();
FundAccount fund = new FundAccount("peter", "A", free, special);
        fund.buy(15000, 500);
        special.withdraw(5000);
fund.buy(2000, 300);
System.out.println("基金餘額:" + fund.balance(300));
System.out.println("售出前活期餘額:" + fund.freeAccount.balance());
        fund.sell(fund.getUnit(), 400);
System.out.println("售出後活期餘額:" + fund.freeAccount.balance());
}
}

第三題

class InternetAccount{
FreeAccount freeAccount;
  SpecialAccount specialaccount;
  DepositAccount deposit;
  FundAccount fund;
  InternetAccount(){}

  void setDeposit(DepositAccount d){deposit = d;}

  void setFree(FreeAccount f){freeAccount=f;}

void setSpecial(SpecialAccount s){specialaccount=s;}

  void setFund(FundAccount fu){fund=fu;}

  int getTotalBalance(){
  return deposit.balance+freeAccount.balance+specialaccount.balance;
  }

}
class JPA06_3 {
public static void main(String args[]) {
DepositAccount deposit = new DepositAccount("peter", 2);
deposit.deposit(5000);
FreeAccount free = new FreeAccount("peter");
free.deposit(20000);
SpecialAccount special = new SpecialAccount("peter");
special.deposit(10000);
deposit.addInterest();
free.addInterest();
special.addInterest();
        FundAccount fund = new FundAccount("peter", "A", free, special);
fund.buy(15000, 500);
        special.withdraw(5000);
fund.buy(2000, 300);
fund.sell(fund.getUnit(), 400);

InternetAccount internet = new InternetAccount();

internet.setDeposit(deposit);
internet.setFree(free);
internet.setSpecial(special);
internet.setFund(fund);

System.out.println("存款總額:" + internet.getTotalBalance());
}
}

第四題

import java.util.*;
class MultiFund{
HashMap <String,FundAccount> list;
MultiFund(){
list =new HashMap<String,FundAccount>();
}
void addFund(String f, FundAccount fn){
list.put(f,fn);
}
void printEachUnit(){
for(String key:list.keySet()){
System.out.println(key+":"+list.get(key).getUnit());
}
}
int getFundBalance(String s, int x){
return list.get(s).balance(x);
}
}

class JPA06_4 {
public static void main(String args[]) {
DepositAccount deposit = new DepositAccount("peter", 2);
deposit.deposit(5000);

FreeAccount free = new FreeAccount("peter");
free.deposit(20000);

SpecialAccount special = new SpecialAccount("peter");
special.deposit(10000);
deposit.addInterest();
free.addInterest();
special.addInterest();

    FundAccount fund = new FundAccount("peter", "A", free, special);
fund.buy(15000, 500);
special.withdraw(5000);
fund.buy(2000, 300);
        fund.sell(fund.getUnit(), 400);

InternetAccount internet = new InternetAccount();
internet.setDeposit(deposit);
internet.setFree(free);
internet.setSpecial(special);
internet.setFund(fund);

MultiFund multi = new MultiFund();
multi.addFund("A", fund);
FundAccount fundB = new FundAccount("peter", "B", free, special);
fundB.buy(2000, 50);
multi.addFund("B", fundB);

FundAccount fundC = new FundAccount("peter", "C", free, special);
fundC.buy(5000, 30);

multi.addFund("C", fundC);
System.out.println("活期餘額:" + free.balance());
multi.printEachUnit();
        System.out.println("B 基金餘額: " + multi.getFundBalance("B", 100));
    }
}

第五題

class Account{
String name;
double rate;
int balance=0;
Account(){}
Account(String s){
name = s;
}
void setRate(double d){
rate = d;
}
void deposit(double x){
balance+=x;
}
void withdraw(int x) throws Exception{
if(balance()<x){
throw new Exception(name+"提款金額:"+x+"大於存款金額:"+balance);
}
else{
balance-=x;
}
}
int balance(){

return balance;
}

void addInterest(){
balance=(int)(balance*(rate+1));
}

}
class FundAccount extends Account{
String fundname;
FreeAccount freeAccount;
  SpecialAccount specialaccount;
  double unit=0;
FundAccount(String s,String f,FreeAccount fr,SpecialAccount sp ){
super(s);
fundname=f;
freeAccount=fr;
specialaccount=sp;
}
void buy(double x ,double y){
try{
if(specialaccount.isEmpt()){
freeAccount.withdraw((int)x);
}else{
freeAccount.withdraw((int)(x*1.02));
}
unit+=(double)x/(double)y;
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
void sell(double u ,double x){

if(specialaccount.isEmpt()){
freeAccount.deposit(u*x);
}else{
freeAccount.deposit(x*u*0.98);
}
unit-=u;
}
int balance(int i){
return (int)(unit*i);
}
double getUnit(){
return unit;
}
}
class JPA06_5 {
public static void main(String args[]) {

DepositAccount deposit = new DepositAccount("peter", 2);
deposit.deposit(5000);
FreeAccount free = new FreeAccount("peter");
free.deposit(20000);
SpecialAccount special = new SpecialAccount("peter");
special.deposit(10000);
deposit.addInterest();
free.addInterest();
special.addInterest();

FundAccount fund = new FundAccount("peter", "A", free, special);
fund.buy(15000, 500);

try {
special.withdraw(5000);
fund.buy(2000, 300);

fund.sell(fund.getUnit(), 400);

InternetAccount internet = new InternetAccount();
internet.setDeposit(deposit);
internet.setFree(free);
internet.setSpecial(special);
internet.setFund(fund);

MultiFund multi = new MultiFund();
multi.addFund("A", fund);
FundAccount fundB = new FundAccount("peter", "B", free, special);
fundB.buy(2000, 50);
multi.addFund("B", fundB);
FundAccount fundC = new FundAccount("peter", "C", free, special);
fundC.buy(5000, 30);
multi.addFund("C", fundC);

            fund.buy(14000, 300);

} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}