Blue Pelican Java Lesson 24 Homework


here is for the specter of a quiz to always be hanging over the student where he knows he must quickly ... In the questions below, consider the following code:.

Blue Pelican Java Exercise, Quiz, & Test Keys by Charles E. Cook

Version 7.0.1A Copyright © 2004 - 2013 by Charles E. Cook; Refugio, Tx (All rights reserved)

This page is intentionally left blank.

Keys for Quizzes/Exercises/Projects The short quizzes for each lesson in this section are not comprehensive and not very difficult. Normally, only basic, superficial questions are asked. The general philosophy here is for the specter of a quiz to always be hanging over the student where he knows he must quickly acquire a general working knowledge of the subject but at the same time knows he will not be asked in-depth or tricky questions. It is hoped that this gentle, but persistent pressure, will encourage the student to keep current with his studies and be rewarded with a frequent “100” on these little quizzes. It is suggested that a quiz be given the day after a new lesson is introduced.

Answers 11-1

Quiz on Lesson 11 In the questions below, consider the following code: int sum = 0; for(int j = 0; j < 3; j++) { sum = sum + 2; } System.out.println(sum); 1. Identify the control expression.

2. Identify the step expression.

3. Identify the initializing expression.

4. How many times does the loop iterate (repeat)?

5. What is printed?

Answers 11-2

Key to Quiz on Lesson 11 In the questions below, consider the following code: int sum = 0; for(int j = 0; j < 3; j++) { sum = sum + 2; } System.out.println(sum); 1. Identify the control expression. j 5) { return n - 1; } else { return n * method(n + 2); } } 1 * 3 * 5 * 6 = 90

Key to Exercises on Lesson 39 In each of the following recursion problems, state what’s printed. 1. System.out.println( rig(4) ); public static int rig(int n) { if ( (n = = 0) ) { return 5; } else if ( n = = 1) { return 8; } else { return rig(n – 1) - rig(n – 2); } } 0 1 2 3 4 5 8 3 -5 -8 2. System.out.println( mm(6) ); // 6 + 5 + 4 + 3 + 2 + 1 + 10 = 31 public static int mm(int n) { if (n 10) return n - 2; else { n = n * 3; return n + zing(n + 2); } } 6. crch(12); public static void crch(int n) { if (n >4>>7>>10 public static void elvis(int n) { if (n >” + (n – 1)); } }

Answers 39-4

Answers 39-5 8. sal(5); public static int sal(int n) { if (n = = 2) { return 100; } else if (n = = 3) { return 200; } else { return (2 * sal(n - 1) + sal(n - 2) + 1); } } 2 3 4 5 100 200 501 1203 9. puf(4); public void puf(int n) { if(n = = 1) { System.out.print(“x”); } else if( n%2 = = 0) //n is even { System.out.print(“{”); puf(n-1); System.out.print(“}”); } else //n is odd { System.out.print(“”); } } 1 x

2 {x}

3

4 {}

Answers 39-6 10. bc(6, 2); public static void bc(int p, int q) { if (p/q = = 0) { System.out.println(p + q + 1); } else { System.out.println(p); bc(p/q, q); } } 6, 2 3, 2 1, 2 6 3 4

6 3 4

Project… Fibonacci, Key

Answers 39-7

public class ModFib { public static int modFibonacci(int n) { if(n == 0) { return 3; } else if(n == 1) { return 5; } else if(n == 2) { return 8; } else { return modFibonacci(n-1) + modFibonacci(n-2) + modFibonacci(n-3); } } }

Unformatted text preview: BugLab 14 – Writing to a file, (Blue Pelican Java Lesson 26) ( Teacher: Refer to Labs 4 and earlier for a detailed discussion of the various GridWorld methods used in these labs (act, canMove, move, & turn). If the students have done the previous labs, then they should already be accustomed to the use of these methods.) Create the project: Create a new project (named BugLab14 ) with your IDE and into the resulting folder, import the two classes in the BasicBug folder. In the IDE BlueJ, it’s done as follows: Create the project with Project | New project , being careful to create this project within the C:\GridworldProjects folder. Then bring in two classes mentioned above with Project | Import (Navigate to the C:\GridWorldProjects\BasicBug folder and then click Import .) Modify BasicBugRunner : At the very bottom of the BasicBugRunner class, add the following code that will create and write to a file on your hard disk: FileWriter fw = new FileWriter( <#1> ); // Student adds code here. PrintWriter output = new PrintWriter(fw); <#2> // Student adds code here. output.close( ); fw.close( ); The presence of FileWriter and PrintWriter will make it necessary to import the java.io.* package. Also, the possibility of a checked exception make it necessary to append throws IOException to the main method signature. There is no need to modify the BasicBug class. It inherits the needed code from Bug via extends Bug . The task at hand: In the area of <#1> place file name outputFile.txt and the path where you would like to store that file. Remember to use “\\” where a “\” would normally be used in the path name. To store the file in this project’s folder, provide just the file name and omit the path. In the area of <#2> use the println method of the output object to describe first the bug1 object and then the rock1 object. The description of each is provided with their toString methods. Thus the area of <#2> should consist of two lines of code. Run main and then from within Windows Explorer , check the contents of outputFile.txt . It should be as follows: BasicBug[location=(5, 3),direction=0,color=java.awt.Color[r=255,g=200,b=0]] info.gridworld.actor.Rock[location=(1, 3),direction=0,color=java.awt.Color[r=0,g=0,b=0]] BL 14 - 2 BugLab 14 Key: //This class will not compile until the BasicBug class first compiles. import info.gridworld.actor.ActorWorld; import info.gridworld.grid.Location; import java.awt.Color; import info.gridworld.actor.Rock; import java.io.*; //Necessary because of FileWriter and PrintWriter public class BasicBugRunner { public static void main(String args) throws IOException { ActorWorld world = new ActorWorld( ); BasicBug bug1 = new BasicBug( ); bug1.setColor(Color.ORANGE); Rock rock1 = new Rock( ); rock1.setColor(Color.BLACK); world.add(new Location(5,3), bug1); world.add(new Location(1, 3), rock1); world.show( ); FileWriter fw = new FileWriter("outputFile.txt"); // Open file PrintWriter output = new PrintWriter(fw); output.println(bug1.toString()); // Send bug1 info to file output.println(rock1.toString()); // Send rock1 info to file output.close( ); fw.close( ); } } BL 15 - 1...
View Full Document

0 comments

Leave a Reply

Your email address will not be published. Required fields are marked *