DOC PREVIEW
Berkeley COMPSCI 61B - Discussion

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS61B, Fall 2002 Discussion #4 Amir KamilUC Berkeley 9/19/02Topics: Review1 Review1.1 AssignmentIn Java, when one variable is assigned to another, the value contained in the variable on the right side of theassignment is copied over to the variable on the left side. Consider:int x = 4;int y = x;y = 1;What is the value of x after this code has run? Figures 1 to 3 show the execution of the code. Modifying ydoes not affect x’s value, since each variable has its own container.Figure 1: The value of x following int x = 4.Figure 2: The value of x and y following int y = 4.Figure 3: The value of x and y following y = 3.1.2 Arrays1.2.1 Simple (1D) Arrays• See 9/5/02 notes, section 7.• Arrays hold the same values as variables, but an array can contain multiple values. This means thata primitive array holds primitive values, while an Object array holds pointers.• Primitive numerical arrays (int[], double[], etc.) are initialized by default to hold all zeroes.1• Primitve boolean arrays are initialized by default to hold false in each location.• Object arrays are initialized by default to hold null in each location.• An array of zero length is not null; it has a length field, and you can call methods on it.1.2.2 Multi-Dimensional Arrays• While it is sometimes useful to think of a two dimensional array as a matrix, that is not what it is.It is actually an array of one dimensional arrays. For example, conifer(3) returns the structure infigure 4.Figure 4: The return value of conifer(3).• For a general array x, x[i] returns the ith element in the array. The same is true for two dimensionalarrays; x[i] would return the ith one dimensional array that it contains.• This can be generalized to higher dimensions. For example, an int[][][] is an array that holds arraysthat hold int arrays.1.3 Inheritance• See 9/12/02 notes, section 1.• For convenience, I have reproduced all the computer code we’ve written so far.The initial Computer class we wrote:/* A simple example of a Java class *//* Imports classes from external packages */import java.util.*;public class Computer {/* Fields */int state = 0; // Let 0 = off, 1 = on, -1 = crashed.String os; // The operating system on the computer.String user;// Initially an empty list; no programs running.LinkedList programs = new LinkedList();/* Turns on the computer, then boots it. */public void turnOn() {state = 1;boot();return;2}/* Boots the computer */private void boot() {if (os.equals("Windows 95")) {state = -1; // crash} else {programs.add(os);}}/* Logs a user into the computer */public void login(String user) {this.user = user;}/* Constructor */public Computer(String os) {this.os = os;user = "no one";}}The Computer class and its descendents, using abstract classes and interfaces:public abstract class Computer {protected int state;protected String os;protected Vector programs;public void turnOn() {state = 1;boot();}protected abstract void boot();... // other methods, fields}public class PC extends Computer {protected void boot() {if (os.equals("Windows 95")) {throw new Exception("CRASH!");} else {3programs.addElement(os);}}}public interface Laptop {public void charge(int min);public void discharge(int min);public void getStolen();}public class PCLaptop extends PC implements Laptop {private int remainingTime;public void charge(int min) {remainingTime += min;}public void discharge(int min) {remainingTime += min;}public void getStolen() {throw new Exception("Finders keepers, loser weepers.");}... // other methods, fields}1.4 Exceptions• See 9/12/02 notes, section


View Full Document

Berkeley COMPSCI 61B - Discussion

Documents in this Course
Lab

Lab

4 pages

Matrix

Matrix

3 pages

Numbers

Numbers

14 pages

Lectures

Lectures

12 pages

Project 1

Project 1

24 pages

Exam

Exam

8 pages

Load more
Download Discussion
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Discussion and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Discussion 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?