The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.

booBoo = booBoo + 2; yogiTheBear++; } System.out.println( yogiTheBear ); System.out.println( booBoo ); 2 pts e) for (int z=1; z!=3; z++) { System.out.println(z); }

Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by , 2016-03-12 04:03:02

CS/120 Exam #2 Sample Exam

booBoo = booBoo + 2; yogiTheBear++; } System.out.println( yogiTheBear ); System.out.println( booBoo ); 2 pts e) for (int z=1; z!=3; z++) { System.out.println(z); }

Name__________________________________

CS/120 Exam #2
Sample Exam

Please show all of your work.

1. For each part below write the term, symbols, or phrase from class that best fits the description.
(Each part is worth 2 points.)

a) This is the Java notation use in boolean expressions to denote the logical AND operation.

b) This is the name of the Java class that is automatically inherited by all other classes.

c) This is the name of any loop that would execute forever, unless the program is manually
terminated.

d) The client-supplier relationship between two classes is often described as a contains_a relation.
Similarly, the inheritance relationship between two classes is described as a(n) _______ relation.

2. Show the precise output (all lines) that result from executing the following segments of code.

6 pts a) int cold = 1;

int hot = 2;
if (cold >= 0 || hot >= 0) {

System.out.println("temperate");
if (cold+hot != 3) {

System.out.println( cold );
} else {

System.out.println( hot );
}
if (2*2 < 2+2) {

System.out.println("fini");
}
} else {
System.out.println("viola");
}
System.out.println( !(hot - cold > 0) );

6 pts b) int moo = 10;

int boo = 10;
while (moo < boo) {

System.out.println("moos not boos");
moo = boo - 1;
}
moo = 10;
boo = 10;
while (moo >= 4) {
moo--;
System.out.println( moo );
moo = moo - 2;
}

4 pts d) int yogiTheBear = 1;

int booBoo = 0;
while (yogiTheBear <= 1000)

booBoo = booBoo + 2;
yogiTheBear++;
}
System.out.println( yogiTheBear );
System.out.println( booBoo );

2 pts e) for (int z=1; z!=3; z++) {

System.out.println(z);
}

6 pts 3. The class diagram to the right depicts classes Rain and Snow that inherit Precip, class

FreezingRain inherits Rain and Cloud is a supplier class for Rain. Assuming the

name indicates the type of each variable, circle all of the following assignment Precip
instructions that are correct using the rules of type conformance (i.e., circle

those that do not result in compile-time errors due to lack of type conformance. )

Snow Rain Cloud

thePrecip = theFreezingRain; FreezingRain
theSnow = thePrecip;
theCloud = theRain;
thePrecip = theCloud;
theSnow = theRain
theRain = theFreezingRain;

Use the following classes to complete Exercises 4 through 7.

public class Thing1 { public class Thing2 extends Thing1 {

private Rectangle rect; private Oval ov;

public Thing1(int k) { public Thing2() {
rect = new Rectangle(1, 1, 1, k); // first line not shown
ov = new Oval(10, 10, 10, 10);
}
}
public void skwug(java.awt.Color c) {
rect.setBackground(c); public void skwug(int j) {
rect.setLocation(rect.getX()+5, 0); Rectangle r = getRect();
super.skwug(java.awt.Color.black);
} r.setLocation(r.getX()+j, 1);

protected Rectangle getRect() { }
return rect;
public void skwug(java.awt.Color c) {
} Rectangle r = getRect();
} r.setLocation(r.getX()+100, 0);

}
}

3 pts 4. Below is the start of a class diagram to describe these classes. Add the proper symbol to show
the relationship between the classes. (Do not worry about instance variables or methods.)

Thing1 Thing2

2 pts 5. The Thing2 class is missing the first instruction in its constructor method. Write an instruction
that would be syntactically correct. (It doesn't matter what the instruction does.)

6 pts 6. Supposing that the code below is located in a Driver class, circle every instruction that is
syntactically incorrect.

Thing1 thing1 = new Thing1(2);
Thing2 thing2 = new Thing2();
thing1.setBounds(1, 2, 3, 4);
thing1.skwug( java.awt.Color.green );
thing1.skwug( 2 );
thing2.skwug( java.awt.Color.blue );
thing2.skwug( 3 );
super.skwug( java.awt.Color.blue );

8 pts 7. What is the X position of the rect variable when each of the following code segments complete
execution?

a) Thing1 thing1 = new Thing1(77);

thing1.skwug( java.awt.Color.white );

b) Thing2 thing2 = new Thing2();

thing2.skwug( 1000 );

c) Thing2 thing2 = new Thing2();

thing2.skwug( java.awt.Color.white );

Assuming the methods below are in the same class, use them to complete Exercises 8 and 9.

private boolean isOrganized(Oval v1, Oval v2, Oval v3) {
return (v1.getX() <= v2.getX() && v2.getX() <= v3.getX())
|| (v3.getX() <= v2.getX() && v2.getX() <= v1.getX()) ;

}

// The method below returns the distance in pixels between
// the center points of v1 and v2.
private double distanceBetweenCenters(Oval v1, Oval v2) {

if (v1.getParent() == null || v2.getParent() == null) {
return 0;

} else if (v1.getParent() != v2.getParent()) {
return 0;

} else {
double centerX1 = v1.getX() + v1.getWidth()/2;
double centerY1 = v1.getY() + v1.getHeight()/2;
double centerX2 = v2.getX() + v2.getWidth()/2;
double centerY2 = v2.getY() + v2.getHeight()/2;
return Math.sqrt( (centerX1-centerX2)*(centerX1-centerX2)
+ (centerY1-centerY2)*(centerY1-centerY2) );

}
}

6 pts 7. What is output via System.out.println when the following code executes?

Oval dotA = new Oval(1, 2, 3, 4);
Oval dotB = new Oval(10, 0, 0, 0);
Oval dotC = new Oval(4, 3, 2, 1);
System.out.println( isOrganized(dotA, dotB, dotC) );
System.out.println( isOrganized(dotB, dotC, dotA) );
System.out.println( isOrganized(dotA, dotA, dotA) );

5 pts 8. Assuming that an array, called dots, stores at least five Oval objects. Complete a

System.out.println instruction below so that it outputs the distance between the Ovals in the
first two array cells.

20 pts 9. On the next page is the beginning of a program intended to grow and shrink an Oval called dot. Your
completed program should automatically change the dot's diameter from 100 to 150 after 10 seconds.
After 2 seconds at a size of 150, the dot's diameter should change to 250; after another 7 seconds it
should change from 250 back to 100. This behavior of changing 100 to 150 after 10 sec. to 150 to
250 after 2 sec. to 250 to 100 after 7 sec., and so forth, should continue as long as the program
executes. Note that you will want to use the EventTimer class diagrammed on the class diagram
sheet.

import javax.swing.*; {

public class Driver

private JFrame win;
private Oval dot;

public Driver() {
win = new JFrame( "Spot Before My Eyes" );
win.setBounds (20, 20, 300, 320 );
win.setLayout( null );
win.setVisible( true );
dot = new Oval(10, 10, 100, 100);
dot.setBackground( java.awt.Color.red );
win.add( dot, 0 );
win.repaint();

}

}

import java.awt.*;
import java.awt.event.*;

public class GrowthTimer

EventTimer

«constructor»
+ EventTimer( int )

«update»
+ void start()
+ void stop()

«event handler»
+ void actionPerformed( java.awt.event.ActionEvent )

SimpleList<ItemType>

«constructor»
+ SimpleList<ItemType>()

«update»
+ void add( ItemType )
+ void remove( )
+ void reset( )

«query»
+ boolean hasNext( )
+ ItemType next( )
+ int size( )

Oval

«constructor»
+ Oval( int, int, int, int )

«update»
+ void setBackground( java.awt.Color )
+ void setBounds( int, int, int, int )
+ void setLocation( int, int )
+ void setSize( int, int )
+ void repaint()
+ void add( java.awt.Component, int )

«query»
+ int getX( )
+ int getY( )
+ int getWidth( )
+ int getHeight( )
+ Color getBackground( )
...


Click to View FlipBook Version