Docsity

Objects and Classes - Computer Science I | MAT 2170, Study notes of Computer Science

Partial preview of the text

Download Objects and Classes - Computer Science I | MAT 2170 and more Study notes Computer Science in PDF only on Docsity!

Mat 2170

Week 9

Objects and Classes

Spring 2009

Student Responsibilities

Exam2 — Thursday, 7pm

I (^) Reading: Textbook, Sections 6.1 – 6.

I (^) Lab 9

I (^) Attendance

Using Static Methods

I (^) A method invocation or call uses its name and supplies arguments that correspond to the parameters in the method implementation.

I (^) A predicate method returns a boolean value.

I (^) You must be aware of the return type of any method you invoke, since you will either be using it in an expression, assigning it to an object or displaying it. if(!isPalindrome(n)) -or- double x = sqrt(y)

I (^) Do not place a print or println statement in a method to display a calculated value unless that is the express purpose of the method. If the return type isn’t void, the method shouldn’t display any results.

Chapter Six: Objects and Classes

Using the RandomGenerator Class

I (^) Before writing our own classes, it helps to look more closely at how to use classes that someone else has developed.

I (^) The RandomGenerator class makes it possible to write programs that simulate random processes, such as flipping a coin or rolling a die.

I (^) Programs that involve random processes like this are said to be non–deterministic.

I (^) Non–determinism is essential to many applications, such as computer games.

I (^) It also has important practical uses in simulations, computer security, and algorithmic research.

I (^) This declaration usually appears outside of any method, and is therefore an example of an instance variable.

I (^) The keyword private indicates that this variable can be used from any method within this class, but is not accessible to other classes.

I (^) To obtain a random value, send a message to the generator (rgen in example), which responds with the result.

Methods to Generate Random Values

int nextInt(int low, int high) Returns a random int in interval [low..high] int nextInt(int n) Returns a random int in interval [0..n−1] double nextDouble(double low, double high) Returns a random double d, low≤ d <high double nextDouble() Returns a random double d,0 ≤ d < 1 boolean nextBoolean() Returns a random boolean, which is true 50% of the time boolean nextBoolean(double p) Returns a random boolean, which is true with probability p, 0 ≤ p < 1 Color nextColor() Returns a random color

Overloading Methods — A Note

I (^) The nextInt(), nextDouble(), and nextBoolean() methods all exist in more than one form.

I (^) Java determines which version is used by checking the number and types of arguments used.

I (^) Methods that have the same name but differ in their argument structure are said to be overloaded.

Examples Generating Random Values

I (^) To set the variable total to the sum of two six–sided dice: int d1 = rgen.nextInt(1, 6); int d2 = rgen.nextInt(1, 6); int total = d1 + d2;

I (^) To flip a coin that comes up heads 60% of the time: boolean isHeads = rgen.nextBoolean(0.6);

I (^) To randomly change the fill color of rect: rect.setFillColor(rgen.nextColor());

Craps Algorithm

Roll two dice, giving total If total is 7 or 11 player automatically wins

otherwise, if total is 2, 3, or 12 player automatically loses

otherwise, player has rolled 4, 5, 6, 8, 9, or 10, their point

player continues to roll dice until they roll their point, and win, or they roll a 7 and lose

Simulating Craps – rollTwoDice()

/* Rolls two dice and returns their sum. */ private int rollTwoDice() { int d1 = rgen.nextInt(1, 6); int d2 = rgen.nextInt(1, 6); return d1 + d2; }

/* Private instance variables */ private RandomGenerator rgen = RandomGenerator.getInstance();

Do not use println() in a method unless that is its purpose.

Craps Program, continued

else { int point = total; println("Your point is " + point + "."); boolean doAgain = true; while (doAgain) {

total = rollTwoDice();

if (total == point) { println("You made your point. You win."); doAgain = false; } else if (total == 7) { println("That’s a 7. You lose."); doAgain = false; } else println("You rolled "+total+", roll again."); } // end of rolling for point } // end of processing dice total } // end of run()

Clients and Implementors

I (^) it is useful to recognize that there are two perspectives that we can take with respect to a particular class.

I (^) Often, we will find ourselves using a class we didn’t write, for example, the RandomGenerator class.

I (^) When this happens, we are acting as a client of the class.

I (^) When we write the code for a method, we are acting as an implementor.

I (^) Clients and Implementors look at a class in different ways.

Layered Abstractions

I (^) The RandomGenerator class is actually implemented as a subclass of a class called Random.

I (^) Some of the methods we call to produce random values are defined in the RandomGenerator class itself; others are inherited from the Random class.

I (^) A client does not need to know which is which.

I (^) Class hierarchies that define methods at different levels are called layered abstractions.

Java Packages

I (^) Every Java class is part of a package, which is a collection of related classes that have been released as a coherent unit.

I (^) The RandomGenerator class is defined in a package called acm.util, which is part of the ACM Java Libraries.

I (^) The Random class is part of the java.util package, which is a collection of general utility classes.

I (^) Whenever we refer directly to a class, we should import the package in which it lives.