Make sure you can answer all of the questions from the first quiz we had. None of the questions below ask you to name types or classes or object methods or class methods, but you should be prepared to answer those.
Also review all of the six homeworks assigned to date.
I put all of the answers in blue. Please try and do all of the problems without looking at the answers. After you are done check your answers against mine. For the programming problems there are many different correct answers, I just give one.
ExamOne
that
prints Hello World
.
public class ExamOne { public static void main(String [] args) { System.out.println("Hello World"); } }
int age = 43; int minutes_in_day = 24 * 60; int minutes_in_year = 365 * minutes_in_day; // Birthday is Aug 19, 12 days left in August // then add in all the days to today for Sep to Feb 21. int days_since_last_birthday = 12 + 30 + 31 + 30 + 31 + 31 + 21; int minutes_alive = age * minutes_in_year + (days_since_last_birthday * minutes_in_day); System.out.println(minutes_alive);
To be really accurate I would have to include leap days.
int count = 0; Okay int age; Okay Double pi = 3.14159; Wrong, double should not be capitalized String myname = "Poindexter; Wrong, missing end quote boolean the answer = true; Wrong, spaces not allowed in variable name
image.jpg
.
Picture p = new Picture("image.jpg"); int num_bytes = p.getWidth() * p.getHeight() * 3; System.out.println(num_bytes);
image.jpg
by 25%. Your program should have a main
class that calls a method darken
. Write the darken
method as well and pretend that you put it in the file Picture.java
.
// Here is the method that goes in the Picture // class in the file Picture.java next to all of the other Picture // methods you have developed. public void darken() { Pixel[] pixelArray = this.getPixels(); for (Pixel pixelObj : pixelArray) { int r = (int) (pixelObj.getRed() * .75); // reduce each color by 25% int g = (int) (pixelObj.getGreen() * .75); int b = (int) (pixelObj.getBlue() * .75); pixelObj.setColor(new Color(r,g,b)); } }
Here is the main program class
public class ExamOne { public static void main(String [] args) { Picture p = new Picture("T:Harcourt/Spring2008/CS140/gallery/bug.jpg"); p.darken(); p.show(); } }
public class CircleTurtle { public static void main(String [] args) { World w = new World(); Turtle t = new Turtle(w); // Have turtle go in circle for (int i = 0; i < 12; ++i) { t.forward(50); t.turn(30); } } }
System.out.println("What do" + 1 + (2 + 3) + 4 + " I print?");
Type this into DrJava and see for yourself.
* ** *** **** *****
for (int i = 1; i <= 5; i++) { // print 5 rows, why do we start i at 1? for (int j = 0; j < i; j++) { // print i stars in a row System.out.print("*"); } System.out.println(); // print an end-of-line character after a row }
public void grayScale() { Pixel[] pixelArray = this.getPixels(); for (Pixel pixelObj : pixelArray) { int r = pixelObj.getRed(); int g = pixelObj.getGreen(); int b = pixelObj.getBlue(); int val = (r + g + b) / 3; pixelObj.setColor(new Color(val,val,val)); } }
int x; x = 1/3; System.out.println(x);
It prints a 0 because 1/3 uses integer division.
public class ExamOne { public static void main(String [] args) { Picture p = new Picture(FileChooser.pickAFile()); p.show(); } }
FileChooser.pickAFile()
a class method or an object method?
It is a class method
Be prepared to answer this question about many of the methods we have used this semester.
public class Exam1 { missing } public static void main (String [] args) { missing [] int XXXX = 7; System.out.println(XXXX); World w = new World(); Turtle tommy = new Turtle(w); missing parameter to Turtle constructor tommy.forward(200); missing ; int sum = 0; for (int i = 0; i < 10; i++) { missing ) sum = sum + i; "I" should be "i" } System.out.println(sum); } missing } }
getHeight()
getHeight()
pic
already declared. Write a statement
that sets the redness of the pixel at coordinate (40,70) to zero.
pic.getPixel(40,70).setRed(0);
pic
already declared. Write a Java statement
that sets the color of the pixel at coordinate (40,70) to black.
pic.getPixel(40,70).setColor(new Color(0,0,0));
int sum = 0; for (int i = 4; i < 9; i++) { sum = sum + i; } System.out.println(sum);
It would print 30.
Picture
method named
copyRegion
that given a picture object to modify and two sets of coordinates
copies a region of the current picture from the original picture object.
The first coordinate represents the upper left hand coordinate of the source picture and the
second coordinate represents the lower right coordinate of the source picture.
Here is how the method should be declared in the Picture
class. The
parameters ux
and uy
is the coordinate of the upper left
part of the region. The parameters lx
and ly
is the
lower right coordinate of the region to be copied.
public void copyRegion(Picture p, int ux, int uy, // upper left coordinate int lx, int ly) { // lower right coordinate // your code goes here }
Here is an example of how the method should be used. The example below
copies the just eye of the bug to the new picture dest
.
public class Exam1Hard { public static void main(String [] args) { Picture orig = new Picture("T:/Harcourt/Spring2008/cs140/gallery/bug.jpg"); Picture dest = new Picture(141,141); orig.copyRegion(dest,476,29,617,170); dest.show(); } }
Here is the copyRegion method that would go in the picture class.
// copy a region of the current picture to // to the picture p. public void copyRegion(Picture p, int ux, int uy, // upper left coordinate int lx, int ly) { // lower right coordinate // keep track of destination x coordinate int destx = 0; int desty = 0; // for each column i in the region for (int i = ux; i < lx; i++) { desty = 0; // for each row j in column i of the region for (int j = uy; j < ly; j++) { Color c = getPixel(i,j).getColor(); p.getPixel(destx,desty).setColor(c); desty++; } destx++; } }