PLS SOLVE IN BLUEJ FORMAT : NO SPAMS OR I'LL REPORT YOU : Given a time in numbers we can convert it into words. For example 5:00 five o'clock 5:10 ten minutes past five 5:15 quarter past five 5:30 half past five 5:40 twenty minutes to six 5 : 45 quarter to six 5:47 thirteen minutes to six. WAP which first input two integers, the first between 1 and 12 (both inclusive) and second between 0 and 59 (both inclusive) and then prints out the time they represents, in words. Your program should follow the format of the examples above. SAMPLE DATA INPUT: TIME: 3,0 OUTPUT 3:00 three o'clock INPUT: TIME: 7,29 OUTPUT 7:29 Twenty nine minutes past seven. INPUT: TIME: 6,34 OUTPUT 6:34 twenty six minutes to seven INPUT: TIME: 12,1 OUTPUT 12:01 one minutes past twelve INPUT: TIME: 12, 45s OUTPUT 12:45 quarter to one INPUT: TIME: 10 , 59 OUTPUT 10:59 one minute to eleven INPUT: TIME: 14,60 OUTPUT: incorrect input. Test your program for the data values given in the examples above and some random data.
please give the entire program solved no spams I'll mark you as a brainllist answer!PLEASE SOLVE IN BLUEJ FORMAT : Given a time in numbers we can convert it into words. For example 5:00 five o'clock 5:10 ten minutes past five 5:15 quarter past five 5:30 half past five 5:40 twenty minutes to six 5 : 45 quarter to six 5:47 thirteen minutes to six. WAP which first input two integers, the first between 1 and 12 (both inclusive) and second between 0 and 59 (both inclusive) and then prints out the time they represents, in words. Your program should follow the format of the examples above. SAMPLE DATA INPUT: TIME: 3,0 OUTPUT 3:00 three o'clock INPUT: TIME: 7,29 OUTPUT 7:29 Twenty nine minutes past seven. INPUT: TIME: 6,34 OUTPUT 6:34 twenty six minutes to seven INPUT: TIME: 12,1 OUTPUT 12:01 one minutes past twelve INPUT: TIME: 12, 45s OUTPUT 12:45 quarter to one INPUT: TIME: 10 , 59 OUTPUT 10:59 one minute to eleven INPUT: TIME: 14,60 OUTPUT: incorrect input. Test your program for the data values given in the examples above and some random data.
please give the entire program solved no spams I'll mark you as a brainllist answer!
Answers
import java.util.Scanner;
import java.util.Hashtable;
public class NaturalTime {
// Validate time input
private boolean validateTime(int hour, int minute) {
// Hour must be between 1 and 12 (inclusive)
if (hour < 1 || hour > 12) {
return false;
}
// Minute must be between 0 and 59 (inclusive)
if (minute < 0 || minute > 59) {
return false;
}
// Both hour and minute are valid
return true;
}
// Take user input for time
public int[] timeInput() {
Scanner in = new Scanner(System.in);
// Take user input
System.out.print("Enter time (Hour, Minute): ");
String userInput = in.nextLine();
// Split at the comma
String time[] = userInput.split(",");
// Extract the hour and minute
int hour = Integer.parseInt(time[0]);
int minute = Integer.parseInt(time[1]);
// Close Scanner object
in.close();
// Validate input
if (!validateTime(hour, minute)) {
// Print appropriate error message and exit
System.out.println("Invalid input. Valid Range: 1 <= Hour <= 12 AND 0 <= Minute <= 59.");
System.exit(1);
}
// Create a integer array and return it
int integerTime[] = { hour, minute };
return integerTime;
}
// Return word for unit's place value
private String unitsWord(int value) {
Hashtable<Integer, String> uw = new Hashtable<>();
uw.put(1, "one");
uw.put(2, "two");
uw.put(3, "three");
uw.put(4, "four");
uw.put(5, "five");
uw.put(6, "six");
uw.put(7, "seven");
uw.put(8, "eight");
uw.put(9, "nine");
return uw.get(value);
}
// Return word in the 10-19 range
private String tensRange(int value) {
Hashtable<Integer, String> word = new Hashtable<>();
word.put(10, "ten");
word.put(11, "eleven");
word.put(12, "twelve");
word.put(13, "thirteen");
word.put(14, "fourteen");
// Special word for 15
word.put(15, "quarter");
word.put(16, "sixteen");
word.put(17, "seventeen");
word.put(18, "eighteen");
word.put(19, "nineteen");
return word.get(value);
}
// Return word for ten's place value
private String tensWord(int value) {
Hashtable<Integer, String> tw = new Hashtable<>();
tw.put(20, "twenty");
// Special word for 30
tw.put(30, "half");
// We don't need words for 40, 50
return tw.get(value);
}
// Construct hour word
private String hour(int hourValue) {
if (hourValue < 10) {
return unitsWord(hourValue);
}
else {
return tensRange(hourValue);
}
}
// Construct minutes word
private String minute(int minuteValue) {
// 1 minute
if (minuteValue == 1) {
return "one minute";
}
// 2-9 minutes
else if (minuteValue < 10) {
return unitsWord(minuteValue) + " minutes";
}
// 15 minutes = quarter
else if (minuteValue == 15) {
return tensRange(minuteValue);
}
// 30 minutes = half
else if (minuteValue == 30) {
return tensWord(minuteValue);
}
// 10-19 minutes (except 15, which is already covered)
else if (minuteValue < 20) {
return tensRange(minuteValue) + " minutes";
}
// 20-29 minutes (30 is already covered)
else {
int unitsPlace = minuteValue % 10;
int tensPlace = (minuteValue / 10) * 10;
if (unitsPlace == 0) {
// Realistically it only returns 20 minutes
return tensWord(tensPlace) + " minutes";
}
else {
// 21-29 minutes
return tensWord(tensPlace) + " " + unitsWord(unitsPlace) + " minutes";
}
}
}
// Construct whole time
public String time(int hour, int minute) {
String currentHour = hour(hour);
String nextHour = hour(hour + 1);
// If hour is 12, we have to loop back to 1
if (hour == 12) {
nextHour = hour(1);
}
// Exact hours
if (minute == 0) {
return currentHour + "'o clock";
}
// 1-30 minutes range ("past")
else if (minute <= 30) {
return minute(minute) + " past " + currentHour;
}
// 31-59 minutes range. Subtract from 60 ("to")
else {
return minute(60 - minute) + " to " + nextHour;
}
}
// Main function for testing
public static void main(String[] args) {
// Create NaturalTime object
NaturalTime nt = new NaturalTime();
// Call user input function
int time[] = nt.timeInput();
// Print out the time
System.out.println("The time is " + nt.time(time[0], time[1]) + ".");
}
}