본문 바로가기
솔루션모음/파워자바 프로그래밍

[파워자바] 8장 프로그래밍 솔루션 답지

by 이얏호이야호 2023. 2. 15.

 

1.

class Circle {
	double r;
	double cx;
	double cy;
	
	public double area() {
		return 3.141592*r*r;
	}

	public double getR() {
		return r;
	}

	public void setR(double r) {
		this.r = r;
	}

	public double getCx() {
		return cx;
	}

	public void setCx(double cx) {
		this.cx = cx;
	}

	public double getCy() {
		return cy;
	}

	public void setCy(double cy) {
		this.cy = cy;
	}
	
}
public class CircleTest {	
	public static void main(String[] args){
		Circle c = new Circle();
		c.setR(10.0);
		System.out.println(c.area());
	}
}

 

2.

lass Book {
	private String title, author;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}
}
public class BookTest {
	public static void main(String[] args){
		Book b = new Book();
		b.setTitle("data structure");
		b.setAuthor("홍길동");
	}
}

 

3.

class Dice {
	private int face;
	int roll() {
		int face = (int)(Math.random() * 6) + 1;
		return face;
	}
}
public class DiceTest{
	public static void main(String[] args){
		Dice dice = new Dice();
		System.out.println("주사위 숫자 : " + dice.roll());
	}
}

 

4.

class Point {
	int x, y;
	public void set(int x, int y)
	{
		this.x = x;
		this.y = y;
	}
	public void print()
	{
		System.out.println("("+x+","+y+")");
	}
	
}
public class PointTest {	
	public static void main(String[] args){
		Point p = new Point();
		p.set(10, 10);
		p.print();
	}
}

 

5.

class Employee{
	private String name;
	private int tel;
	private int sal;
	public void setName(String n){
		name = n;
	}
	public void setTel(int t){
		tel = t;
	}
	public void setSal(int s){
		sal = s;
	}
	public String getName(){
		return name;
	}
	public int getTel(){
		return tel;
	}
	public int getSal(){
		return sal;
	}
}
public class EmployeeTest {
	public static void main(String[] args){
		Employee em = new Employee();
	}
}

 

6.

class BankAccount{
	int accountNumber;
	String owner;
	int balance;
	
	void deposit(int amount){
		balance += amount;
	}
	void withdraw(int amount){
		balance -= amount;
	}
	public String toString(){
		return "현재 잔액은 " + balance + "입니다.";
	}
	public int transfer(int amount, BankAccount otherAccount){
		otherAccount.deposit(amount);
		return (balance-amount);
	}
}
public class BankAccountTest {	
	public static void main(String[] args){
		BankAccount myAccount1 = new BankAccount();
		BankAccount myAccount2 = new BankAccount();
		myAccount1.deposit(10000);
		System.out.println("myAccount1 : " +myAccount1);
		myAccount1.withdraw(8000);
		System.out.println("myAccount1 : " + myAccount1);
		System.out.println("myAccount2 : " + myAccount2);
		int b = myAccount1.transfer(1000, myAccount2);
		myAccount1.withdraw(b);
		System.out.println("myAccount1 : " + myAccount1);
		System.out.println("myAccount2 : " + myAccount2);
		
	}
}

 

7.

class Average {
	public int getAverage(int a, int b){
		return (a+b)/2;
	}
	public int getAverage(int a, int b, int c){
		return (a+b+c)/2;
	}
}
public class AverageTest {
	public static void main(String[] args){
		Average a = new Average();
		System.out.println(a.getAverage(10, 20));
		System.out.println(a.getAverage(10, 20, 30));
	}
}

 

댓글