0

I'm fairly new to Java, only been programming for a couple months with it so far.

I have two classes, TimeSlot and LabGroup.

In the TimeSlot class there is the code--

private Time start;
private Time end;
private String day;

public TimeSlot(String spec) {
    //splits given string on each space
    String[] splitSpec = spec.split(" ");
    day = splitSpec[0];

    //uses the class Time, and passes in the hour and the minute of the time the lab begins.
    this.start = new Time(splitSpec[1]);

    //uses the class Time, and passes in the hour and the minute of the time the lab finishes.
    this.end = new Time(splitSpec[2]);  
}

Then in the LabGroup class there is the code--

public String charLabel;
public TimeSlot timeSpec;
public String lineTime;

public LabGroup(String line) {

    String[] lineSplit = line.split(" ");
    charLabel = lineSplit[0];

    //string a = "Day StartTime EndTime"
    String a = lineSplit[1] + " " + lineSplit[2] + " " + lineSplit[3];

    timeSpec = new TimeSlot(a);


}

along with a toString method--

public String toString() {
    return "Group "+ charLabel + timeSpec+ "\n";

}

An example input into the LabGroup would be "A Mon 13:00 15:00" and should then give the output, through the toString, of --

Group A Mon 13:00 - 15:00
Group B Mon 15:00 - 17:00
Group C Tue 13:00 - 15:00
Group D Tue 15:00 - 17:00

But instead i am getting--

Group AlabExam1.TimeSlot@3f0fbfe5
, Group BlabExam1.TimeSlot@ea0e8b8
, Group ClabExam1.TimeSlot@25eab2ba
, Group DlabExam1.TimeSlot@37528b33
halfer
  • 19,824
  • 17
  • 99
  • 186

3 Answers3

0

You have provided method toString() in class LabGroup - and this method works (with some minor issues). The problem is that you haven’t provided method toString() in class TimeSpec.

0


when you do return "Group "+ charLabel + timeSpec+ "\n"; You're telling the program to return your object timeSpec as a string.
So basically it will call your TimeSlot toString function, which returns your TimeSlot@3f0fbfe5 (ClassName@HashCode). What you need to do is to override TimeSlot's toString, so that when it is called it gives a string in a format you've chosen. Hope it helps.

faz95
  • 48
  • 5
0

You need to override toString method because if you print charLabel it will simply call the toString method in Object class which returns return getClass().getName() + "@" + Integer.toHexString(hashCode());

So, you need to do either of below:

1) implement toString method in TimeSlot like this:

public String toString() {
    return day + " " + start + " - " + end;
}

2) Modify LabGroup toString method like below by introducing getter methods in TimeSlot

public String toString() {
    return "Group " + charLabel.getDay() + " " + charLabel.getStart() + " - " + charLabel.getEnd();

}
Shubhendu Pramanik
  • 2,711
  • 2
  • 13
  • 23