flipHorizontal
, flipVertical
, rotateLeft
, rotateRight
, shrink
, and enlarge
. Make sure that you can
open a picture and call these from the main
method.
// Shrink a picture cutting in half the number of rows // and columns. This just takes every other pixel in // every other row. public void shrink(Picture p) { for (int destX = 0; destX < p.getWidth(); destX++) { for (int destY = 0; destY < p.getHeight(); destY++) { Pixel src_pixel = getPixel(destX*2,destY*2); Pixel dest_pixel = p.getPixel(destX, destY); dest_pixel.setColor(src_pixel.getColor()); } } } // enlarge a picture by doubling the number of c9olumns and rows. public void enlarge(Picture p) { // for every pixel in the bigger (destination) picture for (int destX = 0; destX < p.getWidth(); destX++) { for (int destY = 0; destY < p.getHeight(); destY++) { Pixel src_pixel = getPixel(destX/2,destY/2); Pixel dest_pixel = p.getPixel(destX, destY); dest_pixel.setColor(src_pixel.getColor()); } } } // column i becomes row i // row i becomes column width - i - 1 public void rotateRight(Picture p) { for (int col = 0; col < getWidth(); col++) { for (int row = 0; row < getHeight(); row++) { Color c = getPixel(col,row).getColor(); p.getPixel(p.getWidth() - row - 1, col).setColor(c); } } } // column i becomes row height - i - 1 // row i becomes column i public void rotateLeft(Picture dest) { for (int col = 0; col < getWidth(); col++) { // col x for (int row = 0; row < getHeight(); row++) { // row y Color c = getPixel(col,row).getColor(); dest.getPixel(row, dest.getHeight() - col - 1).setColor(c); } } } // switch pixel (x,y) with pixel (width - x - 1, y) public void flipHorizontal(Picture p) { for (int destX = 0; destX < p.getWidth(); destX++) { for (int destY = 0; destY < p.getHeight(); destY++) { Pixel src_pixel = getPixel(destX,destY); Pixel dest_pixel = p.getPixel(getWidth() - destX - 1, destY); dest_pixel.setColor(src_pixel.getColor()); } } } // switch pixel (x,y) with pixel (x, height - y - 1) public void flipVertical(Picture p) { for (int destX = 0; destX < p.getWidth(); destX++) { for (int destY = 0; destY < p.getHeight(); destY++) { Pixel src_pixel = getPixel(destX,destY); Pixel dest_pixel = p.getPixel(destX, getHeight() - destY - 1); dest_pixel.setColor(src_pixel.getColor()); } } }
yes
if the number is divisible by 173 or not.
Scanner keyboard = new Scanner(System.in); System.out.print("Enter an number: "); int num = keyboard.nextInt(); if (num % 173 == 0) { System.out.println("yes"); }
yes
if the number is between 70 and 100, and no
if it is not.
Scanner keyboard = new Scanner(System.in); System.out.print("Enter an number: "); int num = keyboard.nextInt(); if (num > 70 && num < 100) { System.out.println("yes"); } else { System.out.println("no"); }
x
from the
user and prints x
asterisks on the console.
Scanner keyboard = new Scanner(System.in); System.out.print("Enter an number: "); int num = keyboard.nextInt(); for (int i = 0; i < num; i++) { System.out.print("*"); }
x
, y
, and z
.
if (x > y && x > z) { System.out.println("x is the largest"); } if (y > x && y > z) { System.out.println("y is the largest"); } if (z > x && z > y) { System.out.println("z is the largest"); }
a
, b
, and c
are integer variables.
If there is something wrong with the statements below indicate what the problem is.
a) if a > b { missing parentheses around the conditional expression c = b; } b) if (a > b) { c = b missing semicolon } c) if (a > b { missing ')' parentheses c = b; System.out.println("a is bigger than b"); } else missing else part { } d) if (a > b) { c = b; } okay, all on one line is fine but ugly
!
is logical
negation. So !true
is the same as false and !false
is true. The precedence of &&
, ||, and !
is
that &&
has the highest precedence,
then ||
, and then !
. As an example consider the expression below:
int x = 5; int y = 7; System.out.println( x > y || !(3 > 4) && 5 <= 6 );
The first condition checked is if x > y
. This is false. Next we check if
!(3 > 4)
. This is true, 3 is not greater than 4.
Now we check if 5 is less than 6. This is true. Both sides of && are true so it evaluates to true.
Assuming that x
is 1
What is the output of the following:
int x = 1; System.out.println((true) && (3 > 4)); System.out.println(!(x > 0) && (x > 0)); System.out.println((x > 0) || (x < 0)); System.out.println(x != 0 || x == 0); System.out.println(x >= 0 || x < 0); System.out.println((x != 1) == !(x == 1));
Cut and paste these into DrJava and check the output
int x = 3; int y = 2; int z = 0; if (x > 2) { if (y > 2) { z = x + y; System.out.println("z is " + z); } } else { System.out.println("x is " + x); }
Cut and paste into DrJava and check the output
x
is 3 and y
is 4?
Cut and paste into DrJava and check the output
x
is 2 and y
is 2?
Cut and paste into DrJava and check the output
for (int i = 0; i <= 100; i++) { if (i % 7 == 0 || i % 11 == 0) { System.out.println(i); } }