Answer to Question #295702 in Java | JSP | JSF for bob

Question #295702

Java Code please Input The first line contains an integer which is either 1 or 2. If it is a 1, it means to create a Date via the default constructor. If it is a 2, 3 integers follow in the next line representing the day, month, and year of the Date to be created. This is then followed by a number m representing the number of operations to be performed followed by the operations themselves as specified below: 1 - setDay 2 - setMonth 3 - setYear 4 - setDate 5 - getDay 6 - getMonth 7 - getYear 8 - toString 9 - dayOfTheWeek For operations 1, 2, 3, one integer follows. For setDate, 3 integers follow (D M Y). For the rest of the operations, no other inputs follow.


1
Expert's answer
2022-02-09T14:47:11-0500
import java.time.LocalDate;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("1 - Default; 2 - Manual");
        LocalDate date = null;
        switch (in.nextInt()) {
            case 1:
                date = LocalDate.now();
                break;
            case 2:
                System.out.println("Day");
                int day = in.nextInt();
                System.out.println("Month");
                int month = in.nextInt();
                System.out.println("Year");
                int year = in.nextInt();
                date = LocalDate.of(year, month, day);
                break;
        }
        if (date != null) {
            System.out.println("1 - setDay\n2 - setMonth\n3 - setYear\n" +
                    "4 - setDate\n5 - getDay\n6 - getMonth\n" +
                    "7 - getYear\n8 - toString\n9 - dayOfTheWeek");
            switch (in.nextInt()) {
                case 1:
                    date = LocalDate.of(date.getYear(), date.getMonthValue(), in.nextInt());
                    break;
                case 2:
                    date = LocalDate.of(date.getYear(), in.nextInt(), date.getDayOfMonth());
                    break;
                case 3:
                    date = LocalDate.of(in.nextInt(), date.getMonthValue(), date.getDayOfMonth());
                    break;
                case 4:
                    System.out.println("Day");
                    int day = in.nextInt();
                    System.out.println("Month");
                    int month = in.nextInt();
                    System.out.println("Year");
                    int year = in.nextInt();
                    date = LocalDate.of(year, month, day);
                    break;
                case 5:
                    System.out.println(date.getDayOfMonth());
                    break;
                case 6:
                    System.out.println(date.getMonth());
                    break;
                case 7:
                    System.out.println(date.getYear());
                    break;
                case 8:
                    System.out.println(date);
                    break;
                case 9:
                    System.out.println(date.getDayOfWeek());
                    break;
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS