I'm working on this homework assignment from my professor that asks me to write a class to represent the time in hours and minutes. I have to be able to add hours to the time, Subtract hours from the time, Add minutes to the time, Subtract minutes from the time. I'm stuck on adding minutes to the time. If the time is 11:00 and I want to add 90 minutes. The Correct Output would be 12:30. But with my code, my output is always 12. Can someone please help me revise my code so adding the minutes will work? because I need it to figure out how to subtract minutes as well.
import java.util.Scanner;
public class Time {
private int hour;
private int minute;
public Time() {
}
public Time(int newHour, int newMinute) {
setTime(newHour, newMinute);
}
public void setTime(int h, int m) {
setHour(h);
setMinute(m);
}
public void setHour(int h) {
hour = ((h >= 0 && h < 24) ? h : 0);
}
public void setMinute(int m) {
minute = ((m >= 0 && m < 60) ? m : 0);
}
public int getHour() {
return hour;
}
public int getMinute() {
return minute;
}
public void addHours(int h) {
setHour(hour h);
if (hour == 12)
hour = 12;
else if (hour > 12)
hour = hour - 12;
}
public void subtractHours(int h) {
setHour(hour - h);
if (hour == 0)
hour = 12;
else if (hour > 12)
hour = hour - 12;
if (hour == 12)
hour--;
}
public void addMinutes(int m) {
setMinute(minute m);
if (m > 60)
m = m minute;
else if (m <= 60)
m = m minute;
addHours(m / 60);
}
public void subtractMinutes(int m) {
setMinute(minute - m);
if (m > 60)
m = minute - m;
else if (m <= 60)
m = minute - m;
if (m <= 60)
hour = minute / 60;
else if (m <= 60)
minute = minute % 60;
}
@Override
public String toString() {
return "DateAndTime [hour=" hour ", minute=" minute "]";
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the time");
System.out.print("Hours: ");
int hour = input.nextInt();
System.out.print("Minutes: ");
int minute = input.nextInt();
Time time = new Time(hour, minute);
boolean done = false;
while (!done) {
System.out.println("1. Add Hours");
System.out.println("2. Subtract Hours");
System.out.println("3. Add Minutes");
System.out.println("4. Subtract Minutes");
System.out.print("Choice: ");
switch (input.nextInt()) {
case 1:
time.addHours(input.nextInt());
break;
case 2:
time.subtractHours(input.nextInt());
break;
case 3:
time.addMinutes(input.nextInt());
break;
case 4:
time.subtractMinutes(input.nextInt());
break;
}
System.out.println("Time is: " time.toString());
}
}
}
CodePudding user response:
So, what you need to do is figure out the number of hours are in the specified time value and the figure out the how many minutes are remaining (once you take out the hours)
Luckily, this is relatively easy and relatively common, for example...
int time = 90;
int hours = time / 60;
int minutes = time % 60;
System.out.println(hours);
System.out.println(minutes);
will output
1
30
So, in 90 minutes, you have 1 hour and 30 minutes, this then needs to get added to your base values, however, a slightly better solution would be convert your existing value to minutes, add/subtract the minutes you want and then convert the values back to hours/minutes.
For example...
public void addMinutes(int m) {
int time = ((getHour() * 60) getMinute()) m;
int hours = time / 60;
int minutes = time % 60;
setHour(hours);
setMinute(minutes);
}
public void subtractMinutes(int m) {
addMinutes(-m);
}
So with those modifications, we can use something like...
Time time = new Time(11, 0);
time.addMinutes(90);
System.out.println(time);
which outputs...
DateAndTime [hour=12, minute=30]
CodePudding user response:
Look at this
setMinute(30 90);
->
minute = ((120 >= 0 && 120 < 60) ? m : 0);
minute becomes 0
Afterwards
if (m > 60)
m = m minute;
else if (m <= 60)
m = m minute;
does nothing to minute, only changes the locally scoped variable.
learn to use a debugger.