Results 1 to 3 of 3
  1. #1
    Bravid2001 is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default 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

    Java 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;
    	}
    
    
    
    }
    USE TIME DATE CLASS


    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;
    	}
    }
    Attached Files Attached Files

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: making a Time class but Universal time always says 0

    The hour somehow gets changed to 0 when it goes to the helper method.
    At the end of main() you say

    Java Code:
    System.out.println("The universal time is " + myTime2.toUniversalTime() );
    Looking at that toUniversalTime() method we see:

    Java Code:
    // toUniversalTime method
    public String toUniversalTime()
    {
        return univHr + ":" + getMinute() + ":" + getSecond();
    }
    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.

    -----

    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.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: making a Time class but Universal time always says 0

    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.
    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.

    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

  1. Time lost during time measuring
    By Kobe in forum Advanced Java
    Replies: 6
    Last Post: 08-29-2011, 04:18 PM
  2. Replies: 3
    Last Post: 08-23-2011, 01:46 PM
  3. calculate time diff for particular time period
    By baktha.thalapathy in forum New To Java
    Replies: 2
    Last Post: 05-24-2010, 05:10 PM
  4. Class Time - represents time of day
    By verbazon in forum New To Java
    Replies: 1
    Last Post: 04-13-2009, 02:06 AM
  5. First time with abstract class
    By crazydeo in forum New To Java
    Replies: 0
    Last Post: 06-03-2008, 07:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •