Download Final-F10

yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

String (computer science) wikipedia , lookup

Computer Science 121
Final Exam * Fall 2010
Be sure to show all your work. Remember: exam is closed book.
Cellphones, calculators, ear buds of any sort, slide rules, notes, talking, etc., are not
permitted. Keep all of your scratch work on the exam sheets.
Student Number: _______________________
Problem 1
Problem 2
Problem 3
Problem 4
1. (25) Write a complete, working, one-class application that reads in
exactly 5 Strings from the keyboard, each on a separate line, and then
prints the Strings out in a column in reverse order. You must use an array
in an essential way here. An example: if the entered strings are
is surely
Your code should print:
is surely
Note: for this problem, ignore the possibility that more or fewer than 5
strings are entered.
2. (25) The code on the next page constitutes a definition for the
AMysteryPanel class. When you run this code with AMysteryDriver below,
you get a graphics application that draws an oval on the screen. The
initial screen image is the left picture; the picture on the right results
if you click the Draw buttom 5 times. There’s a problem: the lines of
code for the AMysteryPanel class given on the next page are out of order.
Your job: put the code in order.
public class AMysteryDriver{
public static void main(String[] args){
DisplayWindow d = new DisplayWindow();
AMysteryPanel p = new AMysteryPanel();
a. (10) Write out the lines of code for AMysteryPanel below in the correct
order, so that the intended effect is achieved. (you may use obvious shorthand
here, e.g., you can write “import …” instead of the whole import line) [also:
you may use the back of the opposite page for your answer]
import java.awt.*; import javax.swing.*; import java.awt.event.*;
public class AMysteryPanel extends JPanel implements ActionListener{
setPreferredSize(new Dimension(450,400));
int pos =100;
JButton draw = new JButton("Draw");
pos = pos + 10;
public AMysteryPanel(){
public void paintComponent(Graphics g){
public void actionPerformed(ActionEvent e){
if(e.getSource() == draw){
b. In a few sentences as most, tell what the relationship is, if any, between
the phrase “implements ActionListener”, and the actionPerformed method.
c. In the expression e.getSource() the getSource() method belongs to what class?
d. If you completely remove the statement super.paintComponent(g), then compile
and run the program, how is performance of the application altered?
3. (25) The LineFinder code below reports the longest line in an external text
import java.util.Scanner;
public class LineFinder{
public static void main (String[] args){
Scanner s = new Scanner(;
System.out.println("Enter file name");
String name =;
BigLine b = new BigLine(name);
catch(Exception e){System.out.println(e);}
For example, if you run the LineFinder application on the LineFinder source code
itself, this interaction is obtained:
> run LineFinder
Enter file name
[DrJava Input Box:]
Long line:
catch(Exception e){System.out.println(e);}
(thus, the line beginning “catch…” is reported as the longest line in the file)
Your job: code the BigLine class. (Hint: easiest to do if you use inheritance
to extend the Echo class, below) [note: you may use the back of the opposite
page for your answer]
import java.util.Scanner; import*;
public class Echo{
String fileName; // external file name
Scanner scan; // Scanner object for reading from external file
public Echo(String f) throws IOException{
fileName = f;
scan = new Scanner(new FileReader(fileName));
public void readLines(){ // reads lines, hands each to processLine
scan.close(); }
public void processLine(String line){ // does the real processing work
4. (25) Consider the Book class, below:
public class Book implements Comparable{
private String title;
private int pages;
public Book(String name, int pages){
title = name;
this.pages = pages;
public String getTitle(){return title;}
public int getPages(){return pages;}
public int compareTo(Object other){
String s = this.title;
String t = ((Book)other).title;
return (s.compareTo(t));
a. Suppose books is an array of Book objects in a driver class, and suppose you
execute this command in the driver:
after execution, what can you say about the ordering of the Book objects in
b. Add a setPages method to the Book class. It should take one argument, an int,
and should set the pages value of the calling object to that value.
c. The book constructor contains the line
this.pages = pages;
In a sentence or two, describe what this line means.
d. Suppose again that books is an array of Book objects in a driver class, and
suppose the loop below is in the driver. In one or two sentences at most,
explain what this loop does:
for(Book b : books)
if (b.getTitle().length() > 20) System.out.println(b.getTitle());
e. Add a static method to the Book class called mostPages, which is passed an
array of Book objects, and which returns the Book object in the array with the
most pages. In case of ties, any Book object that is tied for most pages will