1. 다음은 2차원 상의 한 점을 표현하는 Point 클래스이다. 아래를 참고하여 작성하시오.
· 참고
· 작성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class ColorPoint extends Point {
String str;
public ColorPoint(int x, int y, String str) {
super(x, y);
this.str = str;
}
public void setXY(int x, int y) {
super.move(x, y);
}
public void setColor(String str) {
this.str = str;
}
public String toString(String str, int x, int y) {
return str + "색의 (" + super.getX() + "," + super.getY() + ") 의 점";
}
}
|
cs |
2. Point를 상속받아 색을 가진 점을 나타내는 ColorPoint 클래스를 작성하라.
· 참고
· 작성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
class ColorPoint extends Point{
String str;
public ColorPoint(){
this(0, 0, "BLACK");
}
public ColorPoint(int x, int y){
this(x, y, "BLACK");
}
public ColorPoint(int x, int y, String str){
super(x, y);
this.str = str;
}
public void setXY(int x, int y){
super.move(x, y);
}
public void setColor(String str){
this.str = str;
}
public String toString(String str, int x, int y){
return str + "색의 (" + super.getX() + "," + super.getY() + ") 의 점";
}
}
|
cs |
3. Point를 상속받아 3차원의 점을 나타내는 Point3D 클래스를 작성하라.
· 참고
· 작성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class Point3D extends Point{
private int z;
public Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
public String toString() {
return "(" + super.getX() + "," + super.getY() + "," + z + ") 의" ;
}
public int moveUp() {
return z = z + 1;
}
public int moveDown() {
return z = z - 1;
}
public void move(int x, int y) {
super.move(x, y);
}
public void move(int x, int y, int z) {
super.move(x, y);
this.z = z;
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class Point3Dmain {
public static void main(String[] args) {
Point3D p = new Point3D(1,2,3);
System.out.println(p.toString() + " 점입니다.");
p.moveUp();
System.out.println(p.toString() + " 점입니다.");
p.moveDown();
p.move(10, 10);
System.out.println(p.toString() + " 점입니다.");
p.move(100, 200, 300); // x, y, z축으로 이동
System.out.println(p.toString() + " 점입니다.");
}
}
|
cs |
4. 다음을 만족하는 클래스 Employee를 작성하시오
· 참고
· 작성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class Employee{
private String name;
private int age;
private String address;
private String team;
private int pay;
public Employee1(String name, int age, String address, String team){
this.name = name;
this.age = age;
this.address = address;
this.team = team;
}
public int getPay(){
return pay;
}
public void setPay(int pay){
this.pay = pay;
}
public void printInfo() {
System.out.println("이름:" + name + "\n나이:" + age + "\n주소:" + address + "\n부서:" + team);
}
}
|
cs |
1
2
3
4
5
6
|
class Employeemain{
public static void main(String[] args){
Employee1 employee = new Employee1("수정", 20, "서울", "인사");
employee.printInfo();
}
}
|
cs |
5. 다음을 만족하는 클래스 Regular를 작성하시오.
· 참고
· 작성
1
2
3
4
5
6
7
8
9
10
11
12
|
class Regular extends Employee1{
private int setter;
public Regular(String name, int age, String address, String team, int setter) {
super(name, age, address, team);
this.setter = setter;
}
public void printInfo(){
super.printInfo();
System.out.println("정규직" + setter);
}
}
|
cs |
1
2
3
4
5
6
|
class Employeemain{
public static void main(String[] args){
Regular regular = new Regular("수정", 20, "서울", "인사", 500);
regular.printInfo();
}
}
|
cs |
6. 아래를 참고하여 Buyer 클래스를 작성하여라.
· 참고
· 작성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
class Buyer {
private int money = 10000;
private int i = 0;
Product[] cart = new Product[3];
void buy(Product p) {
if (money < p.price) {
System.out.println("잔액이 부족하여 " + p + "을/를 살 수 없습니다.");
return;
}
// 가진 돈 충분하면
money -= p.price;
//장바구니에 물건을 담는다.
add(p);
}
void add(Product p) {
if (i >= cart.length) {
//기존의 장바구니에서 2배의 장바구니 만들기
Product[] temp = new Product[cart.length * 2];
//기존의 있는 내용을 복사.
System.arraycopy(cart, 0, temp, 0, cart.length);
cart = temp;
}
cart[i++] = p;
}
void summary() {
String itemList = "";
int sum = 0;
for (int i = 0; i < cart.length; i++) {
if (cart[i] == null)
break;
itemList += cart[i] + ",";
sum = sum + cart[i].price;
}
System.out.println("구입한 물건" + itemList);
System.out.println("사용한 금액" + sum);
System.out.println("남은금액" + money);
}
}
|
cs |
1
2
3
4
5
6
7
|
class Product {
int price;
Product(int price) {
this.price = price;
}
}
|
cs |
1
2
3
4
5
6
7
8
9
|
class Tv extends Product {
Tv() {
super(100);
}
@Override
public String toString() {
return "Tv";
}
}
|
cs |
1
2
3
4
5
6
7
8
9
|
class Computer extends Product {
Computer() {
super(200);
}
@Override
public String toString() {
return "Computer";
}
}
|
cs |
1
2
3
4
5
6
7
8
9
|
class Audio extends Product {
Audio() {
super(300);
}
@Override
public String toString() {
return "Audio";
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class Buyermain {
public static void main(String[] args) {
Buyer b = new Buyer();
b.buy(new Tv());
b.buy(new Computer());
b.buy(new Tv());
b.buy(new Audio());
b.buy(new Computer());
b.buy(new Computer());
b.buy(new Computer());
b.summary();
}
}
|
cs |
7. 다음 조건을 만족하도록 클래스 Person과 Student를 작성하시오.
· 참고
· 작성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class Person {
private String name;
private int age;
private String address;
Person(String name, int age, String address){
this.name = name;
this.age = age;
this.address = address;
}
public void showInfo() {
System.out.println("이름: " + name);
System.out.println("나이: " + age);
System.out.println("주소: " + address);
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
class Student extends Person{
private String school;
private String major;
private int sNum;
private double[] grade = new double[8];
Student(String name, int age, String address, String school, String major, int sNum) {
super(name, age, address);
this.school = school;
this.major = major;
this.sNum = sNum;
}
public void showInfo() {
super.showInfo();
System.out.println("학교: " + school);
System.out.println("학과: " + major);
System.out.println("학번: " + sNum);
System.out.println("----------------");
}
public void average() {
double avg;
double sum = 0;
Scanner sc = new Scanner(System.in);
System.out.println("8학기 학점을 순서대로 입력하세요.");
for(int i = 0; i < grade.length; i++) {
System.out.print((i + 1) + "학기 학점 → ");
grade[i] = sc.nextDouble();
sum = sum + grade[i];
}
avg = sum / grade.length;
System.out.println("8학기 총 평균 평점은 " + avg + "점 입니다.");
}
}
|
cs |
1
2
3
4
5
6
7
|
class Studentmain {
public static void main(String[] args) {
Student stu = new Student("김다정", 20, "서울시", "동양서울대학교", "전산정보학과", 20132222);
stu.showInfo();
stu.average();
}
}
|
cs |
'bitcamp > JAVA' 카테고리의 다른 글
interface와 추상클래스_연습문제 (0) | 2021.01.20 |
---|---|
interface와 추상클래스 (0) | 2021.01.18 |
클래스의 상속과 오버라이딩 (0) | 2021.01.17 |
배열_연습문제 (0) | 2021.01.17 |
배열 (0) | 2021.01.17 |