It is most beneficial to you to write this mock midterm UNDER EXAM CONDITIONS. This means: Complete the midterm in 1.5 hour(s). Work on your own. Keep your notes and textbook closed. Attempt every question. After the time limit, go back over your work with a different colour or on a separate piece of paper and try to do the questions you are unsure of. Record your ideas in the margins to remind yourself of what you were thinking when you take it up at PASS. The purpose of this mock exam is to give you practice answering questions in a timed setting and to help you to gauge which aspects of the course content you know well and which are in need of further development and review. Use this mock exam as a learning tool in preparing for the actual exam. Please note: Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work. Often, there is not enough time to review the entire exam in the PASS workshop. Decide which questions you most want to review the Facilitator may ask students to vote on which questions they want to discuss in detail. Facilitators do not bring copies of the mock exam to the session. Please print out and complete the exam before you attend. Facilitators do not produce or distribute an answer key for mock exams. Facilitators help students to work together to compare and assess the answers they have. If you are not able to attend the PASS workshop, you can work alone or with others in the class. Good Luck writing the Mock Exam!! Dates and locations of mock exam take-up:
Question 1 In this question, you are going to develop a class called Te mperat ure that will be used to collect data of temperatures using arrays a) This class will use the Rando m class in Java. Write the required code to access the class b) Our class will have five fields: MAX: the maximum size of our collections: a constant to be set to 10 ar : an array of integers curr Max: the current maximum size of our collections (not a constant) count : the current size of the array Define the instance variables:
c) Our class has one constructor. This constructor has no parameters. It sets curr Max to a random number between1 and MAX, using a Rando mobject. The array contains a maximum of currma x items. Write the constructor. Javadoc comments are required for full marks public Temperatures() d) Write method addte mp which takes an int parameter and returns a boolean. If there is space in the array, the method adds the int to the array, after any other values already present, and returns true. Otherwise, the method returns false. Javadoc comments are required for full marks.
e) What changes are required if we insert the int value before any other values already present in the array? Do not re-write the entire method, just modify the code to satisfy the new requirement and explain what it has replaced
f) Write a method call fi ndte mp Val ue which has an int parameter and returns a boolean. It is to search the array for the given parameter and return true if there is at least one copy in the array, and false otherwise. Do not write Javadoc comments. You must use a "for" loop for full marks. g) Write a method call pri nt All which prints all the values in the array. Do not write Javadoc comments. You must use a "for each loop for full marks.
Question 2 We re going to define a class, Car, which represents a car ( ex: in a showroom). The class contains fields for the name and the company of the car, as well as the number of cars in stock for the particular car In the class definition below, fill in the parameter list for the constructor, and the missing code for the constructors public class Car pri vat e Stri ng na me; // na me of t he car pri vat e Stri ng comp; // the company t hat manufact ured t he car pri vat e i nt st ock; // number of cars in stock
* Constructor for objects of class Car * Takes three parameters and sets the * fields based on the parameters entered. * you have to fill in the parameters * public Car ( ) * getname() returns the name of the car
public String getname() SYSC 2004 by * getcomp() returns the company the car was manufactured public String getcomp() * getstock() returns the number of cars in stock public int getstock()
* addstock() increments the stock of the car by 1 public void addstock() * removestock() decrements the stock by 1. The stock cannot go below 0.
* If the method succeeds in decrementing the stock, it returns true. * If it does not (i.e. stock was already 0) it returns false public boolean removestock() // end of class
Question 3 Next we re going to put our cars in a showroom! There is one field: an ArrayList which will contain all the cars in the showroom. Fill in the instance variable, the constructor, and the three methods below. (. Keep in mind the equals() method when checking whether two strings are the same. Refer to the final page for a list of ArrayList methods. public class Showroom // define instance variable * Method carindex takes the name and the company of the car being searched for * and returns its index in the carlist * If it doesn't find a car that matches, it returns -1. * (Hint: you can use this method to help you with the next two)
public int carindex (String name, String comp) SYSC 2004 * Method addcar takes one Car parameter. If the Car is already in * the carlist, addcar increases the stock of the Car to reflect * the combined stock of the Cars added and the one already there. If the Car is not already there, addcar adds it to the end of the carlist.
public void addcar (Car newcar) * Method borrowcar takes the name and company of the car to be borrowed * and decrements its stock by 1. If it succeeds in doing this it returns true. * If it can't find the car, or there is no stock to remove it returns false.
public boolean borrowcar (String name,string comp) // end of class