我有一个Course.java代码和Period.java代码。在Course.java代码中,有一个公共的Period getPeriod();方法和这个方法里面我想返回Period。但是,我不知道我该怎么做。任何人都可以指导我正确的方向吗?谢谢!
Course.java
课程课程{int department;
int courseNum;
String name;
char day;
int timeSlot;
int credits;
Student students[] = new Student[20];
private Period period;
public Course(int department, int courseNum, String name, char day, int timeSlot, int credits) {
this.department = department;
this.courseNum = courseNum;
this.name = name;
this.day = day;
this.timeSlot = timeSlot;
this.credits = credits;
}
public int getDepartment() {
return department;
}
public int getCourseNumber() {
return courseNum;
}
public String getName() {
return name;
}
public Course(Period period){
this.period = period;
}
public Period getPeriod() {
return period;
}
public int getCredits() {
return credits;
}
public Student[] getRoster() {
return students;
}
public String toString(){
return ""+this.department+":"+this.courseNum+" " +"["+this.name+"]"+" "+this.period +" " + "credits:"+this.credits;
}
public boolean equals(Course other){
return courseNum==other.courseNum && department==other.department;
}
}
Period.java 公共课时段{
private char day;
private int timeSlot;
public static void main(String[] args){
Period p1 = new Period('M', 2);
Period p2 = new Period('M', 2);
Period p3 = new Period('F', 4);
int status = p1.compareTo(p2);
if(status == 0){
System.out.println(p1 + " is equal to " + p2);
}
else if(status == -1){
System.out.println(p1 + " comes before " + p2);
}
else{
System.out.println(p1 + " comes after " + p2);
}
status = p1.compareTo(p3);
if(status == 0){
System.out.println(p1 + " is equal to " + p3);
}
else if(status == -1){
System.out.println(p1 + " comes before " + p3);
}
else{
System.out.println(p1 + " comes after " + p3);
}
}
public Period(char day, int timeSlot){
if(day >= 'a' && day <= 'z'){
day = (char) (day + (int)'A' - (int)'a'); // converting to uppercase here
}
this.day = day;
this.timeSlot = timeSlot;
}
public char getDay(){
return this.day;
}
public int getTimeSlot(){
return this.timeSlot;
}
public String toString(){
return ""+this.day + this.timeSlot;
}
public boolean equals(Period p){
return day == p.day && timeSlot == p.timeSlot;
}
public int compareTo(Period other){
if(equals(other)){
return 0;
}
else{
if(other.day == day){
if(timeSlot < other.timeSlot){
return -1;
}
else{
return 1;
}
}
else{
String days = "SMTWF";
int ind1 = days.indexOf(day);
int ind2 = days.indexOf(other.day);
if(ind1 < ind2){
return -1;
}
else{
return 1;
}
}
}
}
public void setTimeSlot(char newSlot){
int count = 1;
while (count !=0){
if ((newSlot < 1) || (newSlot > 9)){
System.out.println("Time Slot must be between 1-9");
}
else {
this.timeSlot = newSlot;
count = 0;
}
}
}
}
答案 0 :(得分:0)
在Course
课程中,制作Period
类型的实例变量,如下所示:
private Period period;
public Course(Period period) {
this.period = period;
}
public Period getPeriod() {
return period;
}