Cs Homework 04-18-17

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Yuriy Belz

April 18, 2017


Courtney
Introduction to Programming 2

Pg. 249 Ex 10.1.1 Pg. 254 10.1.2 Pg. 254 10.1.3 Pg. 256 10.1.4 Pg. 264 10.5.1

10.1.1 - define an interface called visualinterface that contains methods show, hide, and
setcolor

Public interface visualinterface {

Public void show()

Public void hide()

Public void setColor(Color newColor)

10.1.2 - use the definition of the resizable interface to complete the exercises below and on the
following pages.

Public interface resizeable {

Public void resize ( double size);

Public boolean contains ( location point );

A. Write the class header for a class called square that implements the resizable interface
B. What methods must the square class define to legally implement the resizable
interface?
C. Finish the square class. The constructor should take three parameters, a location, a
double for size, and the canvas. It should draw a filled rectangle at the location with each
side having length specified by the size of the parameter. The resize method should set
the height and width of the filledrect to the size determined by the double parameter. The
contains method should return true if the filledrect contains the location that was passed
as a parameter. Otherwise it should return false.

Public class square implements resizeable

Filledrect square;

Public square (location point, double size, canvas acanvas)

Square = new filledrect (point.getx(), point.gety(), size, size, acanvas);

Public void resize ( double size)

square.setwidth(size);

square.setheight(size);

Public boolean contains ( location point );

Return (square.contains (point))

10.1.3 - design a class called circle that resembles square except that the instance variable for
the circle class is a filledoval

Public class circle implements resizeable

Filledoval circle;

Public circle (location point, double size, canvas acanvas)

circle = new filledoval (point.getx(), point.gety(), size, size, acanvas);

}
Public void resize ( double size)

circle.setwidth(size);

circle.setheight(size);

Public boolean contains ( location point );

Return (circle.contains (point))

10.1.4 - using the resizable interface from exercise 10.1.2 and the classes square and circle
write a program called shapesizer. When the mouse is pressed, the program should randomly
decide to draw either a circle or a square at the point of press. When the shape is drawn it
should have size 100. When the mouse is released the shape should shrink to half its original
size.

Public class shapesizer

private RandomIntGenerator generator = new RandomIntGenerator ( 0,1);

Public void begin()

Public void onmousepress (location point)

If (value() == 1)

Circle shape = new circle (point, 100, canvas);

Else

Square shape = new square (point, 100, canvas);

}
public int value()

return generator.nextValue();

Public void onmouserelease(location point)

If (shape.contains (point);

Shape.resize (50);

10.5.1 - given the alphabet interface above and the two classes below that implement the
alphabet interface which statements are legal? Assume that each class has a constructor that
takes no parameters.

A. Private alphabet letter = new a(); not legal


B. Private a letter = new A(); legal
C. Private a letter = new Alphabet(); not legal
D. Private alphabet letter = new ()b; not legal
E. B letter = new b();
letter.v(true); legal
F. alphabet letter = new b();
letter.v(true); not legal
G. Alphabet letter = new a();
letter.w(true); not legal
H. a letter = new a();
letter.w(true); legal
I. Alphabet letter = new a();
letter.x(color.red); legal

You might also like