JAVA练习程序(八)

一、写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;包含的方法:访问器方法(getter和setter方法),返回月利率的方法getMonthlyInterest(),取款方法withdraw(),存款方法deposit()。
在这里插入图片描述

二、写一个用户程序测试Account类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%的Account对象。使用withdraw方法提款30000元,并打印余额。
再使用withdraw方法提款2500元,使用deposit方法存款3000元,然后打印余额和月利率。
提示:在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。

2、创建Account类的一个子类CheckAccount代表可透支的账户,该账户中定义一个属性overdraft代表可透支限额。在CheckAccount类中重写withdraw方法,其算法如下:
如果(取款金额<账户余额):可直接取款
如果(取款金额>账户余额):计算需要透支的额度
判断可透支额overdraft是否足够支付本次透支需要,如果可以,将账户余额修改为0,冲减可透支金额;如果不可以,提示用户超过可透支额的限额
要求:写一个用户程序测试CheckAccount类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%,可透支限额为5000元的CheckAccount对象。使用withdraw方法提款5000元,并打印账户余额和可透支额。再使用withdraw方法提款18000元,并打印账户余额和可透支额。再使用withdraw方法提款3000元,并打印账户余额和可透支额。
提示:
1.子类CheckAccount的构造方法需要将从父类继承的3个属性和子类自己的属性全部初始化。
2.父类Account的属性balance被设置为private,但在子类CheckAccount的withdraw方法中需要修改它的值,因此应修改父类的balance属性,定义其为protected。

package HomeWork;

public class Account {
	private int id;//账号
	protected double balance;//余额
	private double annualInterestRate;//年利率
	
	public Account (int id, double balance, double annualInterestRate) {
		this.id = id;
		this.balance = balance;
		this.annualInterestRate = annualInterestRate;
	}
/**
 * 
 * @return
 */
	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 getAnnualInterestRate() {
		return annualInterestRate;
	}

	public void setAnnualInterestRate(double annualInterestRate) {
		this.annualInterestRate = annualInterestRate;
	}
	/**
	 * 返回月利率
	 * @return
	 */
	public double getMonthlyInterest() {
		return annualInterestRate/12;
	}
	/**
	 * 取款
	 * @param amount
	 */
	public void withdraw (double amount) {
		if(balance>=amount)
			balance -=amount;
		else {
			System.out.println("余额不足");
			System.out.println("您的账户余额为:" + balance);	
		}
	}
	/**
	 * 存款
	 * @param amount
	 */
	public void deposit (double amount) {
		balance +=amount;
		System.out.println("您的账户余额为:" + balance);	
	}
		
	
}

package HomeWork;

public class AccountDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub		
		/**
		 * 测试Account
		 */	
		Account account = new Account(1122, 20000, 0.045);
		 account.withdraw(30000);
		 System.out.println();
		account.withdraw(2500);
		System.out.println();
		account.deposit(3000);
		System.out.printf("月利率为:%.5f",account.getMonthlyInterest());
		
		/**
		 * 测试CheckAccount
		 */
		CheckAccount checkaccount = new CheckAccount(1122, 20000, 0.045, 5000);
		checkaccount.withdraw(5000);
		System.out.println();
		checkaccount.withdraw(18000);
		System.out.println();
		checkaccount.withdraw(3000);	
	}

}

在这里插入图片描述
三、定义一“圆”Cirlcle类,圆心为“点”Point类,构造一圆,求圆的周长和面积,并判断某点与圆的关系。
Math.PI
class Cirlcle{
double radius;//半径
Point point;//圆心
获取半径
获取圆心
获取周长
获取面积
构造方法
}
class Point{
必要的属性;
对属性的set/get方法;
getDistance(…);//Math类里面的static double sqrt(double a)
//返回正确舍入的 double 值的正平方根。

package HomeWork;

public class Cirlcle {
	final static double PI=3.14;
	double radius;//半径
	Point point;//圆心
	/**
	 * 获取周长
	 * @return
	 */
	public double getperimeter() {
		return radius*2*PI;
	}
	/**
	 * 获取面积
	 * @return
	 */
	public double getarea() {
		return radius*radius*PI;
	}
	
	public Cirlcle(double radius,Point point) {
		this.point=point;
		this.radius=radius;
	}
	
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius = radius;
	}
	public Point getPoint() {
		return point;
	}
	public void setPoint(Point point) {
		this.point = point;
	}
	
}

package HomeWork;

public class Point {
	private double X;
	private double Y;
	
	public double getDistance(Point point1) {
		//Math类里面的static double sqrt(double a) 
		//返回正确舍入的 double 值的正平方根。
		return Math.sqrt(Math.abs(((this.X-point1.X)*(this.X-point1.X)) + (this.Y-point1.Y)*(this.Y-point1.Y)));                  			
	}
    
	public double getX() {
		return X;
	}
	public void setX(double x) {
		X = x;
	}
	public double getY() {
		return Y;
	}
	public void setY(double y) {
		Y = y;
	}

	public Point(double X,double Y) {
		this.X=X;
		this.Y=Y;
	}
}

package HomeWork;

public class CirlclePointDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Point point1 = new Point(1,2);
		Point point2 = new Point(3,4);
		Cirlcle cirlcle = new Cirlcle(1, point1);
		System.out.println("周长为:"  + cirlcle.getperimeter());
		System.out.println("面积为:"  + cirlcle.getarea());
		
		if(cirlcle.point.getDistance(point2)>cirlcle.radius) System.out.println("圆外");
		else if(cirlcle.point.getDistance(point2)==cirlcle.radius) System.out.println("圆上");
		else System.out.println("圆内");
	}

}

**4、已知字符串
String str=” ac,42,123,sd Fa,c df,4,adf,5ewRRre ”;
1. 找出统计出字符串里抽的数字个数

public static void getnum(String str) {
		int number =0;
		for(int i=0;i<str.length();i++){
	           if(str.charAt(i)>=48&&str.charAt(i)<=57){
	               number++;
	           }	
		 }
		 System.out.println("数字个数为:" + number);
	}

2. 找出a字符所在的所有索引

public static void findindex(String str,char character) {
		 int a= str.indexOf("a");
		 System.out.println("索引为:" );
		 while(a!=-1) {
			 System.out.println(a);
			 a= str.indexOf(character, a+1);
		 }		 
	}

3. 把里面的大写转化成小写

public static void lowercase(String str) {
		str = str.toLowerCase();
		System.out.println(str);
	}

4. 把里面的小写转化成大写

public static void uppercase(String str) {
		str = str.toUpperCase();
		System.out.println(str);
	}

5. 找出最后一个R所在的索引

public static void findlastindex(String str,char character ) {
		 int a= str.indexOf("a");
		 int index = 0;
		 System.out.println("索引为:" );
		 while(a!=-1) {
			 a= str.indexOf(character, a+1);	 
			 if(a!=-1)
				 index=a;			 
		 }		 
		 System.out.println("最后的索引为:" + index);
	}

6. 统计字符串的长度

public static void getlenth(String str) {
		System.out.println(str.length());
	}

7. 把字符串前后的空格去掉

public static void removeblank(String str,char character) {
		str = str.trim();
		 System.out.println(str);
	}

8. 把字符串中所有空格去掉**

public static void removeallblank(String str,char character) {
		str = str.replace(" ", "");
		System.out.println(str);
	}

**
四、
String str=”123dsgfadsgjlafdjhladDWGDFADFADSFADSFDASnhsdaf!@$%@#45324rdsf”
1.统计字符串大写字母,小写字母,其它的个数
2.编写一个方法countChar(String str),输出一个字符串中指定定字符串出现的次数
**

package HomeWork;

public class StringOperate2 {
	public static void main(String[] args) {
		
		String str="123dsgfadsgjlafdjhladDWGDFADFADSFADSFDASnhsdaf!@$%@#45324rdsf";
		int uppercharacter_num=0;
		int lowercharacter_num=0;
		int number_num = 0;
		for(int i=0;i<str.length();i++){
			if(str.charAt(i)>=48&&str.charAt(i)<=57)
				number_num++;
			else if(str.charAt(i)>=65&&str.charAt(i)<=90)
				uppercharacter_num++;
			else if(str.charAt(i)>=97&&str.charAt(i)<=122)
				lowercharacter_num++;
		}
		System.out.println("数字个数:" + number_num);
		System.out.println("大写字母个数:" + uppercharacter_num);
		System.out.println("小写字母个数:" + lowercharacter_num);
		countstring(str,"FAD");
	}	
	
	public static void countstring(String str,String find_str) {
		int count = 0;//出现次数
		int index = 0;//字符串位置
	    int num = str.indexOf(find_str);
	    while(( index = str.indexOf(find_str)) !=-1) {
	    	count++;
	    	str = str.substring(str.indexOf(find_str) + find_str.length());
	    }
	    System.out.println(find_str + "出现次数" + count);
	}
	
	
	
}

五、完成以下要求
1.生成5个0-100之间的整数随机数。
2.将给定字符串“ABCDEFG”转成字符数组。
3.将字符串按指定格式转换为日期"2014-07-30"->Date,再将此Date按指定格式转换为字符串Date->“2014年07月30日”。
4.随机生成10个15-58之间的整数放到数组中并输出。
5.随机产生100个小写字母并保存到字符数组中,用该数组生成字符串,用String类的方法统计字母’c’出现的次数.

package HomeWork;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Random;

public class StringOperate3 {

	public static void main(String[] args) throws ParseException {
		// TODO Auto-generated method stub
		String str = "ABCDEFG";
		randomnumber();
		turnstring(str);
		Date date = new Date(); 
		System.out.println("日期转字符串:" + DateToString(date)); 
		System.out.println("字符串转日期:" + StringToDate(DateToString(date))); 
		
	}
	/**
	 * 生成5个0-100之间的整数随机数
	 */
	public static void randomnumber() {
		int[] num = new int[5];
		for(int i = 0;i<5;i++) {
			num[i] = (int)(Math.random()*100);
		}
		System.out.println(Arrays.toString(num));
	}
	/**
	 * .将给定字符串“ABCDEFG”转成字符数组
	 */
	public static void turnstring(String str) {
		char[] arr = str.toCharArray();
		System.out.println(Arrays.toString(arr));
	}
	/**
	 * 将字符串按指定格式转换为日期"2014-07-30"->Date
	 * @throws ParseException 
	 */
	public  static Date StringToDate(String str) throws ParseException {	
		 SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		 Date date =  format.parse(str);
		 return date;
	}
	/**
	 * 再将此Date按指定格式转换为字符串Date->"2014年07月30日"
	 */
	public  static String DateToString(Date date) { 		  
		   SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); 
		   String str = format.format(date); 
		   return str; 
		} 
	
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值