Java

민겸·2023년 3월 24일
0

Schoolworks

목록 보기
3/5

#1

Write a program that reads three strings from the keyboard. Although the strings are in no particular order, display the string that would be second if they were arranged lexicographically.

import java.util.Scanner;

public class hw1 {
    public static void main(String[] args) {
        // 키보드로부터 세개의 문자열을 입력받는다
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter first string: "); // 첫번째 문자열 입력
        String string1 = keyboard.nextLine();
        System.out.print("Enter second string: "); // 두번째 문자열 입력
        String string2 = keyboard.nextLine();
        System.out.print("Enter third string: "); // 세번째 문자열 입력
        String string3 = keyboard.nextLine();

        // 입력받은 문자열을 사전순으로 배열했을 때 2번째를 찾기
        if ((string1.compareTo(string2) <= 0 && string2.compareTo(string3) <= 0)
                || (string3.compareTo(string2) <= 0 && string2.compareTo(string1) <= 0)) {
            System.out.println("Second string lexicographically: " + string2);
        } else if ((string2.compareTo(string1) <= 0 && string1.compareTo(string3) <= 0)
                || (string3.compareTo(string1) <= 0 && string1.compareTo(string2) <= 0)) {
            System.out.println("Second string lexicographically: " + string1);
        } else {
            System.out.println("Second string lexicographically: " + string3);
        }
    }
}

#2

Write a program that reads a one-line sentence as input and then displays the following response:

  • If the sentence ends with a question mark (?) and the input contains an even number of characters, display the word Yes.
  • If the sentence ends with a question mark (?) and the input contains an odd number of characters, display the word No.
  • If the sentence ends with an exclamation point (!), display the word Wow.
  • In all other cases, display the words You always say followed by the input string enclosed in quotes. (You always say “입력된 내용”)

Your output should all be on one line. Be sure to note that in the last case, your output must include quotation marks around the echoed input string.

In all other cases, there are no quotes in the output. Your program does not have to check the input to see that the user has entered a legitimate sentence.

import java.util.Scanner;

public class hw2 {
    public static void main(String[] args) {
        // 키보드로부터 문자열을 입력받는다
        Scanner keyboard = new Scanner(System.in);
        System.out.print("문장을 입력하세요: ");
        String sentence = keyboard.nextLine();
        char lastChar = sentence.charAt(sentence.length() - 1); // 마지막 문자 찾기

        if (lastChar == '?') {
            if (sentence.length() % 2 == 0) {
                System.out.println("Yes");// 조건1
            } else {
                System.out.println("No");// 조건2
            }
        } else if (lastChar == '!') {// 조건3
            System.out.println("Wow");
        } else {// 기본조건
            System.out.println("You always say \"" + sentence + "\"");
        }
    }
}

#3

Suppose that we are working for an online service that provides a bulletin board for its users.
We would like to give our users the option of filtering out profanity.
Suppose that we consider the words cat, dog, and llama to be profane(*profane:신성 모독적인).

Write a program that reads a string from the keyboard and tests whether the string contains one of our profane words.
Your program should find words like cAt that differ only in case.

Option: As an extra challenge, have your program reject only lines that contain a profane word exactly.
For example, Dogmatic concatenation is a small category should not be considered profane.

import java.util.Scanner;

public class hw3 {
    public static void main(String[] args) {
        // 키보드로부터 문자열을 입력받는다
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String input = keyboard.nextLine();

        // 대소문자 상관없이 검열하기 위해 소문자로 통일시킨다
        String inputLower = input.toLowerCase();

        // 문자열이 비속어를 포함하는지 검사한다
        if (inputLower.matches(".*\\b(cat|dog|llama)\\b.*")) {
            System.out.println("Input rejected: contains profanity.");
        } else {
            System.out.println("Input accepted: does not contain profanity.");
        }
    }
}

#4

Write a program that reads a string from the keyboard and tests whether it contains a valid date.
Display the date and a message that indicates whether it is valid.
If it is not valid, also display a message explaining why it is not valid.
The input date will have the format mm/dd/yyyy.
A valid month value mm must be from 1 to 12 (January is 1).
The day value dd must be from 1 to a value that is appropriate for the given month.
September, April, June, and November each have 30 days. February has 28 days except for leap years when it has 29.
The remaining months all have 31 days each.
A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400.

import java.util.Scanner;

public class hw4 {
    public static void main(String[] args) {
        // 키보드로부터 월/일/년 순으로 문자열을 입력받는다
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter a date in mm/dd/yyyy format: ");
        String dateStr = keyboard.nextLine();

        // 문자열을 올바른 입력방법으로 입력했는지 확인한다
        if (!dateStr.matches("^\\d{2}/\\d{2}/\\d{4}$")) {
            System.out.println("Invalid format: must be mm/dd/yyyy");
        } else {
            // 입력받은 문자열로부터 필요한 정보 추출
            int month = Integer.parseInt(dateStr.substring(0, 2));
            int day = Integer.parseInt(dateStr.substring(3, 5));
            int year = Integer.parseInt(dateStr.substring(6));

            // 올바른 달을 입력했는지 확인
            if (month < 1 || month > 12) {
                System.out.println("Invalid month value");
            } else {
                // 그 달에 따른 올바른 일수를 입력했는지 확인
                if (month == 4 || month == 6 || month == 9 || month == 11) {
                    if (day < 1 || day > 30) {
                        System.out.println("Invalid day value for the given month");
                    } else {
                        System.out.println("Valid date: " + dateStr);
                    }
                } else if (month == 2) {
                    // 2월을 입력했을 시 년도가 윤년인지 확인
                    boolean leapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
                    if (leapYear && (day < 1 || day > 29)) {
                        System.out.println("Invalid day value for the given month and year");
                    } else if (!leapYear && (day < 1 || day > 28)) {
                        System.out.println("Invalid day value for the given month");
                    } else {
                        System.out.println("Valid date: " + dateStr);
                    }
                } else {
                    if (day < 1 || day > 31) {
                        System.out.println("Invalid day value for the given month");
                    } else {
                        System.out.println("Valid date: " + dateStr);
                    }
                }
            }
        }
    }
}

0개의 댓글