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

Java program Create class DateClass with the following capabilities: Output the date in multiple formats, such...
Java program Create class DateClass with the following capabilities: Output the date in multiple formats, such as MM/DD/YYYY June 14, 1992 DDD YYYY Use overloaded constructors to create DateClass 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...
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?");...
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...
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,...
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...
Create a Java Program to show a savings account balance. using eclipse IDE This can be...
Create a Java Program to show a savings account balance. using eclipse IDE This can be done in the main() method. Create an int variable named currentBalance and assign it the value of 0. Create an int variable named amountToSaveEachMonth. Prompt "Enter amount to save each month:" and assign the result to the int variable in step 2. Create an int variable name numberOfMonthsToSave. Prompt "Enter the number of months to save:" and store the input value into the variable...
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...
I am using NetBeans IDE Java for coding. I would like the code to be commented...
I am using NetBeans IDE Java for coding. I would like the code to be commented for a better understanding. 1. Implement a class Robot that simulates a robot wandering on an infinite plane. The robot is located at a point with integer coordinates and faces north, east, south, or west. Supply methods: public void turnLeft() public void turnRight() public void move() public Point getLocation() public String getDirection() The turnLeft and turnRight methods change the direction but not the location....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT