CSE114 Final Practice 2

docx

School

Stony Brook University *

*We aren’t endorsed by this school

Course

114

Subject

Computer Science

Date

Apr 3, 2024

Type

docx

Pages

8

Uploaded by CoachPartridgeMaster734

Report
CSE 114 Sample Final Exam Version 2 Name: First Last Student ID # _________________________________ Question # Points Available Points Earned 1 10 2 10 3 20 4 20 5 20 6 20 Total: 100 Directions: You have 120 minutes to complete this exam. Write all answers neatly in the space provided. Closed book exam: you are not allowed to use the textbook, lecture notes, cheat sheets or anything else beside your brain during the exam. Use well-named variables and include any documentation where you feel it is necessary to understand your algorithms. CSE 114 – Final Exam Sample Version 1 1
Question 1 Printing problem: A Scottish tartan is a pattern consisting of cross-crossed horizontal and vertical colors. - You have to implement a method that prints the tartan of the Young McJava clan. The method takes a number n as input and prints a matrix of n rows and n columns of interleaved vertical and horizontal n and n-1 separated with spaces, starting with n. Implement the YoungMcJava method using for-loops . Implement the whole program that tests your printing method. Examples: tartan(3) prints: 3 2 3 2 3 2 3 2 3 tartan(4) prints: 4 3 4 3 3 4 3 4 4 3 4 3 3 4 3 4 CSE 114 – Final Exam Sample Version 1 2
Question 2 Recursive printing problem: Implement the YoungMcJava program from problem 1without using loops. You are allowed to use only recursion. CSE 114 – Final Exam Sample Version 1 3
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
Question 3 Extract IPv4 address from int: An IP address is the address of every computer on the Internet written as 4 bytes separated by dots (e.g., 130.245.27.2). Any four bytes can be stored in an int. a. Write a class IPAddress that stores IP addresses in one private field intIP . Write constructors (empty and one that takes an int), accessor and mutator methods for the class. b. Write an extractIPaddressFromInt method that takes an ipAddressAsInt argument, extracts, prints and returns an array of 4 bytes with the 4 components of the IP address. For example: IPAddress ip = new IPAddress(839391568); byte[] bs = ip.extractIPaddressBytes(); would print: IP Address in single int: 839391568 IP Address: 50.8.25.80 and binds bs variable to a byte array with 4 elements: [50, 8, 25, 80]. CSE 114 – Final Exam Sample Version 1 4
c. Write a mutator setIP method that takes as input an array of 4 bytes, sets the IP address to the corresponding int value, and prints it. This method should throw an exception if it doesn’t have enough arguments. For example: byte[] bs = new byte[4]{0,0,1,1} ip.setIntIP(bs); would set the dynamic field intIP to 257 and print: IP Address in single int: 257 Write also a constructor method that takes a byte array as a parameter and creates an IPAddress object. d. Write a test main method for this class to test all methods: create one IP address for 839391568 and one for 50.8.25.81, change both addresses to 50.8.25.82 and print both IP addresses. CSE 114 – Final Exam Sample Version 1 5
Question 4 Create a class Person with: constructors, private fields for name and date of birth, accessor, mutator and toString methods. One constructor takes a string for name and another String for dob, which must be in the format "mm/dd/yyyy". Define a calculateAverageAge method such that calculates and returns the average age of all the Person objects in an array of people. Note that this method should return an integer that is rounded to the nearest whole number, up if .5 or greater, down if strictly smaller than .5. Note that you may not assume that the array is full or packed (Person objects may be scattered at any index in the array with gaps in between). Write a whole program with a main method to test your class. CSE 114 – Final Exam Sample Version 1 6
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
Question 5 The Employee class extends Person and has a salary instance variable. Create a constructor for the class that takes the date ("12/12/1990") and the salary as strings ("150,000"). Write a toString method to print the Employees in the format "John Doe, 01/01/1990 ($150,000)". Provide and parse commas for large numbers (e.g., 150,000). CSE 114 – Final Exam Sample Version 1 7
Question 6 Write a Cards class and a sortCards method such that it sorts a cards array argument such that cards are ordered in groups of Clubs < Diamonds < Hearts < Spades , and then by rank within their suit (where A(Ace) < 2 < 3 < … < 10 < J(Jack) < Q(Oueen) < K(King)). For example, the array ["8H","10H","QD","JD","4S"] (i.e., 8 of Hearts, 10 of Hearts, Queen of Diamonds, Jack of Diamonds, 4 of Spades) is sorted into the sorted array: ["JD","QD","8H","10H","4S"]. CSE 114 – Final Exam Sample Version 1 8