-2
У меня есть код Course.java и Period.java. Внутри кода Course.java есть открытый период getPeriod(); метод и внутри этого метода я хочу вернуть Period. Однако я не знаю, как бы я это сделал. Может ли кто-нибудь вести меня в правильном направлении? Благодаря!Java (Как реализовать один класс внутри другого класса getter?)
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;
}
}
}
}
Какие конструкторы делает 'Период' есть, и Какова связь между «Курсом» (ы) и «Периодом» (ы)? –
Покажите код, который у вас есть. – Andreas
@ Andreas пожалуйста, код выше. –