• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Stuck when being tasked to edit only one method  RSS feed

 
Greenhorn
Posts: 10
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hellooo (First post ever!)

I have two classes here, NumberDisplay: https://hastebin.com/asumilatis.cs and ClockDisplay: https://hastebin.com/cotibotepe.java

I'm stuck on this one for school, I've been tasked with updating the updateDisplay() method in the ClockDisplay class to display the time in 12 hour format, but keep the value stored in 24 hour format. I'm not permitted to edit the NumberDisplay class at all, and I cannot add any instance variables to the ClockDisplay class. I'm only allowed to add constants to the ClockDisplay class, as well as edit the updateDisplay() method.

I've tried various forms of using a modulus calculation or trying to use hours.getValue() - DISPLAY_CONVERSION but the object displays incorrectly every time. I know that I somehow have to change the rollover limit to 12, but I can't seem to figure out how to do that on an already created object. I feel like the answer is in front of my face but I can't see it. Any advice on what I should try?
For reference I'm using BlueJ for this course, so we're creating objects essentially out of 'no where' and not making a main method for this yet.

This is my latest attempt however it just allows the time to roll over to 1 pm and then resets the value at 1 which doesn't store it in a 24 hour format as requested

 
Saloon Keeper
Posts: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
asumilatis.cs:

cotibotepe.java:
 
Ron McLeod
Saloon Keeper
Posts: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The logic can be way simpler.

If the value of hours.getValue() is less than FIRST_EVENING_HOUR then it is must be morning (AM), otherwise it must be afternoon/evening (PM).
 
Ron McLeod
Saloon Keeper
Posts: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to the Ranch
 
Ron McLeod
Saloon Keeper
Posts: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I missed something.  In addition to displaying AM/PM, do you also need to display NOON and MIDNIGHT?
 
Patricio Guerra
Greenhorn
Posts: 10
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:Maybe I missed something.  In addition to displaying AM/PM, do you also need to display NOON and MIDNIGHT?



Thank you for the welcome and response! The challenge I'm having is not displaying AM/PM but the fact that it'll print out "13:02" instead of "01:02" based on the way that the getDisplayValue() method is set up. I'm not allowed to edit that method however, only allowed to edit the updateDisplay() method - hence where my brick wall is  
 
Ron McLeod
Saloon Keeper
Posts: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you expected to store the actual time (hour and minutes) in the NumberDisplay objects, or are they just to be used for display purposes?
 
Patricio Guerra
Greenhorn
Posts: 10
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm expected to store the time in the NumberDisplay objects (If I'm looking at where my code is going correctly)

A little snippet of my requirements:
"Change the ClockDisplay's updateDisplay() method so it no longer displays 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), but the time is now DISPLAYED as follows: 5:30 a.m. or 2:05 p.m."
 
Sheriff
Posts: 24374
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Patricio Guerra wrote:The challenge I'm having is not displaying AM/PM but the fact that it'll print out "13:02" instead of "01:02" based on the way that the getDisplayValue() method is set up. I'm not allowed to edit that method however, only allowed to edit the updateDisplay() method.



And therefore you have to change 13 to 1 in the updateDisplay() method.
 
Patricio Guerra
Greenhorn
Posts: 10
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Patricio Guerra wrote:The challenge I'm having is not displaying AM/PM but the fact that it'll print out "13:02" instead of "01:02" based on the way that the getDisplayValue() method is set up. I'm not allowed to edit that method however, only allowed to edit the updateDisplay() method.



And therefore you have to change 13 to 1 in the updateDisplay() method.



Yeah, that's where I'm stuck... getDisplayValue() is a string, I've tried doing a %12 conversion if it goes above 12 but it won't work out cleanly, it either just causes the display to null out, display the wrong am/pm after converting because it updates the value incorrectly, or doesn't work in general. Might just be really confused on this one.
 
Ranch Hand
Posts: 99
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this solution, that works:


 
Patricio Guerra
Greenhorn
Posts: 10
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately doesn't work for the assignment, I'm only allowed to work within the updateDisplay() method, or alternatively add constants to the ClockDisplay class.

Working on the timeTick() method and changing the NumberDisplay hours variable isn't allowed :-(
 
D.J. Quavern
Ranch Hand
Posts: 99
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I check the minutes, and then the hour. I they are divisible by modulo 12, then it's time to reset the value to 1.
 
D.J. Quavern
Ranch Hand
Posts: 99
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Patricio Guerra wrote:Unfortunately doesn't work for the assignment, I'm only allowed to work within the updateDisplay() method, or alternatively add constants to the ClockDisplay class.

Working on the timeTick() method and changing the NumberDisplay hours variable isn't allowed :-(



You are not allowed to send an updated variables to NumberDisplay - I did not edit it at all, just resetting-?  I thought updateDisplay() was only to print a string...


Aha now I just read your text again and I see it.

It would be weird to increment in the updateDisplay() method, it's not what it is for :/. But if it's the exercise...
 
Patricio Guerra
Greenhorn
Posts: 10
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, looks like it works in general, but for the assignment unfortunately any edits outside of adding in a constant or changing the updateDisplay() method isn't allowed.

I've got a few things I could do if I was able to modify other areas but it wouldn't meet the assignment requirements. So somehow I need to subtract 12 from hours.getDisplayValue() if hours.getValue() is 13 or greater. I've written some different attempts at doing that but it's always changed the value of the object causing it to not be stored properly in a 24 hour format.
 
Patricio Guerra
Greenhorn
Posts: 10
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just saw your other message - I never thought about incrementing within updateDisplay()... maybe that would work? going to go back and see what I can do
 
Ron McLeod
Saloon Keeper
Posts: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Patricio Guerra wrote:The challenge I'm having is not displaying AM/PM but the fact that it'll print out "13:02" instead of "01:02" based on the way that the getDisplayValue() method is set up. I'm not allowed to edit that method however, only allowed to edit the updateDisplay() method.



And therefore you have to change 13 to 1 in the updateDisplay() method.


The fundamental issue I see is that if you use the same object to store the actual time of day and also use it for display purposes, changes that you may make to alter the displayed value will also alter the time of day.

For example, if the time of day is 13:01, and to display in 12h format you modify the value in the hours NumberDisplay object so that the displayed time show 1:01, it will obviously have the side effect of changing the time of day to 1:01.

If your requirements don't disallow it, you could create another instance of NumberDisplay in your updateDisplay() method, just for displaying the hours in 12h format:
 
Marshal
Posts: 64172
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am late to the party, but it appears we are taking 24‑hour hh:mm times and changing them to the 12‑hour clock.
It also appears that the clock object we are using is working correctly, so the hours are always in the range 0...23 inclusive and the minutes are always in the range 0....59 inclusive. That is why you have been told not to alter anything in the other methods. It is normal to be given code as a resource, and to have to write other code to use it. You have here the clock class as a ready‑made resource, and you can get hours and minutes from that.
As you have been told, you are simply creating a display. You don't need number display objects here. You need to create some rules from converting 24‑hour times to 12‑hour times. What you need to do is add am/pm suffixes, and alter the hour. I suggest you start off by calculating the suffixes (you only need the minutes to calculate midnight and noon), and later calculate the hours. I also suggest you use the ?: operator rather than if‑else.
 
Patricio Guerra
Greenhorn
Posts: 10
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Paul Clapham wrote:

Patricio Guerra wrote:The challenge I'm having is not displaying AM/PM but the fact that it'll print out "13:02" instead of "01:02" based on the way that the getDisplayValue() method is set up. I'm not allowed to edit that method however, only allowed to edit the updateDisplay() method.



And therefore you have to change 13 to 1 in the updateDisplay() method.


The fundamental issue I see is that if you use the same object to store the actual time of day and also use it for display purposes, changes that you may make to alter the displayed value will also alter the time of day.

For example, if the time of day is 13:01, and to display in 12h format you modify the value in the hours NumberDisplay object so that the displayed time show 1:01, it will obviously have the side effect of changing the time of day to 1:01.

If your requirements don't disallow it, you could create another instance of NumberDisplay in your updateDisplay() method, just for displaying the hours in 12h format:



Thank you Ron! I initially did something like this with a new NumberDisplay set at rollover limit of 13 but couldn't get it to work, I see the issue was keeping it set to 24. This is the only way I can see it working with the different parameters set for the lab so I'm going to go with this and continue on with the rest of the assignment   Thanks!
 
Campbell Ritchie
Marshal
Posts: 64172
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks very complicated. What is wrong with this sot of thing?
 
Campbell Ritchie
Marshal
Posts: 64172
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A few minutes ago, I wrote:. . . . What is wrong with this sort of thing? . . .

Actually, there is something serious wrong with it, but when you correct that, I still prefer that sort of code.
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!