What is the proper way of storing a String in a wrapper class for a String. For example, I have the following two classes which are only wrapper classes for a string value:
public class ValueClass {
//holds the address
public String value;
}
public class KeyClass {
//holds the name
public String key;
public int hashCode() {
//returns the hashcode of the key
return (key.hashCode());
}
}
I then have another class, Record, which holds the key and the value:
public class Record {
private KeyClass key;
private ValueClass value;
public boolean equals(Record t) {
// comparison based only on KeyClass field
return key.equals(t.key);
}
public int hashCode() {
// depends only on KeyClass field
return key.hashCode();
}
public KeyClass getKey() {
return key;
}
public void setKey(KeyClass key) {
this.key = key;
}
public ValueClass getValue() {
return value;
}
public void setValue(ValueClass value) {
this.value = value;
}
}
When I create an instance of Record, I receive input from the user as two separate strings. How do I put the received Strings in Key and Value respectively?
This is what I thought it should be, but no joy:
KeyClass key = new KeyClass(inStrOne);
ValueClass value = new ValueClass(inStrTwo);
Record insertRecord = new Record();
insertRecord.setKey(key);
insertRecord.setValue(value);
I understand why this doesn't work, KeyClass and ValueClass do not have a constructor, which I am not allowed to have in this assignment for these two classes.
I know that this is going to be something incredibly easy, any insight/help/suggestions?
public class ValueClass {
//holds the address
public String value;
}
public class KeyClass {
//holds the name
public String key;
public int hashCode() {
//returns the hashcode of the key
return (key.hashCode());
}
}
I then have another class, Record, which holds the key and the value:
public class Record {
private KeyClass key;
private ValueClass value;
public boolean equals(Record t) {
// comparison based only on KeyClass field
return key.equals(t.key);
}
public int hashCode() {
// depends only on KeyClass field
return key.hashCode();
}
public KeyClass getKey() {
return key;
}
public void setKey(KeyClass key) {
this.key = key;
}
public ValueClass getValue() {
return value;
}
public void setValue(ValueClass value) {
this.value = value;
}
}
When I create an instance of Record, I receive input from the user as two separate strings. How do I put the received Strings in Key and Value respectively?
This is what I thought it should be, but no joy:
KeyClass key = new KeyClass(inStrOne);
ValueClass value = new ValueClass(inStrTwo);
Record insertRecord = new Record();
insertRecord.setKey(key);
insertRecord.setValue(value);
I understand why this doesn't work, KeyClass and ValueClass do not have a constructor, which I am not allowed to have in this assignment for these two classes.
I know that this is going to be something incredibly easy, any insight/help/suggestions?
