import java.util.*; import java.awt.Color; public class IncrementRedExamination { public static void main(String[] args) { int[][] pixel = new int[5][4]; int[][] copy = new int[5][4]; int[][] more = new int[5][4]; int[][] less = new int[5][4]; int delta = 50; Random die = new Random(); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 3; ++j) { int r = die.nextInt(256); int g = die.nextInt(256); int b = die.nextInt(256); copy[i][j] = pixel[i][j] = colorsToInt(r, g, b); int moreRed = ( r+delta > 255 ) ? 255 : r + delta; int lessRed = ( r-delta < 0 ) ? 0 : r - delta; more[i][j] = colorsToInt( moreRed, g, b); less[i][j] = colorsToInt( lessRed, g, b); } } System.out.println(); System.out.println(); System.out.println( "Picture pixels"); print( pixel ); System.out.println(); System.out.println(); System.out.println( "Increasing red by " + delta ); System.out.println(); System.out.println(); System.out.println( "Expected pixels"); print( more ); System.out.println(); System.out.println(); StudentCode.incrementRed( pixel, delta); System.out.println( "Got pixels"); print( pixel ); boolean redPlus = true; for (int i = 0; i < 4; ++i) { for (int j = 0; j < 3; ++j) { if ( pixel[i][j] != more[i][j] ) { System.out.printf( "Entry [ %1d ][ %1d ] wrong\n", i , j ); redPlus = false; } } } if ( redPlus ) { System.out.println( "Correctly incremented red" ); } System.out.println( " " ); System.out.println(); System.out.println(); System.out.println( "Picture pixels"); print( copy ); System.out.println(); System.out.println(); System.out.println( "Decreasing red by " + delta ); System.out.println(); System.out.println(); System.out.println( "Expected pixels"); print( less ); System.out.println(); System.out.println(); StudentCode.incrementRed( copy, -delta); System.out.println( "Got pixels"); print( copy ); boolean redLess = true; for (int i = 0; i < 4; ++i) { for (int j = 0; j < 3; ++j) { if ( copy[i][j] != less[i][j] ) { System.out.printf( "Entry [ %1d ][ %1d ] wrong\n", i , j ); redLess = false; } } } if ( redLess ) { System.out.println( "Correctly decremented red" ); } } public static void print( int[][] a ) { for (int i = 0; i < 4; ++i) { for (int j = 0; j < 3; ++j) { int c = a[i][j]; System.out.printf(" %8d=(%3d,%3d,%3d)", c, red(c), green(c), blue(c)); } System.out.println(); } } public static int colorsToInt(int r, int g, int b) { return 256*256*r + 256*g + b; } public static int red(int n) { Color c = new Color( n ); return c.getRed(); } public static int green(int n) { Color c = new Color( n ); return c.getGreen(); } public static int blue(int n) { Color c = new Color( n ); return c.getBlue(); } }