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

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

by 푸고배 2018. 9. 22.

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

- String 형의 학과와 정수형의 학번을 필드로 선언

- Student 클래스의 main() 메소드에서 Student 객체를 생성하여 학과와 학번 필드에 적당한 값을 입력 후 출력

<소스코드>

package Chapter5_1;

public class Student {
	String department;
	int class_num;
	Student(String d, int c){
		department=d;
		class_num=c;
	}
	public void print() {
		System.out.println("학과 : "+department);
		System.out.println("학번 : "+class_num);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student s = new Student("정보통신공학과",20150000);
		s.print();
	}

}
​

 

2. 위에서 구현한 Student 클래스를 다음을 만족하도록 기능을 추가하여 작성하시오.

- 필드를 모두 private로 하고, getter와 setter를 구현하고

- Student 클래스의 main() 메소드에서 Student 객체를 생성하여 setter를 사용하여 학과와 학번 필드에 적당한 값을 입력 후 출력

<소스코드>

package Chapter5_2;

public class Student {
	private String department;
	private int class_num;
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public int getClass_num() {
		return class_num;
	}
	public void setClass_num(int class_num) {
		this.class_num = class_num;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student s = new Student();
		s.setDepartment("정보통신공학과");
		s.setClass_num(20150000);
		System.out.println("학과 : "+s.getDepartment());
		System.out.println("학번 : "+s.getClass_num());
	}

}

 

3. 다음에 구현된 Circle 클래스를 참고로 다음을 만족하는 Cylinder 클래스를 작성하시오.

- 원통을 나타내는 Cylinder 클래스는 Circle 형의 원과 실수형의 높이를 필드로 선언

- 메소드 getVolume()은 원통의 부피를 반환

- Cylinder 클래스의 main() 메소드에서 반지름이 2.8, 높이가 5.6의 원통의 부피를 출력

- 다음은 원을 나타내는 클래스 Circle

public class Circle{public double radius;public static double PI = 3.141592;

// 생성자 구현

public Circle(double radius) {

this.radius = radius;

}

// 현재 반지름을 사용하여 원의 면적을 구하는 메소드

public double getArea(){

return radius * radius * PI;

}

 

<소스코드>

Circle.java

package Chapter5_3;

public class Circle {
	public double radius; // 원의 반지름
	public static double PI = 3.141592; // 원주율
	//생성자 구현
	public Circle(double radius) {
		this.radius=radius;
	}
	//현재 반지름을 사용하여 원의 면적을 구하는 메소드
	public double getArea() {
		return radius *radius*PI;
	}
}

 

Cylinder.java

 
package Chapter5_3;

public class Cylinder {
	Circle c;
	double hight; // 높이
	Cylinder(double radius,double hight){
		c = new Circle(radius);
		this.hight=hight;
	}
	public double getVolume() {
		return c.getArea()*hight;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Cylinder cylinder = new Cylinder(2.8,5.6);
		System.out.println("부피 : "+cylinder.getVolume());
	}

}

 

4. 위에서 구현한 Cylinder를 다음 조건에 맞도록 기능을 추가하여 작성하시오.

- 다음과 같은 객체 생성이 가능하도록 생성자를 구현

Cylinder cd = new Cylinder(new Circle(2.8), 5.6);

<소스코드>

Circle.java

package Chapter5_4;

public class Circle {
	public double radius; // 원의 반지름
	public static double PI = 3.141592; // 원주율
	//생성자 구현
	public Circle(double radius) {
		this.radius=radius;
	}
	//현재 반지름을 사용하여 원의 면적을 구하는 메소드
	public double getArea() {
		return radius *radius*PI;
	}
}
​

 

 Cylinder.java

package Chapter5_4;

public class Cylinder {
	Circle c;
	double hight; // 높이
	Cylinder(Circle c,double hight){
		this.c = c;
		this.hight=hight;
	}
	public double getVolume() {
		return c.getArea()*hight;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Cylinder cylinder = new Cylinder(new Circle(2.8),5.6);
		System.out.println("부피 : "+cylinder.getVolume());
	}

}

 

5. 다음을 만족하는 클래스 SalaryMan을 작성하시오.

- 필드 salary는 월 급여액을 저장하며, int형으로 초기 값으로 1000000 저장

- 메소드 getAnnualGross()는 연봉을 반환하는 메소드로 월급에 보너스 500%로 계싼

- 기본 생성자에서 필드 salary의 초기 값을 사용하며, 정수형 인자인 생성자에서 인자가 월 급여액으로 지정

- 다음과 같이 객체를 생성하여 메소드 getAnnualGrass()를 호출하여 출력

System.out.println()new SalaryMan().getAnnualGross());

System.out.println()new SalaryMan(2_000_000).getAnnualGross());

<소스코드>

package Chpater5_5;

public class SalaryMan {
	int salary;
	SalaryMan(){ 
		salary = 1000000;
	}
	SalaryMan(int salary){ // 생성자 오버로딩
		this.salary=salary;
	}
	public int getAnnualGross() { // 연봉반환 메소드
		return salary*12+salary*5;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(new SalaryMan().getAnnualGross());
		System.out.println(new SalaryMan(2_000_000).getAnnualGross());
	}

}

 

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

github 주소 : 

https://github.com/ch1517/Network-programming/tree/master/Chapter5

반응형

댓글