Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Date; import java.util.Scanner; import java.text.*; public clas

ID: 3668585 • Letter: I

Question

import java.util.Date;
import java.util.Scanner;
import java.text.*;

public class TimeLab {
  
  
public static void main(String[] args)
{
Date d = new Date();
SimpleDateFormat SDF = new SimpleDateFormat ("hh:mm");
String sTime = SDF.format(d).toString();
int hours=0;
int minutes=0;
// output the current name of time, hours, minutes.
  
System.out.println("Current hours:minutes is " + sTime);
System.out.println("Time is: "+getTimeName(hours, minutes));
// prompt user for hours and minutes, make sure that the user is
// notified we are using 12 hour time
// you must filter against anything <1 and >12 for hours, <0 and >60 for minutes
//
// output the name of the imput time.
}
public static String getTimeName(int hours, int minutes)
{

return "test";
}
  }

*Using dr.java

Explanation / Answer

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class TimeLab {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);
Date d = new Date();
SimpleDateFormat SDF = new SimpleDateFormat ("hh:mm");
System.out.println("Enter the time");
String sTime = SDF.format(d).toString();
sTime=in.nextLine();
String sHours = sTime.substring(0,2);
String sMinutes = sTime.substring(3);
try{ int hours = Integer.parseInt(sHours);
int minutes = Integer.parseInt(sMinutes);
System.out.println("Time is: "+getTimeName(hours, minutes));
}

catch( NumberFormatException e){
    System.out.println("invalid");//affiche une erreur ici
}
}
public static String getTimeName(int hours, int minutes){
String time_name = "";

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

    String hour_mint []={"", "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 a;
    if (hours==12)
      a = hour_mint [1];// put 'one' if hour is 12
    else
      a = hour_mint[hours+1]; // if hour is not 12 then store an hour ahead of given hour

    System.out.print("time is : "+hours+":"+minutes+".");

    if (minutes==0)
      time_name = hour_mint[hours]+"o'clock";
    else if (minutes==15)
      time_name = "Quarter past "+hour_mint[hours];
    else if (minutes==30)
      time_name = "Quarter past "+hour_mint[hours];
    else if (minutes==45)
      time_name = "Quarter to"+a;
    else if (minutes<30) // for minutes between 1-29
      time_name = hour_mint[minutes]+" past "+hour_mint[hours];     
    else // between 31-59
      time_name = hour_mint[60-minutes]+" to "+a;

}
else
    time_name = "invalid time";

return time_name;
}

}