104k views
4 votes
Write a method string gettimename (int hours, int minutes) that returns the english name for a point in time, such as "ten minutes past two", "half past three", "a quarter to four", or "five o'clock". assume that hours is between 1 and 12.

User Mickp
by
8.0k points

2 Answers

3 votes
Hi!!

Hope I understood your question...

Ten minutes past two = two ten
half past three = three thirty
a quarter to four = four forty-five
Five o'clock = five

then you can add a.m or p.m.

Hope I helped !!

User Robintibor
by
7.6k points
1 vote

Answer:

// Program is written in Java Programming Language

// Comments are used for explanatory purpose

// Program starts here

public static String getTimeName(int hours, int minutes) {

// Check for valid time

if ((hours >= 1 && hours <= 12) && (minutes >= 0 && minutes <= 59)) {

// Create an array to hold valid minutes in English

String validminutes[] = {

"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twenty one", "Twenty two", "Twenty three", "Twenty four", "Twenty five", "Twenty six", "Twenty seven", "Twenty eight", "Twenty nine" };

String validtime;

// Start Conversion

if (hours == 12){ // Set time to be One

validtime = validminutes[1];}

else{ // Set time to be ahead by one hour

validtime = validminutes[hours + 1]; }

// Check for minutes

if (minutes == 0){ // Replace 0 with O' Clock

System.out.println(validminutes[hours] + "O' Clock");}

else if (minutes == 15){// Replace with quarter past

System.out.println("Quarter past " + validminutes[hours]);}

else if (minutes == 30){// Replace with half past

System.out.println("Half past" + validminutes[hours]);}

else if (minutes == 45){// Replace with quarter to

System.out.println("Quarter to" + a);}

else if (minutes < 30){ // for other minutes between 1-29

System.out.println(validminutes[minutes] + " " + "past" + validminutes[hours]);}

else{ // Other minutes

System.out.println(hour_mint[60 - minutes] + " " + "to " +validtime);}

}

// End of Valid Time

else { // Invalid Tine

System.out.println("Time not valid ");

}

}

// End of Program

User Nesv
by
6.9k points