- 设计一个词典类Dic,每个单词包括英文单词及对应的中文含义,并有一个英汉翻译成员函数,通过查词典的方式将一段英语翻译成对应的汉语。
思路: 字典项类DicItem包括EngLish(英语单词)、Chinese(对应中文含义)数据成员,字典类包括一个字典项类的列表,包含Add()(添加单词)和trans(英汉翻译)成员函数。
package Important.shiYanEr;
class Dic{
class DicItem{
String Chinese;
String Enligsh;
//构造方法
public DicItem(String Enligsh,String Chinese) {
this.Chinese = Chinese;
this.Enligsh = Enligsh;
}
public DicItem() {
super();
}
}
DicItem [] words = new DicItem[5];
//当前数组的元素个数
int size = 0;
public int getSize() {
int sum = size;
System.out.println("当前的词典中的单词个数是:"+sum);
return sum;
}
public Dic() {
words[0] = new DicItem("my","我的");
words[1] = new DicItem("your","你的");
words[2] = new DicItem("name","名字");
words[3] = new DicItem("happy","快乐的");
size = 4;
}
//增加单词
public void add(String English,String Chinese) {
int index = this.find(English);
//替换
if(index != -1) {
words[index].Chinese = Chinese;
}
//增加
else {
expond();
words[size++] = new DicItem(English,Chinese);
System.out.println("增加单词成功:"+English+":"+Chinese);
}
}
//扩容
public void expond() {
if(size == words.length) {
DicItem [] newwords = new DicItem[words.length*3/2+1];
System.arraycopy(words, 0, newwords, 0, words.length);
words = newwords;
}
}
//翻译英文
public String transE(String English) {
for(int i = 0;i < size;i++) {
if(English.equals(words[i].Enligsh)) {
String data = words[i].Chinese;
System.out.println(English+" 的中文意思是:"+data);
return data;
}
}
System.out.println("该英文不存在此词典中");
return "该单词不存在此词典中";
}
//翻译中文
public String transC(String Chinese) {
for(int i = 0;i < size;i++) {
if(Chinese.equals(words[i].Chinese)) {
String data = words[i].Enligsh;
System.out.println(Chinese+" 的英文意思是:"+data);
return data;
}
}
System.out.println("该中文不存在此词典中");
return "该中文不存在此词典中";
}
//显示表中的单词及释义
public void showInfo() {
System.out.println("该字典中的单词:");
for(int i = 0;i < size;i++) {
System.out.println(words[i].Enligsh+":"+words[i].Chinese);
}
}
//查找单词
public int find(String English) {
for(int i = 0;i < size;i++) {
if(words[i].Enligsh.equals(English)) {
return i;
}
}
return -1;
}
//删除单词
public void delete(String English) {
int index = this.find(English);
//删除
if(index != -1) {
for(int i = index;i < words.length-1;i++) {
words[i] = words[i+1];
}
size--;
}
else {
System.out.println("该单词不存在此词典中。");
System.out.println("删除失败。");
}
}
}
public class _1 {
public static void main(String[] args) {
Dic d = new Dic();
d.showInfo();
System.out.println();
d.getSize();
System.out.println();
System.out.println("增加单词:");
d.add("james", "詹姆斯");
d.add("champion","冠军");
d.add("student", "学生");
d.add("my", "我的,我自己的");
System.out.println();
d.showInfo();
d.getSize();
System.out.println();
d.delete("james");
d.showInfo();
d.getSize();
System.out.println();
System.out.println("单词的查询:");
d.transE("my");
d.transC("詹姆斯");
}
}
- 自已封装一个动态数组类,可以根据用户传递的数据,动态的对数组的长度进行扩展;
类名是:MyArray
方法有:
void add(int value); //追加一个值
vold remove(int index); //根据索引,删除一个值
void add(int position,int value); //在指定位置插入一个数值
void set(int position,int value); //修改某个位置的数值
int get(int index); //根据索引,获得元数的值
int size(); //获得动态数组中元素的个数;
package Important.shiYanEr;
import java.util.Arrays;
class MyArray{
private int size = 0;//数组的长度
private int [] array = new int[10];
//追加一个值
public void add(int n) {
expand();
array[size++] = n;
}
//扩容
public void expand() {
if(size == array.length) {
int [] newarray = new int[array.length*3/2+1];
System.arraycopy(array, 0, newarray, 0, array.length);
array = newarray;
}
}
//获得数组的元素个数
public int getSize() {
int sum = size;
System.out.println("当前数组的元素个数是:"+sum);
return sum;
}
//获得当前数组的长度
public int getLength() {
int sum = array.length;
System.out.println("当前数组的长度是:"+sum);
return sum;
}
//根据索引获得值
public int get(int index) {
int v = array[index];
System.out.println("该索引对应的值是:"+v);
return v;
}
//修改某个位置上的元素值
public void set(int position,int value) {
System.out.println(position+"位置上的元素值被修改为:"+value);
array[position] = value;
}
//输出数组
public void output() {
System.out.println(Arrays.toString(array));
}
//删除某个位置上的值
public void remove(int index) {
System.out.println("删除位置"+index+"上的元素值后,数组变为:");
array[index] = array[index+1];
for(int i = index+1;i < array.length-1;i++) {
array[i] = array[i+1];
}
}
//在某个位置上插入元素值
public void add(int position,int value) {
System.out.println("在位置"+position+"上插入的元素"+value+"后,数组变为:");
for(int i = size;i > position;i--) {
array[i+1] = array[i];
}
array[position] = value;
}
}
public class _2 {
public static void main(String[] args) {
MyArray a = new MyArray();
for(int i = 0;i < 13;i++) {
a.add(i+20);
System.out.println(i+20);
}
a.getSize();
a.getLength();
a.get(3);
a.set(3, 100);
System.out.println("修改后得数组是:");
a.output();
a.remove(6);
a.output();
a.add(6, 999);
a.output();
}
}
- 编写工资系统,实现不同类型员工(多态)的按月发放工资。如果当月出现某个Employee对象的生日,则将该雇员的工资增加100元。
实验目的:对象、继承、封装、多态、抽象类的组合应用。
实验说明:
(1)定义一个Employee类,该类包含:
private成员变量name,number,birthday,其中birthday 为MyDate类的对象;
abstract方法earnings();toString()方法输出对象的name,number和birthday。
(2)MyDate类包含:
private成员变量month,day,year;
toDateString()方法返回日期对应的字符串:xxxx年xx月xx日
(3)定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处理。该类包括:
private成员变量monthlySalary;
实现父类的抽象方法earnings(),该方法返回monthlySalary值;toString()方法输出员工类型信息及员工的name,number,birthday。
package Important.shiYanEr;
import java.util.*;
abstract class Employee{
private String name;
private int number;
MyDate birthday;
public Employee(String name, int number, MyDate birthday) {
super();
this.name = name;
this.number = number;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public abstract double earnings();
public String toString() {
return " name:" + name + ", number:" + number + ", birthday:" + birthday.toDateString() ;
}
}
class MyDate{
private int month;
private int day;
private int year;
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
//构造器
public MyDate() {
}
public MyDate(int year,int month, int day) {
super();
this.month = month;
this.day = day;
this.year = year;
}
public String toDateString() {
return this.year+"年"+this.month+"月"+this.day+"日";
}
}
class SalariedEmployee extends Employee{
private int monthlySalary = 6000;
public SalariedEmployee(String name,int number,MyDate birthday) {
super(name,number,birthday);
}
public int getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(int monthlySalary) {
this.monthlySalary = monthlySalary;
}
public String toString(){
return "月薪工"+super.toString();
}
public double earnings() {
return monthlySalary;
}
}
class HourlyEmployee extends Employee{
private int hour = 275;
private int wage = 25;
public int getWage() {
return wage;
}
public void setWage(int wage) {
this.wage = wage;
}
public HourlyEmployee(String name,int number,MyDate birthday) {
super(name,number,birthday);
}
public int getHour() {
return hour;
}
public void setHour(int hourlySalary) {
this.hour = hourlySalary;
}
public double earnings() {
return hour*wage;
}
public String toString() {
return "日薪工"+super.toString();
}
}
public class _3 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
Employee [] e = new Employee[3];
e[0] = new SalariedEmployee("张无忌",101,new MyDate(1997,5,11));
e[1] = new SalariedEmployee("赵敏",102,new MyDate(1998,8,31));
e[2] = new HourlyEmployee("周芷若",103,new MyDate(1996,3,6));
System.out.println("当前的月份:");
int month = reader.nextInt();
for(int i = 0;i < e.length;i++) {
if(month == e[i].getBirthday().getMonth()) {
System.out.println(e[i].toString());
System.out.println("此外,本月是"+e[i].getName()+"的生日,您的工资将增加100元,本月工资为"+(e[i].earnings()+100));
System.out.println();
}else {
System.out.println(e[i].toString());
System.out.println();
}
}
}
}
(4)参照SalariedEmployee类定义HourlyEmployee类,实现按小时计算工资的员工处理。该类包括:
private成员变量wage和hour;
实现父类的抽象方法earnings(),该方法返回wage*hour值;toString()方法输出员工类型信息及员工的name,number,birthday。
(4)定义PayrollSystem类,创建Employee变量数组并初始化,该数组存放各类雇员对象的引用。利用循环结构遍历数组元素,输出各个对象的类型,name,number,birthday,以及该对象生日。当键盘输入本月月份值时,如果本月是某个Employee对象的生日,还要输出增加工资信息。
提示:
//定义People类型的数组
People c1[]=new People[10];
//数组元素赋值
c1[0]=new People("John","0001",20);
c1[1]=new People("Bob","0002",19);
//若People有两个子类Student和Officer,则数组元素赋值时,可以使父类类型的数组元素指向子类。
c1[0]=new Student("John","0001",20,85.0);
c1[1]=new Officer("Bob","0002",19,90.5);
2、写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;包含的方法:访问器方法(getter和setter方法),取款方法withdraw(),存款方法deposit()。
Account |
private int id private double balance private double annualInterestRate |
public Account (int id, double balance, double annualInterestRate ) |
public int getId() public double getBalance() public double getAnnualInterestRate() public void setId( int id) public void setBalance(double balance) public void setAnnualInterestRate(double annualInterestRate) public void withdraw (double amount) public void deposit (double amount) |
提示:在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。
2. 创建Customer类。
Customer |
private String firstName private String lastName private Account account |
public Customer(String f,String l) |
public String getFirstName() public String getLastName() public Account getAccount() public void setAccount(Account account) |
a. 声明三个私有对象属性:firstName、lastName和account。
b. 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(f和l)
c. 声明两个公有存取器来访问该对象属性,方法getFirstName和getLastName返回相应的属性。
d. 声明setAccount 方法来对account属性赋值。
e. 声明getAccount 方法以获取account属性。
3.写一个测试程序。
- 创建一个Customer ,名字叫 Jane Smith, 他有一个账号为1000,余额为2000元,年利率为 1.23% 的账户。
- 对Jane Smith操作。
存入 100 元,再取出960元。再取出2000元。
打印出Jane Smith 的基本信息
成功存入 :100.0
成功取出:960.0
余额不足,取款失败
Customer [Smith, Jane] has a account: id is 1000, annualInterestRate is 1.23%, balance is 1140.0
package Important.shiYanEr;
class Account{
private int id;
private double balance;
private double annualintrestRate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualintrestRate() {
return annualintrestRate;
}
public void setAnnualintrestRate(double annualintrestRate) {
this.annualintrestRate = annualintrestRate;
}
//构造器
public Account(int id, double balance, double annualintrestRate) {
super();
this.id = id;
this.balance = balance;
this.annualintrestRate = annualintrestRate;
}
//取款
public void withdraw(double amount) {
if(amount > balance) {
System.out.println("余额不足,取款失败。");
}else {
balance = balance - amount;
System.out.println("成功取出:"+amount+","+"当前余额为:"+balance);
}
}
//存款
public void deposit(double amount) {
balance = balance + amount;
System.out.println("成功存入:"+amount+","+"当前余额为:"+balance);
}
}
class Customer{
private String firstName;
private String lastName;
private Account account;
//构造器
public Customer(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Account setAccount(Account account) {
return this.account = account;
}
public Account getAccount() {
return account;
}
//显示信息
public void showInfo() {
System.out.println("Customer ["+this.firstName+", "+this.lastName+"] has a account: id is "+account.getId()+", annualinterestRate is "+account.getAnnualintrestRate()*100+"%"+", balance is "+account.getBalance()+"\r\n");
}
}
public class _4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Account a = new Account(1000,2000,0.0123);
Customer c = new Customer("Smith","Jane");
c.setAccount(a);
a.deposit(100);
a.withdraw(960);
a.withdraw(2000);
c.showInfo();
}
}