某公司的雇员分为以下若干类:
Employee:这是所有员工总的父类,属性:员工的姓名和生日月份。
方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,
则公司会额外奖励 100 元。
SalariedEmployee:Employee 的子类,拿固定工资的员工。属性:月薪
HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出 160
小时的部分按照 1.5 倍工资发放
属性:每小时的工资、每月工作的小时数
SalesEmployee:Employee 的子类,销售人员,工资由月销售额和提成率决定
属性:月销售额、提成率
BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,
工资由底薪加上销售提成部分 属性:底薪。
public class TestEmployee{
public static void main(String[]args){
Employee[] es = new Employee[5];
es[0] = new Employee("赵君",2);
es[1] = new SalariedEmployee("宋婕", 1, 8000);
es[2] = new HourlyEmployee("王超", 5, 10, 300);
es[3] = new SalesEmployee("秋娥", 2, 200000, 0.05);
es[4] = new BaseSalarySalesEmployee("郭镫鸿", 1, 1000000, 0.1, 10000);
int month = 2;//本月为 2 月
System.out.println("宇宙集团"+month+"月工资表:");
for(int i=0; i<es.length; i++){
System.out.println(es[i].getName()+":"+es[i].getSalary(month));
}
}
}
class Employee{
private String name;
private int birth;
public String getName(){
return name;
}
public Employee(String name, int birth){
this.name = name;
this.birth = birth;
}
public double getSalary(int month){
if(month==birth){
return 100;
}
return 0;}
}
class SalariedEmployee extends Employee{
private double salary;
public SalariedEmployee(String name, int birth, double salary){
super(name, birth);
this.salary = salary;
}
public double getSalary(int month){
return salary + super.getSalary(month);
}
}
class HourlyEmployee extends Employee{
private double hourSalary;
private int hour;
public HourlyEmployee(String name, int birth, double hourSalary, int hour){
super(name, birth);
this.hourSalary = hourSalary;
this.hour = hour;
}
public double getSalary(int month){
if(hour<=160){
return hourSalary*hour+super.getSalary(month);
}else{
return 160*hourSalary+(hour160)*hourSalary*1.5+super.getSalary(month);
}
}
}
class SalesEmployee extends Employee{
private double sales;
private double pre;
public SalesEmployee(String name, int birth, double sales, double pre){
super(name, birth);
this.sales = sales;
this.pre = pre;
}
public double getSalary(int month){
return sales*pre+super.getSalary(month);
}
}class BaseSalarySalesEmployee extends SalesEmployee{
private double baseSalary;
public BaseSalarySalesEmployee(String name, int birth, double sales, double pre, double
baseSalary){
super(name, birth, sales, pre);
this.baseSalary = baseSalary;
}
public double getSalary(int month){
return baseSalary+super.getSalary(month);
}
}
/**
* 在原有的雇员练习上修改代码
* 公司会给 SalaryEmployee 每月另外发放 2000 元加班费,给
* BasePlusSalesEmployee 发放 1000 元加班费
* 改写原有代码,加入以上的逻辑
* 并写一个方法,打印出本月公司总共发放了多少加班费
* @author Administrator
*
*/
public class EmployeeTest {
/**
* @param args
*/
public static void main(String[] args) {
Employee e[] = new Employee[4];
e[0] = new SalariedEmployee("魏威",10,5000);
e[1] = new HourlyEmployee("段利峰",8,80,242);
e[2] = new SalesEmployee("林龙",11,300000,0.1);
e[3] = new BasedPlusSalesEmployee("华溪",1,100000,0.15,1500);
for(int i=0;i<e.length;i++){
System.out.println(e[i].getName()+": "+e[i].getSalary(11));
}
//统计加班费
int result = 0;
// for(int i=0;i<e.length;i++){
// if(e[i] instanceof SalariedEmployee){
// SalariedEmployee s = (SalariedEmployee)e[i];
// result += s.getAddtionalSalary();// }
// if(e[i] instanceof BasedPlusSalesEmployee){
// BasedPlusSalesEmployee b = (BasedPlusSalesEmployee)e[i];
// result += b.getAddtionalSalary();
// }
// }
for(int i=0;i<e.length;i++){
result += e[i].getAddtionalSalary();
}
System.out.println("加班费: "+result);
}
}
interface AddtionalSalary{
int getAddtionalSalary();
}
class Employee implements AddtionalSalary{
private String name;//员工姓名
private int birth;//员工生日月份
public Employee(String name,int birth){
this.name = name;
this.birth = birth;
}
public int getSalary(int month){
int result = 0;
if(month==birth)
result = 100;
return result;
}
public String getName(){
return name;
}
public int getAddtionalSalary(){
return 0;
}
}
class SalariedEmployee extends Employee{
private int salaryPerMonth;
public SalariedEmployee(String name,int birth,int salaryPerMonth){
super(name,birth);this.salaryPerMonth = salaryPerMonth;
}
public int getSalary(int month){
return this.salaryPerMonth + super.getSalary(month)+
this.getAddtionalSalary();
}
public int getAddtionalSalary(){
return 2000;
}
}
class HourlyEmployee extends Employee{
private int salaryPerHour;
private int hoursPerMonth;
public HourlyEmployee(String name,int birth,int salaryPerHour,int hoursPerMonth){
super(name,birth);
this.salaryPerHour = salaryPerHour;
this.hoursPerMonth = hoursPerMonth;
}
public int getSalary(int month){
int result = 0;
if(this.hoursPerMonth<=160){
result = hoursPerMonth*salaryPerHour;
}else{
result = 160*salaryPerHour +
(int)((hoursPerMonth160)*1.5*salaryPerHour);
}
return result+super.getSalary(month);
}
}
class SalesEmployee extends Employee{
private int sales;
private double rate;
public SalesEmployee(String name,int birth,int sales,double rate){
super(name,birth);
this.sales = sales;
this.rate = rate;
}
public int getSalary(int month){
return (int)(sales*rate)+super.getSalary(month);
}
}class BasedPlusSalesEmployee extends SalesEmployee{
private int basedSalary;
public BasedPlusSalesEmployee(String name,int birth,int sales,double rate,int basedSalary){
super(name,birth,sales,rate);
this.basedSalary = basedSalary;
}
public int getSalary(int month){
return this.basedSalary+super.getSalary(month) +
this.getAddtionalSalary();
}
public int getAddtionalSalary(){
return 1000;
}
}
经典算法:
1. 某学校为学生分配宿舍,每 6 个人一间房(不考虑性别差异) ,问需要多少房?
答案: (x+5)/6
注意理解 int 类型数值。
2. 让数值在 0~9 之间循环。
public class test{
public static void main(String[] args){
int i=0;
while(true){
i = (i+1)%10;
System.out.println(i);
}
}
}
作业:
1. 写一个数组类(放对象) :
功能包括:添加(添加不限制多少项)、修改、插入、删除、查询
class MyArray{
private Object[] os = new Object[10];
public void add(Object o);
public void set(int index, Object o);public void insert(int index, Objecto);
public void remove(int index);
public void remove(Object o);
public Object get(int index);
}
public class TestMyArray{
public static void main(String[]args){
MyArray ma = new MyArray();
ma.add("aaa");
ma.add("bbb");
ma.add("ccc");
Object o = ma.get(1);
Iterator it = ma.iterator();
while(it.hasNext()){
Object o1 = it.next();
System.out.println(o1);
}
}
}
作业 1008
1. 随机产生 20 个整数(10 以内的),放入一个 ArrayList 中, 用迭代器遍历这个 ArrayList
2. 并删除其中为 5 的数
3. 再产生 3 个整数,插入到位置 4 处
4. 把所有值为 1 的数都变成 10
import java.util.ArrayList;
class ArrayList{
private Object[] os = new Object[20];
}
public class TestArray{
public static void main(String[]args){
ArrayList a = new ArrayList();
ma.add("aaa");
ma.add("bbb");
ma.add("ccc");Object o = ma.get(1);
Iterator it = ma.iterator();
while(it.hasNext()){
Object o1 = it.next();
System.out.println(o1);
}
}
}
1. 产生 3000 个 10 以内的数,放入 hashSet
2. 遍历它,打印每一个值
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
public class TestHashSet {
public static void main(String[] args) {
Random r = new Random();
HashSet hs1 = new HashSet();
for(int i=0; i<3000; i++){
hs1.add(r.nextInt(10));
}
Iterator it1 = hs1.iterator();
while(it1.hasNext()){
System.out.print(it1.next()+" ");
}
}
}
//由于 HashSet 不能重复,所以只有 10 个数在里面,按哈希排序
2 4 9 8 6 1 3 7 5 0
/*
* 测试 TreeSet 的比较器,
* 在有自己的比较器的情况下,如何实现 Comparable 接口
*/
import java.util.*;
class Teacher{int id;
String name;
int age;
public Teacher() {}
public Teacher(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() { return id; }
public void setId(int id) {this.id = id; }
public String getName() { return name;}
public void setName(String name) { this.name = name;}
public int getAge() {return age;}
public void setAge(int age) {this.age = age;}
public int TeacherComparator(Object o){
Teacher t1 = (Teacher) o;
if(t1.getId() > id){return 1;}
else if (t1.getId() < id){return 1;}
return 0;
}
}
class TreeSet{
}
class Test {
public static void main(String[] args) {
String s1 = new String("aaa");
String s2 = new String("bbb");
String s3 = new String("aaa");
System.out.println(s1==s3);
System.out.println(s1.equals(s3));
HashSet hs = new HashSet();
hs.add(s1);
hs.add(s2);
hs.add(s3);
Iterator it = hs.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.printf("%x\n",s1.hashCode());System.out.printf("%x\n",s2.hashCode());
System.out.printf("%x\n",s3.hashCode());
}
}
1. 在 Map 中,以 name 作 Key,以 Student 类 作 Velue,写一个 HashMap
import java.util.*;
class Student{
int id;
String name;
int age;
public Student() {}
public Student( int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {return id;}
public void setId(int id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public int getAge() {return age;}
public void setAge(int age) {this.age = age;}
}
class TestHashMap{
public static void main(String[] args) {
HashMap hm = new HashMap();
Student s1 = new Student(1,"jacky",19);
hm.put("jacky",s1);
hm.put("tom",new Student(2,"tom",21));
hm.put("kitty",new Student(3,"kitty",17));
Iterator it = hm.keySet().iterator();
while(it.hasNext()){
Object key = it.next();
Student value = (Student) hm.get(key);
System.out.println(key+":id="+value.id+",age="+value.age);
}
System.out.println("=============================");
//比较 KeySet() 和 entrySet() 两种迭代方式for(Iterator i1 = hm.entrySet().iterator(); i1.hasNext(); )
{ Map.Entry me = (Map.Entry) i1.next();
Student s = (Student) me.getValue();
System.out.println(me.getKey()+": id="+s.id+" age="+s.age);
}
}
}
day13 homework
1.
/******************************************************************************
****
自己写一个栈: ( 先进后出 )
建议底层用 LinkedList 实现
参照 java.util.Stack
方法: boolean empty() 测试堆栈是否为空。
E peek() 查看栈顶对象而不移除它。
E pop() 移除栈顶对象并作为此函数的值返回该对象。
E push(E item) 把项压入栈顶。
int search(Object o) 返回对象在栈中的位置,以 1 为基数。
*******************************************************************************
********/
//不能用继承,因为它破坏封装。只需调用即可
import java.util.LinkedList;
class MyStack<E>{
private LinkedList<E> list = new LinkedList<E>();
public boolean empty() {return list.isEmpty();}
public E peek() {return list.peek(); }
public E pop() {return list.poll(); }
public void push(E o) {list.addFirst(o); }
//int indexOf(Object o) 返回此列表中首次出现的指定元素的索引,如果此列表中不
包含该元素,则返回 1。
public int search(Object o){return list.indexOf(o);}
}
2.
/******************************************************************************
*********定义以下类,完成后面的问题,并验证。
Exam 类 考试类
属性: 若干学生 一张考卷
提示:学生采用 HashSet 存放
Paper 类 考卷类
属性:若干试题
提示:试题采用 HashMap 存放,key 为 String,表示题号,value 为试题对象
Student 类 学生类
属性:姓名 一张答卷 一张考卷 考试成绩
Question 类 试题类
属性:题号 题目描述 若干选项 正确答案
提示:若干选项用 ArrayList
AnswerSheet 类 答卷类
属性:每道题的答案
提示:答卷中每道题的答案用 HashMap 存放,key 为 String,表示题号,value 为学生的答
案
问题:为 Exam 类添加一个方法,用来为所有学生判卷,并打印成绩排名(名次、姓名、成
绩)
*******************************************************************************
********/
3.
/***************************************************************************************
项目:商品管理系统
功能:增删改查 (可按各种属性查)
商品属性:名称、价格(两位小数) 、种类
*******************************************************************************
********/
day17 图形界面
1. 计算器
/*****************例题 画出计算器的界面*****************************
界面如下:
1 2 3 +
4 5 6
7 8 9 *
0 . = /
*******************/
import java.awt.*;
import javax.swing.*;
class Calculator {
public static void main(String[] args){
JTextField text = new JTextField();
JFrame f = new JFrame("计算器");
Font font = new Font("宋体", Font.BOLD, 25);//"宋体"想写成默认,则写“null”text.setFont(font); //定义字体
text.setHorizontalAlignment(JTextField.RIGHT);//令 text 的文字从右边起
text.setEditable(false);//设置文本不可修改,默认可修改(true)
f.add(text, BorderLayout.NORTH);//Frame 和 Dialog 的默认布局管理器是 Border Layout
ButtonActionListener listener = new ButtonActionListener(text);//事件反应在 text 中
JPanel buttonPanel = new JPanel();//设法把计算器键盘放到这个 Jpanel 按钮上
String op = "123+456789*0.=/";
GridLayout gridlayout = new GridLayout(4,4,10,10);
buttonPanel.setLayout(gridlayout);//把计算器键盘放到 buttonPanel 按钮上
for(int i=0; i<op.length(); i++){
char c = op.charAt(i); //拿到字符串的第 i 个字符
JButton b = new JButton(c+"");//把字符放到按钮上
b.addActionListener(listener);//在按钮上放置监听器,每次按都会有反应
buttonPanel.add(b);//把按钮放到 buttonPanel 上
}//这个循环很值得学习,很常用
f.add(buttonPanel/*, BorderLayout.CENTER*/); //默认添加到 CENTER 位置
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 250);
f.setVisible(true);//这句要放到最后,等事件完成后再显示
}}
//监听者
class ButtonActionListener implements ActionListener{
private JTextField textField;
public ButtonActionListener(JTextField textField) {
this.textField = textField;
}
public void actionPerformed(ActionEvent e) {//必须覆盖它的 actionPerformed()
textField.append("哈哈,放了几个字\n");
}}
/*********************未实现计算器的具体功能*******************************/
2. 扫雷游戏
3. 俄罗斯方块day19 多线程
写 两 个 线 程 , 一 个 线 程 打 印 1~52 , 另 一 个 线 程 打 印 字 母 AZ 。 打 印 顺 序 为
12A34B56C……5152Z。要求用线程间的通信。
注:分别给两个对象构造一个对象 o,数字每打印两个或字母每打印一个就执行
o.wait()。
在 o.wait()之前不要忘了写 o.notify()。
class Test{
public static void main(String[] args) {
Printer p = new Printer();
Thread t1 = new NumberPrinter(p);
Thread t2 = new LetterPrinter(p);
t1.start();
t2.start();
}
}
class Printer{
private int index = 1;//设为 1,方便计算 3 的倍数
//打印数字的构造方法,每打印两个数字,等待打印一个字母
public synchronized void print(int i){
while(index%3==0){try{wait();}catch(Exception e){}}
System.out.print(" "+i);
index++;
notifyAll();
}
//打印字母,每打印一个字母,等待打印两个数字
public synchronized void print(char c){
while(index%3!=0){try{wait();}catch(Exception e){}}
System.out.print(" "+c);
index++;
notifyAll();
}
}
//打印数字的线程
class NumberPrinter extends Thread{
private Printer p;
public NumberPrinter(Printer p){this.p = p;}
public void run(){
for(int i = 1; i<=52; i++){
p.print(i);}
}
}
//打印字母的线程
class LetterPrinter extends Thread{
private Printer p;
public LetterPrinter(Printer p){this.p = p;}
public void run(){
for(char c='A'; c<='Z'; c++){
p.print(c);
}
}
}
/*如果这题中,想保存需要打印的结果,可在 Printer 类里定义一个成员变量
String s = ""; //不写“”的话是 null,null 跟没有东西是不一样的,它会把 null 当成字符 =_=
然后在两个 print()方法里面,while 循环后分别加上 s = s + " "+i; 以及 s = s +" "+ c;*/ |
|