0

I have a few codes and im at the chapter of my class where im learning composition. I think this is very easy for you guys to fix but right now im having a hard time on this. Its about the code in the test src where im trying to put objects together and display the fields of an object. My System.out.println(tv.getResolution()); is giving me "section7.Resolution@1db9742" while im trying to get the 200,100 int value to display... Can anyone help me ive been stuck on this for like 2 hours and couldnt find anything on the internet. Tried to change the return types etc x.x

public class TV {

private String model;
private Resolution resolution;

public TV(String model, Resolution resolution) {
    this.model = model;
    this.resolution = resolution;
}
public String getModel() {
    return model;
}
public Resolution getResolution() {
    return resolution;
}}

\

public class Room {

private TV tv;
private Bed bed;
private Desk desk;

public Room(TV tv, Bed bed, Desk desk) {
    this.tv = tv;
    this.bed = bed;
    this.desk = desk;
}
public void turnLightsON() {
    roomLighting();
}
private void roomLighting() {
    desk.lamp();
}
public void  getResolution() {
this.tv.getResolution();
}
}

/

public class Resolution {
private int width;
private int height;

public Resolution(int width, int height) {
    this.width = width;
    this.height = height;
}
public int getWidth() {
    return width;
}
public int getHeight() {
    return height;
}}

/

public class Test {

public static void main(String[] args) {
    
    Bed bed = new Bed(100, 250, "LaBed");
    Desk desk = new Desk(110,175,"Black");
    Resolution resolution = new Resolution(200,100);
    TV tv = new TV("Samsung", resolution);
    
    Room room = new Room(tv,bed,desk);
    room.turnLightsON();
    System.out.println(tv.getResolution());
    System.out.println(tv.getModel()); }}
Zerii
  • 15
  • 3

2 Answers2

1

Resolution is an object and not a primitive type, so when you try to print it, you end up using the default toString() method which returns the class name and a hashcode. Either override the toString() method:

public class Resolution {
    // your code here
    public String toString() {
        return String.valueOf(width) + "x" + String.valueOf(height);
    }
}

Or do this:

System.out.println(tv.getResolution().getWidth());
System.out.println(tv.getResolution().getHeight());
Shan S
  • 658
  • 5
  • 18
  • Thankyou for your time and help ive done the 2nd resolution since it seems a bit easier , ill try to do the 1st one now ! – Zerii Jul 01 '20 at 17:06
  • Happy to hear. If this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. – Shan S Jul 01 '20 at 17:13
  • I cant give a +1 yet since im not rep 15 yet but ill come back one day and upvote it :p thankyou again! – Zerii Jul 02 '20 at 11:53
1

You should override toString() method in Resolution class.

You can take reference from here:-

See here