Question

Hello, so I'm trying to write a program that takes time input as integer and returns...

Hello, so I'm trying to write a program that takes time input as integer and returns them in strings. I have most of it working and I just need to write string values for 31~60. Is there a way to write a statement so if input is 31 or greater, subtract from 60 and return minutes + "before" + hour?

Rather than writing 30 more lines for 31-60.

import java.util.Scanner;

public class TimeName
{
   //Main method acquires user input to test
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter hours (1-12): ");
int hours = input.nextInt();
System.out.print("Enter minutes (0-60): ");
int minutes = input.nextInt();
input.close();

System.out.println(getTimeName(hours, minutes));
}

//This method puts hour and minute together
public static String getTimeName(int hours, int minutes) {
String whatTime = "";

if (minutes == 45) {
   whatTime += "a quater to ";
   whatTime += getHours(hours + 1);
} else if (minutes == 30) {
   whatTime += "half past ";
   whatTime += getHours(hours);
} else if (minutes == 0) {
   whatTime += getHours(hours);
   whatTime += " o'clock";
} else {
   whatTime += getMinutes(minutes);
   whatTime += " minutes past ";
   whatTime += getHours(hours);
}

return whatTime;
}
  
//This method transcribes the hours
public static String getHours(int hours) {
if (hours == 12) {
return "twelve";
} else if (hours == 11) {
return "eleven";
} else if (hours == 10) {
return "ten";
} else if (hours == 9) {
return "nine";
} else if (hours == 8) {
return "eight";
} else if (hours == 7) {
return "seven";
} else if (hours == 6) {
return "six";
} else if (hours == 5) {
return "five";
} else if (hours == 4) {
return "four";
} else if (hours == 3) {
return "three";
} else if (hours == 2) {
return "two";
} else if (hours == 1) {
return "one";
} else {
return "";
}
}
  
//This method transcribes the minutes
public static String getMinutes(int minutes) {
if (minutes == 1) {
return "one";
} else if (minutes == 2) {
return "two";
} else if (minutes == 3) {
return "three";
} else if (minutes == 4) {
return "four";
} else if (minutes == 5) {
return "five";
} else if (minutes == 6) {
return "six";
} else if (minutes == 7) {
return "seven";
} else if (minutes == 8) {
return "eight";
} else if (minutes == 9) {
return "nine";
} else if (minutes == 10) {
return "ten";
} else if (minutes == 11) {
return "eleven";
} else if (minutes == 12) {
return "twelve";
} else if (minutes == 13) {
return "thirteen";
} else if (minutes == 14) {
return "fourteen";
} else if (minutes == 15) {
return "fifteen";
} else if (minutes == 16) {
return "sixteen";
} else if (minutes == 17) {
return "seventeen";
} else if (minutes == 18) {
return "eighteen";
} else if (minutes == 19) {
return "nineteen";
} else if (minutes == 20) {
return "twenty";
} else if (minutes == 21) {
return "twenty one";
} else if (minutes == 22) {
return "twenty two";
} else if (minutes == 23) {
return "twenty three";
} else if (minutes == 24) {
return "twenty four";
} else if (minutes == 25) {
return "twenty five";
} else if (minutes == 26) {
return "twenty six";
} else if (minutes == 27) {
return "twenty seven";
} else if (minutes == 28) {
return "twenty eight";
} else if (minutes == 29) {
return "twenty nine";
} else {
return "";
}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//CODE

import java.util.Scanner;

class TimeName
{
//Main method acquires user input to test
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter hours (1-12): ");
int hours = input.nextInt();
System.out.print("Enter minutes (0-60): ");
int minutes = input.nextInt();
input.close();
System.out.println(getTimeName(hours, minutes));
}

//This method puts hour and minute together
public static String getTimeName(int hours, int minutes) {
String whatTime = "";

if (minutes == 45) {
whatTime += "a quater to ";
//If hour is 12 then next hour is 1 else hour + 1
whatTime += getHours((hours%12) + 1);
} else if (minutes == 30) {
whatTime += "half past ";
whatTime += getHours(hours);
} else if (minutes == 0) {
whatTime += getHours(hours);
whatTime += " o'clock";
} else if (minutes < 30){
whatTime += getMinutes(minutes);
whatTime += " minutes past ";
whatTime += getHours(hours);
} else {
whatTime += getMinutes(60-minutes);
whatTime += " minutes before ";
whatTime += getHours((hours%12) + 1);
}

return whatTime;
}

//This method transcribes the hours
public static String getHours(int hours) {
if (hours == 12) {
return "twelve";
} else if (hours == 11) {
return "eleven";
} else if (hours == 10) {
return "ten";
} else if (hours == 9) {
return "nine";
} else if (hours == 8) {
return "eight";
} else if (hours == 7) {
return "seven";
} else if (hours == 6) {
return "six";
} else if (hours == 5) {
return "five";
} else if (hours == 4) {
return "four";
} else if (hours == 3) {
return "three";
} else if (hours == 2) {
return "two";
} else if (hours == 1) {
return "one";
} else {
return "";
}
}

//This method transcribes the minutes
public static String getMinutes(int minutes) {
if (minutes == 1) {
return "one";
} else if (minutes == 2) {
return "two";
} else if (minutes == 3) {
return "three";
} else if (minutes == 4) {
return "four";
} else if (minutes == 5) {
return "five";
} else if (minutes == 6) {
return "six";
} else if (minutes == 7) {
return "seven";
} else if (minutes == 8) {
return "eight";
} else if (minutes == 9) {
return "nine";
} else if (minutes == 10) {
return "ten";
} else if (minutes == 11) {
return "eleven";
} else if (minutes == 12) {
return "twelve";
} else if (minutes == 13) {
return "thirteen";
} else if (minutes == 14) {
return "fourteen";
} else if (minutes == 15) {
return "fifteen";
} else if (minutes == 16) {
return "sixteen";
} else if (minutes == 17) {
return "seventeen";
} else if (minutes == 18) {
return "eighteen";
} else if (minutes == 19) {
return "nineteen";
} else if (minutes == 20) {
return "twenty";
} else if (minutes == 21) {
return "twenty one";
} else if (minutes == 22) {
return "twenty two";
} else if (minutes == 23) {
return "twenty three";
} else if (minutes == 24) {
return "twenty four";
} else if (minutes == 25) {
return "twenty five";
} else if (minutes == 26) {
return "twenty six";
} else if (minutes == 27) {
return "twenty seven";
} else if (minutes == 28) {
return "twenty eight";
} else if (minutes == 29) {
return "twenty nine";
} else {
return "";
}

}
}

//OUTPUT

Add a comment
Know the answer?
Add Answer to:
Hello, so I'm trying to write a program that takes time input as integer and returns...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In previous chapters, you developed classes that hold rental contract information for Sammy's Seashore Supplies. Now...

    In previous chapters, you developed classes that hold rental contract information for Sammy's Seashore Supplies. Now modify the Rental and RentalDemo classes as follows: Modify the Rental class to include an integer field that holds an equipment type. Add a final String array that holds names of the types of equipment that Sammy's rents—personal watercraft, pontoon boat, rowboat, canoe, kayak, beach chair, umbrella, and other. Include get and set methods for the integer equipment type field. If the argument passed...

  • Need help with this java code supposed to be a military time clock, but I need...

    Need help with this java code supposed to be a military time clock, but I need help creating the driver to test and run the clock. I also need help making the clock dynamic. public class Clock { private int hr; //store hours private int min; //store minutes private int sec; //store seconds public Clock () { setTime (0, 0, 0); } public Clock (int hours, intminutes, int seconds) { setTime (hours, minutes, seconds); } public void setTime (int hours,int...

  • JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...

    JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you livein California, a new WorldCLock(3) should show the time in NewYork, three time zones ahead. Include an Alarm feature in the Clock class....

  • 5. Write a static method "f(n)" in the space provide, that returns O if n is...

    5. Write a static method "f(n)" in the space provide, that returns O if n is even, and if n is odd, returns 1 or -1 according as n is greater than or less than 0. importjava.util.Scanner; public class Q_05 Your "f(n)" method: public static void main(String args[]) mana int n; Scanner input = new Scanner(System.in); System.out.print("Please enetrn: "); n=input.nextInt(); System.out.println(f(n)); "Method f(n)" 7. Write a static method "max" in the space provide, that returns the maximum value from 3...

  • Problem 1.(1) Implement a class Clock whose getHours and getMinutes methods return the current time at...

    Problem 1.(1) Implement a class Clock whose getHours and getMinutes methods return the current time at your location. (Call java.time.LocalTime.now().toString() or, if you are not using Java 8, new java.util.Date().toString() and extract the time from that string.) Also provide a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutesmethods. (2) Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you live in California, a new WorldClock(3) should show...

  • [Java] We have learned the class Clock, which was designed to implement the time of day...

    [Java] We have learned the class Clock, which was designed to implement the time of day in a program. Certain application in addition to hours, minutes, and seconds might require you to store the time zone. Please do the following: Derive the class ExtClock from the class Clock by adding a data member to store the time zone. Add necessary methods and constructors to make the class functional. Also write the definitions of the methods and constructors. Write a test...

  • Write the output that would be producded by the following code: public static void printStrings(Scanner input)...

    Write the output that would be producded by the following code: public static void printStrings(Scanner input) { while (input.hasNextInt()) { int times = input.nextInt(); String word = input.next(); for (int i = 0; i < times; i++) { System.out.print(word); } System.out.println(); } } The input file has the following lines: If this method works properly, the lines of text in this file will be reversed. Remember that some lines might be blank.

  • need help with this JAVA lab, the starting code for the lab is below. directions: The...

    need help with this JAVA lab, the starting code for the lab is below. directions: The Clock class has fields to store the hours, minutes and meridian (a.m. or p.m.) of the clock. This class also has a method that compares two Clock instances and returns the one that is set to earlier in the day. The blahblahblah class has a method to get the hours, minutes and meridian from the user. Then a main method in that class creates...

  • Zeller's congruence is an algorithm developed to calculate the day of the week. Write a program...

    Zeller's congruence is an algorithm developed to calculate the day of the week. Write a program that prompts the user to enter a year, month, day of month and it displays the name of the week. Here is my source code. I need it broke down into a flowchart. public static void main(String[] args) {        //all variables are initialized to 0, because local variables require at least an initial value before use        Scanner input = new...

  • Having trouble with the do while/while loop and the switch statement. I got some of the...

    Having trouble with the do while/while loop and the switch statement. I got some of the switch statement but cant get the program to repeat itself like it should.What i have so far for my code is below. Any help is appreciated... i am not sure what I am doing wrong or what i am missing. I am completely lost on the while loop and where and how to use it in this scenario. import java.util.Scanner; public class sampleforchegg {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT