Problems with instantiating in JAVA
- by PUPIALEX
When running, below programme cannot reach the end of the main function..
I am new to JAVA, and cannot find its defections.. I need your help. Thanks.
import java.util.*;
class Schedule {
public String day;
private int startTime, endTime;
public Schedule(String input_day, int input_start, int input_end) {
day = input_day;
startTime = input_start;
endTime = input_end;
}
/* clashWith: to check whether this schedule clash with a Schedule called otherSchedule
* PRE-Condition : input must be of Schedule type
* POST-Condition : return true if two Schedule clash, return false if not.
*/
public boolean clashWith(Schedule otherSchedule) {
if(this.day != otherSchedule.day || this.endTime <= otherSchedule.startTime || this.startTime >= otherSchedule.endTime)
return false;
return true;
}
}
class Module {
String code;
Schedule lecture, tutorial, lab;
public Module(String input_code, Schedule input_lecture, Schedule input_tutorial, Schedule input_lab) {
code = input_code;
lecture = input_lecture;
tutorial = input_tutorial;
lab = input_lab;
}
/* count: to count number of classes(lecture, tutorial, and lab of only this Module) on day.
* For example: when day = "Monday", lecture is on Monday, tutorial is on Monday
* but lab is on Tuesday, then return 2. (lecture and tutorial are on Monday).
* PRE-Condition :
* POST-Condition :
*/
public int count(String day) {
int num = 0;
if(lecture.day == day)
num++;
if(tutorial.day == day)
num++;
if(lab.day == day)
num++;
return num;
}
/* clashWith: to check whether this module clash with a Module called otherModule
* PRE-Condition :
* POST-Condition :
*/
public boolean clashWith(Module otherModule) {
if(lecture.clashWith(otherModule.lecture) || lecture.clashWith(otherModule.tutorial) || lecture.clashWith(otherModule.lab) )
return true;
if(tutorial.clashWith(otherModule.lecture) || tutorial.clashWith(otherModule.tutorial) || tutorial.clashWith(otherModule.lab))
return true;
if(lab.clashWith(otherModule.lecture) || lab.clashWith(otherModule.tutorial) || lab.clashWith(otherModule.lab))
return true;
return false;
}
}
class Timetable {
Vector<Module> listOfModule;
public Timetable() {
}
/* checkClash: to check whether otherModule clash with one of
* the modules in our timetable list.
* PRE-Condition :
* POST-Condition :
*/
public boolean checkClash(Module otherModule) {
for(Module c: listOfModule)
if(c.clashWith(otherModule))
return true;
return false;
}
/* add: to add a new module to the timetable list.
* PRE-Condition :
* POST-Condition :
*/
public void add(Module module) {
listOfModule.add(module);
}
/* count: to count number of classes on day.
* PRE-Condition :
* POST-Condition :
*/
public int count(String day) {
int count_day=0;
for(Module c: listOfModule)
count_day += c.count(day);
return count_day;
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num_operation;
String code ;
Timetable userTimetable = new Timetable();
num_operation = input.nextInt();
for(int i=0;i<num_operation;i++) {
if(input.next() == "MODULE") {
code = input.next();
String day;
int start, end;
Schedule getLecSche = new Schedule(input.next(),input.nextInt(),input.nextInt());
Schedule getTutSche = new Schedule(input.next(),input.nextInt(),input.nextInt());
Schedule getLabSche = new Schedule(input.next(),input.nextInt(),input.nextInt());
Module userModule = new Module(code, getLecSche, getTutSche, getLabSche);
System.out.println("Reached line 162");
if(!userTimetable.checkClash(userModule)) {
userTimetable.add(userModule);
System.out.println("Added");
}
else
System.out.println("Clashed");
}
else if(input.next() == "COUNT"){
code = input.next();
System.out.println(userTimetable.count(code));
}
}
}
}