I am trying to make a clock program using Java, and I got everything to work properly except I cannot get the program to change the negative values to 0. I also cannot get the program to set the values of hours, minutes, and seconds to 0 if they are out of range. I have a tester program that I have to use and the T1 and T2 clock values are incorrect in my code. T1 should be 0:0:0 and T2 should be 0:0:0 as well. However, when I output my code it comes out as T1 being -3:-21:-30 and T2 is 24:60:60. I know there is something wrong with my code, but I can't find the issue, if anyone would be able to help me that would be greatly appreciated. Below is my code and then the second section is the tester code that I have to use.
public class Clock
{
// instance variables
private int hours;
private int minutes;
private int seconds;
public void setHours(int newHours) {
hours = newHours;
if (hours<0 || hours > 24) {
hours = 0;
}
}
public void setMinutes(int newMinutes) {
minutes = newMinutes;
if (minutes<0 || minutes > 60) {
minutes = 0;
}
}
public void setSeconds(int newSeconds) {
seconds = newSeconds;
if(seconds<0 || seconds > 60) {
seconds = 0;
}
}
/**
* Constructor for objects of class Clock
*/
public Clock(int newHour, int newMinute, int newSecond)
{
if (newHour > -1 || newHour < 24) {
this.hours = newHour;
}
else {
setHours(hours);
}
if (newMinute > -1 || newMinute < 60) {
this.minutes = newMinute;
}
else {
setMinutes(minutes);
}
if (newSecond > -1 || newSecond < 60) {
this.seconds = newSecond;
}
else {
setSeconds(seconds);
}
}
public int getHours() {
return hours;
}
public int getMinutes() {
return minutes;
}
public int getSeconds() {
return seconds;
}
public String toString() {
return hours + ":"+minutes+":"+seconds;
}
public void tick() {
seconds = seconds +1;
if(seconds >= 60)
{
minutes ++;
seconds = 0;
}
if(minutes >= 60)
{
hours++;
minutes = 0;
}
if(hours >=24)
{
hours = 0;
}
}
}
The next piece is the tester code.
public class ClockTest {
public static void main(String [] args){
//Create some clocks and print their times
Clock c1 = new Clock(-3,-21,-30);
System.out.println("T1: "+ c1);
c1 = new Clock(24,60,60);
System.out.println("T2: "+ c1);
c1 = new Clock(3,21,30);
System.out.println("T3: "+ c1);
//Tick the clock twice and print its time
c1.tick();
c1.tick();
System.out.println("T4: "+ c1);
c1 = new Clock(3,30,59);
c1.tick();
System.out.println("T5: "+ c1);
c1 = new Clock(3,59,59);
c1.tick();
System.out.println("T6: "+ c1);
c1 = new Clock(23,59,59);
c1.tick();
System.out.println("T7: "+ c1);
c1 = new Clock(0,0,1);
c1.tick();
System.out.println("T8: "+ c1);
c1 = new Clock(1,1,1);
c1.setHours(22);
c1.setMinutes(30);
c1.setSeconds(35);
System.out.println("T9: "+ c1);
System.out.println("T10: " + c1.getHours() + ":"
+c1.getMinutes() + ":" + c1.getSeconds());
}
}
Your condition is wrong.
When you write this:
if (newHour > -1 || newHour < 24) {
You really mean this:
if (newHour > -1 && newHour < 24) {
#nicomp is correct and you should also be using >= 24 and 60 instead of >. You might consider changing the constructor for Clock to
public Clock(int newHour, int newMinute, int newSecond) {
setHours(newHour);
setMinutes(newMinute);
setSeconds(newSecond);
}
and then do all of your validation in the set methods, instead of having some validation in the set methods and some in the constructor.
Related
So I have this assignment that is asking us to take in a String format of time in the order of HH:MM:SSAM or HH:SS:MMPM. The constraint is that it cannot run if it is in wrong format, let it be missing any form of the AM or PM, missing a number, or if it is in 24 Hour Format.
I have the whole idea down, however for my statements, it is giving me the error of:
bad operand types for binary operator '>'
incomparable types: String and int
Did I convert them improperly or am I doing something else wrong?
public static void main(String args[]) {
//Test Methods
String fullTime1 = "03:21:36AM";
secondsAfterMidnight(fullTime1);
}
public static int secondsAfterMidnight(String time) {
String[] units = time.split(":");
int hours = Integer.parseInt(units[0]);
int minutes = Integer.parseInt(units[1]);
int seconds = Integer.parseInt(units[2]);
int totalSeconds = 0;
if (units[0] > 12 || units[1] > 59 || units[2] > 59) { //1st Error applies to these three, units[0] > 12 units[1] > 59 units[2] > 59
return -1;
} else if (time.equalsIgnoreCase("AM") || time.equalsIgnoreCase("PM")) {
totalSeconds = (hours * 3600) + (minutes * 60) + (seconds);
} else if (time.equalsIgnoreCase("AM") && units[0] == 12) { //2nd Error applies to this units[0] == 12
totalSeconds = (minutes * 60) + (seconds);
} else {
return -1;
}
return totalSeconds;
}
You have already parsed the String values and saved them in the variables hours , minutes, seconds. Then you can use those for the check in the if.
Also the presence of AM?PM in the Integer.parseInt() will cause NumberFormatException to avoid it remove the String part from the number by using regex.
Also for checking the presence of AM/PM you can use String.contains.
Please check the reformatted code below:
public static int secondsAfterMidnight(String time) {
String[] units = time.split(":");
int hours = Integer.parseInt(units[0]);
int minutes = Integer.parseInt(units[1]);
int seconds = Integer.parseInt(units[2].replaceAll("[^0-9]", ""));
int totalSeconds = 0;
if (hours > 12 || minutes > 59 || seconds > 59) {
return -1;
} else if (time.contains("AM") || time.contains("PM")) {
totalSeconds = (hours * 3600) + (minutes * 60) + (seconds);
} else if (time.contains("AM") && hours == 12) {
totalSeconds = (minutes * 60) + (seconds);
} else {
return -1;
}
return totalSeconds;
}
Please note that even though you have converted the String to int, you are still comparing String with int. There would also be a RuntimeException when you do this:
int seconds = Integer.parseInt(units[2]);
As units[2] will contain 36AM. So you should be using substring() to remove the "AM/PM" part.
units is of type String and you are trying to compare it with an int hence the compile time error.
You need to convert the String to an int and then compare it, as shown below :
Integer.parseInt(units[0]) > 12
so on and so forth.
Also rather than re-inventing the wheel, you can make use of the already existing java-8's LocalTime to find the number of seconds for a particular time:
public static int secondsAfterMidnight(String time) {
LocalTime localTime = LocalTime.parse(time, DateTimeFormatter.ofPattern("hh:mm:ss a"));
return localTime.toSecondOfDay();
}
I haven't verified your logic to calculate the seconds, but this code has corrections:
public static int secondsAfterMidnight(String time) {
String[] units = time.split(":");
int hours = Integer.parseInt(units[0]);
int minutes = Integer.parseInt(units[1]);
int seconds = 0;
String amPm = "";
if ( units[2].contains("AM") || units[2].contains("PM") ||
units[2].contains("am") || units[2].contains("pm") ) {
seconds = Integer.parseInt(units[2].substring(0, 2));
amPm = units[2].substring(2);
}
else {
seconds = Integer.parseInt(units[2]);
}
int totalSeconds = 0;
if (hours > 12 || minutes > 59 || seconds > 59) {
return -1;
} else if (amPm.equalsIgnoreCase("AM") || amPm.equalsIgnoreCase("PM")) {
totalSeconds = (hours * 3600) + (minutes * 60) + (seconds);
} else if (amPm.equalsIgnoreCase("AM") && hours == 12) {
totalSeconds = (minutes * 60) + (seconds);
} else {
return -1;
}
return totalSeconds;
}
java.time
static DateTimeFormatter timeFormatter
= DateTimeFormatter.ofPattern("HH:mm:ssa", Locale.ENGLISH);
public static int secondsAfterMidnight(String time) {
try {
return LocalTime.parse(time, timeFormatter).get(ChronoField.SECOND_OF_DAY);
} catch (DateTimeParseException dtpe) {
return -1;
}
}
Let’s try it out using the test code from your question:
String fullTime1 = "03:21:36AM";
System.out.println(secondsAfterMidnight(fullTime1));
12096
This is the recommended way for production code.
Only if you are doing an exercise training string manipulation, you should use one of the other answers.
Link: Oracle tutorial: Date Time explaining how to use java.time.
I am attempting to add an increment method for a time class I have. I have modified the original code to only have one private integer "totalseconds" that reads the amount of seconds since midnight. That part of the code works fine but now I am trying to create a method that increments the setSecond, setMinute, and setHour. The problem (I believe) I am having is that these set methods all receive their values from totalseconds not int hour, int minute, int second as before. When I run the test for it the way I have it now I get errors at lines 36, 75 (when attempted to increment seconds via Tick method) and 101 (again only when incrementing seconds via Tick method). I have attached both the Time2 class and Time2Test app for reference.
public class Time2 {
private int totalseconds;
//no argument constructor
public Time2()
{
this(0,0,0); //invoke constructor with three arguments default to 0
}
//constructor with hour supplied minute and second default to 0
public Time2(int hour)
{
this(hour, 0, 0); //invoke constructor with 3 args
}
//constructor with hour and minute supplied seconds default to 0
public Time2(int hour, int minute)
{
this(hour, minute, 0); //invoke constructor with 3 args
}
//Time2 constructor with hour minute and second supplied also tests
public Time2(int hour, int minute, int second)
{
this.totalseconds = (hour * 3600);
this.totalseconds += (minute * 60);
this.totalseconds += (second);
}
public Time2(Time2 time)
{
//invoke constructor with 2 args
this(time.getHour(), time.getMinute(), time.getSecond());
}
// SET and GET methods start here, also Universal time conversion and check
public void setTime(int hour, int minute, int second)
{
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("Hour must be 0-23");
if (minute < 0 || minute >= 59)
throw new IllegalArgumentException("Minute must be 0-59");
if (second < 0 || second >= 59)
throw new IllegalArgumentException("Hour must be 0-59");
this.totalseconds = (hour * 3600);
this.totalseconds += (minute * 60);
this.totalseconds += second;
}
//validate and set hour
public void setHour(int hour)
{
if (hour < 0 || hour >= 23)
throw new IllegalArgumentException("Hour must be 0-23");
this.totalseconds = (hour * 3600);
}
//validate and set minute
public void setMinute(int minute)
{
if (minute < 0 || minute >= 59)
throw new IllegalArgumentException("Minute must be 0-59");
this.totalseconds += (minute * 60);
}
//validate and set second
public void setSecond(int second)
{
if (second < 0 || second >= 59)
throw new IllegalArgumentException("Second must be 0-59");
this.totalseconds += second;
}
//Get Methods start here
//Get hour
public int getHour()
{
return totalseconds / 3600;
}
//get minute
public int getMinute()
{
return (totalseconds - (3600 * getHour())) / 60;
}
//get second
public int getSecond()
{
return totalseconds - (3600 * getHour())- (60 * getMinute());
}
//Assignment 1-2 tick methods start here.
public void Tick()
{
setSecond(totalseconds ++);
if (totalseconds >= 59) incrementMinute();
}
public void incrementMinute()
{
setMinute( totalseconds ++);
if ( totalseconds >= 59) incrementHour();
}
public void incrementHour()
{
setHour ( this.totalseconds ++);
}
//convert our string to universal format (HH:MM:SS)
public String ToUniversalString()
{
return String.format(
"%02d:%02d:%02d", getHour(), getMinute(), getSecond());
}
//conver to standard format (H:MM:SS AM or PM)
public String toString()
{
return String.format("%d:%02d:%02d %s",((getHour() == 0 || getHour() ==
12) ? 12 : getHour() % 12), getMinute(), getSecond(), (getHour()
< 12 ? "AM" : "PM"));
}
}//end class Time2
public class Time2Test
{
public static void main(String[] args)
{
Time2 t1 = new Time2(); //00:00:00
Time2 t2 = new Time2(2); //02:00:00
Time2 t3 = new Time2(21, 34); //21:34:00
Time2 t4 = new Time2(12, 25, 42); //12:25:42
Time2 t5 = new Time2(t4); //12:25:42
System.out.println("Constructed with:");
displayTime("t1: all default arguments", t1);
displayTime("t2: hour specified; defaults for minute and second", t2);
displayTime("t3: hour and minute supplied second defaulted", t3);
displayTime("t4: hour minute and second supplied", t4);
displayTime("t5: Time2 object t4 specified", t5);
//attempt to initialize t6 with invalid args
try
{
Time2 t6 = new Time2(27,74,99); //all invalid values
}
catch (IllegalArgumentException e)
{
System.out.printf("%nException while initializing t6: %s%n",
e.getMessage());
}
System.out.println("Time before increment minute method");
System.out.printf("%s\n", t4.toString());
t4.Tick();
t4.incrementMinute();
t4.incrementHour();
System.out.println("Time after increment minute method");
System.out.printf("%s\n", t4.toString());
}
//display Time2 object in 24 hour and 12 hour formats
private static void displayTime(String header, Time2 t)
{
System.out.printf("%s%n %s%n %s%n", header, t.ToUniversalString(),
t.toString());
}
}
I am really new to programming. I am doing an assignment for my intro to Java. In my assignment, we need to find the total number of seconds since midnight and changed this number to hours, minutes and seconds to show the current time. I have small problem. when I test my code, the totalseconds show 0! any help would be appreciated. Sorry the code is a chaos
package clock;
import java.text.DecimalFormat;
import java.util.Calendar; // to get current time
public class Clock {
public static int totalseconds;
public static int seconds;
public static int minutes;
public static int hours;
public static int test;
public Clock(int hours, int minutes, int seconds ) {
setHours(hours);
setMinutes(minutes);
setSeconds(seconds);
}
// use current time
public Clock() {
Calendar c = Calendar.getInstance();
long now = c.getTimeInMillis();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
long passed = now - c.getTimeInMillis();
long secondsPassed = passed / 1000;
totalseconds = (int) secondsPassed;
}
public void tick() {
addSecond();
}
private void addSecond() {
seconds = totalseconds%60;
}
private void addMinute() {
minutes = totalseconds/60 % 60;
}
private void addHour() {
hours = totalseconds / 3600;
}
public String toString() {
DecimalFormat f = new DecimalFormat("00");
return f.format(hours) + ":" + f.format(minutes) + ":" + f.format(seconds);
}
public int getHours() {
return hours;
}
public int getMinutes() {
return minutes;
}
public int getSeconds() {
return seconds;
}
// the total number of minutes sinces midnight
public int getTotalMinutes() {
return totalseconds / 60 % 60;
}
// the total number of seconds since midnight
public int getTotalSeconds() {
return totalseconds;
}
public void setHours(int hours) {
if (hours > 23 || hours < 0) {
this.hours = 0;
}
else
this.hours = hours;
}
public void setMinutes(int minutes) {
if (minutes > 59 || minutes < 0)
this.minutes = 0;
else
this.minutes = minutes;
}
public void setSeconds(int seconds) {
if (seconds > 59 || seconds < 0)
this.seconds = 0;
else
this.seconds = seconds;
}
// reset hours, minutes and seconds to zero
public void reset() {
hours = minutes = seconds = 0;
}
public static void main(String [] args){
System.out.println("this is total seconds " + test + totalseconds );
}
}
Change main to this.
public static void main(String [] args){
Clock clock = new Clock();
System.out.println("this is total seconds " + test + totalseconds );
}
New you make an instance of Clock and the constructor is called, where all your magic happens.
I agree with what #shmosel suggested. There seems to be confusion in how to use instance variable. Further, I think you should take advantages of Java 8 methods for your time part.
class Clock {
private int totalseconds;
private int seconds;
private int minutes;
private int hours;
int test;
public Clock(int hours, int minutes, int seconds) {
setHours(hours);
setMinutes(minutes);
setSeconds(seconds);
}
// use current time
public Clock() {
LocalTime now = LocalTime.now(ZoneId.systemDefault());
totalseconds = now.toSecondOfDay();
}
public void tick() {
addSecond();
}
private void addSecond() {
seconds = totalseconds % 60;
}
private void addMinute() {
minutes = totalseconds / 60 % 60;
}
private void addHour() {
hours = totalseconds / 3600;
}
public String toString() {
DecimalFormat f = new DecimalFormat("00");
return f.format(hours) + ":" + f.format(minutes) + ":" + f.format(seconds);
}
public int getHours() {
return hours;
}
public int getMinutes() {
return minutes;
}
public int getSeconds() {
return seconds;
}
// the total number of minutes since midnight
public int getTotalMinutes() {
return totalseconds / 60 % 60;
}
// the total number of seconds since midnight
public int getTotalSeconds() {
return totalseconds;
}
public void setHours(int hours) {
if (hours > 23 || hours < 0) {
this.hours = 0;
} else
this.hours = hours;
}
public void setMinutes(int minutes) {
if (minutes > 59 || minutes < 0)
this.minutes = 0;
else
this.minutes = minutes;
}
public void setSeconds(int seconds) {
if (seconds > 59 || seconds < 0)
this.seconds = 0;
else
this.seconds = seconds;
}
// reset hours, minutes and seconds to zero
public void reset() {
hours = minutes = seconds = 0;
}
}
public class SecondsSinceMidnight {
public static void main(String[] args) {
Clock myClock = new Clock();
System.out.println("Total seconds that elapsed since midnight:" + myClock.getTotalSeconds());
System.out.println("Converted to Minutes: " + myClock.getTotalMinutes());
}
}
Below is a class that represents time using hours and minutes (seconds are not represented).
public class ClassTime {
public static int hour;
public static int minute;
public static String amPm;
//Initializes the object using specified parameter values.
public ClassTime(int hour, int minute, String amPm){
//hour value between 1 and 12, both inclusive.
boolean validHour = false;
if (hour >= 1 && hour <= 12){
validHour = true;
this.hour = hour;
} else {
throw new IllegalArgumentException("Invalid hour value");
}
//minute value between 0 and 59, both inclusive.
boolean validMinute = false;
if (minute >= 0 && minute <= 59){
validMinute = true;
this.minute = minute;
} else {
throw new IllegalArgumentException("Invalid minutes value");
}
//amPm is either "am" or "pm".
if (amPm.equalsIgnoreCase("am")){
this.amPm = amPm;
} else if (amPm.equalsIgnoreCase("pm")){
this.amPm = amPm;
} else {
throw new IllegalArgumentException("Invalid am/pm value");
}
}
/*
* Returns a string using the format "hour:minutes am" or "hour:minutes pm".
* A single space is used in between minutes and am/pm. The minutes always
* appear with two digits (e.g., 0 minutes will be "00").
*/
public String toString(){
String toBeReturned = "hour:" + String.format("%02d", this.minute) +
" " + amPm;
return toBeReturned;
}
/*
* Compares two time objects. Two time objects are considered equal if
* they represent the same time.
*/
public boolean equals(ClassTime obj){
boolean equal = false;
if (obj.minute == this.minute && obj.hour == this.hour &&
obj.amPm.equalsIgnoreCase(this.amPm)){
equal = true;
}
return equal;
}
/*
* Compares two time objects. Returns -1 if the current object is a time
* that precedes the time parameter, 0 if the current object and the time
* parameter represent the same time, and 1 if the current object represents
* a time that is after the time parameter.
*/
public int compareTo(ClassTime obj){
int returnNum = 2;
if(this.amPm.equalsIgnoreCase("am") && obj.amPm.equalsIgnoreCase("pm")){
returnNum = -1;
} else if (this.amPm.equalsIgnoreCase("pm") &&
obj.amPm.equalsIgnoreCase("am")){
returnNum = 1;
} else if (this.amPm.equalsIgnoreCase(obj.amPm)){
if (this.hour < obj.hour){
returnNum = -1;
} else if (this.hour > obj.hour){
returnNum = 1;
} else if (this.hour == obj.hour){
if (this.minute < obj.minute){
returnNum = -1;
} else if (this.minute > obj.minute){
returnNum = 1;
} else if (this.minute == obj.minute){
returnNum = 0;
}
}
}
return returnNum;
}
/*
* Returns a new time object corresponding to the time we will have after
* increasing the time parameter by the specified number of minutes.
*/
public static ClassTime increaseByMinutes(ClassTime obj, int minutes){
//create variables that monitor changes in minutes, hours, and amPm.
int minValue = obj.minute + minutes;
int hourValue = obj.hour;
String amPmValue = obj.amPm;
/*
* increments hour and minutes if the total minutes is below two hours,
* but greater tan or equal to 1 hour.
*/
if(minValue > 59 && minValue < 120){
minValue = minValue % 60;
hourValue = hourValue + 1;
if (hourValue > 12){
hourValue = hourValue % 12;
} else if(hourValue == 12 && amPmValue.equalsIgnoreCase("am")){
amPmValue = "pm";
} else if (hourValue == 12 && amPmValue.equalsIgnoreCase("pm")){
amPmValue = "am";
}
/*
* Increment for when the total amount of minutes is greater
* or equal to 2 hours.
*/
} else if (minValue > 119){
for(int i = 0; i <= (minValue/60); i++){
hourValue++;
if(hourValue > 12){
hourValue = hourValue % 12;
} else if (hourValue == 12 && amPmValue.equalsIgnoreCase("am")){
amPmValue = "pm";
} else if (hourValue == 12 && amPmValue.equalsIgnoreCase("pm")){
amPmValue = "am";
}
}
}
/*
* Create a new ClassTime object with the found values of minutes, hours,
* and amPm Value. This is what will be returned.
*/
ClassTime newObject = new ClassTime(hour, minute, amPm);
newObject.minute = minValue;
newObject.hour = hourValue;
newObject.amPm = amPmValue;
return newObject;
}
public static void main(String[] args) {
}
}
I don't know how to test the constructor or the methods in a JUnit test case. So far the only thing I can come up with for the constructor is:
import static org.junit.Assert.*;
import org.junit.Test;
public class JunitTests {
#Test
public void testClassTime() {
ClassTime object1 = new ClassTime(3, 30, "pm");
}
}
How would I finish doing a test for the constructor and maybe just 1 method. Please don't do all of them.
I suggest you read up on unit testing, your question is a bit too broad.
Basically the idea is that given some situation, when a thing happens, then some conditions should be true. (I personally add comments to my tests to help me think in those terms.)
Here's an abstract example:
Given bread and a toaster
When I put the bread in the toaster and get it out when it's done
Then it should be toasted
Look up JUnit's assertion library to help find good ways to assert conditions. When looking up JUnit examples you'll usually see things like assertTrue(...) or assertNotNull(...), these are static methods from the Assert class, I mention this because you seem like a novice and I don't want you to get confused by examples. Usually all these methods are imported statically in the examples.
What you have in your test case currently is the given, a when could be doing toString() (String actual = object1.toString();). Your then clause would be asserting that the String returned equals what it should, which is probably "3:30 PM" (assertEquals("3:30 PM", actual);). I didn't design your code, so I don't know if that's correct, you may expect it to return `"Hello, World!" In any case, I'd run this one first ;)
This is a method that i made for my comp sci class:
public Time (int y, int x)
{
minute = x;
hour = y;
if (minute>59 || minute<0)
{
minute = 0;
}
if (hour>=24 || hour<0)
{
hour=0;
}
}
void addOne()
{//Adds one to the minute. Still in military time
minute++;
if (minute == 60) //Makes minute never exceeds 59 minutes. Hour is added 1 and minute is set to 0
{
minute = 0;
hour++;
if (hour==24)
{
hour=0;
}
}
}
public String convert()
{//Converts to standard time with am and pm
String timer = null;
int counter = 0;
if (hour<12)
{
if (hour<9)
{
if (minute>9)
{
timer = "0"+hour+":"+minute+" A.M.";
}
if (minute<10)
{
timer = "0"+hour+":0"+minute+" A.M";
}
}
if (hour>9)
{
if (minute>9)
{
timer = ""+hour+":"+minute+" A.M.";
}
if (minute<10)
{
timer = ""+hour+":0"+minute+" A.M";
}
}
}
if (hour ==12)
{
if (minute>9)
{
timer = ""+hour+":"+minute+" P.M.";
}
if (minute<10)
{
timer = ""+hour+":0"+minute+" P.M";
}
}
if (hour>12)
{
hour-=12;
if (hour ==12)
{
if (minute>9)
{
timer = ""+hour+":"+minute+" A.M.";
}
if (minute<10)
{
timer = ""+hour+":0"+minute+" A.M";
}
}
if (hour<9)
{
if (minute>9)
{
timer = "0"+hour+":"+minute+" P.M.";
}
if (minute<10)
{
timer = "0"+hour+":0"+minute+" P.M";
}
}
if (hour>9)
{
if (minute>9)
{
timer = ""+hour+":"+minute+" P.M.";
}
if (minute<10)
{
timer = ""+hour+":0"+minute+" P.M";
}
}
}
return timer;
}
And this is the code that is suppose to run:
Time time7 = new Time(23,59);
System.out.println("\ntime7: " + time7);
System.out.println("convert time7: " + time7.convert());
time7.addOne();
System.out.println("increment time7: " + time7);
System.out.println("convert time7: " + time7.convert());
I get the output as:
time7: 2359
convert time7: 11:59 P.M.
increment time7: 1200
convert time7: 12:00 P.M
But I need the output to be:
time7: 2359
convert time7: 11:59 P.M.
increment time7: 0000
convert time7: 12:00 A.M
All other parts to the program works just fine, until i get to time7.
Starting with hour set to 23, then after this code:
if (hour>12)
{
hour-=12;
...
what is the value of hour?
I'm afraid your code is needlessly complex, even if you fix the bug. Look at the String.format() method for a much better way of formatting numbers for output that does not require you to handle all those "special cases" separately.
After you increment and before you convert, you could add:
hour %= 24;
If hour is 24, it would then become 0.