Java语言程序设计,Chap9-T7、Chap10-T7、Chap11-T1

- Chap9-T7

package Account;

/*
 * 9.7  (账户类Account)设计一个名为Account的类,它包括:
一个名为id的int类型私有数据域(默认值为0)。
一个名为balance的double类型私有数据域(默认值为0)。
一个名为annualInterestRate的double类型私有数据域存储当前利率(默认值为0)。假设所有的账户都有相同的利率。
一个名为dateCreated的Date类型的私有数据域,存储账户的开户日期。
一个用于创建默认账户的无参构造方法。
一个用于创建带特定id和初始余额的账户的构造方法。
id、balance和annualInterstRate的访问器和修改器。
dateCreated 的访问器。
一个名为getMonthlyInterestRate()的方法,返回月利率。
一个名为withDraw的方法,从账户提取特定数额。
一个名为deposit的方法向账户存储特定数额。
画出该类的UML图并实现这个类。
提示:方法getMonthlyInterestO用于返回月利息,而不是利率。
月利息是balance*monthly一InterestRate。monthlyInterestRate是annualInterestRate/12。
注意, annualInterestRate是一个百分数,比如4.5%。你需要将其除以100。
编写一个测试程序,创建一个账户ID为1122、余额为20 000美元、年利率为4.5%的Account对象。
使用withdraw方法取款2500美元,使用deposit方法存款3000美元,然后打印余额、月利息以及这个账户的开户日期。
 */

import java.util.Date;

public class Account {

	private int id = 0;   //银行账户
	private double balance = 0;  //金额数
	private double annualInterestRate = 0;  //存储当前利率
	private Date dateCreated = new Date();   //创建账号的日期
	
	public Account() {
	}
	public Account(int id, double balance) {
		this.id = id;
		this.balance = balance;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	public int getId() {
		return this.id;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	public double getBalance() {
		return this.balance;
	}
	public void setAnnualInterestRate(double annualInterestRate) {
		this.annualInterestRate  = annualInterestRate;
	}
	public double getAnnualInterestRate() {
		return this.annualInterestRate;
	}
	
	public Date getDateCreated() {
		return dateCreated;
	}
	public double getMonthlyInterest() {
		double monthlyInterestRate = annualInterestRate / (12*100);
		double monthlyInterest = balance*monthlyInterestRate;
		return monthlyInterest;
	}
	public double withDraw(double withdraw) {
		balance -= withdraw;
		return balance;
	}
	public double deposit(double deposit) {
		balance += deposit;
		return balance;
	}
}
package Account;
import java.text.SimpleDateFormat;
public class HomeworkAccount {
	public static void main(String[] args){
		Account account = new Account(1122,20000);
		account.setAnnualInterestRate(4.5);
		account.withDraw(2500);
		account.deposit(3000);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = sdf.format(account.getDateCreated());
		System.out.print("这个账户的余额是"+account.getBalance()+",月利息是"
		+account.getMonthlyInterest()+",这个账户的开户日期是"+date);
	}
}

Chap10-T7

package ATM;

/*
 * **10.7 (游戏: ATM机)使用编程练习题9.7中创建的Account类来模拟一台ATM机。
 * 创建一个有10个账户的数组,其id为0, 1,..... ,9,并初始化收支为100美元。
 * 系统提示用户输人一个id。如果输人的id不正确,就要求用户输入正确的id。
 * 一旦接受一个id,就显示如运行示例所示的主菜单。
 * 可以选择1来查看当前的收支,选择2表示取钱,选择3表示存钱,选择4表示退出主菜单。一旦退出,系统就会提示再次输入id。
 * 所以,系统一旦启动就不会停止。
 */
import java.util.*;
public class HomeworkATM {
	    public static void main(String[] args) {
	        Scanner input=new Scanner(System.in);
	        accountatm atm=new accountatm();
	        atm.init();
	        System.out.print("enter the id:");
	        int id=input.nextInt();
	        atm.enterid(id);
	    }

	}
	class accountatm{
	    private int id;
	    int count;
	    int choice,count1=0;
	    int mony;
	    Scanner input=new Scanner(System.in);
	    public int []a=new int[10];
	    public void init(){
	        for(int i=0;i<10;i++)
	            a[i]=100;
	    }
	    public void enterid(int id) {
	        this.id=id;
	        count=0;
	        while(this.id<0||this.id>10)
	        {
	            System.out.print("invalid id!plez enter new:");
	            this.id=input.nextInt();
	            if(count++==4)
	            {
	                System.out.println("times to upper limit!");
	                System.exit(1);
	            }       
	        }
	        ptmenu();
	    }
	        void ptmenu() {
	        System.out.printf("1.check balance\n2.withdraw\n3.deposit\n4.exit\n");
	        System.out.print("enter a choice:");
	        choice=input.nextInt();
	        while(choice<1||choice>4) {
	            System.out.println("invalid choice!plez enter the choice: ");
	            choice=input.nextInt();
	            if(count1++==4)
	                System.exit(1);
	        }
	        if(choice==1)
	            checkbalance(id);
	        else if(choice==2)
	        {   
	            id=input.nextInt();
	            mony=input.nextInt();
	            withdraw(id,mony);}
	        else if(choice==3) {

	            id=input.nextInt();
	            mony=input.nextInt();
	            deposit(id,mony);
	        }else{
	            System.out.println("at the end!");
	            System.exit(1);
	        }
	    }
	    public void checkbalance(int id) {
	        System.out.println("mony :"+a[id]);
	        System.out.print("enter 1 to continue:");
	        if(input.nextInt()==1)
	            ptmenu();
	        else
	            System.exit(1);
	    }
	    void withdraw(int id,int mony) {
	        if(mony>a[id]) {
	            System.out.println("over the limit!");
	        }
	        else
	            a[id]-=mony;
	        System.out.println(mony+" you get,the remaining :"+a[id]);
	        System.out.print("enter 1 to continue:");
	        if(input.nextInt()==1)
	            ptmenu();
	        else
	            System.exit(1);

	    }
	    void deposit(int id,int mony) {
	        a[id]+=mony;
	        System.out.println(mony+" you save,the total : "+a[id]);
	        if(input.nextInt()==1)
	            ptmenu();
	        else
	            System.exit(1);
	    }
}

Chap11-T1

package Triangle;

/*
	 * 11.1  (三角形类Triangle)设计一个名为Triangle的类来扩展Geometricobject类。 该类包括:
	● 三个名为sidel、 side2和side3的double数据域表示这个三角形的三条边,它们的默认值是1.0。
	●一个无参构造方法创建默认的三角形。
	●一个能创建带指定side1、 side2和side3的三角形的构造方法。
	●所有三个数据域的访问器方法。
	●一个名为getArea()的方法返回这个三角形的面积。
	●一个名为getPerimeter()的方法返回这个三角形的周长。
	●一个名为toString(的方法返回这个三角形的字符串描述。
	计算三角形面积的公式参见编程练习题2.19。toString() 方法的实现如下所示:
	return "Triangle: sidel =”+ side1 +”side2 =”+ side2 + “side3 =”+ side3;
	画出Triangle类和Geometricobject类的UML图,并实现这些类。
	编写一个测试程序,提示用户输人三角形的三条边、颜色以及一个Boolean值表明该三角形是否填充。
	程序应该使用输入创建一个具有这些边并设置color和filled属性的三角形。
	程序应该显示面积、边长、颜色以及表明是否填充的真或者假的值。
 */

import java.util.Date;
import java.text.SimpleDateFormat;

public class Geometricobject {
	private String color;
	private boolean filled;
	private Date dateCreated = new Date();
	
	public Geometricobject() {
		
	}
	
	public Geometricobject(String color, boolean filled) {
		this.color = color;
		this.filled = filled;
	}
	
	public void setColor(String color) {
		this.color = color;
	}
	public String getColor() {
		return this.color;
	}
	
	public void setFilled(boolean filled) {
		this.filled = filled;
	}
	public boolean isFilled() {
		return filled;
	}
	
	public Date getDateCreated() {
		return this.dateCreated;
	}
	
	public String toString() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = sdf.format(dateCreated);
		String boo;
		if (isFilled() == true) {
			boo = "该几何体的颜色已经填充,该颜色是"+ getColor();
		}else {
			boo = "该几何体的颜色还未填充";
		}
		return boo+"。该几何体的创建日期是:"+date;
	}
}
package Triangle;
public class Triangle extends Geometricobject{
	private double side1 = 1.0;
	private double side2 = 1.0;
	private double side3 = 1.0;
	
	public Triangle() {
		
	}
	public Triangle(double side1, double side2, double side3) {
		this.side1 = side1;
		this.side2 = side2;
		this.side3 = side3;
	}
	public Triangle(double side1, double side2, double side3,String color, boolean filled) {
		this.side1 = side1;
		this.side2 = side2;
		this.side3 = side3;
		super.setColor(color);
		super.setFilled(filled);
	}
	
	public void setSide1(double side1) {
		this.side1 = side1;
	}
	public double getSide1() {
		return this.side1;
	}
	public void setSide2(double side2) {
		this.side2 = side2;
	}
	public double getSide2() {
		return this.side2;
	}
	public void setSide3(double side3) {
		this.side3 = side3;
	}
	public double getSide3() {
		return this.side3;
	}
	
	public double getArea() {
		double p = (side1 + side2 +side3)/2;
		double a = p*(p-side1)*(p-side2)*(p-side3);
		double area = Math.sqrt(a);
		return area;
	}
	
	public double getPerimeter() {
		double perimeter = side1 + side2 + side3;
		return perimeter;
	}
	
	public String toString() {
		return "三角形的三条边长为:"+side1+","+side2+","+side3+"。"+"三角形的面积是:"+getArea()+",周长为:"+getPerimeter()+"。三角形已填充颜色为"+isFilled()+",填充的颜色为"+getColor();
	}
}
package Triangle;
import java.util.Scanner;

public class HomeworkTriangle {
	public static void main(String[] args) {
		Scanner input1 = new Scanner(System.in);
		System.out.print("请输入三角形的第一条边长");
		double side1 = input1.nextDouble();
		Scanner input2 = new Scanner(System.in);
		System.out.print("请输入三角形的第二条边长");
		double side2 = input2.nextDouble();
		Scanner input3 = new Scanner(System.in);
		System.out.print("请输入三角形的第三条边长");
		double side3 = input3.nextDouble();
		Scanner input4 = new Scanner(System.in);
		System.out.print("请输入三角形的颜色");
		String color = input4.next();
		Scanner input5 = new Scanner(System.in);
		System.out.print("请输入三角形是否填充的属性");
		boolean filled = input4.nextBoolean();
		
		Triangle t = new Triangle(side1,side2,side3,color,filled);
		System.out.print(t.toString());
	}
}

Time题

package Time;

/*
 * 设计一个名为Time的类。这个类包含:
	●表示时间的数据域hour、minute和second。
	●一个以当前时间创建Time对象的无参构造方法(数据域的值表示当前时间)。
	●一个构造Time对象的构造方法,这个对象有一个特定的时间值,
	   这个值是以毫秒表示的、从1970年1月1日午夜开始到现在流逝的时间段(数据域的值表示这个时间)。
	●一个构造带特定的小时、分钟和秒的Time对象的构造方法。
	●三个数据域hour、minute和second各自的get方法。
	●一个名为setTime(long elapseTime)的方法使用流逝的时间给对象设置一个新时间。
	例如,如果流逝的时间为55550000毫秒,则转换为10小时、10 分钟、10 秒。
		画出该类的UML图并实现这个类。
		编写一个测试程序,创建两个Time对象(使用newTime()和new Time(555550000)),然后显示它们的小时、分钟和秒。
		提示:前两个构造方法可以从流逝的时间中提取出小时、分钟和秒。
		对于无参构造方法,当前时间可以使用System. currentTimeMil1s获取当前时间,如程序清单2-7所示。
 */

public class Time {
	
	private int hour;
	private int minute;
	private int second;
	public long time = System.currentTimeMillis();
	
	public Time() {
	}
	
	public Time(long time) {
		this.time =time;
	}
	
	public Time(int hour,int minute,int second,long time) {
		this.hour = hour;
		this.minute = minute;
		this.second = second;
		this.time = time;
	}
	
	public void setTime(long elapseTime) {
		long H = elapseTime/(60*60*1000);
		long M = (elapseTime % (60*60*1000))/60;
		long S = (elapseTime % (60*60*1000))%60;
		System.out.println("如果流逝的时间为"+ elapseTime +"毫秒,则转换为"+H+"小时、"+M+"分钟、"+S +"秒。");
	}
	
	public void setHour(int hour) {
		this.hour = hour ;
	}
	public int getHour() {
		return this.hour;
	}
	
	public void setMinute(int minute) {
		this.minute =  minute ;
	}
	public int getMinute() {
		return this.minute;
	}
	
	public void setSecond(int second) {
		this.second =  second ;
	}
	public int getSecond() {
		return this.second;
	}

}
package Time;
public class HomeworkTime {
	public static void main(String[] args) {
		Time time1 = new Time();
		Time time2 = new Time(555550000);
		time1.setTime(time1.time);
		time2.setTime(555550000);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值