본문 바로가기
Programming Study/절대JAVA

[절대JAVA]5장 프로그래밍 연습문제(2)

by 푸고배 2018. 9. 23.

6. 다음을 만족하는 클래스 Account를 작성하시오.

- 다음의 2개의 필드를 선언

private String owner;

private long balanc;

- 위 모든 필드에 대한 getter와 setter의 구현

- 위 모든 필드를 사용하는 가능한 모든 생성자의 구현

<소스코드>

package Chapter5_6;

public class Account {
	private String owner;
	private long balance;
	public String getOwner() {
		return owner;
	}
	public void setOwner(String owner) {
		this.owner = owner;
	}
	public long getBalance() {
		return balance;
	}
	public void setBalance(long balance) {
		this.balance = balance;
	}
	Account(){
		owner = "JSH";
		balance=0;
	}
	Account(String owner){
		this.owner = owner;
		this.balance=0;
	}
	Account(long balance){
		this.owner="JSH";
		this.balance=balance;
	}
	Account(String owner, long balance){
		this.owner = owner;
		this.balance = balance;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

 

 

7. 위에서 구현된 클래스 Account에서 다음 기능을 추가하여 작성하시오.

- 메소드 deposit()의 헤드는 다음과 같으며 인자인 금액을 저축하는 메소드

    public long deposit(long amount)

- 메소드 withdraw()의 헤드는 다음과 같으며 인자인 금액을 인출하는 메소드

    public long 

withdraw(long amount)

- Account 클래스의 main() 메소드에서 Account 객체를 생성하여 적당한 저축과 인출을 수행한 후 잔금을 출력

<소스코드>

package Chapter5_7;

public class Account {
	private String owner;
	private long balance;
	public String getOwner() {
		return owner;
	}
	public void setOwner(String owner) {
		this.owner = owner;
	}
	public long getBalance() {
		return balance;
	}
	public void setBalance(long balance) {
		this.balance = balance;
	}
	Account(){
		owner = "JSH";
		balance=0;
	}
	Account(String owner){
		this.owner = owner;
		this.balance=0;
	}
	Account(long balance){
		this.owner="JSH";
		this.balance=balance;
	}
	Account(String owner, long balance){
		this.owner = owner;
		this.balance = balance;
	}
	public long deposit(long amount) { // 저축 메소드
		this.balance+=amount;
		return this.balance;
	}
	public long withdraw(long amount) { // 인출 메소드
		this.balance-=amount;
		return this.balance;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Account a = new Account("SH",100_000);
		a.deposit(10000);
		a.withdraw(5000);
		System.out.println(a.getBalance());
	}

}

 

8. 위에서 구현된 메소드 withdraw()를 다음 조건에 맞게 다시 작성하시오.

- 인출 상한 금액은 잔액까지로 하며, 이 경우 이러한 상황을 출력

- 클래스 AccountTest의 main() 메소드에서 인출 상한 이상의 금액을 인출하려는 메소드를 호출하여 출력

<소스코드>

package Chapter5_8;

public class AccountTest {
	private String owner;
	private long balance;
	public String getOwner() {
		return owner;
	}
	public void setOwner(String owner) {
		this.owner = owner;
	}
	public long getBalance() {
		return balance;
	}
	public void setBalance(long balance) {
		this.balance = balance;
	}
	AccountTest(){
		owner = "JSH";
		balance=0;
	}
	AccountTest(String owner){
		this.owner = owner;
		this.balance=0;
	}
	AccountTest(long balance){
		this.owner="JSH";
		this.balance=balance;
	}
	AccountTest(String owner, long balance){
		this.owner = owner;
		this.balance = balance;
	}
	public long deposit(long amount) { // 저축 메소드
		this.balance+=amount;
		return this.balance;
	}
	public long withdraw(long amount) { // 인출 메소드
		if(this.balance>=amount)
			this.balance-=amount;
		else
			System.out.println("잔액이 부족합니다.");
		return this.balance;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		AccountTest a = new AccountTest("SH",100_000);
		a.withdraw(150_000);
		a.deposit(10000);
		a.withdraw(5000);
		System.out.println(a.getBalance());
	}

}

 

9. 다음을 만족하는 클래스 Rectangle을 작성하시오.

- 사각형의 가로와 세로로 객체를 생성하는 생성자

- 면적을 반환하는 메소드 getArea(), 둘레를 반환하는 메소드 getCircumference(),

- 다음과 같이 클래스 Rectangle 이용

Rectangle rc = new Rectangle(3.82, 8.65);

System.out.println("면적 : " + rc.getArea());

System.out.println("둘레 : " + rc.getCircumference());

<소스코드>

package Chapter5_9;

public class Rectangle {
	double width;
	double height;
	Rectangle(double width, double height){
		this.width = width;
		this.height = height;
	}
	public double getArea() {
		return this.width*this.height;
	}
	public double getCircumference() {
		return 2*(this.width+this.height);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Rectangle rc = new Rectangle(3.82, 8.65);
		System.out.println("면적 : "+rc.getArea());
		System.out.println("둘레 : "+rc.getCircumference());
	}

}

 

10. 다음을 만족하는 클래스 Computer를 작성하시오.

- 다음을 상수 필드로 선언

    public ... String[] osType = {"윈도7", "애플 OS X", "안드로이드"};

- 다음과 같은 클래스 Computer의 객체의 사용 결과에 적합하도록 생성자와 메소드 구현

Computer pc = new Computer(0, 16);

Computer apple = new Computer(1, 12);

Computer galaxy = new Computer(2, 16);

pc.print();

apple.print();

galaxy.print();

 



 운영체제 : 윈도7, 메인메모리 :16


 운영체제 : 애플 OS X, 메인메모리 : 32


 운영체제 : 안드로이드, 메인메모리 : 16 

<소스코드>

package Chapter5_10;

public class Computer {
	public String[] osType = {"윈도7", "애플 OS X", "안드로이드"};
	int name;
	int memory;
	Computer(int name, int memory){
		this.name = name;
		this.memory = memory;
	}
	public void print() {
		System.out.println("운영체제 : "+osType[this.name]+", 메인메모리 : "+this.memory);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Computer pc = new Computer(0, 16);
		Computer apple = new Computer(1, 32);
		Computer galaxy = new Computer(2, 16);
		pc.print();
		apple.print();
		galaxy.print();
	}

}

참고자료 : 인피니티북스 절대 JAVA

github 주소 : 

 

GitHub - ch1517/Network_programming: 교재 : 절대 자바(인피니티북스)

교재 : 절대 자바(인피니티북스). Contribute to ch1517/Network_programming development by creating an account on GitHub.

github.com

 

반응형

댓글