study

(230510)알고리즘-다이아몬드만들기 / JAVA - 인터페이스, 추상클래스

hjkeeeem 2024. 7. 25. 20:26

알고리즘

- 다이아몬드 만들기

pivot = 중심이 되는 선으로 n / 2 해준다. 

1. 핵심로직 형태를 만들어준다.

1 3

3 2

5 1

7 0

5 1

3 2

1 3

 

2. if(i<=pivot){

//피라미드

public class DrawDiamond {
    public static void main(String[] args) {
        int h = 7 ;
        int pivot = h/2;
        for (int i = 0; i < 7; i++) {
            System.out.printf("pivot : %d %d\n",pivot, i);
            }
        }
    }
public static void main(String[] args) {
            int h = 7 ;
            int pivot = h/2;
            for (int i = 0; i < h; i++) {
                if(i<=pivot){
                 //피라미드 로직
                 // 초항이 3 등차가 -2 :: -2+pivot-i
                    System.out.printf("pivot : %d %d  0:%d  *:%d \n",pivot, i,-2+h-i-2, 2*i+1);
                  
                }else {
                    // 역피라미드 로직
                    System.out.printf("pivot : %d %d  0:%d  *:%d\n",pivot, i, i-pivot, 2*(h-i)-1);
                  
                   }
                   }
                   }

3. 메소드 분리하기

    public class DrawDiamond {
        public static String getRepeatSymbol(String symbol, int n){
            return symbol.repeat(n);
        }
        public static void main(String[] args) {
            int h = 7 ;
            int pivot = h/2;
            for (int i = 0; i < h; i++) {
                if(i<=pivot){
                 //피라미드 로직   
                    System.out.printf("%s%s\n", getRepeatSymbol("0", -2+h-i-2), getRepeatSymbol("*",2*i+1));

                }else {
                    // 역피라미드 로직                    
                    System.out.printf("%s%s\n", getRepeatSymbol("0",i-pivot), getRepeatSymbol("*",2*(h-i)-1));
                }
                }
            }
        }

4. makeALine

package com.example.javaproject2.week4.day3;
public class DrawDiamond2 {
    public static String getRepeatSymbol(String symbol, int n){
        return symbol.repeat(n);
    }
    public static String makeALine(int h, int i) {
        int pivot = h / 2;
        if (i <= pivot) {
           return String.format("%s%s\n", getRepeatSymbol("0", -2 + h - i - 2), getRepeatSymbol("*", 2 * i + 1));
        } else {
            return String.format("%s%s\n", getRepeatSymbol("0", i - pivot), getRepeatSymbol("*", 2 * (h - i) - 1));
        }
    }
    public static void main(String[] args) {
        int h = 7 ;
        DrawDiamond2 drawDiamond2 = new DrawDiamond2();
        for (int i = 0; i < h; i++) {
            System.out.print(makeALine(h,i));
        }
    }
}

인터페이스

- 껍데기만 있는 ㄱ서

 

- 파일만들기

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class RightTrianglePrinter {
    //System.out --> Console
    //BufferedWriter --> Console, File

    public String makeALine(int h, int i){
        return String.format("%s%s\n", "", "*".repeat(i));
    }

    public void printToFile() throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("./aaa.txt"));
        bw.append("aaa");
        bw.flush();
        bw.close();
    }

    public static void main(String[] args) throws IOException {
        RightTrianglePrinter rtp = new RightTrianglePrinter();
        rtp.printToFile();
    }
}

package com.example.javaproject2.week4.day3;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class RightTrianglePrinter {
    //System.out --> Console
    //BufferedWriter --> Console, File

    public String makeALine(int h, int i){
        return String.format("%s%s\n", "", "*".repeat(i));
    }

    public void printToFile(String[] lines) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("./aaa.txt"));
        for (int i = 0; i < lines.length; i++) {
            bw.append(lines[i]);
            bw.flush();
        }
        bw.close();
    }
    public void printToConsole(String[] lines){
        for (int i = 0; i < lines.length; i++) {
            System.out.print(lines[i]);
        }
    }

    public void printShape(int h){ //모양 출력하기
        String[] lines = new String[h];
        for (int i = 0; i < h; i++) {
            lines[i] = makeALine(h,i+1);
        }
        for (int i = 0; i < lines.length; i++) {
            System.out.print(lines[i]);
        }
        printToConsole(lines);
    }

    public static void main(String[] args) throws IOException {
        RightTrianglePrinter rtp = new RightTrianglePrinter();
        rtp.printShape(5);
    }
}

 

 

 

 

Abstract Class와 Interface차이

abstract class -> class를 만들다가 겹치는 부분이 나와서 중첩되는 부분을 남기고 분리

interface -> 메소드의 형태만 등록 해놓고 구현해서 쓰는 것

 

implements -> 구현하겠다