1. 추상 클래스 와 interface 의 차이는?
- 추상클래스와 인터페이스의 차이
- 추상클래스는 abstract가 아닌 일반 메소드가 들어갈 수 있다.(일반 클래스에 추상 메소드를 추가할 수 있는 클래스)
- 인터페이스는 상수랑 abstract메소드만 가능
2. Object 클래스의 11개의 함수를 나열해 보시오.
public final native Class<?> getClass();
public native int hashCode();
public boolean equals(Object obj) {
return (this == obj);
}
protected native Object clone() throws CloneNotSupportedException;
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
public final native void notify();
public final native void notifyAll();
public final void wait() throws InterruptedException {
wait(0L);
}
public final native void wait(long timeoutMillis) throws InterruptedException;
public final void wait(long timeoutMillis, int nanos) throws InterruptedException {
if (timeoutMillis < 0) {
throw new IllegalArgumentException("timeoutMillis value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos > 0 && timeoutMillis < Long.MAX_VALUE) {
timeoutMillis++;
}
wait(timeoutMillis);
}
@Deprecated(since="9")
protected void finalize() throws Throwable { }
3. 아래의 프로그램을 하시오(equals 함수).
public static void main(String[] args){
Member obj1 = new Member("Black");
Member obj2 = new Member("Black");
Member obj3 = new Member("White");
System.out.println(obj1.equals(obj2));
System.out.println(obj1.equals(obj3));
}
true
false
class Member{
private String color;
public Member(String color) {
this.color = color;
}
@Override
public boolean equals(Object obj) {
Member Mcolor = (Member) obj;
if(this.color.equals(Mcolor.color)) {
return true;
}
return false;
}
}
public class MemberMain {
public static void main(String[] args) {
Member obj1 = new Member("Black");
Member obj2 = new Member("Black");
Member obj3 = new Member("White");
System.out.println(obj1.equals(obj2));
System.out.println(obj1.equals(obj3));
}
}
true
false
4.아래를 프로그래밍 하시오.(toString() 함수)
public static void main(String[] args){
Circle circle = new Circle(10);
System.out.println(circle);
}
원의 넓이는 314.1592..이고 반지름 10인 원
class Circle{
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public double getArea() {
return radius*radius*Math.PI;
}
@Override
public String toString() {
return "원의 넓이는 " + getArea() +"이고 반지름은 " + getRadius() +"인 원";
}
}
public class CircleMain {
public static void main(String[] args) {
Circle circle = new Circle(10);
System.out.println(circle);
}
}
원의 넓이는 314.1592653589793이고 반지름은 10.0인 원
5. 아래를 구현하시오.
abstract class Calc{
protected int a;
protected int b;
void setValue(int a, int b) {
this.a = a; this.b = b;
}
abstract int calculate();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("두 정수와 연산자를 입력하시오>> ");
int a = sc.nextInt();
int b = sc.nextInt();
char c = sc.next().charAt(0);
if(c == '+'){
Add add = new Add();
add.setValue(a, b);
System.out.println(add.calculate());
}
else if(c == '-'){
Sub sub = new Sub();
sub.setValue(a, b);
System.out.println(sub.calculate());
}
else if(c == '*'){
Mul mul = new Mul();
mul.setValue(a, b);
System.out.println(mul.calculate());
}
else if(c == '/'){
Div div = new Div();
div.setValue(a, b);
System.out.println(div.calculate());
}
}
import java.util.Scanner;
abstract class Calc {
protected int a;
protected int b;
void setValue(int a, int b) {
this.a = a;
this.b = b;
}
abstract int calculate();
}
class Add extends Calc {
@Override
int calculate() {
return a + b;
}
}
class Sub extends Calc {
@Override
int calculate() {
return a - b;
}
}
class Mul extends Calc {
@Override
int calculate() {
return a * b;
}
}
class Div extends Calc {
@Override
int calculate() {
if (b == 0) {
return 0;
}
return a / b;
}
}
public class CalculatorMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("두 정수와 연산자를 입력하시오>> ");
int a = sc.nextInt();
int b = sc.nextInt();
char c = sc.next().charAt(0);
if (c == '+') {
Add add = new Add();
add.setValue(a, b);
System.out.println(add.calculate());
} else if (c == '-') {
Sub sub = new Sub();
sub.setValue(a, b);
System.out.println(sub.calculate());
} else if (c == '*') {
Mul mul = new Mul();
mul.setValue(a, b);
System.out.println(mul.calculate());
} else if (c == '/') {
Div div = new Div();
div.setValue(a, b);
System.out.println(div.calculate());
}
}
}