



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The cs 61b midterm 1 exam from spring 2001, focusing on java programming. The exam includes multiple-choice problems related to java assignment statements, error identification in given code, and writing java methods. The exam also covers the islamicdate and account classes.
Typology: Exams
1 / 7
This page cannot be seen from the preview
Don't miss anything!
Put your login name on each page. Also make sure you have provided the information requested on the first page.
Write a single Java assignment statement that declares and assigns the variable maryAcct to have the value shown in the picture. Solutions of more than one statement may earn partial credit. The Account class definition from lab assignment 2 appears at the end of this exam.
Your solution:
The two parts of this problem both involve an IslamicDate class that is somewhat simpler than that used for homework assignment 2. An implementation of this class appears near the end of this exam.
Part a Given below is an implementation of a DateTester class that could be used with the abbreviated IslamicDate class provided at the end of this exam. The statements in the body of the main method are not necessarily correct; errors include both compile-time and run-time errors.
For each statement, indicate whether it is OK or will result in an error by circling the appropriate word. You may assume that all methods in the abbreviated IslamicDate work correctly and that each line with an error is fixed before you consider the lines that follow it.
public class DateTester { public static void main (String [ ] args) { IslamicDate d1 = IslamicDate (1, 1); OK error System.out.println ("" + d1); OK error IslamicDate d2 = d1.makeTomorrow (); OK error IslamicDate d3 = null; OK error System.out.println (d3.tomorrow ()); OK error }
CS 61B, Spring 2001 Midterm #1 Professor Paul N. Hilfinger 1
Part b Give the Java code for a DateTester method isLaterThan that determines if the first of its argument dates is later than the second. The isLaterThan method will be used by adding the statement
System.out.println ("IS " + d1 + " later in the year than "
to the end of the (Corrected) main method in the DateTester class of part a. (As in part a, DateTester will be used with the abbreviated IslamicDate class, not the class you implemented in homework assignment 2. You aren't allowed to change the IslamicDate class.)
// REQUIRES: two nonnull IslamicDate references, representing dates // in the same year. // MODIFIES: nothing // EFFECT: returns true if the first argument is later in the year // than the second; returns false otherwise.
Consider the following (incorrect) version of the contains1MoreThan method, similar to those you worked with in lab assignment 3. Assume that it's defined as part of a class StringToCheck5 ; the rest of the code from lab assignment 3 appears at the end of this exam.
// REQUIRES: s is not null. // EFFECT: returns true when myString is the result of inserting // exactly one character into s; returns false otherwise.
public boolean contains1MoreThan (String s) { if (s.length( )==0 { return true; } else if (myString.length( )==0 { return false; } else { StringToCheck5 remainder = new StringToCHeck5 (myString.substring(1)); if (myString.charAt(0) == s.charAt(0)) { return remainder.contains1MoreThan (s.substring(1)); } else { return remainder.contains1MoreThan (s); } } }
Part a Can this method crash when given an argument that satisfies the REQUIRES clause? Briefly explain.
Part b Describe all pairs of Strings myString and s for which contains1MoreThan should return false but doesn't. For partial credit, you may give a single pair of Strings for which contains1MoreThan should return false but doesn't.
Problem #2 (8 points, 20 minutes) 2
return; } myDay = 1; myMonth++; if (isLegal ( )) { return; } myMonth = 1; }
// EFFECT: returns a date that represents the next calendar day. public IslamicDate tomorrow ( ) { IslamicDate d = new IslamicDate (myMonth, myDay); d.makeTomorrow ( ); return d; }
// EFFECT: returns true if this represents a legal Islamic date; // returns false otherwise. private boolean isLegal ( ) { // body of isLegal would go here. }
private int myMonth; private int myDay; }
public class Account { /**
/**
Framework of an abbreviated IslamicDate class 4
myBalance = balance; myParent = overdraftAcct; }
/**
private int myBalance; private Account myParent; }
public class Lab3Tester { // Test teh various contains1MoreThan methods. public static void main (String [ ] args) throws Exception { ... (various calls to check) }
// Call one of the StringToCheck contains1MoreThan methods // to see if the String tryLarger is the result of inserting // exactly one character into the String trySmaller. // The value of whichTest indicates which StringToCheck class // is used: whichTest == 1 means StringToCheck1, 2 means
Framework for the Account class used in lab assignment 2 5
Posted by HKN (Electrical Engineering and Computer Science Honor Society) University of California at Berkeley If y 7