Docsity

Assignment 8: Methods and Algorithms - Computer Science I | MAT 2170, Lab Reports of Computer Science

Toggle sidebar
bg1
Mat 2170
Week 8
Methods – Algorithms
Spring 2009
1
Student Responsibilities
IReading: Textbook, Chapter 5, 6.2
IPrelab & Lab
IAttendance
ILab 8, Exercise 3: set graphics window size to 700 by 700 pixels
2
Applying Programming Tools to Problems
ISuppose you have learned how to use a hammer, sander, drill, and
saw, and to apply polyurethane finishes to wood — by building a
small bird house.
INow, suppose further that you have been given the task of building
a large roll–top desk. And that you will be judged on the
sturdiness, usefulness, and elegance of this desk.
IHow would you begin? Some sort of plan is needed.
IIn programming, this plan is called an algorithm.
3
Algorithms
IAlgorithms express the logic of a solution strategy – the steps
necessary to accomplish a task.
IA programming problem should be broken down into logical
sub–problems by finding a general algorithm — one that outlines
your overall solution strategy.
IThe algorithms for these sub–problems are then further refined,
until they are easily implementable.
IThese specific algorithms should then be implemented as
methods, while the general algorithm provides the plan for the
order and way in which these methods will be used to solve the
given problem.
4
Methods
IMethods are important in programming because
1. they are the building blocks of a solution
2. they allow for easier re–use of key blocks of code
3. they give meaningful names to logical blocks of code
4. their interfaces describe exactly the values needed and returned
5. they allow us to more easily solve large problems, and to test our
solutions for correctness
IAlgorithms for solving a particular problem can vary widely in their
efficiency — it makes sense to think carefully when developing
an algorithm because making a bad choice can be extremely costly.
5
Where to Get Information on Classes and Methods
Iread the textbook, notes, etc.
Ijavadoc:jtf.acm.org/javadoc/student/index.html
Inetbeans and acmLibrary inspection
Isearch the internet for information (java.sun.com)
Is the class part of acmLibrary or another library, such as
java.awt?
6
pf3
pf4
pf5

Partial preview of the text

Download Assignment 8: Methods and Algorithms - Computer Science I | MAT 2170 and more Lab Reports Computer Science in PDF only on Docsity!

Mat 2170

Week 8

Methods – Algorithms

Spring 2009

1

Student Responsibilities

I (^) Reading: Textbook, Chapter 5, 6.

I (^) Prelab & Lab

I (^) Attendance

I (^) Lab 8, Exercise 3: set graphics window size to 700 by 700 pixels

2

Applying Programming Tools to Problems

I (^) Suppose you have learned how to use a hammer, sander, drill, and saw, and to apply polyurethane finishes to wood — by building a small bird house.

I (^) Now, suppose further that you have been given the task of building a large roll–top desk. And that you will be judged on the sturdiness, usefulness, and elegance of this desk.

I (^) How would you begin? Some sort of plan is needed.

I (^) In programming, this plan is called an algorithm.

3

Algorithms

I (^) Algorithms express the logic of a solution strategy – the steps necessary to accomplish a task.

I (^) A programming problem should be broken down into logical sub–problems by finding a general algorithm — one that outlines your overall solution strategy.

I (^) The algorithms for these sub–problems are then further refined, until they are easily implementable.

I (^) These specific algorithms should then be implemented as methods, while the general algorithm provides the plan for the order and way in which these methods will be used to solve the given problem.

4

Methods

I (^) Methods are important in programming because

  1. they are the building blocks of a solution
  2. they allow for easier re–use of key blocks of code
  3. they give meaningful names to logical blocks of code
  4. their interfaces describe exactly the values needed and returned
  5. they allow us to more easily solve large problems, and to test our solutions for correctness

I (^) Algorithms for solving a particular problem can vary widely in their efficiency — it makes sense to think carefully when developing an algorithm because making a bad choice can be extremely costly.

Where to Get Information on Classes and Methods

I (^) read the textbook, notes, etc.

I (^) javadoc: jtf.acm.org/javadoc/student/index.html

I netbeans and acmLibrary inspection

I (^) search the internet for information (java.sun.com)

Is the class part of acmLibrary or another library, such as java.awt?

The GPoint Class

I (^) The acm.graphics package provides the class GPoint which allows us to combine two double values into a single encapsulated unit.

I (^) A GPoint can represent a point in the graphics window, a point in the Euclidean plane, or just a couple of related values.

I (^) The primary advantage of encapsulating two individual values into a composite object is that the object can then be passed from one method to another as a single entity, via the return statement.

7

GPoint Constructor and Methods

new GPoint(x, y) Creates a new GPoint object containing the coordinate values x and y.

object.getX() returns the x component of a GPoint

object.getY() returns the y component of a GPoint

object.setLocation(x, y) Changes the coordinates of the object to the point (x, y)

object.translate(dx, dy) Modifies the GPoint object by adding dx to its x coordinate and dy to its y coordinate.

8

Sample GPoint Instantiation

GPoint p = new GPoint(x, y);

GPoint WorldCenter = new GPoint(0.0, 0.0);

GPoint JuliaTerm = new GPoint(-0.9, 0.12);

GPoint JuliaTerm = new GPoint(1.0, 0.0);

Methods are able to return a GPoint object, for example: return p; // as created above or return new GPoint(Re, Im);

9

Accessing GPoint Coordinates

Given the method header:

public Color JuliaColor(GPoint p)

we can gain access to p’s x and y values by:

GPoint Z = new GPoint(p.getX(), p.getY());

10

Julia Sets

I (^) To every point (a, b) in the plane we can associate a function of two variables — referred to as the Julia Map for (a, b) — denoted by F(a,b).

I (^) F(a,b) is described by the formula:

F(a,b)(x, y ) = (x^2 āˆ’ y 2 + a, 2 xy + b).

I (^) For example:

F(āˆ’ 1 ,0)(x, y ) = (x^2 āˆ’ y 2 + (āˆ’1), 2 xy + 0) = (x^2 āˆ’ y 2 āˆ’ 1 , 2 xy )

I (^) If we start with a point P 0 = (x 0 , y 0 ) and compute P 1 = F(a,b)(P 0 ), P 2 = F(a,b)(P 1 ), P 3 = F(a,b)(P 2 ), P 4 = F(a,b)(P 3 ) etc., we observe two qualitatively different types of behavior.

I The sequence of points P 0 , P 1 , P 2 , P3,... either

I (^) starts to get farther and farther away from the origin, or

I (^) the sequence stays pretty close to the origin

I (^) The third lab exercise this week is to complete a skeleton program to generate pictures of Julia sets and their surroundings.

I (^) A grid of tiny square blocks (much like the checker boards we’ve already seen) will be drawn in a graphics window.

I (^) These blocks represent a grid of points in a square region of the Cartesian plane.

I (^) Each block is to be colored according to how the Julia set coloring algorithm would color the corresponding points in the plane. (The Julia coloring code is provided for you.)

19

The Big Picture

I (^) In order to know what color a block in the graphics window should be, we need to know the Julia Color of it’s corresponding point in the Cartesian plane.

I (^) The main idea in this problem, then, is to map each block in the graphics window to a corresponding point in the ā€œworldā€ region that we are representing.

I (^) This is accomplished by the ScreenToWorld() method.

I (^) ScreenToWorld() is passed a GPoint representing a point in the graphics window, and it returns a GPoint representing the coordinates of the corresponding point in the world region.

20

For each block in the window grid:

  1. find its upper left corner position (using method BlockCorner())
  2. find the corresponding point in the world region (using method ScreenToWorld())
  3. find the color this point (and hence, block) should get (using method JuliaColor())
  4. draw a block of the correct size, location, and color to represent the Julia map

The method Norm() is used by JuliaColor(), and the method NextPoint() is just the Julia map mentioned earlier.

21

JuliaSet Constants

// Size of graphics window public static final double ScreenSize = 700;

// Size of the real world (Euclidean plane) region public static final double WorldSize = 4.0;

// Center of the world region public static final GPoint WorldCenter = new GPoint(0.0, 0.0);

// Number of rows and columns in screen grid public static final int GridSize = 350;

22

// size of squares public static final double BlockSize = ScreenSize / GridSize;

// Number of colors used for the display public static final int MaxColors = 11;

// Maximum number of iterations before a // number is declared in the Julia set public static final int MaxIterations = 40;

// Distance beyond which a point will not // return public static final double Threshold = 2.0;

To Be Completed

// the position of the world point corresponding // to the screen point

public GPoint ScreenToWorld(GPoint p) { // Replace this code return new GPoint(0.0, 0.0); }

// position of block at row, col

public GPoint BlockCorner(int row, int col) { // Replace this code return new GPoint(0.0, 0.0); }

To Be Completed

public class JuliaSet extends DualSliderProgram {

public void init() { setSize(700, 700); super.init(); setRangeA(-75, 75); setRangeB(0, 100); }

public void run() { // the a and b of F_a,b(x,y) double a = getA() / 100.0; double b = getB() / 100.0; GPoint JuliaTerm = new GPoint(a, b);

// add code to draw Julia blocks here

} }

25

Scaling Between Graphic and Cartesian Coordinates

4 units

(0, 0)

(0, 0)

700 pixels

700 pixels 4 units

graphics window

Cartesian plane

26

Scaling Between Graphic and Cartesian Coordinates

4 units

(0, 0)

700 pixels

700 pixels 4 units

graphics window

(gx, gy)

plane

Cartesian

(cx, cy)

Scaling Between Graphic and Cartesian Coordinates

graphics window

LeftX

WorldCenter

TopY

WorldSize

WorldSize

ScreenSize

ScreenSize

(gx, gy) (cx, cy)

(0, 0)

Cartesian plane

"Real World"