CSCI161 Fall 2023 -- Exam #1 Solutions Problem 1: ---------- a) What is the difference between an object and a class? A class is the "blueprint" that describes all of the state and behaviors (variables and methods) that should be included in each instance of the class that we create. The terms instance and object mean the same thing -- both refer to the software items that are produced when we call "new". For example, the Circle class describes the instance variables and methods that should be present in each Circle object (instance of the Circle class) that we create. b) What do we mean by the “state” of an object? Informally, an object's state consistes of the values "it remembers". More specifically, the state is the set of values stored in an object's fields. c) What’s the difference between declaring and initializing a variable? When we declare a variable we're asking Java to set aside a "box" in memory to hold a value of a particular type. Declaring the variable doesn't say anything about the VALUE we'd like stored in the box though. Initializing a variable is the process of assigning a value to it for the first time. Problem 2: ---------- public class Turtle { private int xDirection; private int yDirection; private int distance; private Circle timmy; public void mystery(Turtle t) { if (xDirection == -1 || t.xDirection == -1) { xDirection = -1; t.xDirection = -1; yDirection = 0; t.yDirection = 0; } else { xDirection = 1; t.xDirection = 1; yDirection = 0; t.yDirection = 0; } } } a) If mystery is called on a Turtle that’s facing west, and is passed a reference to a Turtle facing north, it would end up making both turtles face west. The turtle on which the method is called has an xDirection of -1, so the boolean expression is true and we take the first branch of the IF statement. Those assignment statements make both turtles face north (xDirection of -1, yDirection of 0). b) In general, this method "aligns" two turtles so they're both facing the same direction. If either of them is facing west they both end up facing west, otherwise they both end up facing east. Problem 3: ---------- /** * For full credit, the two IF statements need to be independent. * (That is, there's no ELSE between them.) */ public void statusReport() { if (xDirection != 0) { // One way of checking System.out.print("Moving horizontally."); } if (yDirection == 1 || yDirection == -1) { // Could also do this System.out.print("Moving vertically."); } } Problem 4: ---------- /** * Applies the appropriate late penalty to an assignment score and * returns the adjusted score. * @param daysLate An integer number of days late (0 or more) * @param score The score being adjusted * @return What's left of score after applying late penalty */ public double adjustedScore(int daysLate, double score) { if (daysLate == 0) { return score; } else { if (daysLate == 1) { return score - score*.05; } else { if (daysLate == 2) { return score - score*.1; } else { return 0; } } } } /** * This shorter version takes advantage of the fact that the formula * for producing the adjusted score is the same for 0, 1, and 2 days * late, so we really only have TWO cases to worry about. */ public double adjustedScore2(int daysLate, double score) { if (daysLate <= 2) { return score - daysLate*.05*score; } else { return 0; } }