Lesson 4 practice problems

pdf

School

Community College of Rhode Island *

*We aren’t endorsed by this school

Course

1510

Subject

Computer Science

Date

Jan 9, 2024

Type

pdf

Pages

5

Uploaded by mburke1217796

Report
COMI 1510 Java Programming Lesson 4 Practice Problems New version to get access to CourseHero and all of your cheating. To do 1. Solve the warm-up problems. 2. Solve the Practice Problem and the Kennel Problem. 3. Ask your professor any questions that come up! Warm-ups 1. (Checkpoint 5.4) What message will the following program display if the user enters 5? What if the user enters 10? What if the user enters 100? import javax.swing.JOptionPane; public class Checkpoint { public static void main(String[] args) { String input; int number; input = JOptionPane.showInputDialog(“Enter a number.”); number = Integer.parseInt(input); if (number < 10) { method1(); } else { method2(); } System.exit(0); } public static void method1() { JOptionPane.showMessageDialog(null, “Able was I.”); } public static void method2() { JOptionPane.showMessageDialog(null, “I saw Elba.”); } } 2. (Checkpoint 5.7) Look at the following method header:
public static void myMethod(int num) Which of the following calls to the method will cause a compiler error? a) myMethod(7); b) myMethod(6.2); c) long x = 99; myMethod(x); d) short s = 2; myMethod(s) 3. (Checkpoint 5.8) Suppose a method named showValues accepts two int arguments. Which of the following method headers is written correctly? a) public static void showValues() b) public static void showValues(int num1, num2) c) public static void showValues(num1, num2) d) public static void showValues(int num1, int num2) 4. (Checkpoint 5.10) What will the following program display? public class Checkpoint { public static void main(String[] args) { int num1 = 99; double num2 = 1.5; System.out.println(num1 + “ “ + num2); myMethod(num1, num2); System.out.println(num1 + “ “ + num2); } public static void myMethod(int i, double d) { System.out.println(i + “ “ + d); i = 0; d = 0.0; System.out.println(i + “ “ + d); } } 5. (Checkpoint 5.12-5.14) Write the following method headers: a) A method named days that returns an int and has three int parameters variables: years , months , and weeks .
b) A method named distance that returns a double and has two double parameters variables: rate and time . c) A method named lightYears that returns a long and has one long parameter variable: miles . 6. (Algorithm Workbench 1) Examine the following method headers, and then write an example call to each method: public static void doSomething1(int x) public static int doSomething2(double x, int y) public static double doSomething3(String a) 7. (Algorithm Workbench 7) Write a method named timesTen . The method should accept a double argument, and return a double value that is ten times the value of the argument. 8. (Programming Challenges 5) Falling Distance When an object is falling because of gravity, the following formula can be used to determine the distance the object falls in a specific time period: 𝑑 = 1 2 𝑔𝑡 2 The variables in the formula are as follows: d is the distance in meters, g is 9.8, and t is the amount of time, in seconds, that the object has been falling. Write a method named fallingDistance that accepts an object’s falling time (in seconds) as an argument. The method should return the distance, in meters, that the object has fallen during that time interval. Demonstrate the method by calling it in a loop that passes the values 1 through 10 as arguments, and displays the return value. 9. Complete the AreasImproved.java program from the videos by finishing the calcRectArea method and implementing the calcTriangleArea method. Use the getValue method to obtain the base and height of the triangle. 10. (Algorithm Workbench 4) Desk-check the following code to show what it will display. (You will need more lines than are provided below. 1. public class ChangeParam { 2. public static void main(String[] args) { 3. int x = 1; 4. double y = 3.4; 5. System.out.println(x + “ “ + y); 6. changeUs(x, y); 7. System.out.println(x + “ “ + y); }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
8. public static void changeUs(int a, double b) { 9. a = 0; 10. b = 0.0; 11. System.out.println(a + “ “ + b); } } Line # x y a b output The Practice Problem Write a text adventure game using methods that allows the user to move around a map or choose quit. The game must keep track of the user’s current location and allow the user to enter a directional command (N,S,E,W) or quit (Q). Validate the user’s input using an input validation loop (do not let the user proceed until they have entered a valid command). Then move the user to the appropriate location based on the directional command, or end program execution if they choose quit. One design suggestion is to implement a method for each room. An example of a program running might look like this: You are in the library. You can move (N)orth or (E)ast. What is your command? O I don't understand. You are in the library. You can move (N)orth or (E)ast. What is your command? (N,S,E,W, or (Q)uit: P I don't understand. You are in the library. You can move (N)orth or (E)ast. What is your command? (N,S,E,W, or (Q)uit: S You can't walk through a bookcase. You are in the library. You can move (N)orth or (E)ast. What is your command? N You are in a living room. You can move (S)outh or (E)ast. What is your command? E You are in the dining room. You can move (S)outh or (W)est. What is your command? W You are in a living room. You can move (S)outh or (E)ast. What is your command? S You are in the library. You can move (N)orth or (E)ast. What is your command? E You are in a kitchen. You can move (N)orth or (W)est. What is your command? E Careful! That's the fridge. You are in a kitchen. You can move (N)orth or (W)est. What is your command? Q Library Liv. Room Din. Room Kitchen
This example is for the following room layout: After you have this working perfectly, you can add the following enhancements in this order: 1. Give the player health points. Fill one room with poisonous gas. If the player mistypes their command, decrement their health points. Report their health points to them each turn. If their health points drop to zero, end the game. 2. Randomly add an item to a room. Describe the item each time the user enters the room. 3. Add a save command. Write the current room and health points to a file when the user saves. 4. Add a restore command. Read the current room and health points from a file when the user restores. Move them to that room and restore their health points. 5. Add an inventory of one item. Put multiple items in the game. Add a command to pick up and a command to drop an item. Do not allow the user to pick up an item if they are already holding an item. 6. Add rooms to the game. 7. Add a goal to the game and allow the user to win by achieving the goal. The Kennel Problem You’re wondering if you should continue to take calls from your friend who works in a kennel, because now they would like another update to the food calculation program (see problem 1 in Labs 1, 2, and 3). Please modify the program to read data from a text file and produce a report (to the console). The data in the file will include dog’s name, ideal adult weight, and then a “Y” or an “N” for whether vegetables should be part of the dog’s meal. The report should be the meal calculations for each dog (dog’s name, followed by weight of each of the ingredients for the meal), with a summary at the end such as you produced for lab 3. You realize that the food calculation program might be easier to read and modify in the future if it were broken into methods. Devise a good functional decomposition design for the program and re- implement it with at least one method (besides main).