当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Guidance with inheritance/overriding?

问题描述:

The program is an 'appointments program', what the monthly class needs to do is In Monthly, if a query date is no earlier than the appointment date and matches the date in the month then the occursOn method should return true. For instance, suppose you have a monthly appointment, “go hiking”, set on 20 May 2003. For a query date, 20 May 2012, the method occursOn should return true.

I assume occursOn method needs to be overwritten but I want guidance here, as this is one of the few inheritance programs I have done.

I have 3 classes at the moment

import java.util.Scanner;

public class AppointmentDemo

{

public static void main(String[] args)

{

Appointment[] appointments = new Appointment[2];

appointments[0] = new Monthly(2003, 5, 20, "Go hiking.");

appointments[1] = new Onetime(2012, 4, 2, "Dentist appointment.");

System.out.println("Enter a date (year month day) to list appointments: (e.g. 2000 8 13)");

Scanner in = new Scanner(System.in);

int year = in.nextInt();

int month = in.nextInt();

int day = in.nextInt();

boolean found = false;

for (int i = 0; i < appointments.length; i++)

{

if (appointments[i].occursOn(year, month, day))

{

found = true;

System.out.println(appointments[i]);

}

}

if(!found){

System.out.println("No appointment found.");

}

}

}

Which is the main class

the super class:

public class Appointment

{

private String description;

private int year;

private int month;

private int day;

public Appointment(int year, int month, int day, String description)

{

this.year = year;

this.month = month;

this.day = day;

this.description = description;

}

public int getYear() { return year; }

/**

Returns the month of the appointment

@return the month

*/

public int getMonth() { return month; }

/**

Returns the day of the appointment

@return the day

*/

public int getDay() { return day; }

public boolean occursOn(int year, int month, int day)

{

return (year == this.year) && (month == this.month) && (day == this.day);

}

/**

Converts appointment to string description

*/

public String toString() { return description; }

}

and a monthly class

public class Monthly extends Appointment {

private int year;

private int day;

private int month;

private String description;

public Monthly(int year, int month, int day, String description) {

super(year, month, day, description);

// TODO Auto-generated constructor stub

}

}

网友答案:

Overwrite occursOn in Monthly so that it doesn't check year.

分享给朋友:
您可能感兴趣的文章:
随机阅读: