Intro to classes, objects, and methods

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2034 Views - Last Post: 12 February 2012 - 08:01 PM Rate Topic: -----

#1 jruss1212  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Intro to classes, objects, and methods

Posted 09 February 2012 - 09:37 PM

Hi I'm working on a program for an intro to java class and this is the our first one in creating classes. The program is to prompt the user for the time (eg. 12 22 34). It will print it out in standard time and universal time. I have most of the program done but I'm having trouble with my "private String twoDigit(int value)" class. Basically this is to make sure the minutes and seconds are two digits. So if either is less than 10 it should concat a 0(eg. 5 = 05). I am not sure how to use "Integer.toString" which we are supposed to for this, also we are to call the "twoDigits" from the "public String toString" class and the "public toUniversal" class. Aside from this issue I'm also just wondering if I have this set up correctly since it is the first time I'm introducing classes and objects. The program has been working the way it should but I think that doesn't necessarily mean it is right. Here is the code:

 /**
 * MyTimeDemo - tests the MyTime class
 *
 * The MyTime class must be compiled before this class can be compiled.
 */
import java.util.Scanner;

	public class Assignment3
	{
	    // Instance variables holding the time (in 24 hour universal form).
	    private int mHour;      // 0 - 23
	    private int mMinute;    // 0 - 59
	    private int mSecond;    // 0 - 59

	    /**
	     * Sets the time value for this MyTime object. If *any* of the parameter
	     * values are out of range, the time is set to 00:00:00.
	     *
	     * @param hour      the hour (0 to 23).
	     * @param minute    the minute (0 to 59).
	     * @param second    the second (0 to 59).
	     */
	    public void setTime(int hour, int minute, int second)
	    {

	        if(hour >= 0 && hour <= 23)
	        {
	        	mHour = hour ;
	        }
	        else
	        {
	        	mHour = 00 ;
	        }
	        if(minute >= 0 && minute <= 59)
	        {
	        	mMinute = minute ;
	        }
	        else
	        {
	        	mMinute = 00 ;
	        }
	        if(second >= 0 && second<= 59)
	        {
	        	mSecond = second ;
	        }
	        else
	        {
	        	mSecond = 00 ;
	        }
	        if(hour <0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59)
	        {
	        	mHour = 00;
	        	mMinute = 00;
	        	mSecond = 00;
	        }
	    }

	    /**
	     * Returns the hour value represented by this MyTime.
	     *
	     * @return the hour value of this object as an int.
	     */
	    public int getHour()
	    {
	        return mHour;  // TODO - replace
	    }

	    /**
	     * Returns the minute value represented by this MyTime.
	     *
	     * @return the minute value of this object as an int.
	     */
	    public int getMinute()
	    {
	        return mMinute;  // TODO - replace
	    }

	    /**
	     * Returns the second value represented by this MyTime.
	     *
	     * @return  the second value of this object as an int.
	     */
	    public int getSecond()
	    {
	        return mSecond;  // TODO - replace
	    }

	    /**
	     * Compares this MyTime object against another MyTime object.
	     * The result is true if and only if the argument is not null
	     * and represents a time that is the same time as represented
	     * by this object
	     *
	     * @param  other    the object to be compared.
	     * @returns  true if both objects represent the same time value,
	     *           false otherwise.
	     */
	    public boolean equals(Assignment3 other)
	    {
	        if(mHour == other.mHour && mMinute == other.mMinute && mSecond == other.mSecond)
	        {
	        	return true;
	        }

	        return false;
	    }

	    /**
	     * Returns the time of this MyTime object in HH:MM:SS AM/PM form.
	     *
	     * @return the time value represented by this object as a String
	     *         formatted as HH:MM:SS AM/PM .
	     */
	    public String toString()
	    {
	        String result = "";
	        result = mHour + ":" + mMinute + ":" + mSecond ;
	        int newHour;
	        
	        if(mHour >= 1 && mHour <= 11)
	        {
	        	result += " AM";
	        }
	        else if(mHour >= 13 && mHour <= 23)
	        {
	        	newHour = mHour - 12;
	        	result = newHour + ":" + mMinute + ":" + mSecond + " PM";
	        }
	        else
	        {
	        	newHour = 12;
	        	result = newHour + ":" + mMinute + ":" + mSecond + " AM";
	        }
	   
	       
	        
	        // TODO - add code to format the time in the instance variables
	        // into HH:MM:SS AM/PM time (hour ranges from 1 to 12, AM or PM)
	        // as a string and return it.

	        return result;
	    }

	    /**
	     * Returns the time of this MyTime object in 24 hour universal form.
	     *
	     * @return the time value represented by this object as a String
	     *         formatted as HH:MM:SS universal time.
	     */
	    public String toUniversalString()
	    {
	        String result = "";
	      
	        if(mHour >= 0 && mHour <= 11)
	        {
	        	result = mHour + ":" + mMinute + ":" + mSecond; 
	        }
	        else if(mHour >= 12 && mHour <= 23)
	        {
	        	
	        	result = mHour + ":" + mMinute + ":" + mSecond;
	        }
	      

	        // TODO - add code to format the time in the instance variables
	        // into HH:MM:SS universal time (hour ranges from 0 to 23)
	        // as a string and return it.

	        return result;
	    }

	    /**
	     * Returns the parameter value as a two digit String (with leading 0 if needed)
	     *
	     * @return the parameter 'value' formatted as a two digit String
	     *         (with leading zero if needed).
	     */
	    private String twoDigits(int value)
	    {
	        String result = "";
	        Integer.toString(mMinute);
	        if(mMinute < 10)
	        {
	        	
	        	result = mHour + ":" + mMinute + ":" + mSecond ;
	        }

	        // TODO - Use the Integer.toString(int) method to convert
	        // 'value' to String format, and add a leading "0" if
	        // value is less than 10 in order to return a string with
	        // two digits.

	        return result;
	    }

	
    /**
     * main - Assignment 4
     */
    public static void main (String[] args)
    {
        Scanner sc = new Scanner(System.in);

        // Create a MyTime object and set to noon (12 PM).
        Assignment3 noon = new Assignment3();
        noon.setTime(12, 0, 0);

        Assignment3 wakeup = new Assignment3();
        wakeup.setTime(6, 30, 0);

        // Ask for time to be input, then create a new MyTime and set the time
        int inHour, inMinute, inSecond;
        do {
            System.out.println("Enter hour minute and second (-1 to exit)");
            inHour = sc.nextInt();
            if (-1 == inHour)
                break;
            inMinute = sc.nextInt();
            inSecond = sc.nextInt();
            System.out.println();
            System.out.println("Values read were: " + inHour + " " +
                               inMinute + " " + inSecond);

            // Create a new MyTime object
            Assignment3 t = new Assignment3();

            // Set the time to the time read from the keyboard
            t.setTime(inHour,inMinute,inSecond);

            System.out.println(t.toString());
            System.out.println(t.toUniversalString());
            System.out.println("hour is " + t.getHour() + "  minute is " +
                                t.getMinute() + "  second is " + t.getSecond());

            // Test if the time read matches the 'noon' MyTime object's value.
            if (t.equals(noon))
                System.out.println("Matches noon time");
            // Test if the time read matches the 'wakeup' MyTime objet's value
            if (t.equals(wakeup))
                System.out.println("It's time to wakeup!");

        } while (-1 != inHour);

        // Keep console window alive until 'enter' pressed (if needed).
        System.out.println();
        System.out.println("Done - press enter key to end program");
        String junk = sc.nextLine();
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Intro to classes, objects, and methods

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Intro to classes, objects, and methods

Posted 09 February 2012 - 10:27 PM

Ok. First let's get your nomenclature correct. The class is Assignment3. toString(), toUniversal(), setTime(), etc... are all methods. An object is an instance of your class, such as noon or wakeup.

Next, You don't need the last if statement in your setTime() method. That is what your else clauses do in your previous if statements, so it is redundant.

Now for your twoDigit method, you might want to consider having it work on one single value instead of the whole time. For example, you input a 5 to the method, and it returns the string "05". You were on the right track
private String twoDigits(int value) {
   // Try setting the result to the string value of 'value'
   String result = Integer.toString(value);

   // Now if value < 10, add a leading 0 to result

   // return result
}

Was This Post Helpful? 0
  • +
  • -

#3 jruss1212  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Re: Intro to classes, objects, and methods

Posted 10 February 2012 - 05:46 AM

I set up the twoDigits method the way you explained but I don't know how to call the "twoDigits" from the "public String toString" method and the "public toUniversal" method. This is what the twoDigits method looks like now:

 private String twoDigits(int value)
	    {
	        String result = "";
	        result = Integer.toString(value);
	        if(value < 10)
	        {
	        	
	        	result = "0" + result ;
	        }

	       	        return result;
	    }

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,479
  • Joined: 27-December 08

Re: Intro to classes, objects, and methods

Posted 10 February 2012 - 06:09 AM

If all these methods are located in the same class, just invoke String result = twoDigits(someIntValue);.
Was This Post Helpful? 0
  • +
  • -

#5 jruss1212  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Re: Intro to classes, objects, and methods

Posted 10 February 2012 - 10:38 AM

How do I incorporate the twoDigits method in the main method? Do I have to somehow put it in the String toString method and toUniversal method?
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Intro to classes, objects, and methods

Posted 10 February 2012 - 10:42 AM

No you use it as a method call like macosxnerd101 said.
String result = twoDigits(someIntValue);

If a method is in the same class then you can call them in other methods without needing a calling object because you have direct access to it.
Was This Post Helpful? 0
  • +
  • -

#7 jruss1212  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Re: Intro to classes, objects, and methods

Posted 10 February 2012 - 10:57 AM

I'm still having trouble with it, I'm really not sure what you mean. Are you saying to put the "String result = twoDigits(someIntValue);" in the main method? I'm not even sure if I have the twoDigits method set up correctly to concat a zero to the minute if its less than 10. If it is correct I don't really understand what I do for the main method. This is what I got for the twoDigits:

private String twoDigits(int value)
	    {
	        String result = "";
	        Integer.toString(value);
	        if(value < 10)
	        {
	
	        	result = mHour + ":" + "0" + mMinute + ":" + mSecond;
	        }

	        return result;
	    }

Was This Post Helpful? 0
  • +
  • -

#8 jruss1212  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Re: Intro to classes, objects, and methods

Posted 10 February 2012 - 12:04 PM

Someone, anyone.........
Was This Post Helpful? 0
  • +
  • -

#9 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Intro to classes, objects, and methods

Posted 10 February 2012 - 12:26 PM

That will return a blank string if it is already 2 digits. So you need to make an else statement to that.
String result = "";
if(value <10)
    result = mHour + ":" + "0" + mMinute + ":" + mSecond;
else result = mHour + ":" + mMinute + ":" + mSecond;
return result;



Because you never assign the line Integer.toString(value); to a variable it is actually doing nothing. So I took it out of the code.

When you have an int, if it is being added onto a String at all, it will automatically become a String representation, it is called being concatnated.

Ex.
int value = 10;
String newVal = value; // This will throw an error.
String aNewVal = value + ""; // This will work because "" is technically a String so it got concatnated.



Make sense?

I am sure that jon.kiparsky will elaborate or prove me wrong with something! :)

This post has been edited by Fuzzyness: 10 February 2012 - 12:27 PM

Was This Post Helpful? 0
  • +
  • -

#10 jruss1212  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Re: Intro to classes, objects, and methods

Posted 10 February 2012 - 12:59 PM

So what would I put in the main method? It is outputting the toString method and the toUniversal method. So basically what do I need to put in main for the two methods to use the twoDigits method? Here is main:

 public static void main (String[] args)
    {
        Scanner sc = new Scanner(System.in);

        // Create a MyTime object and set to noon (12 PM).
        Assignment3 noon = new Assignment3();
        noon.setTime(12, 0, 0);

        Assignment3 wakeup = new Assignment3();
        wakeup.setTime(6, 30, 0);

        // Ask for time to be input, then create a new MyTime and set the time
        int inHour, inMinute, inSecond;
        do {
            System.out.println("Enter hour minute and second (-1 to exit)");
            inHour = sc.nextInt();
            if (-1 == inHour)
                break;
            inMinute = sc.nextInt();
            inSecond = sc.nextInt();
            System.out.println();
            System.out.println("Values read were: " + inHour + " " +
                               inMinute + " " + inSecond);

            // Create a new MyTime object
            Assignment3 t = new Assignment3();

            // Set the time to the time read from the keyboard
            t.setTime(inHour,inMinute,inSecond);

            System.out.println(t.toString());
            System.out.println(t.toUniversalString());
            System.out.println("hour is " + t.getHour() + "  minute is " +
                                t.getMinute() + "  second is " + t.getSecond());

            // Test if the time read matches the 'noon' MyTime object's value.
            if (t.equals(noon))
                System.out.println("Matches noon time");
            // Test if the time read matches the 'wakeup' MyTime objet's value
            if (t.equals(wakeup))
                System.out.println("It's time to wakeup!");

        } while (-1 != inHour);

        // Keep console window alive until 'enter' pressed (if needed).
        System.out.println();
        System.out.println("Done - press enter key to end program");
        String junk = sc.nextLine();
    }

Was This Post Helpful? 0
  • +
  • -

#11 jruss1212  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Re: Intro to classes, objects, and methods

Posted 10 February 2012 - 02:00 PM

Ok so I think I have the twoDigits set up correctly but it is doing nothing since I havn't done anything with it in main or the toString and toUniversal methods. So my question is what do I write in either of the methods regarding the twoDigits?
Was This Post Helpful? 0
  • +
  • -

#12 jruss1212  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Re: Intro to classes, objects, and methods

Posted 10 February 2012 - 09:45 PM

How would I call the result variable from the twoDigit method to the String toString method?
Was This Post Helpful? 0
  • +
  • -

#13 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Intro to classes, objects, and methods

Posted 10 February 2012 - 09:58 PM

You have your class that has two public methods, and one private method. The private method can only be called from within that class. So you call it from within the public methods, which can be called from any class. (or in this case, your main method)
public class MyClass {
   private int mMinute;

   public MyClass(int x) {
      mMinute = x;
   }

   public String toString() {
      String minuteString = twoDigits(mMinute);
      return minuteString;
   }

   private String twoDigits(int value) {
      // ...
      return result;
   }
}

This post has been edited by Sheph: 10 February 2012 - 09:59 PM

Was This Post Helpful? 0
  • +
  • -

#14 jruss1212  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Re: Intro to classes, objects, and methods

Posted 11 February 2012 - 06:20 AM

If I want to call the mMinute and mSecond from the twoDigits methos to another method but not together, how would I do that? Here is the code before the main method:

/**
 * MyTimeDemo - tests the MyTime class
 *
 * The MyTime class must be compiled before this class can be compiled.
 */
import java.util.Scanner;

	public class MyTime
	{
	    // Instance variables holding the time (in 24 hour universal form).
	    private int mHour;      // 0 - 23
	    private int mMinute;    // 0 - 59
	    private int mSecond;    // 0 - 59

	    /**
	     * Sets the time value for this MyTime object. If *any* of the parameter
	     * values are out of range, the time is set to 00:00:00.
	     *
	     * @param hour      the hour (0 to 23).
	     * @param minute    the minute (0 to 59).
	     * @param second    the second (0 to 59).
	     */
	    public void setTime(int hour, int minute, int second)
	    {

	        if(hour >= 0 && hour <= 23)
	        {
	        	mHour = hour ;
	        }
	        else
	        {
	        	mHour = 00 ;
	        }
	        if(minute >= 0 && minute <= 59)
	        {
	        	mMinute = minute ;
	        }
	        else
	        {
	        	mMinute = 00 ;
	        }
	        if(second >= 0 && second<= 59)
	        {
	        	mSecond = second ;
	        }
	        else
	        {
	        	mSecond = 00 ;
	        }
	        if(hour <0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59)
	        {
	        	mHour = 00;
	        	mMinute = 00;
	        	mSecond = 00;
	        }
	       
	    }

	    /**
	     * Returns the hour value represented by this MyTime.
	     *
	     * @return the hour value of this object as an int.
	     */
	    public int getHour()
	    {
	        return mHour;  // TODO - replace
	    }

	    /**
	     * Returns the minute value represented by this MyTime.
	     *
	     * @return the minute value of this object as an int.
	     */
	    public int getMinute()
	    {
	        return mMinute;  // TODO - replace
	    }

	    /**
	     * Returns the second value represented by this MyTime.
	     *
	     * @return  the second value of this object as an int.
	     */
	    public int getSecond()
	    {
	        return mSecond;  // TODO - replace
	    }

	    public boolean equals(MyTime other)
	    {
	        if(mHour == other.mHour && mMinute == other.mMinute && mSecond == other.mSecond)
	        {
	        	return true;
	        }

	        return false;
	    }

	    public String toString()
	    {
	        String result = "";
	        result = twoDigits(mHour) ;
	        int newHour;
	        
	        if(mHour >= 1 && mHour <= 11)
	        {
	        	result += " AM";
	        }
	        else if(mHour >= 13 && mHour <= 23)
	        {
	        	newHour = mHour - 12;
	        	result = newHour + ":" + twoDigits(mMinute) + ":" + mSecond + " PM";
	        }
	        else
	        {
	        	newHour = 12;
	        	result = newHour + ":" + twoDigits(mMinute) + ":" + mSecond + " AM";
	        }
	     	        return result;
	    }

	   	    public String toUniversalString()
	    {
	        String result = "";
	      
	        if(mHour >= 0 && mHour <= 11)
	        {
	        	result = mHour + ":" + mMinute + ":" + mSecond; 
	        }
	        else if(mHour >= 12 && mHour <= 23)
	        {
	        	
	        	result = mHour + ":" + mMinute + ":" + mSecond;
	        }
	   	        return result;
	    }

	    private String twoDigits(int value)
	    {
	        String result = "";
	        String hour;
	        String min;
	        String sec;
	        
	        hour = Integer.toString(mHour);
	        
	        if(mMinute < 10)
	        {
	        	min = Integer.toString(mMinute);
	        	min= "0" + min;
	        	
	        }
	        else
	        {
	        	min = Integer.toString(mMinute);
	        }
	        if(mSecond < 10)
	        {
	        	sec = Integer.toString(mSecond);
	        	sec = "0" + sec;
	        }
	        else
	        {
	        	sec = Integer.toString(mSecond);
	        }
	        result = hour + ":" + min + ":" + sec;

	      
	        return result;
	    }

Was This Post Helpful? 0
  • +
  • -

#15 jruss1212  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 202
  • Joined: 04-February 12

Re: Intro to classes, objects, and methods

Posted 11 February 2012 - 08:32 AM

Ok so I have my whole program done. The only problem is the output for certain inputs. It seems to only have a problem when the minute entered is a double digit and the second is a single digit and vice versa. For example if 12 12 12 is entered the output is fine but say they enter 12 2 12. It should output 12:02:12 but it outputs 12:12:12. Another example, lets say I enter 10 10 2, the output is 10:02:02 when it should be 10:10:02. I think the error is in my twodigits method but I'm not sure. Here is all the code:

/**
 * MyTimeDemo - tests the MyTime class
 *
 * The MyTime class must be compiled before this class can be compiled.
 */
import java.util.Scanner;

	public class MyTime
	{
	    // Instance variables holding the time (in 24 hour universal form).
	    private int mHour;      // 0 - 23
	    private int mMinute;    // 0 - 59
	    private int mSecond;    // 0 - 59

	    /**
	     * Sets the time value for this MyTime object. If *any* of the parameter
	     * values are out of range, the time is set to 00:00:00.
	     *
	     * @param hour      the hour (0 to 23).
	     * @param minute    the minute (0 to 59).
	     * @param second    the second (0 to 59).
	     */
	    public void setTime(int hour, int minute, int second)
	    {

	        if(hour >= 0 && hour <= 23)
	        {
	        	mHour = hour ;
	        }
	        else
	        {
	        	mHour = 00 ;
	        }
	        if(minute >= 0 && minute <= 59)
	        {
	        	mMinute = minute ;
	        }
	        else
	        {
	        	mMinute = 00 ;
	        }
	        if(second >= 0 && second<= 59)
	        {
	        	mSecond = second ;
	        }
	        else
	        {
	        	mSecond = 00 ;
	        }
	        if(hour <0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59)
	        {
	        	mHour = 00;
	        	mMinute = 00;
	        	mSecond = 00;
	        }
	       
	    }

	    /**
	     * Returns the hour value represented by this MyTime.
	     *
	     * @return the hour value of this object as an int.
	     */
	    public int getHour()
	    {
	        return mHour;  // TODO - replace
	    }

	    /**
	     * Returns the minute value represented by this MyTime.
	     *
	     * @return the minute value of this object as an int.
	     */
	    public int getMinute()
	    {
	        return mMinute;  // TODO - replace
	    }

	    /**
	     * Returns the second value represented by this MyTime.
	     *
	     * @return  the second value of this object as an int.
	     */
	    public int getSecond()
	    {
	        return mSecond;  // TODO - replace
	    }

	    /**
	     * Compares this MyTime object against another MyTime object.
	     * The result is true if and only if the argument is not null
	     * and represents a time that is the same time as represented
	     * by this object
	     *
	     * @param  other    the object to be compared.
	     * @returns  true if both objects represent the same time value,
	     *           false otherwise.
	     */
	    public boolean equals(MyTime other)
	    {
	        if(mHour == other.mHour && mMinute == other.mMinute && mSecond == other.mSecond)
	        {
	        	return true;
	        }

	        return false;
	    }

	    /**
	     * Returns the time of this MyTime object in HH:MM:SS AM/PM form.
	     *
	     * @return the time value represented by this object as a String
	     *         formatted as HH:MM:SS AM/PM .
	     */
	    public String toString()
	    {
	        String result = "";
	        int newHour;
	        
	        if(mHour >= 1 && mHour <= 11)
	        {
	        	result = mHour + ":" + twoDigits(mMinute) + ":" + twoDigits(mSecond) + " AM";
	        }
	        else if(mHour >= 13 && mHour <= 23)
	        {
	        	newHour = mHour - 12;
	        	result = newHour + ":" + twoDigits(mMinute) + ":" + twoDigits(mSecond) + " PM";
	        }
	        else
	        {
	        	newHour = 12;
	        	result = newHour + ":" + twoDigits(mMinute) + ":" + twoDigits(mSecond) + " PM";
	        }
	      
	       
	        
	        // TODO - add code to format the time in the instance variables
	        // into HH:MM:SS AM/PM time (hour ranges from 1 to 12, AM or PM)
	        // as a string and return it.

	        return result;
	    }

	    /**
	     * Returns the time of this MyTime object in 24 hour universal form.
	     *
	     * @return the time value represented by this object as a String
	     *         formatted as HH:MM:SS universal time.
	     */
	    public String toUniversalString()
	    {
	        String result = "";
	      
	        if(mHour >= 0 && mHour <= 11)
	        {
	        	result = mHour + ":" + twoDigits(mMinute) + ":" + twoDigits(mSecond); 
	        }
	        else if(mHour >= 12 && mHour <= 23)
	        {
	        	
	        	result = mHour + ":" + twoDigits(mMinute) + ":" + twoDigits(mSecond);
	        }
	      

	        // TODO - add code to format the time in the instance variables
	        // into HH:MM:SS universal time (hour ranges from 0 to 23)
	        // as a string and return it.

	        return result;
	    }

	    /**
	     * Returns the parameter value as a two digit String (with leading 0 if needed)
	     *
	     * @return the parameter 'value' formatted as a two digit String
	     *         (with leading zero if needed).
	     */
	    private String twoDigits(int value)
	    {
	        String result = "";
	        String hour;
	        String min;
	        String sec;
	        
	        hour = Integer.toString(mHour);
	        
	        if(mMinute < 10)
	        {
	        	min = Integer.toString(mMinute);
	        	min = "0" + min;
	        	result = min;
	        	
	        }
	        else
	        {
	        	min = Integer.toString(mMinute);
	        	result = min;
	        }
	        if(mSecond < 10)
	        {
	        	sec = Integer.toString(mSecond);
	        	sec = "0" + sec;
	        	result = sec;
	        }
	        else
	        {
	        	sec = Integer.toString(mSecond);
	        	result = sec;
	        }
	     

	        // TODO - Use the Integer.toString(int) method to convert
	        // 'value' to String format, and add a leading "0" if
	        // value is less than 10 in order to return a string with
	        // two digits.

	        return result;
	    }

	
    /**
     * main - Assignment 4
     */
    public static void main (String[] args)
    {
        Scanner sc = new Scanner(System.in);

        // Create a MyTime object and set to noon (12 PM).
        MyTime noon = new MyTime();
        noon.setTime(12, 0, 0);

        MyTime wakeup = new MyTime();
        wakeup.setTime(6, 30, 0);

        // Ask for time to be input, then create a new MyTime and set the time
        int inHour, inMinute, inSecond;
        do {
            System.out.println("Enter hour minute and second (-1 to exit)");
            inHour = sc.nextInt();
            if (-1 == inHour)
                break;
            inMinute = sc.nextInt();
            inSecond = sc.nextInt();
            System.out.println();
            System.out.println("Values read were: " + inHour + " " +
                               inMinute + " " + inSecond);

            // Create a new MyTime object
            MyTime t = new MyTime();

            // Set the time to the time read from the keyboard
            t.setTime(inHour,inMinute,inSecond);

            System.out.println(t.toString());
            System.out.println(t.toUniversalString());
            System.out.println("hour is " + t.getHour() + "  minute is " +
                                t.getMinute() + "  second is " + t.getSecond());

            // Test if the time read matches the 'noon' MyTime object's value.
            if (t.equals(noon))
                System.out.println("Matches noon time");
            // Test if the time read matches the 'wakeup' MyTime objet's value
            if (t.equals(wakeup))
                System.out.println("It's time to wakeup!");

        } while (-1 != inHour);

        // Keep console window alive until 'enter' pressed (if needed).
        System.out.println();
        System.out.println("Done - press enter key to end program");
        String junk = sc.nextLine();
    }
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2