大家好,我是Java进阶者。
一、使用Hashtable来检验随机数的随机性
1.首先是创建Hashtable,使用for循环和定义一个产生随机数的r,key值对应随机数的value值。
import java.util.*;
class Counter{
int i =1;
public String toString(){
return Integer.toString(i);
}
}
public class T12 {
public static void main(String[] args) {
Hashtable ht = new Hashtable();
for (int i =0;i < 500; i++){
Integer r = new Integer((int)(Math.random()*10));
if(ht.containsKey(r)){
((Counter)ht.get(r)).i++;
}
else{
ht.put(r, new Counter());
}
}
System.out.println(ht);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
运行的结果如下所示:
二、 设计一个模拟银行账户功能的类Account
1.要求如下所示:
(a)属性:账号(card,字符串类型)、储户姓名(name,字符串类型)、地址(address,字符串类型)、存款余额(balance,浮点型)、最低余额(minBalance,浮点型)
(b)方法:初始化所有属性的构造方法、存款(deposit方法)、取款(draw方法)、查询(query方法)
要求:存款操作需显示储户原有余额、将要存款数额及最终存款余额;取款时,若最后余额小于最小余额,拒绝取款并显示"取款失败,至少保留余额XXX";查询操作能显示储户账号、姓名、地址、存款余额及最低余额。
2.例子的实现:
public class Account {
String card;//账号
String name;//姓名
String address;//地址
double balance;//存款余额
static double minBalance;//最低余额
Account(String card,String name,String address,double balance){
this.card=card;
this.name=name;
this.address=address;
this.balance=balance;
Account.minBalance=5.0;
}
//存款方法
public void deposit(double m){
System.out.println("您账户余额:"+balance);
System.out.println("存款数额 :"+m);
balance+=m;
System.out.println("最终存款余额:"+balance);
}
//取款(draw方法)
public void draw(double m){
double temp=balance-m;
if(temp<minBalance){
System.out.println("取款失败,至少保留余额为"+minBalance);
}else{
balance=temp;
}
}
//查询方法
public void query(){
System.out.println("您的账号:"+card+"\n姓名:"+name+"\n地址:"+address+"\n存款余额:"+balance+"\n最低余额:"+minBalance);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Account a=new Account("123456", "张三", "广东省xx", 200.00);
a.deposit(300.0);
a.draw(200);
a.query();
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
运行的结果如下所示:

三、设计一个描述二维平面上点的类Point
1.要求如下所示:
(a)该类需要描述点的浮点型的横坐标x和纵坐标y。
(b)提供能够初始化横纵坐标的有参构造方法,要求参数名称与属性同名。
(c)计算两点间距离的方法distance。
提示:两点之间距离等于两点横纵坐标之差的平方和再开方Math类中求平方根的方法:static double sqrt(double a)
2.例子的实现:
public class Point {
double x,y;//横坐标x和纵坐标y
//有参数构造方法
Point(double x,double y){
this.x=x;
this.y=y;
}
//计算两点间距离的方法distance
double distance(Point a){
double i=Math.pow((a.x-this.x), 2);
double j=Math.pow((a.y-this.y), 2);
double c=Math.sqrt(i+j);
return c;
}
public static void main(String[] args) {
Point p1=new Point(2,1);
Point p2=new Point(1,1);
double d=p1.distance(p2);
System.out.print("两点之间的距离为:"+d);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
运行的结果如下所示:
四、 数据类型类
1.数据类型类:数据类型类也称为包装类,它是封装了基本的数据类型。
如下图所示:
2.数据类型类的常用方法
(a)对象名.intValue()是获取对象的基本数据类型例如int,float,char等。
(b)对象名.toString()是把基本数据类型转换成字符串。
(c)类名.valueOf(String str)是把str转换成对应的数据类型对象。
五、总结
设计一个模拟银行账户功能的类Account根据它的要求实现它的功能,掌握类和对象编程。设计一个描述二维平面上点的类Point根据要求实现功能,掌握构造方法的使用。数据类型类也称为包装类,它是封装了基本的数据类型。