Reputation: 33
below is a program where I need to store String time and int datum as objects in an array and then print them out. This is what I am up to currently. I believe it is an issue with my recordEvent method.
class RecordEvents4 {
public static void main(String[] args){
Recorder4 r1 = new Recorder4(100,100,"Wombat Detection");
r1.recordEvent("10:53",20);
r1.recordEvent("10:59",20);
r1.recordEvent("11:05",20);
r1.recordEvent("12:59",10);
r1.recordEvent("13:59",10);
r1.recordEvent("14:06",10);
r1.printEvents();
}
}
class Recorder4 {
int xPos, yPos;
String eventType;
EventInformation[] event = new EventInformation[10];
int xevent = 0;
final int EVENT_MAX = 10;
Recorder4(int xPos, int yPos, String eventType){
this.xPos = xPos;
this.yPos = yPos;
this.eventType = eventType;
}
public void recordEvent(String time, int datum){
/*for(int i = 0; i<event.length; i++){
event[i] = new EventInformation(time, datum);
}*/
event[xevent] = new EventInformation(time, datum);
xevent++;
if(xevent > EVENT_MAX){
System.out.println("Event log overflow - terminating");
System.exit(1);
}
}
void printEvents(){
System.out.println("Record of " + eventType + " events at [" + xPos + "," + yPos + "]");
for(int i = 0; i < xevent; i++){
System.out.println("Event number " + i + " was recorded at " + event[i] + " with datum = " + event[i]);
}
}
}
class EventInformation {
String eventTime;
int eventDatum;
EventInformation(String eventTime, int eventDatum){
this.eventTime = eventTime;
this.eventDatum = eventDatum;
}
}
This is what it is outputting:
Record of Wombat Detection events at [100,100]
Event number 0 was recorded at EventInformation@15db9742 with datum = EventInformation@15db9742
Event number 1 was recorded at EventInformation@6d06d69c with datum = EventInformation@6d06d69c
Event number 2 was recorded at EventInformation@7852e922 with datum = EventInformation@7852e922
Event number 3 was recorded at EventInformation@4e25154f with datum = EventInformation@4e25154f
Event number 4 was recorded at EventInformation@70dea4e with datum = EventInformation@70dea4e
Event number 5 was recorded at EventInformation@5c647e05 with datum = EventInformation@5c647e05
This is what I am expecting:
Record of Wombat Detection events at [100,100]
Event number 0 was recorded at 10:53 with datum = 20
Event number 1 was recorded at 10:59 with datum = 20
Event number 2 was recorded at 11:05 with datum = 20
Event number 3 was recorded at 12:59 with datum = 10
Event number 4 was recorded at 13:59 with datum = 10
Event number 5 was recorded at 14:06 with datum = 10
I have been advised to use toString() but I am unsure whether I am even in the right direction with the recordEvent() where I am meant to be assigning the method arguments to to an EventInformation object, a reference to which is stored in an element of the event array.
Upvotes: 1
Views: 367
Reputation: 248
Your printEvents()
method should look like this:
void printEvents(){
System.out.println("Record of " + eventType + " events at [" + xPos + "," + yPos + "]");
for(int i = 0; i < xevent; i++){
System.out.println("Event number " + i + " was recorded at " + event[i].eventTime + " with datum = " + event[i].eventDatum);
}
}
In your original code, you are trying to print the instance of EventInformation
two times, instead of printing first the eventTime
field and then eventDatum
. Hence you get the defualt String representation of the instance, as defined in Object
.
That, or write getter methods for EventInformation
, if you'd like to, while setting the fields as private
, which would probably be better practice, especially if there's more you'd like to do with the class later on.
EDIT: Same as Oliver-R said.
Upvotes: 0
Reputation: 173
You are effectively asking Java to print out an element of your events array. In your declaration of this array, you gave elements inside it the EventInformation
type. Java, by default, obviously doesn't know how to print this, since you defined it yourself! To fix this, change your print statement:
System.out.println("Event number " + i + " was recorded at " + event[i].eventTime + " with datum = " + event[i].eventDatum);
Now you are asking Java to print eventTime
(a string) and eventDatum
(an int). These are both things that Java knows how to print, since these are defined by Java itself.
Upvotes: 1