Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck with what object the method is triggered
    primarykey
    data
    text
    <p>I call this method in java:</p> <pre><code>private void updateDisplay() { displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); } </code></pre> <p>What triggers this method two times for hours and minutes:</p> <pre><code>public String getDisplayValue() { if(value &lt; 10) { return "0" + value; } else { return "" + value; } } </code></pre> <p>My question is how can i check in <code>getDisplayValue</code> if the method is triggerd as minute or as hour? For example:</p> <pre><code>public String getDisplayValue() { if(this == minutes){ if(value &lt; 10) { return "0" + value; } else { return "" + value; } } </code></pre> <p>Entire code:</p> <pre><code>public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; private String displayString; // simulates the actual display /** * Constructor for ClockDisplay objects. This constructor * creates a new clock set at 00:00. */ public ClockDisplay() { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); updateDisplay(); } /** * Constructor for ClockDisplay objects. This constructor * creates a new clock set at the time specified by the * parameters. */ public ClockDisplay(int hour, int minute) { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); setTime(hour, minute); } /** * This method should get called once every minute - it makes * the clock display go one minute forward. */ public void timeTick() { minutes.increment(); if(minutes.getValue() == 0) { // it just rolled over! hours.increment(); } updateDisplay(); } /** * Set the time of the display to the specified hour and * minute. */ public void setTime(int hour, int minute) { hours.setValue(hour); minutes.setValue(minute); updateDisplay(); } /** * Return the current time of this display in the format HH:MM. */ public String getTime() { return displayString; } /** * Update the internal string that represents the display. */ private void updateDisplay() { displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); } } </code></pre> <p>And:</p> <pre><code>public class NumberDisplay { private int limit; private int value; /** * Constructor for objects of class NumberDisplay. * Set the limit at which the display rolls over. */ public NumberDisplay(int rollOverLimit) { limit = rollOverLimit; value = 0; } /** * Return the current value. */ public int getValue() { return value; } /** * Return the display value (that is, the current value as a two-digit * String. If the value is less than ten, it will be padded with a leading * zero). */ public String getDisplayValue() { if(value &lt; 10) { return "0" + value; } else { return "" + value; } } /** * Set the value of the display to the new specified value. If the new * value is less than zero or over the limit, do nothing. */ public void setValue(int replacementValue) { if((replacementValue &gt;= 0) &amp;&amp; (replacementValue &lt; limit)) { value = replacementValue; } } /** * Increment the display value by one, rolling over to zero if the * limit is reached. */ public void increment() { value = (value + 1) % limit; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload