Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-10-2010, 08:01 AM   PM User | #1
dubsjw
New to the CF scene

 
Join Date: Nov 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
dubsjw is an unknown quantity at this point
Help with writing program for class.

Sorry...The title is misleading, I do not want you to do the homework for me at all...but rather help me understand what I have to do. I've been working on this program for 3 hours trying to understand what my teacher wants us to do. It may just be the wording he is using but I'm having a hard time understanding exactly what he wants this program to accomplish. We have just started to learn about oop and this is my first assignment implementing that type of procedure so bare with me. If you all could take a look at my assignment and maybe explain to me what he wants or just try and clarify what I have to do, it would greatly be appreciated. Thanks

-dubsjw


Problem Description.

• Our first OOP consists of two classes – Time and TimeApp. The class Time represents the time of day. The class TimeApp is an application class in which the main method creates one object of class Time and invokes its methods. These classes must be declared in separate files because they are both public classes.

• The class Time contains 3 private instance variables of type int – hour, minute and second – that represent the time in military–time format (24-hour clock format in which hours are in the range 0-23). The class Time also contains four public methods Time (constructor), setTime, displayMilitary, and displayStandard. These methods are also called the public services or the public interface that the class provides to its clients.

• Here is the class Time design using UML:


Time

- hour: int
- minute: int
- second: int


+ Time ( ) :
+ setTime (h,m,s: int) : void
+ displayMilitary ( ) : void
+ displayStandard ( ) : void



• Here is the partial Class Time definition:
Code:
        // File name: Time.java
	// Time class maintains the time in 24-hour format.

	public class Time
   	{	
		// Instance fields (variables)
		private int hour;  	// 0-23
		private	int minute;  	// 0-59
		private	int second;  	// 0-59

		


	
			// Instance methods
		public Time ( int , int, int)	// Constructor

		{
			// sets the starting  hour, minute, and second at 0.
		}

			public void setTime (int h, int m, int s)
			{	
				// stores values passed as argument in the fields.
			}

			public void displayMilitary ( )
			{
				// display in military-time format (e.g., 14:20:26 )
			}

			public void displayStandard ( )	
			{
				// display in standard-time format (e.g., 1: 25:05  AM or PM)
			}	

		}   // end of class
• Complete the Time class definition

• Write the TimeApp (file name: TimeApp.java ) application class (including the main method) that involves the following activities:

- create and initialize a Time object.
- output string representations of the time.
- change time and output updated time.
- set time with invalid values: output updated time.

Output

Your program output will look just like the following to receive full credit.

The initial military time is: 00:00:00
The initial standard time is: 12:00:00 AM

Military time after setTime is: 13:27:06
Standard time after setTime is: 1:22:06 PM

After attempting invalid setting:
Military time: 00:00:00
Standard time: 12:00:00 AM

Last edited by dubsjw; 11-10-2010 at 08:10 AM.. Reason: adding code
dubsjw is offline   Reply With Quote
Old 11-10-2010, 01:13 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
So far the only thing wrong with the class is the constructor. The UML lists that the constructor has no arguments, but this one has three. I'd chain them and keep both to give you an option:
PHP Code:
public Time()
{
    
this(000);

The displayMilitary will be the easiest to do. Its a simple print that separates each of the hour, minute and second variables with a colon. The settime method needs to enforce validation rules. Hour must be between 0 and 23, and minutes and seconds between 0 and 59. If either of these fail, reset all to 0. You can accomplish this by either checking the variables after all are set for errors, or using exceptions to try/catch and reset. And finally, the display standard needs to check if hour is > 12. If it is, subtract 12 and show pm instead of am.
The timeApp just has a main that has a few calls to the time class. I can't really say much else on that without doing it for you. Update what you have, and if you still have some problems than come back and post back what the problem is.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 11-17-2010, 09:16 AM   PM User | #3
dubsjw
New to the CF scene

 
Join Date: Nov 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
dubsjw is an unknown quantity at this point
ok...i caught up on what i missed in my class and i understand ALOT more than i did prior to this post. One thing im still confused about is the whole am and pm thing. In order to do this I will have to create a string and enforce it to change according to what the the number is but there is nowhere in the uml that it lists that i need a variable for it which makes me confused...is there another way to do this?
Here is what i have so far:


Code:
public class Time
{
   private int hour;
   private int minute;
   private int second;
   
   public Time()
   {
    hour = 0;
    minute = 0;
    second = 0;
    }
    
   public void setTime(int h, int m, int s)
   {
    
       if (h > 23)
    {
        h = 0;
    }
    
    if (m > 59)
    {
        m = 0;
    }
       
    if (s > 59)
    {
        s = 0;
    }
    
    hour = h;
    minute = m;
    second = s;
        
    }
    
   public void displayMilitary()
   {
    
    }
   
   public void displayStandard()
   {
    
    
     
    }
}

Last edited by dubsjw; 11-17-2010 at 09:25 AM..
dubsjw is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:13 AM.


Advertisement
Log in to turn off these ads.