I'm really sorry on advance that the codes are little long - but they're very simple to read and understand because I'm a beginner.
The problem is that I'm trying to get this output:
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)
and instead of that, I'm just getting errors.
Here are my 3 codes:
RGBColor Class:
/**
* 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 class:
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]);
}
}
}
}
and my StudentTester class which has to print the output I wrote in the beginning:
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!");
}
}
The error is:
Black Image Constructor:
RGBImage@47520d
Constructor with RGBColor[][] Array Parameter:
RGBImage@1aa4b35
Have a Nice Work!
EDIT:
I've added this now,
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;
}
and I'm not getting the "Constructor with RGBColor[][] Array Parameter:" output yet. still working on it, thanks guys!
solution: (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]);
}
}
}
Here you are printing an object.
System.out.println(rgbImg0);
But you have not defined what you want to happen when you print a RGBImage
object. When you pass an object reference to a print statement, the objects toString();
method is called and the result is printed out. By default Object
subclasses print out a reference, something like RGBImage@47520d
which is ClassName + @ + Hashcode
.
You can override the toString()
method of RGBColor
to print what you need.
@Override
public String toString() {
return "("+_red+","+_green+","+_blue+")";
}
Then you can override the toString()
method of RGBImage
class to print your array of RBGColor
objects.
public String toString() {
// If your not too bothered about formatting, you can do it in a single line.
// return Arrays.deepToString(_pixels);
String pixelSet="";
for(int i=0; i<_pixels.length;i++){
for(int j=0 ;j<_pixels[i].length;j++){
pixelSet+=this._pixels[i][j].toString();
}
pixelSet +="\n";
}
return pixelSet;
}
I hope this helps.
Hi @Illan Aizelman WS,
I don't see any compilation error in your code, but looking at the output I could see that what you are getting is the toString
representation of the object you are trying to print. If you want to get the output as indicated in your question, simply override toString
method of java.lang.Object
class in RGBImage
. If you directly try to print the object using System.out.printXX
you will not get the desired output. Refer to the following steps.
toString
method in RGBImage
class. toString
method. You have to implement toString in your RgbImage class as well (similar to your RgbColor class). The standard implementation simply prints the objectId of your RgbImage instance.
Think about what should happen if you print a RgbImage instance. You could for instance call toString of all individual pixels of that image, you could apply some extra formatting, etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.