BlueJ Object Oriented Programming

163 views Asked by At

public class DigitalClock {

private int m;
private int h;
private int s;
//Fill in the necessary fields below
/**
 * Constructor for objects of class DigitalClock
 * Replace the constructor. 
 * Rather than assigning the fields with the parameters in 3 different statements,
 * make a call to the setTime method using the constructor's parameters as the 
 * setTime method's parameters
 */
public DigitalClock(int h, int m, int s)
{
    setTime( h, m, s);
}

/**
* Assigns the fields by calling the appropriate set methods. 
* In order to set the time, you must set the hour, set the minute, and set the second.
*/
public void setTime(int h, int m, int s)
{
    setHour(h);
    setMinute(m);
    setSecond(s);
}

/**
* Mutator method for the hour field. 
* It should check if the parameter is valid. If the parameter is less than 1,
* assign hour a value of 1. If the parameter is greater than 12, assign hour a value of 12.
* Fill in below. 
*/
public void setHour(int h)
{
        if (h < 1) {
            this.h = 1;
        }
        else if (h > 12) {
            this.h = 12;
        }
        else 
        {
            this.h = h;
    }

}

/**
 * Mutator method for the hour field. 
 * It should check if the parameter is valid. If the parameter is invalid,
 * assign it a value of 0.
 * Fill in below. 
 */
public void setMinute(int m)
{
        if (m < 0) {
            this.m = 1;
        }
        else if (m > 60) {
            this.m = 0;
        }
        else 
        {
            this.m = m;
    }    
}

/**
 * Mutator method for the hour field. 
 * It should check if the parameter is valid. If the parameter is invalid,
 * assign it a value of 0.
 * Fill in below. 
 */
public void setSecond(int s)
{ 
        if (s < 0) {
            this.s = 1;
        }
        else if (s > 60) {
            this.s = 0;
        }
        else 
        {
            this.s = s;
    }    
}


/**
 * Update the hour field to the next hour.
 * Take note that nextHour() of 23:47:12 is 00:47:12. 
 */
public void nextHour()
{}

/**
 * Update the minute field to the next minute.
 * Take note that nextMinute() of 03:59:13 is 04:00:13. 
 */
public void nextMinute()
{}

/**
 * Update the second field to the next second.
 * Take note that nextSecond() of 23:59:59 is 00:00:00. 
 */
public void nextSecond()
{}

/**
 * Accessor method for the hour field. 
 * Replace below. 
 */
public int getHour()
{
    return hour;
}

/**
 * Accessor method for the minute field. 
 * Replace below. 
 */
public int getMinute()
{
    return minute;
}

/**
 * Accessor method for the second field. 
 * Replace below. 
 */
public int getSecond()
{ 
    return second;
}

/**
 * returns "HH:MM:SS"
 * Hint: You might find it helpful to create a local String variable and progressively add to it.
 * Replace below. 
 */
@Override 
public String toString()
{
    String h,m,s;
    h = " " + hour
    s = " " + second
    m = " " + minute
    return hour + ":" + minute ":" + second'
}

(What do I do to update the time and get return values. I am doing a homework assignment and am lost on where to go after the hour information. The assignement was to make a working digital clock inside of BlueJ. I have been stuck on this part for three days and dont want to just copy without knowing what Im doing. If possible I would also want to know what people recommend to practice coding java based OOP.

1

There are 1 answers

1
squidwardsface On

To set an object's local variables (m,h,s in this case), you can do this.m = X; to set m to X.

Also, I think you have a bug in your setHour function. Check the condition for your first if-statement ; )

EDIT:

As a starting point, here is what the setHour function might look like. I do not feel bad writing this out for you because I understand all too well how hard intro CS is! It also seems like you are genuinely interested in learning.

public void setHour(int h) {

    if (h < 1) {
        this.h = 1;
    }
    else if (h > 12) {
        this.h = 12;
    else {
        this.h = h;
    }
}