Você está na página 1de 7

Name: NetID:

Intro to Computer Science Midterm Exam #2, Fall 2013

Fill-in-the-blank

(2% each)

1.

_______________________ are a useful data structure for storing and manipulating grid-like tabular data in code.

2. 3. 4. 5. 6.

The System.out.printf() method is used to _______________________ An object is an _______________________ of a class. The new operator in Java is used to _______________________ Dot-notation (using the period . in code) is used to indicate _______________________ Fill in two blanks to make this class inherit from the Mammal class and explicitly call its constructor when instantiated. !"#$%&'&$())'*+,'____________ -(..($'/' ''''012%3,'3(.45' ''''*+,67'/' ''''''''____________ 88&($$'194'!(2431'&$()):)'&+3)12"&1+2' ''''''''0;)14.<+"1<!2%31$36=>9++?@A75'88$41'")'B3+C'19(1'%1:)'($%D4' ''''E' E'

7.

Getters and setters are used to get the value or set the value of an _______________________ variable or attribute.

8.

The _______________________ keyword is used to indicate that a method or attribute belongs to the class, and not to any particular object.

9.

_______________________ is the term used to indicate that a user of a class should be able to treat the class like a black box.

10.

_______________________ of variables and methods within an object or class is one of the techniques by which the answer to #9 is usually achieved by making those variables and methods belong to a particular object or class.

Page 1

Name: NetID:

Intro to Computer Science Midterm Exam #2, Fall 2013

Multiple choice

(2% each)

1.

Each element in a two-dimensional array must have the same number of sub-elements. a. True b. False All programs in Java are object-oriented as a requirement of the language. a. True b. False Constructors in a class definition (select all that apply): a. must accept no parameters b. must have no return value c. must be overloaded at least once d. must not be declared as public or private The keyword super is a reference to a classs (select all that apply): a. no-args constructor b. parent class c. instantiated object d. package e. access modifier To adhere to the concept of abstraction, where possible, data fields in a Java program should be made. a. public b. static c. private d. void e. -None of the above-

2.

3.

4.

5.

Page 2

Name: NetID:

Intro to Computer Science Midterm Exam #2, Fall 2013

6.

Taken from a larger class definition, the following method accepts as its parameter

public static void changeAPairOfGlasses(Glasses thisPairOfGlasses) { thisPairOfGlasses.dirtify(); } a. b. c. d. e. 7. A Glasses object, passed by value A Glasses object, passed by reference A static Glasses class null None of the above-

In the following code, what does the variable x point to? (assume the variable x has been
initialized properly in code not displayed)

F'G'34C'H314,426IJ75' a. b. c. d. e. 8. a primitive an object a class a constructor -All of the above-

The == operator performs a comparison. a. by reference b. by value

K<

All Processing programs inherit from PApplet, which has draw() and setup() methods. When you build your own custom Processing sketch, you create your own draw() and setup() methods. These methods (select all that apply) ' a. overload the PApplet classs methods b. override the PApplet classs methods c. are constructors for the PApplet class d. are setter methods e. are instance attributes The special keyword, this, when used within a static method of a class, refers to a. b. c. d. e. the current class the current object the parent object All of the aboveNone of the above-

10.

Page 3

Name: NetID:

Intro to Computer Science Midterm Exam #2, Fall 2013

Algorithms

(20% each)

1.

Write a program that uses a two-dimensional array to store 5 students names and midterm #2 grades, including yourself as one of those students. Include a method that accepts as its sole parameter the name of a student, and that outputs that students name and grade. Format the output of this method nicely such that the name occupies 15 characters of the output, and the grade occupies 4 characters.

Page 4

Name: NetID:

Intro to Computer Science Midterm Exam #2, Fall 2013

2.

Write a class that has the attributes and methods represented in the following UML diagram. Getter and setter methods for all private properties have not been included in the UML diagram, but must be included in the code. Also include two constructors: one that can accepts parameters to set all a ChocolateBar objects properties, and one that can be used with parameters for only the isDark and percentCocoaSolids properties, and which gives default values to the other properties. Create a main method that creates two of these objects one using each constructor. Call each ChocolateBars melt() method.
ChocolateBar

-isOrganic : boolean -isFairTrade : boolean -isDark : boolean +percentCocoaSolids : int -sourceCountry : String +melt() : void

Page 5

Name: NetID:

Intro to Computer Science Midterm Exam #2, Fall 2013

3.

Create a ComputerScienceCourse class and a Student class, using the following outlines. Anything not included or specified in the outline left up to your discretion.
ComputerScienceCourse Student name : String email : String year : int (use 1 for freshman, 2 for sophomore, 3 for junior, 4 for senior) grade: String assignGrade(grade : String)

title : String description : String days : String array (a String for each day, such as M, T, W, R, F, etc) time : String (such as 9:30-10:45) students : Student array (an array of 50 Student objects) registerStudent(student : Student) : void

a.

Include a main() method that creates a ComputerScienceCourse object representing this course. b. Create a Student object representing yourself, and one other Student object representing someone else. c. Register both Student objects for this course by calling the registerStudent() method and passing it the Student objects. d. The registerStudent() method should add these two Students to the first two spots in the ComputerScienceCourse objects students array. e. Assign the Student objects whatever grade you think you deserve by calling the Student objects assignGrade() method. This should update the Student objects grade property.

Page 6

Name: NetID:

Intro to Computer Science Midterm Exam #2, Fall 2013

Extra credit

(10% extra)

1. Create a Processing program that shows a circle that oscillates back and forth in the horizontal x direction. The following instance methods and attributes available to all programs that inherit from the PApplet class might help (assume all attributes are of the float data type): void setup() automatically called once to set up the window void draw() automatically called continuously to draw each frame of the animation void size(width, height) creates a window void background(r,g,b) fills the window with a solid background color. void ellipse(x, y, width, height) creates an ellipse at the specified position with the specified dimensions width holds with width of the window height holds the height of the window

Page 7

Você também pode gostar