Download Final Exam (110 points)

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
no text concepts found
Transcript
Final Exam
Name: ______KEY_______________
Answer each question in the space provided. Point values are listed next to each question
number. You may use your textbook and class notes while completing this exam.
1. Write a comment describing what each marked line of code does in the code fragment
below (lines are maked with letters A through E):
PrintWriter pw;
FileOutputStream fos;
String fName;
Scanner kb = new Scanner(System.in);
System.out.print("Enter name of file: ");
fName = kb.nextLine();
try {
fos = new FileOutputStream(fName);
} catch (FileNotFoundException e) {
System.exit(1);
}
// A
pw = new PrintWriter (fos);
// C
pw.printf ("That's all folks!\n");
// D
pw.close();
// E
// B
A: Opens an input file stream using the user-supplied file name
B: Closes program if exception was thrown
C: Creates PrintWriter object using the output file stream created in A
D: Writes “That’s all folks” to file
E: Releases file resources and flushes output buffer
Computer Science I
Sheller
f06 Final Exam - flash
Fall 2006
Page 1
2. The program on the next two pages contains some missing pieces of code, represented
by items A through J below. Fill in the correct letters where the blank spaces appear in
the program. Each letter should be used exactly once.
A.
B.
C.
D.
E.
F.
G.
H.
I.
J.
b.addActionListener(this);
FortuneTeller ft = new FortuneTeller();
extends JFrame
add(buttonPart, BorderLayout.NORTH);
input.close();
throws FileNotFoundException
ft.setVisible(true);
implements ActionListener
setLayout(new BorderLayout());
bg = new Color(r,g,b);
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class FortuneTeller __h__ __c__{
JPanel textPart, buttonPart;
Scanner input;
String [] fortunes;
JButton b;
JLabel text;
Random rg;
Color bg;
public FortuneTeller () __f___{
setSize(400,100);
__i___
input = new Scanner(new FileInputStream("fortunes.txt"));
fortunes = new String[10];
rg = new Random();
setupText();
setupButton();
___d__
add(textPart, BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void setupText() {
int x=0;
while (input.hasNextLine()) {
fortunes[x] = input.nextLine();
x++;
Computer Science I
Sheller
f06 Final Exam - flash
Fall 2006
Page 2
} // ends loop; setupText method continues
___e__
textPart = new JPanel();
getColor();
text = new JLabel();
getFortune();
textPart.add(text);
repaint();
}
public void setupButton() {
buttonPart = new JPanel();
buttonPart.setBackground(Color.BLACK);
b = new JButton("Hit me!");
___a__
buttonPart.add(b);
}
public void getColor() {
int r = rg.nextInt(256);
int g = rg.nextInt(256);
int b = rg.nextInt(256);
___j__
}
public void getFortune() {
int f = rg.nextInt(10);
String s = fortunes[f];
text.setText(s);
}
public void paint (Graphics g) {
super.paint(g);
g.setColor(bg);
textPart.setBackground(bg);
}
public void actionPerformed (ActionEvent e) {
getColor();
getFortune();
repaint();
}
public static void main (String [] args) throws FileNotFoundException {
___b__
___g__
}
}
Computer Science I
Sheller
f06 Final Exam - flash
Fall 2006
Page 3
3. (25 points) Draw a picture of the window produced by the code in question 3, and write
a brief description of what the program does.
Reads a series of Strings (“fortunes”) from a file into an array, then displays the window
above. Randomly chooses one fortune and a background color to display whenever the
button is clicked.
4. (15 points) Write a line (or fragment of code) that performs the operations described in
parts a through e below:
a. activates a button named b that is part of a class that implements ActionListener
b.addActionListener(this);
b. begins the class definition of a class named Window that inherits from JFrame
public class Window extends JFrame {
c. calls the paint method of an ancestor class
super.paint(g);
d. calls a method to redraw a window
repaint();
e. declares and creates a Container object named c that holds the body of a JFrame
Container c = getContentPane();
Computer Science I
Sheller
f06 Final Exam - flash
Fall 2006
Page 4
(25 points) Write a paint() method that draws the following picture on a 300 x 300
window. You do not need to take into account the title bar of the frame. The circles are
red and white, and the lines are black:
public void paint (Graphics g) {
g.setColor (Color.red);
g.fillOval(75,75,150,150);
g.setColor(Color.white);
g.fillOval(90,90,120,120);
g.setColor(Color.red);
g.fillOval(105,105,90,90);
g.setColor(Color.white);
g.fillOval(120,120,60,60);
g.setColor(Color.red);
g.fillOval(135,135,30,30);
g.setColor(Color.black);
g.drawLine(0,0,300,300);
g.drawLine(0,300,300,0);
}
Computer Science I
Sheller
f06 Final Exam - flash
Fall 2006
Page 5
Related documents