5. 과일, 사과, 배, 포도를 표현한 클래스를 만들고 이들 간의 관계를 고려하여 하나의 클래스를 추상 클래스로 만들어 메소드 print()를 구현하고 다음과 같은 소스와 결과가 나오도록 클래스를 작성하시오.
- 소스
Fruit fAry[] = {new Grape(), new Apple(), new Pear());
for(Fruit f : fAry)
f.print();
- 결과
나는 포도이다.
나는 사과이다.
나는 배이다.
<소스코드>
Fruit.java
package Chapter6_5;
public abstract class Fruit { // 추상 클래스
public abstract void print(); // 추상 메소드
}
Grape.java
package Chapter6_5;
public class Grape extends Fruit{
@Override
public void print() {
System.out.println("나는 포도이다.");
}
}
Apple.java
package Chapter6_5;
public class Apple extends Fruit{
@Override
public void print() {
System.out.println("나는 사과이다.");
}
}
Pear.java
package Chapter6_5;
public class Pear extends Fruit{
@Override
public void print() {
System.out.println("나는 배이다.");
}
}
Chapter6_5.java
package Chapter6_5;
public class Chapter6_5 {
public static void main(String[] args) {
Fruit fAry[] = {new Grape(), new Apple(), new Pear()};
for(Fruit f:fAry)
f.print();
}
}
6. 다음 조건을 만족하도록 클래스 Car를 작성하시오.
- 필드는 정수형으로 maxSpeed(최대속도)와 speed(현재속도)로 선언
- 생성자 : 최대속도를 지정하는 생성자 구현
- 메소드 : speedUp(), speedUp(int)과 speedDown(), speedDown(int) 으로 메소드 오버로딩 구현
- 다음은 클래스 Car 객체로 SpeedUp()과 speedDown()을 점검하는 소스
Car mycar = new Car(300);
mycar.speedUp();
mycar.speedUp();
mycar.speedUp(-50);
mycar.speedUp(50);
mycar.speedDown(-30);
mycar.speedDown(30);
mycar.speedDown(30);
mycar.speedDown(30);
mycar.speedUp(100);
mycar.speedUp(300);
- 다음은 위 소스의 실행결과 : 각 줄이 하나의 메소드 호출의 결과가 되도록 한다.
speedUp() 호출 : 최대속도 : 300, 현재속도 : 5
speedUp() 호출 : 최대속도 : 300, 현재속도 : 10
speedUp(-50) 호출 : 오류 : 속도가 음수이므로 0으로 지정 : 최대속도 : 300, 현재속도 10
speedUp(50) 호출 : 최대속도: 300, 현재속도 : 60
speedDown(-30) 호출 : 오류 : 속도가 음수이므로 0으로 지정 : 최대속도 : 300, 현재속도 : 60
speedDown(30) 호출 : 최대속도 : 300, 현재속도 : 30
speedDown(3) 호출 : 최대속도 300, 현재속도 : 0
speedDown(30) 호출 : 속도가 0ㅗ다 작아져 0으로 지정, 최대속도 : 300, 현재속도 : 0
speedUp(100) 호출 : 최대속도 : 300, 현재속도 : 100
speedUp(300) 호출 : 최대속도보다 높아 최대속도로 지정, 최대속도 : 300, 현재속도 : 300
<소스코드>
package Chapter6_6;
public class Car {
private int maxSpeed; // 최대속도
private int speed; // 현재속도
// 생성자
Car(int maxSpeed){
this.maxSpeed = maxSpeed;
this.speed=0;
}
public void print(int s) {
if(speed>maxSpeed) {
System.out.print("최대속도보다 높아 최대속도로 지정, ");
speed=maxSpeed;
}
if(speed<0) {
System.out.print("속도가 0보다 작아져 0으로 지정, ");
speed+=s; // 원래속도로 되돌리기
}
System.out.println("최대속도: "+maxSpeed+", 현재속도 : "+speed);
}
public void speedUp() {
speed+=5;
System.out.print("speedUp() 호출 : ");
print(5);
}
public void speedUp(int s) {
System.out.print("speedUp("+s+") 호출 : ");
if(s<0) { // 인자가 음수일 때
System.out.print("오류 : 속도가 음수이므로 0으로 지정 : ");
s=0;
}
speed+=s;
print(s);
}
public void speedDown() {
speed-=5;
System.out.print("speedDown() 호출 : ");
print(5);
}
public void speedDown(int s) {
System.out.print("speedDown("+s+") 호출 : ");
if(s<0) { // 인자가 음수일 때
System.out.print("오류 : 속도가 음수이므로 0으로 지정 : ");
s=0;
}
speed-=s;
print(s);
}
public static void main(String[] args) {
Car mycar = new Car(300);
mycar.speedUp();
mycar.speedUp();
mycar.speedUp(-50);
mycar.speedUp(50);
mycar.speedDown(-30);
mycar.speedDown(30);
mycar.speedDown(30);
mycar.speedDown(30);
mycar.speedUp(100);
mycar.speedUp(300);
}
}
7. 다음 조건을 만족하도록 클래스 Person과 Student를 작성하시오.
- 클래스 Person
* 필드 : 이름, 나이, 주소 선언
- 클래스 Student
* 필드 : 학교명, 학과, 학번, 8개 평균평점을 저장할 배열로 선언
* 생성자 : 학교명, 학과, 학번 지정
* 메소드 average() : 8개 학기 평균평점의 평균을 반환
- 클레스 Person과 Student 프로그램 테스트 프로그램의 결과 : 8개 학기의 평균평점은 표준입력으로 받도록한다.
이름 : 김다정나이 : 20
주소 : 서울시 관악구
학교 : 동양서울대학교
학과 : 전산정보학과
학번 : 20132222
----------------------------------------
8학기 학점을 순서대로 입력하세요
1학기 학점 → 3.37
2학기 학점 → 3.89
3학기 학점 → 4.35
4학기 학점 → 3.76
5학기 학점 → 3.89
6학기 학점 → 4.26
7학기 학점 → 4.89
8학기 학점 → 3.89----------------------------------------
8학기 총 평균 평점은 4.0375점입니다.
<소스코드>
Person.java
package Chapter6_7;
public class Person {
String name;
int age;
String address;
// 생성자
Person(String name, int age, String address){
this.name = name;
this.age = age;
this.address = address;
}
}
Student.java
package Chapter6_7;
import java.util.Scanner;
public class Student extends Person{
String school; // 학교
String department; // 학과
int classNum; // 학번
double score[]; // 점수 배열
// 생성자
Student(String name, int age, String address,
String school, String department, int classNum) {
super(name, age, address);
this.school = school;
this.department = department;
this.classNum = classNum;
score = new double[8];
}
// 평균 구하기
double average() {
double sum=0;
for(double i : score) {
sum+=i;
}
return sum/score.length;
}
public void printInfo() {
Scanner s = new Scanner(System.in);
System.out.println("이름 : "+name);
System.out.println("나이 : "+age);
System.out.println("주소 : "+address);
System.out.println("학교 : "+school);
System.out.println("학과 : "+department);
System.out.println("학번 : "+classNum);
System.out.println("--------------------------");
System.out.println("8학기 학점을 순서대로 입력하세요");
for(int i=0;i<score.length;i++) {
System.out.print(i+1+"학기 학점 → ");
score[i]=s.nextDouble();
}
System.out.println("--------------------------");
System.out.println("8학기 총 평균 평점은 "+average()+"점입니다");
}
}
Chapter6_7.java
package Chapter6_7;
public class Chapter6_7 {
public static void main(String[] args) {
Student student = new Student("김다정", 20, "서울시 관악구", "동양서울대학교", "전산정보학과", 20132222);
student.printInfo();
}
}
8. 수학의 복소수를 추상화한 클래스 Complex를 작성하시오.
- 필드 : 복소수의 실수부와 허수부를 위한 변수 선언
- 생성자 : 실수부와 허수부를 인자로 하는 생성자
- 정적 메소드 add(Complex c1, Complex c2) : 복소수 c1과 c2의 더한 결과인 복소수를 반환
* 복소수 a + bi와 c + di의 합 : (a+c) + (c+d)i
- 정적 메소드 sub(Complex c1, Complex c2) : 복소수 c1과 c2의 뺀 결과인 복소수를 반환
* 복소수 a +bi와 c + di의 빼기 : (a-c)+(c-d)i
- 메소드 abs() : 복소수의 절대 값 반환
* 복소수 a+bi의 절대 값 연산식 : Math.sqrt(a^2+b^2)
- 메소드 print() : a + bi 형태로 출력
- 복소수 x = 3.4 + 4.5i, y =5.2 + -2.4i를 생성하여 더한 결과, 뺀 결과, x와 y의 절대 값을 출력하는 프로그램도 작성
<소스코드>
Complex.java
package Chapter6_8;
public class Complex {
double realNum;
double imaginaryNum;
// 생성자
Complex(double realNum, double imaginaryNum){
this.realNum = realNum;
this.imaginaryNum = imaginaryNum;
}
// 덧셈
public static Complex add(Complex c1, Complex c2) {
return new Complex(c1.realNum+c2.realNum, c1.imaginaryNum+c2.imaginaryNum);
}
// 뺄셈
public static Complex sub(Complex c1, Complex c2) {
return new Complex(c1.realNum-c2.realNum, c1.imaginaryNum-c2.imaginaryNum);
}
// 절대값
public double abs() {
return Math.abs(realNum*realNum+imaginaryNum*imaginaryNum);
}
void print() {
System.out.println(realNum+" + "+imaginaryNum+"i");
}
}
Chapter6_8.java
package Chapter6_8;
public class Chapter6_8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Complex c1 = new Complex(3.4,4.5);
Complex c2 = new Complex(5.2,-2.4);
System.out.print("더한 결과 : ");
Complex.add(c1,c2).print();
System.out.print("뺀 결과 : ");
Complex.sub(c1,c2).print();
System.out.println("x의 절대값 : "+c1.abs());
System.out.println("y의 절대값 : "+c2.abs());
}
}
참고자료 : 인피니티북스 절대 JAVA
github 주소 :
https://github.com/ch1517/Network-programming/tree/master/Chapter6
'Programming Study > 절대JAVA' 카테고리의 다른 글
[절대JAVA]7장 프로그래밍 연습문제(2) (0) | 2018.09.25 |
---|---|
[절대JAVA]7장 프로그래밍 연습문제(1) (0) | 2018.09.24 |
[절대JAVA]6장 프로그래밍 연습문제(1) (0) | 2018.09.23 |
[절대JAVA]5장 프로그래밍 연습문제(2) (0) | 2018.09.23 |
[절대JAVA]5장 프로그래밍 연습문제(1) (3) | 2018.09.22 |
댓글