Мне очень жаль, что коды немного длинны, но их очень просто читать и понимать, потому что я новичок.Не удается получить действительный вывод массива
Проблема заключается в том, что я пытаюсь получить этот результат:
Black Image Constructor:
(0,0,0) (0,0,0) (0,0,0) (0,0,0)
(0,0,0) (0,0,0) (0,0,0) (0,0,0)
(0,0,0) (0,0,0) (0,0,0) (0,0,0)
Constructor with RGBColor[][] Array Parameter:
(0,0,0) (0,0,0) (0,0,0) (0,0,0)
(1,1,1) (1,1,1) (1,1,1) (1,1,1)
(2,2,2) (2,2,2) (2,2,2) (2,2,2)
и вместо этого, я просто получаю сообщение об ошибке.
Вот мои 3 коды:
RGBColor Класс:
/**
* This program is used to represent 3 Colors: Red, Green, Blue. (RGB)
* These colors hold values between 0 and 255.
*
*
* @Author Ilan Aizelman.
*/
public class RGBColor {
/**
* attributes: red, green and blue component of a color.
*/
private int _red,_green,_blue;
/**
* final variables.
*/
private final int MAX_VALUE = 255,MIN_VALUE = 0;
private final double THIRTY_PERCENT = 0.3,FIFTY_NINE_PERCENT = 0.59,ELEVEN_PERCENT = 0.11;
/**
* Consctructor which gets 3 colors (RGB), we check here if their range is valid (0 - 255), if not we assign black to it.
*
* @param red - The red color component value.
* @param green - The green color component value.
* @param blue - The blue color component value
*/
public RGBColor(int red, int green, int blue)
{
if(isValid(red,green,blue))
{
_red = red;
_green = green;
_blue = blue;
}
else
doBlack();
}
/**
* Construct a black RGBColor. i.e. red = green = blue = 0
*/
public RGBColor()
{
doBlack();
}
/**
* Here we check if the color number was entered correctly.
* It has to be an integer (whole number) between 0-255.
*
* @param nums - a component value, should be the number between 1-4
* @param return - return true if the number is between 1-4, false otherwise.
*/
private boolean isValid(int nums)
{
return ((nums >= MIN_VALUE) && (nums <= MAX_VALUE));
}
/**
* Here we check if the color number was entered correctly.
* It has to be an integer (whole number) between 0-255.
*
* @param red - the red component
* @param green - the green component
* @param blue - the red component
* @param return true if values are correct, false otherwise.
*/
private boolean isValid(int red, int green, int blue)
{
return ((red <= MAX_VALUE && red >= MIN_VALUE &&
green <= MAX_VALUE && green >= MIN_VALUE &&
blue <= MAX_VALUE && blue >= MIN_VALUE));
}
/**
* Returns RGB color string triplet with numbers between 0-255, i.e. (0,127,127)
*/
public String toString()
{
return ("(" + _red + "," + _green + "," + _blue + ")");
}
/**
* RGBColor will become the color Black. (0,0,0)
*/
private void doBlack()
{
_red = _green = _blue = 0;
}
}
RGBImage класс:
public class RGBImage
{
private int _rows, _cols;
final int ZERO_VALUE = 0;
private RGBColor[][] _pixels;
/**
* Constructor for objects of class RGBImage
*/
public RGBImage(int rows, int cols)
{
_rows = rows;
_cols = cols;
_pixels = new RGBColor[_rows][_cols];
for(int i = 0; i < _rows; i++)
{
for(int j = 0; j < _cols; j++)
{
_pixels[i][j] = new RGBColor();
}
//System.out.println();
}
}
public RGBImage(RGBColor[][] pixels)
{
_pixels = new RGBColor[pixels.length][pixels[0].length];
for(int i = 0; i < pixels.length; i++)
{
for(int j 0; j < pixels[0].length; j++)
{
_pixels[i][j] = new RGBColor(pixels[i][j]);
}
}
}
}
и мой StudentTester класс, который должен напечатать вывод, который я написал в начале:
public class StudentTester {
public static void main(String[] args) {
System.out.println("Black Image Constructor:");
RGBImage rgbImg0 = new RGBImage(3,4);
System.out.println(rgbImg0);
System.out.println("Constructor with RGBColor[][] Array Parameter:");
RGBColor[][] rgbArray1 = new RGBColor[3][4];
for (int i=0; i<rgbArray1.length;i++)
for (int j=0; j<rgbArray1[0].length;j++)
rgbArray1[i][j] = new RGBColor(i,i,i);
RGBImage rgbImg1 = new RGBImage(rgbArray1);
System.out.println(rgbImg1);
System.out.println("Have a Nice Work!");
}
}
Ошибка:
Black Image Constructor:
[email protected]
Constructor with RGBColor[][] Array Parameter:
[email protected]
Have a Nice Work!
EDIT:
Я добавил это сейчас,
I've added this:
public String toString() {
String pixelSet="";
for(int i=0; i<_rows;i++){
for(int j=0 ;j<_cols;j++){
pixelSet+=this._pixels[i][j].toString();
}
pixelSet +="\n";
}
return pixelSet;
}
и я не получаю «Конструктор с RGBColor [] [] массив параметров : «выход еще. все еще работаю над этим, спасибо, ребята!
решение: (edit2)
public RGBImage(RGBColor[][] pixels)
{
_rows = pixels.length;
_cols = pixels[0].length;
_pixels = new RGBColor[_rows][_cols];
for(int i = 0; i < _rows; i++)
{
for(int j = 0; j < _cols; j++)
{
_pixels[i][j] = new RGBColor(pixels[i][j]);
}
}
}
Просьба указать, что такое ошибка? Что такое выход ??? –
Вместо того, чтобы извиняться за длинный код, было бы более полезно, если бы вы создали пример * short *, который демонстрирует проблему, - попробуйте избавиться от битов кода за один раз, пока вы не решите проблему, или вы У нас есть короткий пример. Часто вы обнаружите, что процесс разработки короткой, но полной программы - это все, что вам нужно, чтобы найти проблему самостоятельно. –
[This] (http://stackoverflow.com/help/mcve) поможет вам написать минимальный, полный, проверенный пример, который легко ответить. – FlyingPiMonster