© 2003 Walter Savitch These slides are for the exclusive ...

55
F-1 © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003. They may not be copied or used for any other purpose without the written permission of Walter Savitch. [email protected]

Transcript of © 2003 Walter Savitch These slides are for the exclusive ...

Page 1: © 2003 Walter Savitch These slides are for the exclusive ...

F-1

© 2003 Walter Savitch

These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter 2003.

They may not be copied or used for any other purpose without the written permission of Walter [email protected]

Page 2: © 2003 Walter Savitch These slides are for the exclusive ...

F-2

CSE 11March 4, 2003

Page 3: © 2003 Walter Savitch These slides are for the exclusive ...

import java.io.*; //page 583public class TextFileOutputDemo{ public static void main(String[] args) { PrintWriter outputStream = null; try { outputStream = new PrintWriter( new FileOutputStream("out.txt")); } catch(FileNotFoundException e) { System.out.println("Error"); System.exit(0); }

System.out.println("Enter 3 lines"); String line = null; int count; for (count = 1; count <= 3; count++) { line = SavitchIn.readLine(); outputStream.println(count + " " + line); } outputStream.close();

F-3

Page 4: © 2003 Walter Savitch These slides are for the exclusive ...

4

}}

Page 5: © 2003 Walter Savitch These slides are for the exclusive ...

F-5

KeyboardA tall treein a short forest is likea big fish in a small pond.

File out.txt1. A tall tree2. in a short forest is like3. a big fish in a small pond.

Page 6: © 2003 Walter Savitch These slides are for the exclusive ...

F-6

import java.io.*; //page 591public class TextFileInputDemo{ public static void main(String[] a) { try { BufferedReader inputStream = new BufferedReader( new FileReader("data.txt"));

String line = null; line = inputStream.readLine(); System.out.println("First line"); System.out.println(line);

line = inputStream.readLine(); System.out.println("Second line"); System.out.println(line); inputStream.close(); } catch(FileNotFoundException e) { System.out.println("..."); } catch(IOException e) { System.out.println("Error");}}}

Page 7: © 2003 Walter Savitch These slides are for the exclusive ...

F-7

Checking for the End of a Text FileThe method readLine of the class BufferedReader returns null when it tries to read beyond the end of a text file. The method read of the class BufferedReader returns -1 when it tries to read beyond the end of a text file. (Neither of these methods would throw an EOFException.)

Page 8: © 2003 Walter Savitch These slides are for the exclusive ...

try{ BufferedReader inputStream = new BufferedReader( new FileReader("story")); PrintWriter outputStream = new PrintWriter(new FileOutputStream("storylines"));

int count = 0; String line = inputStream.readLine(); while (line != null) { count++; outputStream.println( count + " " + line); line = inputStream.readLine(); } System.out.println(count +"...");

inputStream.close(); outputStream.close();}catch(FileNotFoundException e){ System.out.println("File not found.");}catch(IOException e)

F-8

Page 9: © 2003 Walter Savitch These slides are for the exclusive ...

9

{

Page 10: © 2003 Walter Savitch These slides are for the exclusive ...

F-10

The File ClassFile fileObject = new File("stuff.txt");

if(!fileObject.exists( )) System.out.println("Nope");else if(!fileObject.canRead( )) System.out.println( "Not readable.");

File class is a wrapper class for Strings."stuff.txt" is just a string; it knows nothing about files.File("stuff.txt") knows it is suppose to name a file.

Page 11: © 2003 Walter Savitch These slides are for the exclusive ...

F-11

Quiz TimeBe sure you get a Card and write your

name and login name legibly on the card.

Page 12: © 2003 Walter Savitch These slides are for the exclusive ...

F-12

A reference is

a. a memory address.b. a way of referring to an object of a class.c. is the kind of value that is stored in a variable of type String.d. all of the above.e. none of the above.

Page 13: © 2003 Walter Savitch These slides are for the exclusive ...

If all the instance variables in a class are private, then in a pro-gram that uses the class

a. mutator methods must be used to change the values of instance variables.b. accessor methods must be used to find out the values of instance variables.c. accessor methods must be privated. mutator methods must be private.e all of the abovef. only a and b.g. only c and dh. none of the above.

F-13

Page 14: © 2003 Walter Savitch These slides are for the exclusive ...

Which of the following are wrapper classes?

a. Integerb. SavitchInc. Stringd. all of the above.e. none of the above.

F-14

Page 15: © 2003 Walter Savitch These slides are for the exclusive ...

A constructor normally does which of the following?

a. obtains input from the user.b. gives output to the user.c. initialize instance variable.d. all of the above.e. none of the above.

F-15

Page 16: © 2003 Walter Savitch These slides are for the exclusive ...

The this parameter

a. is useful when a formal parameter has the same name as an instance variable.b. is used as a name for a constructor of a base class.c. can be used as the name of an instance variable when defin-ing a class.d. all of the above.e. none of the above.

F-16

Page 17: © 2003 Walter Savitch These slides are for the exclusive ...

A reference is

a. a memory address.b. a way of referring to an object of a class.c. is the kind of value that is stored in a variable of type String.d. all of the above.e. none of the above.

F-17

Page 18: © 2003 Walter Savitch These slides are for the exclusive ...

A static method

a. cannot be invoked with a calling object.b. cannot be invoked using the class name in place of a calling object.c. must be a void method.d. all of the abovee. none of the above.

F-18

Page 19: © 2003 Walter Savitch These slides are for the exclusive ...

Instance variables of a class

a. must be of a primitive type for good style.b. must be of a class type for good style.c. must be private for good style.d. all of the above.e. none of the above.

F-19

Page 20: © 2003 Walter Savitch These slides are for the exclusive ...

An array type

a. is a primitive type.b. is a class type.c. is a reference type.d. all of the above.e. none of the above.

F-20

Page 21: © 2003 Walter Savitch These slides are for the exclusive ...

If a is an array, the index of the last element in a is

a. a.lengthb. a.length()c. a.sized. a.size() e. none of the above.

F-21

Page 22: © 2003 Walter Savitch These slides are for the exclusive ...

A two dimensional array

a. is actually an array of arraysb. is an object of the class Vector.c. is an object of the class MultidimensionalArrayd. all of the above.e. none of the above.

F-22

Page 23: © 2003 Walter Savitch These slides are for the exclusive ...

In order to compile, a recursive method definition

a. must have a base case (also called a stopping case).b. must have a name that starts with a lowercase letter.c. must be a void methodd. must be a static methode. all of the above.f. none of the above.

F-23

Page 24: © 2003 Walter Savitch These slides are for the exclusive ...

A vector is

a. an array of arraysb.an object of the class Vector.c. used with square brackets [ ] in the same way than an array is used with square brackets.d. all of the above.e. none of the above.

F-24

Page 25: © 2003 Walter Savitch These slides are for the exclusive ...

Suppose v is a vector and v.capacity() returns 100, then

a. v cannot contain more than 100 elementsb. v can only contain more than 100 elements if the program includes an invocation of v.ensureCapacity.c. v can contain more than 100 elements and the program need not do anything special to ensure that this is true.d. all of the above. e. none of the above.

F-25

Page 26: © 2003 Walter Savitch These slides are for the exclusive ...

Suppose Child is a derived class of Parent, Parent is a derived class of Grandparent, and suppose x is an object of type Child, then.

a. x is also of type Parent.b. x is also of type Grandparent.c. all of the aboved. none of the above

F-26

Page 27: © 2003 Walter Savitch These slides are for the exclusive ...

Suppose Undergraduate is a derived class of Student then.

a. Undergraduate has all the public methods of Student.b. Undergraduate has all the public instance variables of Stu-dent.c. Undergraduate has all the private instance variables of Stu-dent.d. all of the abovee. none of the above

F-27

Page 28: © 2003 Walter Savitch These slides are for the exclusive ...

The WindowDestroyer class

a. is defined in the library javax.swingb. is defined in the library java.awt.c. is part of the core of Java and not in any libraryd. is written in Java, bur is not part of the official Java lan-guage.e. none of the above.

F-28

Page 29: © 2003 Walter Savitch These slides are for the exclusive ...

Which of the following are reference types

a. charb. Characterc. char[]d. all of the above.e. a and bf. a and c.g.. b and ch. none of the above.

F-29

Page 30: © 2003 Walter Savitch These slides are for the exclusive ...

Which of the following are reference types

a. doubleb. booleanc. Stringd. all of the abovee. none of the above.

F-30

Page 31: © 2003 Walter Savitch These slides are for the exclusive ...

A mutator method is used

a. to find out the value of an instance variable.b. to change the value of an instance variable.c. to initialize instance variables.d. all of the above.e. none of the above.

F-31

Page 32: © 2003 Walter Savitch These slides are for the exclusive ...

An instance variable of a class

a. can be of a primitive type.b. can be of a class type.c. can be an array type.d. all of the above.e. none of the above.

F-32

Page 33: © 2003 Walter Savitch These slides are for the exclusive ...

A default constructor

a. is produced automatically by Java for every class.b. must be defined for every class or the class will not compile.c. is a constructor with no parameters.d. all of the above.e. none of the above.

F-33

Page 34: © 2003 Walter Savitch These slides are for the exclusive ...

You may overload a method name by giving two definition

a. that return different types.b. that have different numbers of parameters.c. for which one is static and one is not.d. all of the above.e. none of the above.

F-34

Page 35: © 2003 Walter Savitch These slides are for the exclusive ...

Suppose that in some class definition you have the following method invocation: doStuff(5, 4.3);Below are the headings of method definition in the class. Which method definition will be used in this invocation?

a. public void doStuff(double x, double y)b. private void doStuff(int x, double y)c. public static void doStuff(int x, int y).d. none of the above.

F-35

Page 36: © 2003 Walter Savitch These slides are for the exclusive ...

A static method

a. cannot be invoked with a calling object.b. can be invoked using the class name in place of a calling object.c. must be a void method.d. all of the abovee. none of the above.

F-36

Page 37: © 2003 Walter Savitch These slides are for the exclusive ...

A private static instance variable of a class

a. can be referenced by name by all the objects of the class..b. can be used to pass information from one object of the class to another object of the class..c. has only one copy for all objects of the class.d. all of the abovee. none of the above.

F-37

Page 38: © 2003 Walter Savitch These slides are for the exclusive ...

The value null may be assigned

a. to any variable.b. to any variable of a primitive type.c. to any variable of a class type.d. none of the above.

F-38

Page 39: © 2003 Walter Savitch These slides are for the exclusive ...

The size of an array

a. is determined by the array type.b. is determined when the array is created using the new opera-tor..c. can change when more indexed variables are needed.d. none of the above.

F-39

Page 40: © 2003 Walter Savitch These slides are for the exclusive ...

Suppose Species is a class type and you want to create an array of 10 objects of type Species. How many times must you use the operator new.

a. oneb. ninec. tend. elevene. none of the above.

F-40

Page 41: © 2003 Walter Savitch These slides are for the exclusive ...

Which of the following are true of vectors?

a. A vector can increase it’s size until the size reaches the capac-ity of the vector. b. A vector can increase it’s size without limit (other than run-ning our of memory).. c. has base type Object..d. all of the above. e. a and b f. a and c g. b and c.h. none of the above

F-41

Page 42: © 2003 Walter Savitch These slides are for the exclusive ...

A method may return a value of type

a. int[]b. String[]c. Integer[]d. all of the abovee. none of the above.

F-42

Page 43: © 2003 Walter Savitch These slides are for the exclusive ...

Suppose Child is a derived class of the Parent. Suppose a method named doStuff() is defined in Parent and has its definition overridden (changed) in the defini-tion of class Child. Suppose c is an object of type Child, what def-inition of doStuff() is used in c.doStuff():

a. The one given in the definition of Child. b. The one given in the definition of Parent. c. It depends on the type of the variable used to name c. d. You need more information to decide which definition of doStuff() is used. e. none of the above.

F-43

Page 44: © 2003 Walter Savitch These slides are for the exclusive ...

Suppose Child is a derived class of Parent and Parent is a derived class of GrandParent. Which of the following are true?

a. An object of the class Child is of type Parent.b. An object of the class Child is of type GrandParent. c. An object of the class Child is of type Child. d. all of the above e. none of the above

F-44

Page 45: © 2003 Walter Savitch These slides are for the exclusive ...

Suppose Child is a derived class of Parent and doStuff() is a pri-vate method in the class Parent. Which of the following are true?

a. doStuff() may be used in the definition of methods in Child.b. doStuff() may not be used in the definition of methods in Child. c. In some, but not all cases, doStuff() may be used in the defi-nition of methods in Child.. d. none of the above

F-45

Page 46: © 2003 Walter Savitch These slides are for the exclusive ...

Every exception class a programmer defines

a. has a method named getMessage.b. must be a derived class of some other exception class c. is a descendent class of the class Object.d. all of the above. e. none of the above

F-46

Page 47: © 2003 Walter Savitch These slides are for the exclusive ...

Which of the following are methods of the class Object?

a. toString. b. equalsIgnoreCase. c. equals d. all of the above e. a and bf. a and c. g. b and c h. none of the above

F-47

Page 48: © 2003 Walter Savitch These slides are for the exclusive ...

Which of the following are methods in the class File?

a. canRead b. canWrite c. writeUTF.d. all of the above. e. a and b f. a and c g. b and c.h. none of the above

F-48

Page 49: © 2003 Walter Savitch These slides are for the exclusive ...

Which of the following are true of interfaces?

a. An interface is a class. b. An interface is a type. c. An interface is an object.d. all of the above. e. a and b f. a and c g. none of the above

F-49

Page 50: © 2003 Walter Savitch These slides are for the exclusive ...

The method setActionCommand is in which of the following?

a. The class JButton. b. The class JMenuItem. c. The class AbstractButton. d. all of the above. e. none of the above

F-50

Page 51: © 2003 Walter Savitch These slides are for the exclusive ...

Which of the following can be added to a JMenu?

a. A JMenuItem. b. A JMenu. c. both a and b. d. none of the above.

F-51

Page 52: © 2003 Walter Savitch These slides are for the exclusive ...

Which of the following are layout managers?

a. BoxLayout. b. BorderLayout. c. CardLayout. d. all of the above. e. none of the above.

F-52

Page 53: © 2003 Walter Savitch These slides are for the exclusive ...

The content pane of a JFrame is of type

a. Container. b. JComponent. c. JPanel. d. all of the above. e. none of the above.

F-53

Page 54: © 2003 Walter Savitch These slides are for the exclusive ...

F-54

Page 55: © 2003 Walter Savitch These slides are for the exclusive ...

F-55