-2

I am new to Java and relatively to programming. I have been working on this problem for a week and can't seem to get it. I am trying to create an object in a test class from a class that contains int type parameters and 1 array parameter in the constructor, getter and setter methods in the class from which the object is being created. I think the problem is when I am trying to pass the arguments into the parameters of the object but I am not sure.

Also, This is my first time using this site, let me know if I am suppose to format this text box differently. I read something about the code button for readability, I tried it but not sure if it worked?

// Constructor
public House(int numberOfFloors, int numberOfWindows, int bathrooms, int[] numberOfRooms) {
    super(numberOfFloors, numberOfWindows); 
    numberOfBathrooms = bathrooms;
    rooms = numberOfRooms;
}

// Accessor/Getter
public int[] getRooms() {
return rooms;
}

// Mutator/Setter
public void setRooms(int[] rooms){
    this.rooms = rooms;
}

// toString
public String toString() {
    String result;
    result = "This House has " + numberOfBathrooms + " bathrooms, and "
    + rooms + " rooms. " + super.toString();
    return result;
}

// Object
House myHouse = new House(2, 9, 3, new int[]{28, 45, 65, 23, 45});
System.out.println(myHouse);   

// Output
// This House has 3 bathrooms, and [I@2a139a55 rooms.

Ryan

Gabriel Ilharco
  • 1,649
  • 1
  • 21
  • 34
Ryan
  • 11
  • 1
  • 2
    You need to provide the error you are getting and your desired behavior. – Luke Joshua Park Jan 23 '16 at 23:18
  • The code button does not reformat your code. It is roughly equivalent of surrounding it with `
    ...
    `. Your code needs to be properly indented before you paste it ... if you want it to look readable.
    – Stephen C Jan 23 '16 at 23:24
  • Also, note that this is not valid Java code, as written. There is no `class` declaration, etcetera. – Stephen C Jan 23 '16 at 23:27
  • I think the answer you're looking for is [here](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) in part and [here](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(int[])) as a simple solution – MadProgrammer Jan 23 '16 at 23:27
  • "This House has 3 bathrooms, and \[I@2a139a55 rooms." Is this your problem? If so, check @MadProgrammer's comment. As for the overall question quality: you did not post your whole code and you did not state your expected and observed behaviour clear enough (which is vitally important when seeking debugging-help). You may want to take a look at the [How do I ask a good question? - page](http://stackoverflow.com/help/how-to-ask) as well as the [On-Topic - page](http://stackoverflow.com/help/on-topic). – Turing85 Jan 23 '16 at 23:33

2 Answers2

1

I assume your problem is the output of "[I@2a139a55" instead something like {10, 20, 30}

You intend to print the content of your rooms variable to the console.

But that variable is an array of int.

Java does not provide/implement the toString for array types, so as default the address/reference of the object (=pointer) is printed to the console.

If you want to print the values of your array, you can use a "for loop" to iterate through the values of the array.

Boardwish
  • 495
  • 3
  • 16
1

I am not sure why rooms is an array here, but let's suppose it contains room numbers/IDs. Then, you would like to have the Arrays.toString() method to get a proper string of an array, as the default toString() method of an object is not much useful here, see object toString()

// toString
public String toString() {
    String result;
    result = "This House has " + numberOfBathrooms + " bathrooms, and "

+ Arrays.toString(rooms) + " rooms. " + super.toString(); return result; }

Community
  • 1
  • 1
Dongxu Li
  • 46
  • 1