Time method help: I need help in writing a specific time method. Using all given data from the object class. public class Exercise4_16 extends Object { // Write a Time method public int timeInMinutes(): // The executor tells the total number of minutes that have passed since midnight. private int itsHour; private int itsMin; public int timeInMinutes(); { super(); itsHour = hour; itsMin = minute; } please help if you have time.
object class: public class Time extends Object { private int itsHour; private int itsMin; /** Create an object for the given hour and minute. If min is negative, adjust the values to * Make 0 <= min < 60. */ public Time (int hour, int min) // constructor { super(); itsHour = hour; for (itsMin = min; itsMin < 0; itsMin = itsMin + 60) itsHour--; } //=========================== /** Return the time expressed in millitary time. */ public String toString() { if (itsHour < 10) return ("0" + itsHour) + itsMin; else return ("" + itsHour) + itsMin; } //========================== public Time add (Time that) { Time valueToReturn = new Time (this.itsHour + that.itsHour, this.itsMin + that.itsMin); if (valueToReturn.itsMin >= 60) { valueToReturn.itsMin = valueToReturn.itsMin - 60; valueToReturn.itsHour++; } valueToReturn.itsHour = valueToReturn.itsHour % 24; return valueToReturn; } //===========================
Well, what sort of problem are you having with it? Also, if you put ``` above and below a chunk of code it will work better. See, if I copy and paste what you did, I get: public class Exercise4_16 extends Object { // Write a Time method public int timeInMinutes(): // The executor tells the total number of minutes that have passed since midnight. private int itsHour; private int itsMin; public int timeInMinutes(); { super(); itsHour = hour; itsMin = minute; } That is just hard to work with. If you use the code formatting system it would look more like this: ``` /** A test class I use to try things out. */ public class Test { public static void main(String[] args) { String isTrue; System.out.println("Setting isTrue to Yes"); isTrue="Yes"; if (isTrue.equals("Yes")){ System.out.println("WooWoo, yes worked!"); } else { System.out.println("This should only happen if it is not yes."); } System.out.println("\nSetting isTrue to No"); isTrue="No"; if (isTrue.equals("Yes")){ System.out.println("WooWoo, yes worked!"); } else { System.out.println("This should only happen if it is not yes."); } System.out.println("\nSetting isTrue to shazbat"); isTrue="shazbat"; if (isTrue.equals("Yes")){ System.out.println("WooWoo, yes worked!"); } else { System.out.println("This should only happen if it is not yes."); } } } ```
I see, but I don't understand what my instructions in the // comment are asking me to do. I keep looking at the object class, and things confuse me. I feel like I need someone to help me make a list of what I have to do exactly in order to get this exercise done.
// Write a Time method public int timeInMinutes(): A method means it is inside this class. They have given you the skeleton. It is public, it returns an integer, and it is named timeInMinutes. So, what is it supposed to do? // The executor tells the total number of minutes that have passed since midnight. OK, so it needs to find the present time and compare that to midnight and return the number of minutes that have passed since midnight.
Join our real-time social learning platform and learn together with your friends!