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() + ".");

    }
}