Question

In: Computer Science

Java Program using Netbeans IDE Create class Date with the following capabilities: a. Output the date...

Java Program using Netbeans IDE

Create class Date with the following capabilities:

a. Output the date in multiple formats, such as

MM/DD/YYYY

June 14, 1992

DDD YYYY

b. Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first of which represents the day number in the year.

Solutions

Expert Solution


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Objects;

public class Date {

        public Date(int month, int day, int year) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.DATE, day);
                calendar.set(Calendar.MONTH, month - 1);
                calendar.set(Calendar.YEAR, year);
                SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
                System.out.println(format.format(calendar.getTime()));
        }

        public Date(String monthName, int day, int year) {
                if (Objects.nonNull(monthName) && !monthName.equals("")) {
                        try {
                                java.util.Date date = new SimpleDateFormat("MMMM").parse(monthName);
                                Calendar calendar = Calendar.getInstance();
                                calendar.setTime(date);
                                calendar.set(Calendar.DATE, day);
                                calendar.set(Calendar.YEAR, year);
                                SimpleDateFormat format = new SimpleDateFormat("MMMM dd, yyyy");
                                System.out.println(format.format(calendar.getTime()));
                        } catch (ParseException e) {
                                System.out.println("Invalid month name.!");
                        }
                } else {
                        System.out.println("Please provide a month name.!");
                }
        }

        public Date(int day, int year) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.DATE, day);
                calendar.set(Calendar.YEAR, year);
                SimpleDateFormat format = new SimpleDateFormat("ddd yyyy");
                System.out.println(format.format(calendar.getTime()));
        }

        public static void main(String[] args) {
                new Date(6, 20, 2020);
                new Date("may", 14, 1992);
                new Date(20, 1992);

        }
}

I have created 3 constructors for the Date class.

1st constructor has 3 parameters - month, day, and year respectively. We created a calendar class instance and set date, month, and year with the parameters.

calendar.set(Calendar.MONTH, month - 1);


We subtract the month by 1 because the month value starts from 0 in the Calendar class in Java,  Then we created the SimpleDateFormat class instance with the date format given in the question. Finally, we print the date in the corresponding format using the format method of SimpleDateFormat class. calendar.getTime() returns the date object with the date values we set.

In the 2nd constructor, we pass the month name, day, and year respectively. First, we check if the month's name is null or empty. if yes, we print ''Please provide a month name.!". else we create a date object with the month name. and create the Calendar instance and set the date using the "setTime" method. Then we set the day and year values just like we did in the first constructor. Finally, we create the SimpleDateFormat with the second date format in the question. And we format the date object and print the date.

In the 3rd constructor, we create the Calendar instance and set the date and year values only. Then we create the SimpleDateFormat object with the 3rd date format given in the question. And finally, print the formatted date just like we did in the first and second constructor.

In the main method, we create the instances of our Date class using the 3 overloaded constructors and pass the respective values.

That's all we did in this program.


Related Solutions

Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑ Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. ☑ Add a method addToCollection. In this method,...
Steps to making your Netbeans IDE (Interactive Development Environment): Download and install Netbeans 8.2 IDE (Java...
Steps to making your Netbeans IDE (Interactive Development Environment): Download and install Netbeans 8.2 IDE (Java SE version only). If you do not have the latest Java installation to match, Netbeans will direct you to download the needed version of Java. If needed, download and install the latest Oracle Official Java Software Development Kit Standard Edition (Java SDK SE), and JUnit. I suggest you do this via the bundle referred to as The Java Development Kit (JDK). Download the architecture...
Create a Java Program to calculate luggage costs. USING ECLIPSE IDE The Business Rules are: A....
Create a Java Program to calculate luggage costs. USING ECLIPSE IDE The Business Rules are: A. Two bags per person are free. B. The Excess Bag Charge is $75 per bag. The program needs to do the following: 1. In your main method(),    Create integers to store the number of Passengers and also the total number of bags    Prompt for the number of passengers on a ticket.    Prompt for the total number of bags for all passengers...
Part A Java netbeans ☑ Create a project and in it a class with a main....
Part A Java netbeans ☑ Create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class , after the package statement, paste import java.util.Scanner; As the first line inside your main method, paste Scanner scan = new Scanner(System.in); Remember when you are getting a value from the user, first print a request, then use the Scanner to read the right type...
(Using Date Class) The following UML Class Diagram describes the java Date class: java.util.Date +Date() +Date(elapseTime:...
(Using Date Class) The following UML Class Diagram describes the java Date class: java.util.Date +Date() +Date(elapseTime: long) +toString(): String +getTime(): long +setTime(elapseTime: long): void Constructs a Date object for the current time. Constructs a Date object for a given time in milliseconds elapsed since January 1, 1970, GMT. Returns a string representing the date and time. Returns the number of milliseconds since January 1, 1970, GMT. Sets a new elapse time in the object. The + sign indicates public modifer...
Create the following java program with class list that outputs: //output List Empty List Empty List...
Create the following java program with class list that outputs: //output List Empty List Empty List Empty Item not found Item not found Item not found Original list Do or do not. There is no try. Sorted Original List Do There do is no not. or try. Front is Do Rear is try. Count is 8 Is There present? true Is Dog present? false List with junk junk Do or moremorejunk do not. There is no try. morejunk Count is...
C++ * Program 2:      Create a date class and Person Class.   Compose the date class...
C++ * Program 2:      Create a date class and Person Class.   Compose the date class in the person class.    First, Create a date class.    Which has integer month, day and year    Each with getters and setters. Be sure that you validate the getter function inputs:     2 digit months - validate 1-12 for month     2 digit day - 1-3? max for day - be sure the min-max number of days is validate for specific month...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT