1、将你在实验3中设计的模拟堆栈程序,封装成一个类(注意访问控制的运用、getter和setter的运用、构造方法的设计等),并用这个堆栈类重写中缀表达式转换成后缀表达式程序。
public class StackImiate {
private int maxSize;
private char[] a;
private int top;
StackImiate(int maxSize) {
this.maxSize=maxSize;
a=new char[maxSize];
top=-1;
}
public void push(char str) {
a[++top]=str;
}
public char pop() {
return a[top--];
}
public boolean isEmpty() {
return(top==-1);
}
public boolean isFull() {
return(top==maxSize);
}
public int length()
{return top+1;}
public char peek() {
return a[top];
}
public char peekN(int n) {
//System.out.println(a[n]);
return a[n];
}
public void displayStack(String s) {
System.out.print(s);
System.out.print(" Stack (bottom-->top): ");
for(int j=0;j<maxSize;j++) {
System.out.print(peekN(j)+" ");
}
System.out.println();
}
}
public class InToPost {
public static void main(String[] args) {
String strResult=new String();
String Input="a+b*c+(d*e+f)*g";
System.out.println("中缀表达式"+Input);
strResult=strResult+doTrans(Input);
System.out.println();
System.out.println("后缀表达式"+strResult);
}
public static StringBuffer doTrans(String Input) {
StackImiate str=new StackImiate(50);
StringBuffer strResultTemp=new StringBuffer();
for (int i = 0; i <Input.length(); i++) {
char ch = Input.charAt(i);
str.displayStack("Get " + ch + " ");
switch (ch) {
case '+':
case '-':
gotOper(ch, 1,str,strResultTemp);
break;
case '*':
case '/':
case '%':
gotOper(ch, 2,str,strResultTemp);
break;
case '(':
str.push(ch);
break;
case ')':
gotParen(ch,str,strResultTemp);
break;
default:
strResultTemp =strResultTemp.append(ch);
break;
}
}
while (!str.isEmpty()) {
str.displayStack("out ");
strResultTemp = strResultTemp.append(str.pop());
}
str.displayStack("End ");
return strResultTemp;
}
public static void gotParen(char ch,StackImiate str,StringBuffer strResultTemp) {
// TODO Auto-generated method stub
while (!str.isEmpty()) {
char chx = (char) str.pop();
if (chx == '(') {
break;
} else {
strResultTemp =strResultTemp.append(chx);
}
}
}
private static void gotOper(char ch, int i,StackImiate str,StringBuffer strResultTemp) {
// TODO Auto-generated method stub
while (!str.isEmpty()) {
char opTop = (char) str.pop();
if (opTop == '(') {
str.push('(');
break;
} else {
int k;
if (opTop == '+' || opTop == '-') {
k = 1;
} else {
k = 2;
}
if (k < i) {
str.push(opTop);
break;
} else {
strResultTemp = strResultTemp.append(opTop);
}
}
}
str.push(ch);
}
}
2、设计一个时间类MyDate:(日期均大于1900年)
a、其中包括年、月、日、时、分、秒。其他成员变量可根据需要增设;
b、可以直接获得时间的年、月、日、小时、分钟、秒数;(public修饰)
c、计算日期的dayOfWeek(星期几);
d、输入任意一个月份,将此月的日历输出(按星期格式);
e、输入任意年份,将此年的年历输出;
f、输出时间,要求三种格式:
yyyy年MM月dd日hh小时mm分ss秒;(Chinese)
yyyy-MM-dd hh:mm:ss(European)
yyyyMMddhhmmss(compact)
g、设计三个构造方法:
1、参数只有年月日的构造方法,小时分钟秒设为0;
2、完全参数的构造方法;
3、如果使用无参数的构造方法则初始化为系统时间。
h、不要直接使用Java提供的Date类,那样就达不到练习的效果了。
import java.text.SimpleDateFormat;
public class MyDate {
public int year,month,day,hour,minute,second;
String strDate;
public MyDate()
{SimpleDateFormat myDateFormat =new SimpleDateFormat("yyyyMMddHHmmss");
java.util.Date date0=new java.util.Date();
strDate=myDateFormat.format(date0);
year = Integer.parseInt( strDate.substring(0,4));
month = Integer.parseInt( strDate.substring(4,6));
day = Integer.parseInt( strDate.substring(6,8));
hour = Integer.parseInt( strDate.substring(8,10));
minute = Integer.parseInt( strDate.substring(10,12));
second = Integer.parseInt( strDate.substring(12,14));
}
public MyDate(int year0,int month0,int day0)
{year=year0;
month=month0;
day=day0;
hour=0;
minute=0;
second=0;}
public MyDate(int year0,int month0,int day0,int hour0,int minite0,int second0)
{year=year0;
month=month0;
day=day0;
hour=hour0;
minute=minite0;
second=second0;}
public int getYear()
{return year;}
public int getMonth()
{return month;}
public String getDate()
{return day+"/"+month+"/"+year;}
public int getHour()
{return hour;}
public int getMinite()
{return minute;}
public int getSecond()
{return second;}
public void displayChinese()
{System.out.println(year+"年"+month+"月"+day+"日"+hour+"时"+minute+"分"+second+"秒");}
public void displayEU()
{System.out.println(year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second+" ");}
public void displayCompact()
{System.out.println(year+" "+month+" "+day+" "+hour+" "+minute+" "+second);}
public void displayMonth(int year,int month) //输出一月的日历
{ int sum=0;
for(int i=1900;i<year;i++)
{ if(i%4==0&&i%100!=0||i%400==0)
{sum+=366;}
else
{sum+=365;}
}
for(int i=1;i<month;i++)
{ if(i==2)
{if(year%4==0&&year%100!=0||year%400==0)
{sum+=29;}
else
{sum+=28;}
}
else
{ if(i==4||i==6||i==9||i==11)
{sum+=30;}
else
{sum+=31;}
}
}
sum+=1;
int wekday=sum%7;
System.out.println("日\t一\t二\t三\t四\t五\t六");
for(int i=1;i<=wekday;i++)
{System.out.print("\t");}
if(year%4==0&&year%100!=0||year%400==0)
{if(month!=2)
{if(month==4||month==6||month==9||month==11)
{for(int i=1;i<=30;i++)
{if(sum%7==6)
{System.out.print(i+"\n");}
else{
System.out.print(i+"\t");}
sum++;}}
else
{for(int i=1;i<=31;i++)
{if(sum%7==6)
{System.out.print(i+"\n");}
else
{System.out.print(i+"\t");}
sum++;}
}}
else
{for(int i=1;i<=29;i++)
{if(sum%7==6)
{System.out.print(i+"\n");}
else
{System.out.print(i+"\t");}
sum++;
}}}
else {if(month!=2)
{if(month==4||month==6||month==9||month==11)
{for(int i=1;i<=30;i++)
{if(sum%7==6)
{System.out.print(i+"\n");}
else{
System.out.print(i+"\t");}
sum++;}}
else
{for(int i=1;i<=31;i++)
{if(sum%7==6)
{System.out.print(i+"\n");}
else
{System.out.print(i+"\t");}
sum++;}
}}
else
{for(int i=1;i<=28;i++)
{if(sum%7==6)
{System.out.print(i+"\n");}
else
{System.out.print(i+"\t");}
sum++;
}}}}
public void displayYear(int year) //输出一年的日历
{for(int i=1;i<13;i++)
{System.out.println(" 第"+i+"月");
displayMonth(year,i);
System.out.println();
}
}
public String dayOfWeek(int year,int month,int day) //判断某天为一周的星期几
{int sum=0;
for(int i=1900;i<year;i++)
{ if(i%4==0&&i%100!=0||i%400==0)
{sum+=366;}
else
{sum+=365;}
}
for(int i=1;i<month;i++)
{ if(i==2)
{
if(year%4==0&&year%100!=0||year%400==0)
{sum+=29;}
else
{sum+=28;}
}
else
{ if(i==4||i==6||i==9||i==11)
{sum+=30;}
else
{sum+=31;}
}
}
sum+=1;
int wekFirstday=sum%7;
int dayOfWeek=wekFirstday;
int i=0;
while(i<day)
{i++;dayOfWeek++;}
if(dayOfWeek!=7)
{return "第"+year+"年"+month+"月"+day+"日为星期"+dayOfWeek;}
else
{return "第"+year+"年"+month+"月"+day+"日为星期日";}
}}
public class UseMyDate {
public static void main(String[] args) {
MyDate myDate=new MyDate();
myDate.displayChinese();
myDate.displayEU();
myDate.displayCompact();
MyDate myDate1=new MyDate(2008,8,8);
myDate1.displayChinese();
myDate1.displayEU();
myDate1.displayCompact();
MyDate myDate2=new MyDate(2008,8,8,20,0,0);
myDate2.displayChinese();
myDate2.displayEU();
myDate2.displayCompact();
myDate.displayMonth(2014,4);
System.out.println();
myDate.displayYear(2011); }
}
3、设计一个字符串链表节点类:
a)其中包括两个成员变量:Value(String类型)和下一个节点nextNode,方法有:setValue(),getValue()和display();
b)用此类生成一个循环链表来解决斗地主发牌的问题
public class ChainNode {
public String Value;
public ChainNode nextNode;
public String suit;
public ChainNode()
{Value="";
nextNode=null;
suit="";}
public ChainNode(String Value,ChainNode nextNode, String suit)
{this.Value=Value;
this.nextNode=nextNode;
this.suit=suit;}
public ChainNode(String Value, String suit)
{this.Value=Value;
this.suit=suit;}
public void setValue(String suit,String value)
{this.Value=value;
this.suit=suit;}
public String getValue()
{return Value;}
public void display()
{System.out.print(suit+Value+" ");}
}
public class DealCards {
public static void main(String[] args) {
String suits[]=new String[] {"黑桃","梅花","方块","红桃"};
String Value[]=new String[] {"A","2","3","4","5","6","7","8","9","10","J","Q","K",};
ChainNode[] cardsOfPlayer = new ChainNode[3];
for(int i = 0; i<3; i++)
{
cardsOfPlayer[i] = new ChainNode();
}
ChainNode firstCard=new ChainNode("","大王");
firstCard.nextNode=new ChainNode("","小王");
ChainNode lastCard=firstCard.nextNode;
for(int i=0;i<4;i++)
{ for(int j=0;j<13;j++)
{lastCard.nextNode= new ChainNode(Value[j],suits[i]);
lastCard=lastCard.nextNode;
}
}
lastCard.nextNode=firstCard;
int random=0;
int players=0;
ChainNode choose=firstCard;
ChainNode before=lastCard;
for(int num=54;num>3;num--)
{ random=(int)(Math.random()*num);
for(;random>1;random--)
{before=choose;
choose=choose.nextNode;}
before.nextNode=choose.nextNode;
choose.nextNode=cardsOfPlayer[players].nextNode;
cardsOfPlayer[players].nextNode=choose;
choose=before.nextNode;
players++;
players=players%3;
}
for(int i = 0; i<3; i++)
{
System.out.println("\n第"+(i+1)+"个玩家的牌是");
ChainNode pri = cardsOfPlayer[i].nextNode;
while(pri != null)
{
pri.display();
pri = pri.nextNode;
}
}
System.out.println("\nThe Last Three Cards:");
for(int i = 0; i<3; i++)
{
choose.display();
choose = choose.nextNode;
}
System.out.println();
System.out.println();
}
}
4、设计一个用于管理银行客户的类BankCustomer:
仅描述客户的几个重要方面:帐号、身份证号、姓名、联系方式、密码、账户余额。所有的成员变量均用private访问控制,因此每一个成员变量就要有相应的存取器(getter和setter,即获取和设置其值的相应的成员方法。需要setter还是getter,还是两者都要,视情况而定)
import java.text.SimpleDateFormat;
public class BankCustomer {
private String ID;
private String peopleID;
private String name;
private String phone;
private String password;
private double balance;
public BankCustomer(String peopleID,String name,String phone)
{String strDate;
SimpleDateFormat myDateFormat =new SimpleDateFormat("yyyyMMddHHmmss");
java.util.Date date0=new java.util.Date();
strDate=myDateFormat.format(date0);
ID=strDate;
this.peopleID=peopleID;
this.name=name;
this.phone=phone;
this.password="666666";
this.balance=0;
}
public double getbalance()
{return balance;}
public String getID()
{return ID;}
public String getPhone()
{return phone;}
public String getPelpleID()
{return peopleID;}
public String getName()
{return name;}
public void setName(String password,String name)
{if(ifPassword(password))
{this.name=name;
System.out.println("您的姓名已设置");}
else
{System.out.println("密码错误");}
}
public void setPeopleID(String password,String peopleID)
{if(ifPassword(password))
{this.peopleID=peopleID;
System.out.println("您的身份证号已设置");}
else
{System.out.println("密码错误");}}
public void setPhone(String password,String phone)
{if(ifPassword(password))
{this.phone=phone;
System.out.println("您的电话已设置");}
else
{System.out.println("密码错误");}
}
public boolean ifPassword(String password)//判断密码是否正确
{if(this.password==password)
return true;
else return false;
}
public void deposit(String password,double money)
{ if(ifPassword(password))
{balance+=money;
System.out.println("您的账户已充值"+balance+"元,您的余额为"+balance);}
else
{System.out.println("密码错误");}
}
public void withdraw(String password,double money)
{if(ifPassword(password))
{if(balance>money)
{balance-=money;
System.out.println("您的账户已取出"+balance+"元,您的余额为"+balance);}
else
{System.out.println("您的余额为不足");}}
else
{System.out.println("密码错误");}
}
public void displayCustomer(String password)
{if(ifPassword(password))
{System.out.println(" 您的身份证为"+peopleID);
System.out.println(" 您的余额为"+balance);
System.out.println(" 您的账号为"+ID);
System.out.println(" 您的姓名为"+name);
System.out.println(" 您的电话为"+phone);
}
else
{System.out.println("密码错误");}
}
public void changePassWord(String oldPassword,String newPassword1,String newPassword2)
{if(ifPassword(oldPassword))
{if( newPassword1== newPassword2)
{if(newPassword1.length()>5)
{this.password=newPassword1;
System.out.println("已修改密码");}
else
{System.out.println("你输入的密码不到6位,请重新输入");}}
else
{System.out.println("你输入的两次密码不一样,请重新输入");}
}
else
{System.out.println("密码错误");}
}
}
package text4;
public class Customer {
public static void main(String[] args) {
BankCustomer myCard=new BankCustomer("430725000000000000","xxxx","000000000000");
myCard.setName("666666","");
myCard.setName("567891","");
myCard.setPeopleID("666666","xxxxxxxxxx");
myCard.setPeopleID("567891","xxxxxxxxxx");
myCard.setPhone("666666","xxxxxxxxxx");
myCard.setPhone("567891","xxxxxxxxxx");
myCard.getbalance();
myCard.getID();
myCard.getPhone();
myCard.getPelpleID();
myCard.getName();
myCard.deposit("666666", 5000);
myCard.deposit("567891", 5000);//deposit money
myCard.withdraw("666666",10000);
myCard.withdraw("567891",10000);
myCard.withdraw("567891",5000);//withdraw money
myCard.displayCustomer("666666");
myCard.displayCustomer("567891");//displayCustomer
myCard.changePassWord("666666", "567891", "567891");
myCard.changePassWord("888888", "567891", "567891");
myCard.changePassWord("567891", "567891", "666666");//change password situations
myCard.deposit("666666", 5000);
myCard.deposit("567891", 5000);//when change password deposit money
myCard.withdraw("666666",10000);
myCard.withdraw("567891",10000);
myCard.withdraw("567891",5000);//when change password withdraw money
myCard.displayCustomer("666666");
myCard.displayCustomer("567891");//when change password displayCustomer
myCard.getbalance();
myCard.getID();
myCard.getPhone();
myCard.getPelpleID();
myCard.getName();
myCard.setName("666666","");
myCard.setName("567891","");
myCard.setPeopleID("666666","xxxxxxxxxx");
myCard.setPeopleID("567891","xxxxxxxxxx");
myCard.setPhone("666666","xxxxxxxxxx");
myCard.setPhone("567891","xxxxxxxxxx");
}
}