Need help creating a basic java string program using nested if/else, return loops or while loops or charAt methods while returning information using a console, Please leave notes as to compare my program and see where I went wrong or could've used a different method.
secondsAfterMidnight
Input: String that represents time of day
Returns: integer number of seconds after midnight (return -1 if String is not valid time of day)
General time of day format HH:MM:SS(AM/PM)
These are examples where when input the return value is answered in a console:
// Input String Return Value
// "12:34:09AM" 2049
// "12:00:00PM" 43200 (common noon)
// "12:00:02am" 2 (AM/PM case insensitive)
// "3:03:03Pm" 54183 (two digit MM and SS required)
// "7:11:03A" -1
// "7:11:3AM" -1
// "7:91:73PM" -1
// "23:45:12" -1 (do not allow 24 hour clock format)
public static int secondsAfterMidnight(String) {
Note:
In case of any issue please comment
//################### PGM START ###################
import java.util.Scanner;
public class TimeCalculation {
//function to calculate seconds after midnight
public static int secondsAfterMidnight(int h,int m,int
s,String t){
int result=-1;
//checking the value of hour minute
and second
if(h>12 || m>60 ||
s>60){
result=-1;
}
//if the time is in pm
else
if(t.equalsIgnoreCase("pm")){
int hour;
//if the time is
less than 12 clk then add value upto 12 clk
if(h<12)
hour=(12*60*60)+(h*60*60);
else
hour=(h*60*60);
int
minute=m*60;
int
second=s;
result=(hour+minute+second);
}
//if the time is in am
else
if(t.equalsIgnoreCase("am")){
int hour;
//if the time is
12 clk hours will be 0
if(h==12)
hour=0;
else
hour=(h*60*60);
int
minute=m*60;
int
second=s;
result=(hour+minute+second);
}
//return result
return result;
}
//main method
public static void main(String[] args) {
//result hold the number of seconds
after midnight
int result=0;
//requesting input from user
Scanner s=new
Scanner(System.in);
System.out.println("Enter the
time{HH:MM:SS(AM/PM)}: ");
String input=s.nextLine();
//splitting input based on :
String
values[]=input.split(":");
//getting the hours
int
hour=Integer.parseInt(values[0]);
//verifying minute is of length 2
and seconds+am is of length 4
if(values[1].length()!=2){
result=-1;
}else
if(values[2].length()!=4){
result=-1;
}else{
//getting the
minute value
int
minute=Integer.parseInt(values[1]);
//getting
am/pm
String
t=values[2].substring(2, 4);
if(!(t.equalsIgnoreCase("am") || t.equalsIgnoreCase("pm"))){
result=-1;
}else{
//getting the seoncd
int
second=Integer.parseInt(values[2].substring(0, 2));
//calculating seonds after midnight after
primary format check
result=secondsAfterMidnight(hour,minute,second,t);
}
}
System.out.println("Seconds after
midnight ="+result);
s.close();
}
}
//############## PGM END
#########################################
OUTPUT
##########
Need help creating a basic java string program using nested if/else, return loops or while loops...
1. Create a Time class which is designed to contain objects representing times in 12-hour clock format. Eg: 6:58am, 11:13pm, 12:00am (= midnight), 12:00pm (= noon). You can assume the user will enter times in the format given by the previous examples.Provide a default constructor, a set method which is given a String (eg: “9:42am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night” appropriate...
Can someone solve it with C plz Write a program that inputs a time from the console. The time should be in the format “HH:MM AM” or “HH:MM PM”. Hours may be one or two digits, for example, “1:10 AM” or “11:30 PM”. Your program should include a function that takes a string parameter containing the time. This function should convert the time into a four digit military time based on a 24 hour clock. For example, “1:10 AM” would...
1. Create a MyTime class which is designed to contain objects representing times in 12-hour clock format. Eg: 7:53am, 10:20pm, 12:00am (= midnight), 12:00pm (= noon). You can assume the user will enter times in the format given by the previous examples. Provide a default constructor, a set method which is given a String (eg: “7:53am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night”...
What to submit: your answers to exercises 1, and 2 and separate the codes to each question. Create a MyTime class which is designed to contain objects representing times in 12-hour clock format. Eg: 7:53am, 10:20pm, 12:00am (= midnight), 12:00pm (= noon). You can assume the user will enter times in the format given by the previous examples. Provide a default constructor, a set method which is given a String (eg: “7:53am”), a getHours method, a getMinutes method and a...
I need help implementing class string functions, any help would be appreciated, also any comments throughout would also be extremely helpful. Time.cpp file - #include "Time.h" #include <new> #include <string> #include <iostream> // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for the hours, // an integer variable for the minutes, and a char variable for...
JAVA 1. Create a MyTime class which is designed to contain objects representing times in 12-hour clock like 7:53am, 10:20pm, 12:00am (= midnight), 12:00pm (= noon). Provide a default constructor, a set method which is given a String (eg, “7:53am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night” appropriate (in your opinion) to the time. Please see the NOTE below concerning input validation and...
Time.cpp: #include "Time.h" #include <iostream> using namespace std; Time::Time(string time) { hours = 0; minutes = 0; isAfternoon = false; //check to make sure there are 5 characters if (//condition to check if length of string is wrong) { cout << "You must enter a valid military time in the format 00:00" << endl; } else { //check to make sure the colon is in the correct...
This is in Java The Problem: A common task in computing is to take data and apply changes (called transactions) to the data, then saving the updated data. This is the type of program you will write. You will read a file of flight reservation data to be loaded into an array with a max size of 20. A second file will hold the transactions that will be applied to the data that has been stored in the array....
I need help with a Python program that has error checking loops. Hour must be a number from 1 to 12. Minute and second must be numbers from 0 to 59. Also check whether “AM” or “PM” is entered. Every time an invalid value is entered, display an error message and ask the user to re-enter a valid value immediately. Functions are not covered yet and are not necessary. Thanks. The following is an example: Enter hour: 0 Hour must...
C++ really need help I will rate you well promise Write a program that will use a linked list to hold the following information: Date (int), Time (int), TZ (string), Size (Int) and Name (string) Follow the guidelines below: Records should be read from a file by the main program. And content should be stored in the doubly linked list or single linked list. After that, you can print separately dates or times or names ... or print hole line ...