Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment, you will write a Java program that repeatedly asks the user for a date (of the form m/d/y), and then either prints

In this assignment, you will write a Java program that repeatedly asks the user for a date (of the form m/d/y), and then either prints that the date is invalid or prints the date in "long" format (monthName day, year). Here is a sample run of the program (user input is in red):

Enter date (m/d/y): 2/27/2018 2/27/2018 is February 27, 2018 Go again (y)? y Enter date (m/d/y): 4/31/2018 4/31/2018 is invalid. Go again (y)? Y Enter date (m/d/y): 2/29/2018 2/29/2018 is invalid. Go again (y)? y Enter date (m/d/y): 13/2/2018 13/2/2018 is invalid. Go again (y)? Y Enter date (m/d/y): 2/29/2016 2/26/2016 is February 29, 2016 Go again (y)? n 

Requirements

This program should contain a single class (called Proj5). This project has been started for you -- you will need to download Proj5.javaimage text in transcribed from Canvas. Copy all the text in that file, and when you create a new class in BlueJ, paste the code in the BlueJ file. Do not change any of the completed code from that file.

This project contains several methods, each of which contains documentation on what that method does or is supposed to do. You will need to complete the following methods:

public static String monthNumToName(int m)

public static String getLongFormat(int m, int d, int y)

public static boolean checkValidity(int m, int d, int y)

public static void main(String[] args)

I suggest completing the methods in the order listed above.

Assumptions

You may make the following assumptions in your program:

The user will always enter the date in the form m/d/y, where m, d, y are all integers

The user will always enter either y, Y, n, or N when asked if they want to go again

................................

Proj 5

import java.util.*;

public class Proj5 {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

char more = 'y';

do {

//This code is done -- it gets the date and breaks it into month,day,year

System.out.print("Enter date (m/d/y): ");

String date = s.nextLine();

StringTokenizer st = new StringTokenizer(date, "/");

int month = Integer.parseInt(st.nextToken());

int day = Integer.parseInt(st.nextToken());

int year = Integer.parseInt(st.nextToken());

//Now if the user typed 2/27/2018, then month=2, day=27, and year=2018

//YOU DO THIS

//Call checkValidity to see if the date is valid

//If it isn't, print an error

//Otherwise

//Call getLongFormat to get the long format of this date

//Print the result from getLongFormat

//This code asks if the user wants to go again. It will loop while they enter y/Y.

System.out.print(" Go again (y)? ");

more = (s.nextLine()).charAt(0);

System.out.println();

} while (more == 'y' || more == 'Y');

}

/**

* Gets the month name for a given month number

*

* @param m The month number (1-12)

* @return The corresponding month name (January-December)

*/

public static String monthNumToName(int m) {

//YOU DO THIS

//return "January" if m is 1, "February" if m is 2, etc.

//leave this here in case m is not between 1-12

return "Invalid Month";

}

/**

* Gets the long format for a date (Month day, year)

*

* @param m The month

* @param d The day

* @param y The year

* @return The corresponding long format (Month day, year)

*/

public static String getLongFormat(int m, int d, int y) {

//YOU DO THIS

//Call monthNumToName to convert m to a String

//return the given date in long format

//(e.g., if m=1,d=1,y=2018, you would return "January 1, 2018")

//remove this return line after you finish the method

return "";

}

/**

* Gets whether the given year is a leap year

*

* @param y The year

* @return Whether y is a leap year

*/

public static boolean isLeapYear(int year) {

//This method is DONE!

if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {

return true;

}

else return false;

}

/**

* Gets whether a date is a valid date

*

* @param m The month

* @param d The day

* @param y The year

* @return Whether m/d/y is a valid date

*/

public static boolean checkValidity(int m, int d, int y) {

//YOU DO THIS

//if m is not between 1-12 - return false

//if y is negative - return false

//Use if/else if statements to see which month you are in (m)

//make sure the day (d) is >= 1 and

//if it is - return true

//if not - return false

//month numbers with 31 days: 1,3,5,7,8,10,12

//month numbers with 30 days: 4,6,9,11

//February (m=2) usually has 28 days, unless it is a leap year (call isLeapYear to check)

//delete this return line after you are done

return false;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions