java面向对象编程练习:将对象作为参数传递给方法、Account_Customer

一、将对象作为参数传递给方法

(1)定义一个Circle类,包含一个double型的radius属性代表圆的半径,一个findArea()方法返回圆的面积。

(2)定义一个类PassObject,在类中定义一个方法printAreas(),该方法的定义如下:

public void printAreas(Circle c, int time)

在printAreas方法中打印输出1到time之间的每个整数半径值,以及对应的面积。例如,times为5,则输出半径1,2,3,4,5,以及对应的圆面积。

在main方法中调用printAreas()方法,调用完毕后输出当前半径值。

public class Circle {
	
	private double radius;//半径

	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}
	
	//计算圆面积
	public double findArea() {
		return 3.14*radius*radius;
	}
	

}
public class PassObject {

	public static void main(String[] args) {
		PassObject p = new PassObject();
		Circle c = new Circle();
		p.printAreas(c,3);

	}
	
	//打印输出1到time之间的每个整数半径值,以及对应的面积
	public void printAreas(Circle c, int time) {
		int r = 0;
		System.out.println("radius"+"\t"+"areas");
		for(r=1; r<=time; r++) {
			c.setRadius(r);
			System.out.println(c.getRadius()+"\t"+c.findArea());
		}
		System.out.println("当前半径为:"+r);
	}

}

java面向对象编程练习:将对象作为参数传递给方法、Account_Customer_第1张图片

二、Account_Customer

1、写一个名为Account的类模拟账户

该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;包含的方法:访问器方法(getter和setter方法),取款方法withdraw(),存款方法deposit()。

Account

private int id

private double balance

private double annualInterestRate

public Account (int id, double balance, double annualInterestRate )

public int getId()

public double getBalance()

public double getAnnualInterestRate()

public void setId( int id)

public void setBalance(double balance)

public void setAnnualInterestRate(double annualInterestRate)

public void withdraw (double amount)

public void deposit (double amount)

提示:在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。

public class Account {

	private int id;//账号
	private double balance;//余额
	private double annualInterestRate;//年利率
	
	public Account (int id, double balance, double annualInterestRate ) {
		this.id = id;
		this.balance = balance;
		this.annualInterestRate = annualInterestRate;
	}
	
	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;
	}
	
	//取款
	public void withdraw (double amount) {//amount取钱数
		if(balance >= amount) {
			balance -= amount;
			System.out.println("交易成功!取款:"+amount+"元");
			System.out.println("目前账户余额为:"+balance+"元");
		}else {
			System.out.println("余额不足,交易失败!");
		}
	}
	
	//存款
	public void deposit (double amount) {
		balance += amount;
		System.out.println("交易成功!存款:"+amount+"元");
		System.out.println("目前账户余额为:"+balance+"元");
	}
	
}

2、创建Customer类。

Customer

private String firstName

private String lastName

private Account account

public Customer(String f,String l)

public String getFirstName()

public String getLastName()

public Account getAccount()

public void setAccount(Account account)

a. 声明三个私有对象属性:firstName、lastName和account。

b. 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(f和l)

c. 声明两个公有存取器来访问该对象属性,方法getFirstName和getLastName返回相应的属性。

d. 声明setAccount 方法来对account属性赋值。

e. 声明getAccount 方法以获取account属性。

public class Customer {
	
	private String firstName;
	private String lastName;
	private Account account;
	
	public Customer(String f,String l) {
		this.firstName = f;
		this.lastName = l;
	}

	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}
	
	
}

3、写一个测试程序。

  1. 创建一个Customer ,名字叫 Jane Smith, 他有一个账号为1000,余额为2000元,年利率为 1.23% 的账户。
  2. 对Jane Smith操作。存入 100 元,再取出960元。再取出2000元。
public class CustomerTest {
	public static void main(String[] args) {
		Customer cust = new Customer("Jane","Smith");
		cust.setAccount(new Account(1000, 2000, 0.0123));
		
		Account account = cust.getAccount();
		account.deposit(100);
		account.withdraw(960);
		account.withdraw(2000);
	}

}

java面向对象编程练习:将对象作为参数传递给方法、Account_Customer_第2张图片

 

你可能感兴趣的:(编程练习,java)