Backend/Spring & JPA

[Spring] 220724 4차 과제 : 문제 은행[2],[4] 풀기

JOKUN 2022. 8. 1. 22:04

 

문제 은행 [2] 

 📑문제

https://cafe.naver.com/eddicorp/298

 

문제 은행 [ 2 ]

1. 65 ~ 122 사이의 랜덤한 문자를 생성하도록 한다. 여기서 소문자나 대문자가 아니라면 다시 생성하도록 프로그램을 만들어보자 2. 1, 1, 2, 3, 5, 8...

cafe.naver.com

문제 1번의 경우 아래 링크에 나온 아스키코드 참고

https://blog.naver.com/PostView.nhn?blogId=jysaa5&logNo=221831226674

 

[Java] ASCII (아스키코드)/ 문자 ↔ 숫자

ASCII: American Standard Code for Information Interchange, 미국 정보 교환 표준 부호) 예제)...

blog.naver.com

 

 

🔍풀이

com.example.demo.controller.basic.fourth

FourthRestController

package com.example.demo.controller.basic.fourth;

import com.example.demo.entity.basic.fourth.NumberLoop;
import com.example.demo.entity.basic.fourth.Series;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@RestController
@RequestMapping("/fourth")
public class FourthRestController {

    private final int FIBONACCI = 1;
    private final int CUSTOM = 2;
    private final int EXAMPLE2_DATA = 20;
    private final int EXAMPLE3_DATA = 25;
    private final int PRINT = 3;
    private final int ADD = 7;
    private final int RANDOM = 8;
    private final int REFRESH_RANDOM_SUM = 16;
    private final int REFRESH_RANDOM_MOVEMENT = 32;
    private final int EVEN = 2;
    private final int THREE = 3;
    private final int FOUR = 4;
    private final int NOT_IMPORTANT = 7777;

    @GetMapping("/find-fibonacci-series")
    public String findFibonacciSeries() {
        log.info("피보나치 수열을 검색합니다.");
        Series series = new Series(FIBONACCI, EXAMPLE2_DATA);
        return "피보나치 수열의 " + EXAMPLE2_DATA + "번째 항은 "
                + series.findSeriesNum();
    }

    // 내가 여기서 뭘 하려고 하는가 ?
    //
    @GetMapping("/find-custom-series")
    public String findCustomSeries() {
        log.info("커스텀 수열을 검색합니다.");
        Series series = new Series(CUSTOM, EXAMPLE3_DATA);
        return "커스텀 수열의 " + EXAMPLE3_DATA + "번째 항은 "
                + series.findSeriesNum();
    }

    @GetMapping("/find-even")
    public String findEven() {
        log.info("지정된 숫자에서 짝수를 찾습니다.");

        NumberLoop numberLoop = new NumberLoop(1, 100);

        return numberLoop.operateWithCondition(EVEN);
    }

    @GetMapping("/find-three-times")
    public String findThreeTimes() {
        log.info("지정된 숫자에서 3의 배수를 찾습니다.");

        NumberLoop numberLoop = new NumberLoop(1, 100);

        return numberLoop.operateWithCondition(THREE);
    }

    @GetMapping("/find-four-times-sum")
    public String findFourTimes() {
        log.info("findFourTimes()");

        NumberLoop numberLoop = new NumberLoop(1, 100, ADD);

        return numberLoop.operateWithCondition(FOUR);
    }

    @GetMapping("/find-specific-random-times")
    public String findSpecificRandomTimes() {
        log.info("findSpecificRandomTimes()");

        NumberLoop numberLoop = new NumberLoop(1, 100, RANDOM);

        return numberLoop.operateWithCondition(NOT_IMPORTANT);
    }

    @GetMapping("/find-refresh-random-sum")
    public String findRefreshRandomSum() {
        log.info("findRefreshRandomSum()");

        NumberLoop numberLoop = new NumberLoop(1, 100, REFRESH_RANDOM_SUM);

        return numberLoop.operateWithCondition(NOT_IMPORTANT);
    }

    @GetMapping("/find-refresh-random-movement")
    public String findRefreshRandomMovement() {
        log.info("findRefreshRandomMovement()");

        NumberLoop numberLoop = new NumberLoop(1, 100, REFRESH_RANDOM_MOVEMENT);

        return numberLoop.operateWithCondition(NOT_IMPORTANT);
    }
}

 

 

 

om.example.demo.entity.basic.fourth

NumberLoop

package com.example.demo.entity.basic.fourth;


import com.example.demo.utillity.basic.third.CustomRandom;
import lombok.AllArgsConstructor;

@AllArgsConstructor
public class NumberLoop {

    private int start;
    private int end;
    private int operationMode;

    private final int PRINT = 3;
    private final int ADD = 7;
    private final int RANDOM = 8;
    private final int REFRESH_RANDOM_SUM = 16;
    private final int REFRESH_RANDOM_MOVEMENT = 32;

    public NumberLoop(int start, int end) {
        this.start = start;
        this.end = end;
        this.operationMode = PRINT;
    }

    public String printSimpleFindMatching(int condition) {
        String msg = "";

        for (int i = start; i <= end; i++) {
            if (i % condition == 0) {
                msg += i + ", ";
            }
        }

        return msg;
    }

    public String printSumFindMatching(int condition) {
        String msg = "";
        int sum = 0;

        for (int i = start; i <= end; i++) {
            if (i % condition == 0) {
                sum += i;
            }
        }

        return "최종 합산 결과는 " + sum;
    }

    public String printRandomFindMatching(int condition) {
        final int MIN = 2;
        final int MAX = 10;

        String msg = "";
        int randNum = CustomRandom.makeIntCustomRandom(MIN, MAX);

        for (int i = start; i <= end; i++) {
            if (i % randNum == 0) {
                msg += i + ", ";
            }
        }

        return msg;
    }

    public String printSumRefreshRandomFindMatching(int condition) {
        final int MIN = 2;
        final int MAX = 10;

        String msg = "";
        int randNum;
        int sum = 0;

        // 7의 배수가 나왔음
        // 그 다음 루프에서 9의 배수
        // 그 다음 루프에서 2의 배수
        // 그 다음 루프에서 4의 배수
        randNum = CustomRandom.makeIntCustomRandom(MIN, MAX);

        for (int i = start; i <= end; i++) {
            if (i % randNum == 0) {
                randNum = CustomRandom.makeIntCustomRandom(MIN, MAX);
                sum += i;
            }
        }

        return "최종 합산 결과는 " + sum;
    }

    public String printRefreshRandomMovement(int condition) {
        final int MIN = 2;
        final int MAX = 10;

        int randNum;
        int sum = 0;

        for (int i = start; i <= end; i += randNum) {
            randNum = CustomRandom.makeIntCustomRandom(MIN, MAX);
            sum += randNum;
        }

        return "최종 합산 결과는 " + sum;
    }

    public String operateWithCondition (int condition) {

        switch (operationMode) {
            case PRINT:
                return printSimpleFindMatching(condition);

            case ADD:
                return printSumFindMatching(condition);

            case RANDOM:
                return printRandomFindMatching(condition);

            case REFRESH_RANDOM_SUM:
                return printSumRefreshRandomFindMatching(condition);

            case REFRESH_RANDOM_MOVEMENT:
                return printRefreshRandomMovement(condition);

            default:
                System.out.println("잘못된 값을 입력하였습니다!");
                return null;
        }
    }
}

 

Series

package com.example.demo.entity.basic.fourth;

// 1. 내가 어떤 정보를 이 클래스를 통해 제어하려고 하는가? 수열
// 2. 이 정보에 영향을 미칠 수 있는 인자는 어떤 것들이 있는가 ? 수열 패턴, 수열 개수

public class Series {
    private final int FIBONACCI_SERIES = 1;
    private final int CUSTOM_SERIES = 2;
    private int[] seriesArr;

    public Series(int seriesNum, int length) {
        seriesArr = new int[length];

        if (seriesNum == FIBONACCI_SERIES) {
            makeFibonacciSeries();
        } else if (seriesNum == CUSTOM_SERIES) {
            makeCustomSeries();
        } else {
            System.out.println("잘못된 정보입니다!");
        }
    }

    public void makeFibonacciSeries() {
        seriesArr[0] = 1;
        seriesArr[1] = 1;

        for (int i = 2; i < seriesArr.length; i++) {
            seriesArr[i] = seriesArr[i - 1] + seriesArr[i - 2];
        }
    }

    public void makeCustomSeries() {
        seriesArr[0] = 1;
        seriesArr[1] = 1;
        seriesArr[2] = 1;
        seriesArr[3] = 2;

        for (int i = 4; i < seriesArr.length; i++) {
            seriesArr[i] = seriesArr[i - 2] + seriesArr[i - 3] + seriesArr[i - 4];
        }
    }

    public int findSeriesNum() {
        return seriesArr[seriesArr.length - 1];
    }
}

 

 

 

com.example.demo.utillity.basic.third

CustomRandom

package com.example.demo.utillity.basic.third;

import java.util.Random;

public class CustomRandom {

    public static int makeIntCustomRandom(int min, int max){
        Random random = new Random();
        return random.nextInt(max - min) + min;
    }
}

 

 

 

 

 

문제 은행 [4] 

📑문제

https://cafe.naver.com/eddicorp/301

 

문제 은행 [ 4 ]

*** 초보자용 *** 1. 2 by 2 이중 배열을 초기화해서 아무 값이나 넣어보세요. 2. 1번 문제에서 초기화한 배열을 출력해봅시다. 3. 고양이 클래스를 만들어주...

cafe.naver.com

 

 

🔍풀이

(얘는 java로만)

questionBank.practice4

Student

package questionBank.practice4;

import jdk.swing.interop.SwingInterOpUtils;

import java.util.Scanner;

public class Students {
    static StudentsScore studentsAve = new StudentsScore();

    public static void main(String[] args) {
        subjectScore();
    }


    /**
     * 과목 점수를 입력하면 평균점수/분산/표준편차를 출력해주는 메서드
     */
    public static void subjectScore(){
        Scanner sc = new Scanner(System.in);
        System.out.print("수학 점수 : ");
        int mathScore = sc.nextInt();
        System.out.print("영어 점수 : ");
        int engScore = sc.nextInt();
        System.out.print("국어 점수 : ");
        int korScore = sc.nextInt();

        System.out.println();
        System.out.println("과목 평균 점수 : " +
                studentsAve.ave(mathScore, engScore, korScore));

        System.out.println("분산 : " +
                studentsAve.variance(mathScore, engScore, korScore));

        System.out.println("표준편차 : " +
                studentsAve.standardDeviation(mathScore, engScore, korScore));
    }
}

 

 

StudentsScore

package questionBank.practice4;

public class StudentsScore {
    private int mathScore;
    private int engScore;
    private int korScore;

    public StudentsScore() {
    }

    /**
     * 입력 받은 과목 점수의 평균을 반환하는 메서드
     * @param mathScore 수학 점수
     * @param engScore 영어 점수
     * @param korScore 국어 점수
     * @return 85.33333333333333
     */
    public double ave(int mathScore, int engScore, int korScore) {
        this.mathScore = mathScore;
        this.engScore = engScore;
        this.korScore = korScore;
        return (double) (mathScore + engScore + korScore) / 3;
    }

    /**
     * 입력 받은 과목 점수의 분산 값을 반환하는 메서드
     * @param mathScore 수학 점수
     * @param engScore 영어 점수
     * @param korScore 국어 점수
     * @return
     */
    public double variance(int mathScore, int engScore, int korScore) {
        this.mathScore = mathScore;
        this.engScore = engScore;
        this.korScore = korScore;
        double ave = ave(mathScore, engScore, korScore);
        //과목 별 편차 구하기
        mathScore = (int) Math.pow(mathScore-ave, 2);
        engScore = (int) Math.pow(engScore-ave, 2);
        korScore = (int) Math.pow(korScore-ave, 2);

        return (mathScore + engScore + korScore) / 3;
    }


    /**
     * 입력 받은 과목 점수의 표준편차 값을 반환 하는 메서드
     * @param mathScore 수학 점수
     * @param engScore 영어 점수
     * @param korScore 국어 점수
     * @return
     */
    public double standardDeviation(int mathScore, int engScore, int korScore) {
        this.mathScore = mathScore;
        this.engScore = engScore;
        this.korScore = korScore;
        return Math.pow(variance(mathScore, engScore, korScore), 2);
    }
}