【问题标题】:BlueJ Object Oriented ProgrammingBlueJ 面向对象编程
【发布时间】:2021-11-09 03:47:08
【问题描述】:

公共类 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'
}

(我该怎么做才能更新时间并获得返回值。我正在做家庭作业,并且在下班后的信息中迷失了去哪里。任务是在 BlueJ 内部制作一个工作数字时钟。我有在这部分卡了三天,不想在不知道我在做什么的情况下复制。如果可能的话,我还想知道人们推荐什么来练习基于 Java 的 OOP 编码。

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: java oop digital object-oriented-database


【解决方案1】:

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

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

编辑:

首先,下面是setHour 函数的样子。我为你写这篇文章并不难过,因为我非常了解介绍 CS 的难度!看起来你真的对学习很感兴趣。

public void setHour(int h) {

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

【讨论】:

  • 我更新了我的文档
猜你喜欢
  • 2010-09-18
  • 2011-07-09
  • 2015-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多