首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BlueJ面向对象编程

BlueJ面向对象编程
EN

Stack Overflow用户
提问于 2021-11-09 03:47:08
回答 1查看 43关注 0票数 0

公共类DigitalClock {

代码语言:javascript
复制
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'
}

(如何更新时间并获取返回值。我正在做家庭作业,不知道下班后该去哪里。任务是在BlueJ内部制作一个可以正常工作的数字时钟。我已经被这部分卡住了三天了,我不想在不知道自己在做什么的情况下只是抄袭。如果可能的话,我也想知道人们在练习基于面向对象编程的java编码时会推荐什么。

EN

回答 1

Stack Overflow用户

发布于 2021-11-09 03:50:17

要设置对象的局部变量(在本例中为m、h、s),可以执行this.m = X;m设置为X

另外,我认为您的setHour函数中有一个bug。检查第一个if语句的条件;)

编辑:

作为起点,下面是setHour函数可能的样子。我并不觉得写这篇文章很糟糕,因为我很清楚介绍CS有多难!看起来你真的对学习很感兴趣。

代码语言:javascript
复制
public void setHour(int h) {

    if (h < 1) {
        this.h = 1;
    }
    else if (h > 12) {
        this.h = 12;
    else {
        this.h = h;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69892581

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档