본문 바로가기
학부공부/Java_Basic

Chapter 13. Abstract Classes and Interfaces[Java Basic]

by sonpang 2021. 11. 6.
반응형

13.A - 비교가능한 GeometricObject

Time Limit: 1s Memory Limit: 128MB

 

DESCRIPTION

GeometricObject를 비교할 수 있도록 Comparable 인터페이스를 구현하고, 주어진 두 개의 GeometricObject 객체 중 더 큰 객체를 반환하는 정적 메소드 max 를 정의하시오.

(Enable GeometricObject comparable) Modify the GeometricObject class to implement the Comparable interface, and define a static max method in the GeometricObject class for finding the larger of two GeometricObject objects.

 

INPUT

Line 1 : 문자열의 개수 N

Line 2 ~ 1+N : radius color weight 순으로 출력

 

OUTPUT

Line 1 ~ N : radius 크기 순서대로 정렬

 

SAMPLE CODE

import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        GeometricObject1[] aCircle1 = new Circle1[N];
        for (int i = 0; i < N; i++) {
            aCircle1[i] = new Circle1(sc.nextDouble(), sc.next(), sc.nextDouble());
        }
        Arrays.sort(aCircle1);
        for (GeometricObject1 c1 : aCircle1) {
            System.out.println(c1);
        }
    }
}

YOUR_CODE

 

SAMPLE INPUT

3

5.1 white 2

3 black 1

9 red 10

 

SAMPLE OUTPUT

radius:3.0, color:black, weight:1.0

radius:5.1, color:white, weight:2.0

radius:9.0, color:red, weight:10.0

 

class GeometricObject1{
    GeometricObject1(){ }
}
class Circle1 extends GeometricObject1 implements Comparable<Circle1>{
    private double r, w;
    private String c;
    Circle1(double r, String c, double w){
        this.r = r;
        this.c = c;
        this.w = w;
    }
    public String toString(){
        return "radius:" + String.format("%.1f",r) + ", color:" + c + ", weight:" + String.format("%.1f",w);
    }
    public int compareTo(Circle1 c){
        if(this.r < c.r)
            return -1;
        else if(this.r == c.r)
            return 0;
        else
            return 1;
    }
}

 

Wrong Answer 0
class GeometricObject1{
    GeometricObject1(){ }
}
class Circle1 extends GeometricObject1 implements Comparable<Circle1>{
    private double r, w;
    private String c;
    Circle1(double r, String c, double w){
        this.r = r;
        this.c = c;
        this.w = w;
    }
    public String toString(){
        return "radius:" + r + ", color:" + c + ", weight:" + w;
    }
    public int compareTo(Circle1 c){
        if(this.r < c.r)
            return -1;
        else if(this.r == c.r)
            return 0;
        else
            return 1;
    }
}

 

 

13.B - Colorable 인터페이스

Time Limit: 1s Memory Limit: 128MB

 

DESCRIPTION

howToColor() method를 가지고 이름이 Colorable인 interface를 디자인하시오. Colorable을 구현하고 GeometricObject에 기능을 추가한 Square, Rectangle 클래스를 디자인 하시오. howtocolor method는 howToColor: xxx을 출력 해야 합니다.

(The Colorable interface) Design an interface named Colorable with a void method named howToColor(). Every class of a colorable object must implement the Colorable interface. Design a class named Square that extends GeometricObject and implements Colorable. Implement howToColor to display the message Color all four sides.

 

INPUT

* Line 1 : 도형의 개수 N

* Line 2 ~ N : Square or Rectangle / Square이면 한변의 길이 a, Rectangle 이면 두 변의 길이 a, b

 

OUTPUT

* Line 1 ~ 4N : 각 테스트 케이스마다 샘플 출력과 같이 4줄씩 출력

Line 1: 도형 모양, Line 2 : howToColor: xxx, Line 3 : area: 넓이, Line 4 : perimeter: 네 변의 길이 합

 

SAMPLE CODE

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        GeometricObject[] aObject = new GeometricObject[N];
        for (int i = 0; i < N; i++) {
            String shape = sc.next();
            if (shape.equals("Square")) {
                aObject[i] = new Square(sc.nextDouble());
            } else if (shape.equals("Rectangle")) {
                aObject[i] = new Rectangle(sc.nextDouble(), sc.nextDouble());
            }
        }
        for (int i = 0; i < aObject.length; i++) {
            if (aObject[i] instanceof Colorable)
                ((Colorable) aObject[i]).howToColor();
            System.out.println("area: " + aObject[i].getArea());
            System.out.println("perimeter: " + aObject[i].getPerimeter());
        }
    }
}

YOUR_CODE

 

SAMPLE INPUT

3

Square 3 Square 5 Rectangle 10 2

 

SAMPLE OUTPUT

Square

howToColor: xxx area: 9.0 perimeter: 12.0

Square

howToColor: xxx area: 25.0 perimeter: 20.0

Rectangle

howToColor: xxx area: 20.0 perimeter: 24.0

 

interface Colorable{
    public void howToColor();
}
abstract class GeometricObject implements Colorable{
    GeometricObject(){ }
    double d1, d2;
    String s;
    public double getArea(){
        return Math.floor(d1 * d2 * 10) / 10.0;
    }
    public double getPerimeter(){
        return Math.floor(2 * (d1 + d2) * 10) / 10.0;
    }
    public void howToColor(){
        System.out.println(s + "howToColor: xxx");
    }
}
class Square extends GeometricObject{
    private double d;
    Square(double d){
        d1 = d;
        d2 = d;
        s = "Square\n";
    }
}
class Rectangle extends GeometricObject{
    Rectangle(double d1, double d2){
        this.d1 = d1;
        this.d2 = d2;
        s = "Rectangle\n";
    }
}
반응형

 

13.C - 유리수 클래스1

Time Limit: 1s Memory Limit: 128MB

 

DESCRIPTION

분모와 분자를 BigInteger로 사용하는 Rational 클래스를 구현하시오.

(Use BigInteger for the Rational class) Redesign and implement the Rational class in Listing 13.13 using BigInteger for the numerator and denominator.

 

INPUT

* Line 1 : 테스트케이스의 개수 N

* Line 2 ~ N+1 : 각 케이스 별 숫자 a b c d

 

OUTPUT

* Line 1 ~ 4N : 각 테스트 케이스마다 다음과 같이 4줄씩 출력

- Line 1 : a/b + c/d = 결과, Line 2 : a/b - c/d = 결과, Line 3 : a/b * c/d = 결과, Line 4 : a/b / c/d = 결과

- 모든 숫자는 분수 형태의 유리수

 

SAMPLE CODE

import java.math.*;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int T = sc.nextInt();
        for (int t = 0; t < T; t++) {
            Rational r1 = new Rational(new BigInteger(sc.next()), new BigInteger(sc.next()));
            Rational r2 = new Rational(new BigInteger(sc.next()), new BigInteger(sc.next()));

            System.out.println(r1 + " + " + r2 + " = " + r1.add(r2));
            System.out.println(r1 + " - " + r2 + " = " + r1.subtract(r2));
            System.out.println(r1 + " * " + r2 + " = " + r1.multiply(r2));
            System.out.println(r1 + " / " + r2 + " = " + r1.divide(r2));
        }
    }
}

YOUR_CODE

 

SAMPLE INPUT

3

5 9 1 -6

-3 -9 -8 -2

-7 3 –6 -3

 

SAMPLE OUTPUT

5/9 + -1/6 = 7/18 5/9 - -1/6 = 13/18 5/9 * -1/6 = -5/54 5/9 / -1/6 = -10/3

1/3 + 4 = 13/3 1/3 - 4 = -11/3 1/3 * 4 = 4/3 1/3 / 4 = 1/12

-7/3 + 2 = -1/3 -7/3 - 2 = -13/3 -7/3 * 2 = -14/3 -7/3 / 2 = -7/6

 

class Rational{
    BigInteger na, nb, aa, ab, gcd;
    Rational(BigInteger a, BigInteger b){
        na = a;
        nb = b;
        if((gcd = na.gcd(nb)) != BigInteger.ONE){
            na = na.divide(gcd);
            nb = nb.divide(gcd);
        }
        if(nb.compareTo(BigInteger.ZERO) < 0){
            na = na.multiply(BigInteger.ZERO.subtract(BigInteger.ONE));
            nb = nb.multiply(BigInteger.ZERO.subtract(BigInteger.ONE));
        }
    }
    public String toString(){
        if(nb.equals(BigInteger.ONE))
            return na + "";
        return na + "/" + nb;
    }
    public String add(Rational m){
        aa = (na.multiply(m.nb)).add(m.na.multiply(nb));
        ab = nb.multiply(m.nb);
        check();
        if(ab.equals(BigInteger.ONE))
            return aa + "";
        return aa + "/" + ab;
    }
    public String subtract(Rational m){
        aa = (na.multiply(m.nb)).subtract(m.na.multiply(nb));
        ab = nb.multiply(m.nb);
        check();
        if(ab.equals(BigInteger.ONE))
            return aa + "";
        return aa + "/" + ab;
    }
    public String multiply(Rational m){
        aa = na.multiply(m.na);
        ab = nb.multiply(m.nb);
        check();
        if(ab.equals(BigInteger.ONE))
            return aa + "";
        return aa + "/" + ab;
    }
    public String divide(Rational m){
        aa = na.multiply(m.nb);
        ab = nb.multiply(m.na);
        check();
        if(ab.equals(BigInteger.ONE))
            return aa + "";
        return aa + "/" + ab;
    }
    public void check(){
        if((gcd = aa.gcd(ab)) != BigInteger.ONE){
            aa = aa.divide(gcd);
            ab = ab.divide(gcd);
        }
        if(ab.compareTo(BigInteger.ZERO) < 0) {
            aa = aa.multiply(BigInteger.ZERO.subtract(BigInteger.ONE));
            ab = ab.multiply(BigInteger.ZERO.subtract(BigInteger.ONE));
        }
    }
}

 

 

13.D - 유리수 클래스2

Time Limit: 1s Memory Limit: 128MB

 

DESCRIPTION

사용자가 실수를 입력하면 분수로 바꿔주는 프로그램을 작성하시오.

힌트 : 실수를 string으로 읽을 때, 정수부분과 소수부분으로 나누고 Rational 클래스의 BigInteger를 사용해서 소수를 유리수 형식(a/b)으로 쓰세요.

(Convert decimals to fractions) Write a program that prompts the user to enter a decimal number and displays the number in a fraction. Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the BigInteger implementation of the Rational class in Programming Exercise 13.15 to obtain a rational number for the decimal number.

 

INPUT

* Line 1 : 테스트케이스의 개수 N

* Line 2 ~ N+1 : 각 케이스 별 소수점 숫자 a b

 

OUTPUT

* Line 1 ~ 4N : 각 테스트 케이스마다 다음과 같이 4줄씩 출력

- Line 1 : a + b = 결과

- Line 2 : a - b = 결과

- Line 3 : a * b = 결과

- Line 4 : a / b = 결과

- 모든 숫자는 분수형태의 유리수

 

SAMPLE CODE

import java.math.*;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for (int t = 0; t < T; t++) {
            Rational r1 = Rational.getFraction(sc.next());
            Rational r2 = Rational.getFraction(sc.next());
            System.out.println(r1 + " + " + r2 + " = " + r1.add(r2));
            System.out.println(r1 + " - " + r2 + " = " + r1.subtract(r2));
            System.out.println(r1 + " * " + r2 + " = " + r1.multiply(r2));
            System.out.println(r1 + " / " + r2 + " = " + r1.divide(r2));
        }
    }
}

YOUR_CODE

 

SAMPLE INPUT

2

3.25 -3

-1.0 -9

 

SAMPLE OUTPUT

13/4 + -3 = 1/4

13/4 - -3 = 25/4

13/4 * -3 = -39/4

13/4 / -3 = -13/12

-1 + -9 = -10

-1 - -9 = 8

-1 * -9 = 9

-1 / -9 = 1/9

 

class Rational{
    BigInteger na, nb, aa, ab, gcd;
    public static Rational getFraction(String s){
        String a[] = s.split("-|\\.");
        if(s.contains("-") && s.contains("."))
            return new Rational((new BigInteger(a[1] + a[2])).multiply(BigInteger.ZERO.subtract(BigInteger.ONE)), new BigInteger(String.valueOf((int)Math.pow(10, a[2].length()))));
        else if(s.contains("-") && !s.contains("."))
            return new Rational((new BigInteger(a[1])).multiply(BigInteger.ZERO.subtract(BigInteger.ONE)), BigInteger.ONE);
        else if(!s.contains("-") && s.contains("."))
            return new Rational(new BigInteger(a[0] + a[1]), new BigInteger(String.valueOf((int)Math.pow(10, a[1].length()))));
        else
            return new Rational(new BigInteger(a[0]), BigInteger.ONE);
    }
    Rational(BigInteger a, BigInteger b){
        na = a;
        nb = b;
        if((gcd = na.gcd(nb)) != BigInteger.ONE){
            na = na.divide(gcd);
            nb = nb.divide(gcd);
        }
        if(nb.compareTo(BigInteger.ZERO) < 0){
            na = na.multiply(BigInteger.ZERO.subtract(BigInteger.ONE));
            nb = nb.multiply(BigInteger.ZERO.subtract(BigInteger.ONE));
        }
    }
    public String toString(){
        if(nb.equals(BigInteger.ONE))
            return na + "";
        return na + "/" + nb;
    }
    public String add(Rational m){
        aa = (na.multiply(m.nb)).add(m.na.multiply(nb));
        ab = nb.multiply(m.nb);
        check();
        if(ab.equals(BigInteger.ONE))
            return aa + "";
        return aa + "/" + ab;
    }
    public String subtract(Rational m){
        aa = (na.multiply(m.nb)).subtract(m.na.multiply(nb));
        ab = nb.multiply(m.nb);
        check();
        if(ab.equals(BigInteger.ONE))
            return aa + "";
        return aa + "/" + ab;
    }
    public String multiply(Rational m){
        aa = na.multiply(m.na);
        ab = nb.multiply(m.nb);
        check();
        if(ab.equals(BigInteger.ONE))
            return aa + "";
        return aa + "/" + ab;
    }
    public String divide(Rational m){
        aa = na.multiply(m.nb);
        ab = nb.multiply(m.na);
        check();
        if(ab.equals(BigInteger.ONE))
            return aa + "";
        return aa + "/" + ab;
    }
    public void check(){
        if((gcd = aa.gcd(ab)) != BigInteger.ONE){
            aa = aa.divide(gcd);
            ab = ab.divide(gcd);
        }
        if(ab.compareTo(BigInteger.ZERO) < 0) {
            aa = aa.multiply(BigInteger.ZERO.subtract(BigInteger.ONE));
            ab = ab.multiply(BigInteger.ZERO.subtract(BigInteger.ONE));
        }
    }
}
반응형

댓글