Unformatted text preview:

Name USC Student ID Number USC NetID e g ttrojan CS 455 Midterm Exam 1 Spring 2024 Bono Tuesday Feb 13 2024 There are 6 problems on the exam with 54 points total available There are 10 pages to the exam 5 pages double sided including this one make sure you have all of them If you need additional space to write any answers or scratch work pages 9 and 10 are left mostly blank for that purpose If you use those pages for answers you just need to direct us to look there Do not detach any pages from this exam Note if you give multiple answers for a problem we will only grade the first one Avoid this issue by labeling and circling your final answers and crossing out any other answers you changed your mind about though it s fine if you show your work Put your name USC Student ID and USC username a k a NetID at the top of the exam Also put your NetID at the top right of the front side of each page of the exam including the last page Please read over the whole test before beginning Good luck Selected methods of Java Point class shown by example for problem 1 new Point x y Constructs point object with given x and y values Constructs point object with the same x and y values as point object p has new Point p p translate dx dy Changes x and y values of p by dx and dy respectively I e if p had coordinates x y its new value is a point with coordinates x dx y dy 1 Problem 1 10 pts total Consider the following program Note more about Point class on the cover page of the exam public class Prob1 public static void main String args Point p new Point 6 12 Point q new Point 15 10 Point r new Point p p q r p q translate 3 3 r translate 3 3 System out println p q r Part A 7 In the space below draw a single box and pointer diagram a k a memory diagram showing all object variables objects and their state as they have changed during the code sequence Part B 3 What is printed by the code For the purpose of this problem assume a Point is printed as follows x y 2 USC NetID return value of lastLetter str 6 5 5 2 1 1 1 Problem 2 6 points Consider a method to find the location of the last letter in a String object If there are no letters in the string it returns 1 Here are some examples of input and corresponding return values that should be produced by the method str big boy bigboy 23xyz xyz12 123 The following implementation of the method described above doesn t always do the right thing Note the Character class method isLetter ch tells whether its char argument is a letter public static int lastLetter String str for int i str length 1 i 0 i if Character isLetter str charAt i return i else return 1 return 1 Do not modify the code Show two example data values and the result of calling the method above on each of them the first one should be one where the existing method returns an incorrect value and a second one such that the method returns a correct value return value of lastLetter str str 1 wrong 2 right 3 Problem 3 13 points Implement the class DigitExtractor which breaks up an integer into its individual digits After being initialized with a positive integer each call to nextDigit returns the next digit in the integer starting from the leftmost most significant digit For full credit call the helper method largestPowerOf10 only once in the lifetime of a DigitExtractor object Here is an example of code to use the class It extracts all of the digits of 27 054 DigitExtractor extractor new DigitExtractor 27054 while extractor hasNextDigit System out println extractor nextDigit Corresponding output 2 7 0 5 4 You may assume we have already written the following helper method for you that works as described i e that means you may call this method in your solution Returns the largest power of 10 that is less or equal to num or put another way returns a power of 10 that has the same number of digits as num PRECONDITION num 0 Examples num return value of largestPowerOf10 num 999 100 1000 1000 1001 1000 private static int largestPowerOf10 int num For full credit call largestPowerOf10 only once per DigitExtractor object DigitExtractor hints The modulus operator gives the remainder of dividing two integers For example the result of 17 3 is 2 17 divided by 3 has a remainder of 2 Reminder Integer division is done with the operator performed on two integers For example the result of 17 3 is 5 Don t forget cases such as 9000 The class interface and space for your answer is provided on the next page 4 USC NetID Problem 3 cont Complete the implementation of DigitExtractor details of this problem are on the previous page DigitExtractor breaks up a positive integer into its individual digits public class DigitExtractor Creates digit extractor for the given integer param anInt the integer to extract from PRECONDITION anInt 0 public DigitExtractor int anInt Returns true iff there are more digits left to extract public boolean hasNextDigit Extracts the the next digit in the integer starts from leftmost most significant digit and goes rightward PRECONDITION hasNextDigit return the digit public int nextDigit largestPowerOf10 see comment on previous page private static int largestPowerOf10 int num already written 5 Reminder of some Linux shell commands and other shell syntax for problem 2 below ls cd cp mv rm mkdir rmdir more diff Problem 4 4 pts The directory in Vocareum that has all the lecture code examples is ASNLIB public in the Vocareum Lecture code area Write a sequence of one or more Linux commands to copy all the files from the 02 08 subdirectory of the lecture code examples directory to a new directory named practice in your own Lecture code workspace home directory Relevant information You may assume there are no subdirectories in the existing 02 08 directory just regular files ASNLIB is a shell variable defined on Vocareum that expands to the correct absolute path to get to that specific directory this problem A reminder of the names of some common Linux commands and related syntax appears above The commands below is the Linux prompt show the starting conditions in your Lecture code workspace including the current contents of that workspace cd ls arrayTests classTests 6 USC NetID Problem 6 6 points Consider the following implementation of a class Circle public class Circle private double radius private double circumference create a circle with given radius public Circle double radius this radius radius circumference 2 Math PI radius get the circumference public double getCircumference return circumference halve the


View Full Document

USC CSCI 455x - Midterm Exam 1

Download Midterm Exam 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Midterm Exam 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Midterm Exam 1 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?