家庭作业问题.编写日历约会程序的方法时遇到问题


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(( 方法时尤其遇到问题。这应该返回程序运行时我在给定日期的所有约会的列表。 我是Java的新手,并且无法掌握这个窍门。任何帮助或提示将不胜感激。

/** * 此方法应返回是否有预约安排在 * 一天中的任何时间。 * * 如果在此任何时间安排了约会,则@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: 

如果在此时间的任何小时内安排了约会,则返回 true

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方法,每次迭代你都在切换值。 如果您一天中的所有时间都是真实的,那么您将只得到当天的最后一次约会。 而且,如果您全天都有约会,除了最后一小时,您将获得: 一整天都没有安排约会

最新更新