[英]BlueJ Object Oriented Programming
公共类数字时钟{
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 编码。
要设置对象的局部变量(在本例中为 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.