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...
in C++ Write a program that converts a time in 12-hour format to 24-hour format. The program will prompt the user to enter a time in HH:MM:SS AM/PM form. (The time must be entered exactly in this format all on one line.) It will then convert the time to 24 hour form. You may use a string type to read in the entire time at once including the space or you may choose to use separate variables for the hours,...
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...
For this project you will be writing up a simple Clock program to keep track of time. SimpleClock.java - contains your implementation of the SimpleClock class. You will need to provide the code for a constructor as well as the mutator methods set and tick and the accessor method toString. Look at the comments for each method to see how they should be implemented - the trickiest method is probably tick, which requires that you deal with the changing of...
This lab will create a digital clock based on nested FOR loops for hours, minutes, and seconds, and a WHILE loop for doing over and over so that your clock works for all year long without interruption. Note: If a group of students is interested, we could create a real digital wall-mounted clock to be placed in our classroom, so I can keep track of the time during my lectures. The actual clock would be based on the LED Strip...
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...