功课问题。为日历约会程序编写方法时遇到问题
Homework issues. Trouble writing methods for a calendar appointment program
import java.util.Arrays;
/**
* Class Day: This class is used to represent one day of a schedule. The data is
* an array of boolean values that represent whether an appointment is scheduled
* for a particular hour of the day. There is also a description that is
* associated with each hour of the day - saved in an array of Strings.
*
* Last Modified: [10FEB2020]
* Author: [Adam Vieth]
*/
public class Day extends HandleInput {
// Attributes
// There are 24 hours in a day, so this
// array should have 24 elements. If an
// element is true, that means an appointment
// is scheduled during that hour. If it is
// false, then there is no appointment scheduled
// during that hour.
private boolean[] isBusy;
// This also should have 24 elements, one
// for each hour of the day. If an appointment
// is scheduled for an hour, then this array
// should hold a String description of the
// appointment.
private String[] appointmentDescription;
/**
* Constructor for the Day class. This should allocate memory for the attribute
* arrays, and should initialize each hour to have no appointments.
*
*/
public Day()
{
isBusy = new boolean[24];
appointmentDescription = new String[24];
}
/**
* This method should return whether or not there is an appointment scheduled
* for a particular time of this day.
*
* @param hour
* The hour during this day whose status is being checked.
* @return true if there is an appointment scheduled during the requested hour,
* false if there is not.
*/
public boolean checkTime(int hour) {
if(isBusy[hour] == false)
{
return false;
}
else
{
return true;
}
}
/**
* This method should return whether or not there is an appointment scheduled at
* any hour of this day.
*
* @return true if there is an appointment scheduled during any hour of this
* day, false if there are no appointments for this entire day.
*/
public boolean checkDay()
{
for(int i = 0; i < isBusy.length; i++)
{
if(isBusy[i] == false)
{
return true;
}
}
return false;
}
/**
* This method should return the appointment description for the appointment at
* the given time of this day.
*
* @param hour
* The hour during this day whose appointment description should be
* returned.
* @return If an appointment is scheduled at the given hour, then the
* appointment description should be returned, otherwise the text "No
* appointment scheduled" should be returned.
*/
public String getDescription(int hour)
{
if(isBusy[hour] == true)
{
return appointmentDescription[hour];
}
else
{
return null;
}
}
/**
* This method should add a new appointment to the given hour for this day. This
* method should modify the isBusy and appointmentDescription for the given day
* only if there is not already an appointment for the given hour.
*
* @param hour
* The hour during this day for which an appointment should be made.
* @param description
* The text description of the new appointment.
* @return false if there is already an appointment scheduled during the given
* hour (the previous appointment information should not be modified),
* and true if adding the appointment was successful (there was no
* previous appointment).
*/
public boolean addAppointment(int hour, String description) {
if(isBusy[hour] == false)
{
appointmentDescription[hour] = description;
isBusy[hour] = true;
return true;
}
else
{
return false;
}
}
/**
* This method should remove an appointment for a given hour for this day. There
* should be no effect if there was not an appointment scheduled in the first
* place.
*
* @param hour
* The hour during this day for which an appointment removed.
*/
public void removeAppointment(int hour) {
if(isBusy[hour] == true)
{
isBusy[hour] = false;
appointmentDescription[hour] = null;
}
}
/**
* This method should generate a String of all appointment times and
* descriptions for a given hour for this day. There should be no print
* statements in this method, only the code to create a String.
*
* @return A String whose text is one line for each appointment on this day.
* Each line should have the appointment time and the appointment
* description. See the program description for the exact formatting.
*/
public String toString() {
/* TODO - write this method */
String output = "";
for(int i = 0; i < isBusy.length; i++)
{
if(appointmentDescription[i] == null)
{
return "No appointments scheduled for this entire day";
}
if(appointmentDescription[i] != null)
{
output = appointmentDescription[i];
}
}
return output;
}
}
目前我遇到了很多关于这个程序的问题。我觉得我写的一些方法不能正常工作。我在编写 toString() 方法时尤其遇到问题。这应该是 return 程序为 运行 时我在给定日期的所有约会的列表。
我是 java 的新手,很难掌握这个窍门。任何帮助或提示将不胜感激。
/**
* 这个方法应该return 是否有约会安排在
* 当天的任何时间。
*
* @return 如果在此期间的任何时间安排了约会,则为真
* 天,如果这一天没有约会,则为 false。
*/
public boolean checkDay()
{
for(int i = 0; i < isBusy.length; i++)
{
if(isBusy[i])
{
return true;
}
}
return false;
}
This method is not correct, see, at the description you said:
return true if there is an appointment scheduled during any hour of this
but, when there is no appointment, you return true.
public String toString() {
StringBuilder output = new StringBuilder();
// String is an immutable object. Consider use StringBuilder
for(int i = 0; i < isBusy.length; i++)
{
if(appointmentDescription[i] != null)
{
output.append(appointmentDescription[i]);
if( i < isBusy.length - 1){
output.append(", ");
}
}
}
if (output.isEmpty()) {
return "No appointments scheduled for this entire day";
}
return output.toString();
}
这个toString方法,每次迭代你都在切换值。
如果您将一天中的所有时间都设置为 true,您将只会获得当天的最后一个约会。
而且,如果您除了最后一个小时之外全天都有约会,您将得到:这一天没有安排约会
import java.util.Arrays;
/**
* Class Day: This class is used to represent one day of a schedule. The data is
* an array of boolean values that represent whether an appointment is scheduled
* for a particular hour of the day. There is also a description that is
* associated with each hour of the day - saved in an array of Strings.
*
* Last Modified: [10FEB2020]
* Author: [Adam Vieth]
*/
public class Day extends HandleInput {
// Attributes
// There are 24 hours in a day, so this
// array should have 24 elements. If an
// element is true, that means an appointment
// is scheduled during that hour. If it is
// false, then there is no appointment scheduled
// during that hour.
private boolean[] isBusy;
// This also should have 24 elements, one
// for each hour of the day. If an appointment
// is scheduled for an hour, then this array
// should hold a String description of the
// appointment.
private String[] appointmentDescription;
/**
* Constructor for the Day class. This should allocate memory for the attribute
* arrays, and should initialize each hour to have no appointments.
*
*/
public Day()
{
isBusy = new boolean[24];
appointmentDescription = new String[24];
}
/**
* This method should return whether or not there is an appointment scheduled
* for a particular time of this day.
*
* @param hour
* The hour during this day whose status is being checked.
* @return true if there is an appointment scheduled during the requested hour,
* false if there is not.
*/
public boolean checkTime(int hour) {
if(isBusy[hour] == false)
{
return false;
}
else
{
return true;
}
}
/**
* This method should return whether or not there is an appointment scheduled at
* any hour of this day.
*
* @return true if there is an appointment scheduled during any hour of this
* day, false if there are no appointments for this entire day.
*/
public boolean checkDay()
{
for(int i = 0; i < isBusy.length; i++)
{
if(isBusy[i] == false)
{
return true;
}
}
return false;
}
/**
* This method should return the appointment description for the appointment at
* the given time of this day.
*
* @param hour
* The hour during this day whose appointment description should be
* returned.
* @return If an appointment is scheduled at the given hour, then the
* appointment description should be returned, otherwise the text "No
* appointment scheduled" should be returned.
*/
public String getDescription(int hour)
{
if(isBusy[hour] == true)
{
return appointmentDescription[hour];
}
else
{
return null;
}
}
/**
* This method should add a new appointment to the given hour for this day. This
* method should modify the isBusy and appointmentDescription for the given day
* only if there is not already an appointment for the given hour.
*
* @param hour
* The hour during this day for which an appointment should be made.
* @param description
* The text description of the new appointment.
* @return false if there is already an appointment scheduled during the given
* hour (the previous appointment information should not be modified),
* and true if adding the appointment was successful (there was no
* previous appointment).
*/
public boolean addAppointment(int hour, String description) {
if(isBusy[hour] == false)
{
appointmentDescription[hour] = description;
isBusy[hour] = true;
return true;
}
else
{
return false;
}
}
/**
* This method should remove an appointment for a given hour for this day. There
* should be no effect if there was not an appointment scheduled in the first
* place.
*
* @param hour
* The hour during this day for which an appointment removed.
*/
public void removeAppointment(int hour) {
if(isBusy[hour] == true)
{
isBusy[hour] = false;
appointmentDescription[hour] = null;
}
}
/**
* This method should generate a String of all appointment times and
* descriptions for a given hour for this day. There should be no print
* statements in this method, only the code to create a String.
*
* @return A String whose text is one line for each appointment on this day.
* Each line should have the appointment time and the appointment
* description. See the program description for the exact formatting.
*/
public String toString() {
/* TODO - write this method */
String output = "";
for(int i = 0; i < isBusy.length; i++)
{
if(appointmentDescription[i] == null)
{
return "No appointments scheduled for this entire day";
}
if(appointmentDescription[i] != null)
{
output = appointmentDescription[i];
}
}
return output;
}
}
目前我遇到了很多关于这个程序的问题。我觉得我写的一些方法不能正常工作。我在编写 toString() 方法时尤其遇到问题。这应该是 return 程序为 运行 时我在给定日期的所有约会的列表。 我是 java 的新手,很难掌握这个窍门。任何帮助或提示将不胜感激。
/** * 这个方法应该return 是否有约会安排在 * 当天的任何时间。 * * @return 如果在此期间的任何时间安排了约会,则为真 * 天,如果这一天没有约会,则为 false。 */
public boolean checkDay()
{
for(int i = 0; i < isBusy.length; i++)
{
if(isBusy[i])
{
return true;
}
}
return false;
}
This method is not correct, see, at the description you said:
return true if there is an appointment scheduled during any hour of this
but, when there is no appointment, you return true.
public String toString() {
StringBuilder output = new StringBuilder();
// String is an immutable object. Consider use StringBuilder
for(int i = 0; i < isBusy.length; i++)
{
if(appointmentDescription[i] != null)
{
output.append(appointmentDescription[i]);
if( i < isBusy.length - 1){
output.append(", ");
}
}
}
if (output.isEmpty()) {
return "No appointments scheduled for this entire day";
}
return output.toString();
}
这个toString方法,每次迭代你都在切换值。 如果您将一天中的所有时间都设置为 true,您将只会获得当天的最后一个约会。 而且,如果您除了最后一个小时之外全天都有约会,您将得到:这一天没有安排约会