Download Final-F10

Survey
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

Transcript
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.
Name:__________________________________
Student Number: _______________________
Problem 1
Problem 2
Problem 3
Problem 4
Total
1
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
now
is surely
the
time,
friends!
Your code should print:
friends!
time,
the
is surely
now
Note: for this problem, ignore the possibility that more or fewer than 5
strings are entered.
2
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();
d.addPanel(p);
d.showFrame();
}
}
3
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.*;
}
}
}
}
}
repaint();
g.drawOval(0,90,2*pos,pos);
public class AMysteryPanel extends JPanel implements ActionListener{
setPreferredSize(new Dimension(450,400));
this.add(draw);
int pos =100;
JButton draw = new JButton("Draw");
pos = pos + 10;
public AMysteryPanel(){
public void paintComponent(Graphics g){
draw.addActionListener(this);
public void actionPerformed(ActionEvent e){
super.paintComponent(g);
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?
4
3. (25) The LineFinder code below reports the longest line in an external text
file:
import java.util.Scanner;
public class LineFinder{
public static void main (String[] args){
try{
Scanner s = new Scanner(System.in);
System.out.println("Enter file name");
String name = s.next();
BigLine b = new BigLine(name);
b.readLines();
b.reportLongLine();
}
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: LineFinder.java]
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 java.io.*;
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
while(scan.hasNext()){
processLine(scan.nextLine());
}
scan.close(); }
public void processLine(String line){ // does the real processing work
System.out.println(line);
}
}
5
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:
java.util.Arrays.sort(books);
after execution, what can you say about the ordering of the Book objects in
books?
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.
6
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
do.
7