• Post Reply Bookmark Topic Watch Topic
  • New Topic

Update Display Method  RSS feed

 
Harry Peters
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a Java project with 4 classes. ClockDisplay is the main one. One class I'm starting with on this lab assignment is the ClockDisplay. This was already written for us. It has a method: "updateDisplay()" that was showing the time in 24 hour format. The time is still STORED as 24 hours(i.e. do not change or add any new instance variables). I'm supposed to update it so it no longer displays the time in 24-hour format. It's supposed to display as follows(examples):

5:30 a.m.

12:00 p.m. (noon)    //literally; put the word (noon) like that too

12:00 a.m. (midnight)  //literally; put the word (midnight) like that too

1:05 p.m.

etc......

Below is the code so far. My instructor gave me a hint to start it with something like if hours is greater than 12.....but class ended. How can I do it? Lines 89-95, I added to the class to start but I know that's not the right way to start it. This is where I'm supposed to find the right if statements.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shall give you another hint:-Now write down the logic remembering about the % and ?: operators. You may want to use minutes as well, remembering not to return 12:00am or 12:00pm; if one minute after noon is 12:01pm then it goes 11:59am→12 noon (or 12:00 noon)→12:01pm. You may wish to change the method to accept 24:00 o'clock. Use my class template to complete and consider changing that method, adding other methods, and correcting any spellling errrors.
 
Harry Peters
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool thanks for the help Campbell! So I was researching it online and I found some code that worked. Except now I need to add code so that if I put noon or midnight, it returns: "12:00 p.m. (noon)" or "12:00 a.m. (midnight)". The code I found can return everything else how I need it. Especially without magic numbers/strings since that's a no-no. So how can I edit what I did so that it can properly display noon and midnight? Check out lines: 90-97. That's my latest input. Thanks a bunch.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about creating a new String that is either empty or holds "(noon)" or "(midnight)" and appending it to your displayString?  Now, how would you set this String?  You have all the pieces you need already in updateDisplay().
 
Harry Peters
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been working on it, trying to get closer and this is where I'm at. Lines 90-106 I added since last post:

 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nice job. 

You can tighten up the logic of the if statements, though.  First, you have two else clauses that do essentially the same thing.  Next, think about what you know is true just before the second if.

If you need more of a hint, have you used else if before?
 
Harry Peters
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried else ifs before and couldn't make it work. I re-did the method and I'm closer. I can now do noon and midnight, but lost the 12 hour format. On line 12, I try and add, "hours = hours - 12; and line 15, "hours = hours + 12" but because this is linked with another class called NumberDisplay, NumberDisplay can't be converted to an int. When you look at my first post, you can see the NumberDisplay variable. So seems like that is my last hurdle. I have to use that NumberDisplay class to subtract or convert hours 13-24. It's simple without using other classes but in this case, how do I do it?
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just add back the code that deals with h and you'll be there!
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neal Parrett wrote:

What about hour==12 && minute>0 ?
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can simplify the code and make it easier to read by just retrieving the hours and minutes once instead of multiple times in each if-branch.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps even easier
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks good , except that I have never seen anybody write p.m.(noon) or a.m.(midnight). I have only seen noon and midnight written.
 
Harry Peters
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks a bunch everyone, I got it! I followed what Knute said about adding back h and at first didn't work. I didn't put all the strings on top and that is what I missed. I had things out of order, I just needed to tidy it up. You can see my result below:


I also appreciate the hints and guiding me. Helps me try and figure it out and learn. I'm new to coderanch and finding it very useful so far! Cheers.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still fails for hr==12 and minute>0.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!