共5道大题 最后一题为自动洗牌发牌系统
1) 编写一个类实现银行帐户的概念。包括的属性有:帐号、储户姓名、地址、存款余额,包括的方法有:存款、取款、查询、计算利息、累加利息等。
public class bank { private int id; private String name; private String address; private double money; public bank() { this.id = 0; this.name = "默认姓名"; this.address = "默认地址"; this.money = 0; } public String toString() { return "bank [id=" + id + ", name=" + name + ", address=" + address + ", money=" + money + "]"; } public bank(int id, String name, String address, int money) { super(); this.id = id; this.name = name; this.address = address; this.money = money; } 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 String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } /** * 存款 */ public void saving(int m,int y) { double a=getMoney(); double tax=y*0.001*m; setMoney(getMoney()+m+tax); System.out.println("存款成功:"+a+"元 "+y+"年后--->"+getMoney()+"元"); } /** * 取款 */ public void take(int m) { double a=getMoney(); if(getMoney()<0) { System.out.println("余额不足取款失败"); } setMoney(getMoney()-m); System.out.println("取款成功:"+a+"元--->"+getMoney()+"元"); } }
2)编写一个类实现桌子的概念,包括的属性有长、宽、高和颜色,包括的方法有:计算体积、显示桌子信息、设置桌子颜色及长宽高。
public class desk { private double length; private double width; private double height; private String color; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public desk(double length, double width, double height, String color) { super(); this.length = length; this.width = width; this.height = height; this.color = color; } public double V() { return length*height*width; } @Override public String toString() { return "desk [length=" + length + ", width=" + width + ", height=" + height +", volume="+V()+ "]"; } }
3)编写一个类实现一个圆,属性包括圆心、半径,方法主要包括显示的圆的信息、求面积等。圆心是Point类的对象,有两个属性x,y,代表横纵坐标。
public class circle { private double r; private double x; private double y; public double getR() { return r; } public void setR(double r) { this.r = r; } publ4ic double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } public circle(point p, double r) { this.r = r; this.x = p.getX(); this.y = p.getY(); } public double area() { return (double)((int)(Math.PI*Math.pow(r, 2)*100))/100; //保留两位小数 } @Override public String toString() { return "circle [r=" + r + ", x=" + x + ", y=" + y + ", area=" + area() + "]"; } } 圆心类 public class point { private int x; private int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public point(int x, int y) { this.x = x; this.y = y; } public point() { this.x=0; this.y=0; } }
4)设计一个扑克洗牌算法。将52张牌(不包括大、小王)用对象的形式表示,每张牌包括花色和大小。大小按如下序号排列:(2、3、4、5、6、7、8、9、10、J、Q、K、A), 花色为: ♠ 、♣、♥、♦ ,首先将扑克按顺序存放到一个一维数组中,然后按洗牌算法进行洗牌,最后,按东、南、西、北进行分发,显示扑克时将同一花色放在一起。
card类
public class card { private String color; private String num; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } @Override public String toString() { return " "+color+"-"+num; } }
具体算法代码:建牌库,洗牌,理牌
public class cards_test { public static void main(String[] args) { final int N=52,M=13; card[] cards=new card[N]; card[] East=new card[M]; card[] West=new card[M]; card[] North=new card[M]; card[] South=new card[M]; for(int i=0;i<M;i++) East[i]=new card(); for(int i=0;i<M;i++) South[i]=new card(); for(int i=0;i<M;i++) West[i]=new card(); for(int i=0;i<M;i++) North[i]=new card(); String[] color= {"红桃","草花","红心","方片"}; String[] num= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; for(int i=0;i<N;i++) { cards[i]=new card(); cards[i].setNum(num[i%13]); cards[i].setColor(color[i/13]); } System.out.println("洗牌前"); for(int i=0;i<N;i++) { System.out.print(cards[i].toString()); if((i+1)%13==0) System.out.println(); } for(int i=0;i<N;i++) { int index=(int)(Math.random()*10000)%52; String temp=cards[i].getNum(); cards[i].setNum(cards[index].getNum()); cards[index].setNum(temp); String co=cards[i].getColor(); cards[i].setColor(cards[index].getColor()); cards[index].setColor(co); } System.out.println("洗牌后"); String[] position= {"东:","西:","南:","北:"}; for(int i=0;i<N;i++) { if(i%13==0) System.out.print(position[i/13]); if(i>=0&&i<=12) East[i]=cards[i]; else if(i>=13&&i<=25) South[i-13]=cards[i]; else if(i>=26&&i<=38) West[i-26]=cards[i]; else North[i-39]=cards[i]; System.out.print(cards[i].toString()); if((i+1)%13==0) System.out.println(); } System.out.print("理牌后(相同花色放一起)"); System.out.print(" "+position[0]); for(int j=0;j<4;j++) for(int i=0;i<M;i++) if(East[i].getColor().equals(color[j])) System.out.print(East[i].toString()); System.out.print(" "+position[1]); for(int j=0;j<4;j++) for(int i=0;i<M;i++) if(West[i].getColor().equals(color[j])) System.out.print(West[i].toString()); System.out.print(" "+position[2]); for(int j=0;j<4;j++) for(int i=0;i<M;i++) if(South[i].getColor().equals(color[j])) System.out.print(South[i].toString()); System.out.print(" "+position[3]); for(int j=0;j<4;j++) for(int i=0;i<M;i++) if(North[i].getColor().equals(color[j])) System.out.print(North[i].toString()); } }