Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Final Exam Name: ________________________ 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 Computer Science I Sheller f06 Final Exam - flash // B 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 _____ _____{ JPanel textPart, buttonPart; Scanner input; String [] fortunes; JButton b; JLabel text; Random rg; Color bg; public FortuneTeller () ______{ setSize(400,100); ______ input = new Scanner(new FileInputStream("fortunes.txt")); fortunes = new String[10]; rg = new Random(); setupText(); setupButton(); ______ 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 ______ 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!"); ______ buttonPart.add(b); } public void getColor() { int r = rg.nextInt(256); int g = rg.nextInt(256); int b = rg.nextInt(256); ______ } 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 { ______ ______ } } 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. 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. begins the class definition of a class named Window that inherits from JFrame c. calls the paint method of an ancestor class d. calls a method to redraw a window e. declares and creates a Container object named c that holds the body of a JFrame Computer Science I Sheller f06 Final Exam - flash Fall 2006 Page 4 5. (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: Computer Science I Sheller f06 Final Exam - flash Fall 2006 Page 5