Results 1 to 3 of 3
- 10-24-2011, 06:18 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 8
- Rep Power
- 0
making a Time class but Universal time always says 0
Hello all,
I'm have entered panick mode. I have spent 2 hours trying to fix this with no results.
I am making a Time and Date class. So far, I have only made the Time class, I will get to Date after I fix this problem. I have a UseTimeDate class to test out the Time class. In the Time class, I need to have a toStandardTime() and a toUniversalTime(). The universal time needs to be military time. My information (hour, minute, second) is being entered from a Scanner. I have made a helper method to convert the standard time to military time. I am only sending the hour to the helper method since this is the only thing that needs to change when converting to military time. The hour somehow gets changed to 0 when it goes to the helper method. I can't figure out why this is.
My next problem is when the user types in hour, minute , or second 1 through 9, I get a single digit character. If I were using a printf statement, I could change this using %2d. But, since I'm using getters, setters and toString, I can't figure out how to format the integer to give me two digits.
My code is below and I will attach both files. I will list the Time class 1st, followed by the UseTimeDate class. You will find the problem helper method on line 166 in the Time class. Thanks.
Time class
USE TIME DATE CLASSJava Code:/** * This class creates a Time class that holds information about the time * *@author Brad Stewart * * Created using text pad */ public class Time { //create data fields private int hour; private int minute; private int second; private String am_pm; private int univHour; /** * Time () - default constructor * * */ public Time() { hour = 24; minute = 0; second = 0; am_pm = ""; } /** * Time () - 2nd Constructor * *@param - newHour - int *@param - newMinute - int *@param - newSecond - int * */ public Time (int newHour, int newMinute, int newSecond) { hour = newHour; minute = newMinute; second = newSecond; } /** * Time () - 3rd Constructor * *@param - newHour - int *@param - newMinute - int *@param - newSecond - int *@param - newAM_PM - String * */ public Time (int newHour, int newMinute, int newSecond, String newAM_PM) { hour = newHour; minute = newMinute; second = newSecond; am_pm = newAM_PM; } /** * getHour() - returns the hour * * * @return hour - int * */ public int getHour() { return hour; } /** * setHour () - sets the hour * * @param newHour - int */ public void setHour (int newHour) { hour = newHour; } /** * getMinute() - returns the minute * * * @return minute - int * */ public int getMinute() { return minute; } /** * setMinute () - sets the minute * * @param newMinute - int */ public void setMinute (int newMinute) { minute = newMinute; } /** * getSecond() - returns the second * * * @return second - int * */ public int getSecond() { return second; } /** * setSecond () - sets the second * * @param newSecond - int */ public void setSecond (int newSecond) { second = newSecond; } /** * getAM_PM() - returns AM or PM * * * @return am_pm - String * */ public String getAM_PM() { return am_pm; } /** * setAM_PM () - sets AM or PM * * @param newAM_PM - int */ public void setAM_PM (String newAM_PM) { am_pm = newAM_PM; } // toStandardTime method public String toStandardTime() { return getHour() + ":" + getMinute() + ":" + getSecond() + getAM_PM() ; } // Invoke helper method int univHr = determineUniversalHour(getHour(), am_pm); // toUniversalTime method public String toUniversalTime() { return univHr + ":" + getMinute() + ":" + getSecond(); } // define helper method public static int determineUniversalHour(int hr, String amOrPm){ if (amOrPm == "PM" && hr < 12) { hr = hr + 12; } else if (amOrPm == "AM" && hr == 12) { hr = 00; } return hr; } }
Java Code:/** * This class is a client of the Time and Date class * *@author Brad Stewart * * Created using text pad */ import java.util.Scanner; public class UseTimeDate { public static void main (String[] args) { //Create variables int myHour = 0; int myMinute = 0; int mySecond = 0; String amOrPm = ""; int myDay = 0; int myMonth = 0; int myYear = 0; // Create a new Time object Time myTime2 =new Time(myHour, myMinute, mySecond, amOrPm); //create a new scanner Scanner input = new Scanner (System.in); System.out.println("To enter a time:"); // input hour System.out.println("Enter the hour"); myHour = input.nextInt(); // invoke helper method to get correct format of numbers int formatHr = format(myHour); // input minute System.out.println("Enter the minute"); myMinute = input.nextInt(); // input second System.out.println("Enter the second"); mySecond = input.nextInt(); // input AM or PM System.out.println("Enter AM or PM"); amOrPm = input.next(); //invoke setHour method myTime2.setHour(formatHr); //invoke setMinute method myTime2.setMinute(myMinute); //invoke setSecond method myTime2.setSecond(mySecond); //invoke setAM_PM method myTime2.setAM_PM(amOrPm); System.out.println("The standard time " + myTime2.toStandardTime() ); System.out.println("The universal time is " + myTime2.toUniversalTime() ); } // define helper method to get correct format public static int format(int time) { if ((time / 10) < 1) { time = (0) + (time) ; } return time; } }
- 10-24-2011, 07:21 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: making a Time class but Universal time always says 0
At the end of main() you sayThe hour somehow gets changed to 0 when it goes to the helper method.
Looking at that toUniversalTime() method we see:Java Code:System.out.println("The universal time is " + myTime2.toUniversalTime() );
The important thing about this method is that it does not invoke the helper method. It merely uses the value of univHr that was calculated back when the hour really was zero. I suggest you remove univHr as it is serving no useful purpose. Instead use the variable univHour which was declared but doesn't seem to be used anywhere. Call the helper method from within toUniversalTime() to make sure univHour gets a valid value.Java Code:// toUniversalTime method public String toUniversalTime() { return univHr + ":" + getMinute() + ":" + getSecond(); }
-----
You might want to consider whether you want a univHour variable either. The point is that its value goes "stale" every time one of the setter methods is called. So you might as well call the helper method whenever you need the 24hr version of the hour and use the value it returns without assigning to a variable.
- 10-24-2011, 07:55 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: making a Time class but Universal time always says 0
An important conceptual point is that int values (integers) do not have digits, numerals (written names of those values) are the things that (may) have digits. So, the getter, setter, or other methods that deal with int values need not worry themselves about digits. Note that nextInt() returns an int not a " single digit character" as you suggest.My next problem is when the user types in hour, minute, or second 1 through 9, I get a single digit character. If I were using a printf statement, I could change this using %2d. But, since I'm using getters, setters and toString, I can't figure out how to format the integer to give me two digits.
Digits only enter the picture when a method want to work with Strings. String has a format() method that works like printf(). Note, however, that "%2d" left pads with a space whereas you want to pad with a zero digit.
Java Code:public class Eg { public statiic void main(String[] args) { int val = 7; System.out.println(String.format("%d", val)); System.out.println(String.format("%2d", val)); System.out.println(String.format("%02d", val)); val = 12; System.out.println(String.format("%02d", val)); } }
Similar Threads
-
Time lost during time measuring
By Kobe in forum Advanced JavaReplies: 6Last Post: 08-29-2011, 03:18 PM -
JAVA Programmers Wanted - Full-Time and Part-Time Positions open
By javatrek2020 in forum Jobs OfferedReplies: 3Last Post: 08-23-2011, 12:46 PM -
calculate time diff for particular time period
By baktha.thalapathy in forum New To JavaReplies: 2Last Post: 05-24-2010, 04:10 PM -
Class Time - represents time of day
By verbazon in forum New To JavaReplies: 1Last Post: 04-13-2009, 01:06 AM -
First time with abstract class
By crazydeo in forum New To JavaReplies: 0Last Post: 06-03-2008, 06:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks