Hello, I have been trying to figure out how to do this for some time since I am
ID: 3880883 • Letter: H
Question
Hello, I have been trying to figure out how to do this for some time since I am getting back into programming. I know it is an easy question but maybe someone could help so I could at least move forward with my program. The goal is to print what looks like water(~) into a fish tank (a 2D array [8][32] size all with '~' characters.) This is what I have so far but it keeps giving me the wrong answer. An explanation of what I am doing incorrectly would help nicely. I have left instructors comments on the goal of the program.
public class Main {
public static void main(String[] args) {
char [][] tilde = new char [8][32];
fillTank(tilde, '~');
}
/**
* Copies the water character into every position in the tank array. The two-dimensional tank
* array can have dimensions of any size(s).
*
* @param tank will contain all water characters after this method is called.
* @param water is the character copied into the tank.
*/
public static void fillTank(char[][] tank, char water)
{
for (int row = 0; row < tank.length; ++row) {
for (int column = 0; column < tank[row].length; ++column) {
tank [row][column] = '~';
}
System.out.println();
}
renderTank(tank);
}
/**
* Prints the contents of the tank into the console in row major order, so that the
* smallest row indexes are on top and the smallest column indexes are on the left. For
* example:
* tank[0][0] tank[0][1] tank[0][2] ...
* tank[1][0] tank[1][1] tank[1][2] ...
* ...
* Each row is on its own line, and this method should work for two-dimensional tanks with
* dimensions of any size.
*
* @param tank contains the characters that will be printed to the console.
*/
public static void renderTank(char[][] tank)
{
System.out.print(tank);
}
}
RESULT IS:
[[C@7852e922
Explanation / Answer
It got fixed by printing each row of the 2d char array as a string, after converting. Because print or println method would do a byte printout when you call with a char array.
public class Main {
public static void main(String[] args) {
char [][] tilde = new char [8][32];
fillTank(tilde, '~');
}
/**
* Copies the water character into every position in the tank array. The two-dimensional tank
* array can have dimensions of any size(s).
*
* @param tank will contain all water characters after this method is called.
* @param water is the character copied into the tank.
*/
public static void fillTank(char[][] tank, char water)
{
for (int row = 0; row < tank.length; ++row) {
for (int column = 0; column < tank[row].length; ++column) {
tank [row][column] = '~';
}
System.out.println();
}
renderTank(tank);
}
/**
* Prints the contents of the tank into the console in row major order, so that the
* smallest row indexes are on top and the smallest column indexes are on the left. For
* example:
* tank[0][0] tank[0][1] tank[0][2] ...
* tank[1][0] tank[1][1] tank[1][2] ...
* ...
* Each row is on its own line, and this method should work for two-dimensional tanks with
* dimensions of any size.
*
* @param tank contains the characters that will be printed to the console.
*/
public static void renderTank(char[][] tank)
{
for(int row = 0; row < tank.length; ++row){
System.out.println(new String(tank[row]));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.