CS49J Lesson 3 – Using Graphics

Ch 2: 2.7 - 2.10

Big Java 6

When you come in

  1. Connect to the Internet
  2. Log in to Piazza
  3. Start Eclipse or your IDE
  4. Navigate to laughton.com/obrien/sjsu/
    cs49j/lessons and open this lesson

Questions

Activity - Object reference

Record your participation in Piazza clicker question Lesson3 Q1

Given this definition

Rectangle rec = new Rectangle(100, 200, 50, 80);

what does the variable rec hold?

  1. the object
  2. the location of the object
  3. the data in the object
  4. can not be determined from the information given

 

Activity - Two references to the same object

Record your participation in Piazza clicker question Lesson3 Q2

Given this code

Rectangle block = new Rectangle(10, 20, 30, 50);
Rectangle block2 = block;
block2.grow(0, 10);
System.out.println(block.getHeight() + " : " + block2.getHeight());

What is printed? (Remember the grow method adds to both sides.)

  1. 50.0 : 70.0
  2. 70.0 : 70.0
  3. 50.0 : 50.0
  4. 70.0 : 50.0

 

Concept - Pass by value, pass by reference

Activity - Creating two rectangles with same values

Record your participation in Piazza clicker question Lesson3 Q3

What does this code print? (Remember the grow method adds to both sides.)

Rectangle tile1 = new Rectangle( 10, 30, 50, 100);
Rectangle tile2  = new Rectangle( 10, 30, 50, 100);
tile2.grow(5, 0);
System.out.println(tile1.getWidth() + " " +  tile2.getWidth());
  
  1. 60 50
  2. 50 50
  3. 50 60
  4. 60 60

Activity - String methods

Record your participation in Piazza clicker question Lesson3 Q4

What is printed here?

	   String farewell = "Goodbye, Moon";
farewell.toLowerCase();
farewell.replace("o", "");
System.out.println(farewell);
  1. goodbye, moon
  2. gdbye, mn
  3. Goodbye, Moon
  4. none of the above

Activity - String methods

Record your participation in Piazza clicker question Lesson3 Q5

What is printed here?

	   String farewell = "Goodbye, Moon";
String copy = farewell.toLowerCase();
copy = copy.replace("o", "");
copy = copy.replace("e", "3");
System.out.println(copy);
System.out.println(farewell);

  1. gdby3, mn
    gdby3, mn
  2. gdby3, mn
    Goodbye, Moon
  3. something else

Activity - Constructing graphics shapes

Record your participation in Piazza clicker question Lesson3 Q6

Draw a staircase like the one below. The upper left hand corner is at (20, 10). Each step is 20 pixels tall. The first step is 20 pixels wide. Each of the following steps is 20 pixels wider than the one above.

''

Here is a zip containing the frame and component classes needed for drawing in a graphical application. Make a project in your IDE and add them. You can use this same set of starter classes for any graphics project we do.