Starting from:

$25

CS213 - Final Exam - Solved

Q1 Object Design
10 Points

 Consider a college where a faculty member could either be a teacher or a researcher, or both. You are asked to design the most appropriate object-oriented relationship between faculty member, teacher, and researcher. Outline a Java object-oriented implementation with minimal class members: at least one field that distinguishes instances of a class from instances of other classes, one constructor, and one method in each class. You may NOT use abstract classes or interfaces. Credit will be proportional to the appropriateness of your design for this scenario. You are NOT asked to draw a UML diagram.

Enter your answer here

Q2 UML Class Diagram
20 Points

You are modeling a small part of an online flight reservation system, according to the following description. A flight is a single non-stop hop between a pair of cities. A booking can include several flights and travelers (think of a family vacation hitting multiple cities), with the requirement that all the travelers are on all the flights in a booking.

Furthermore, every booking has a single owner who is one of the travelers on that booking (think one family member booking the tickets for the entire family), and the owner can manage functionality on the system that other travelers cannot. A separate ticket is issued and priced individually for each traveler on a booking, and the ticket applies to all flights within that booking.

"Draw" (see below for special drawing instructions) the most appropriate UML class diagram for your model. Identify entities (classes/abstract classes/interfaces) clearly, and separate them according to their functionality--the separation should be faithful to the description above. Set up relationships between entities as precisely as possible. Inside each class in the diagram, only list the class name and minimal number of attributes required to characterize objects of that class (no operations needed). For the attributes, you are not required to show access level.

Instructions for how to draw (Do NOT upload pictures):

You must show all drawing in text, as in the problem set UML solutions.

Class/Interface:

Just list class name, and specify attribute names in that class in parentheses, e.g. Student (name, major)  

  Put down "abstract" or "interface" before entity name, if applicable Relationships:

 Subclass: A <|--- B

Subinterface, Interface implementation: same as subclass

Association: ------  (with < or > on either side for direction) Aggregation: <>----  (for composition just write "composition" above or below the association line)

  Dependency: same as unidirectional association, but write

"dependency" above or below the association line

  Association class: If A is association class for an X--Y association, simply spell it out instead of drawing a line hanging off the X--Y association.

(e.g. "A is an association class for X--Y")

  Multiplicity: write above either end as in the UML problem set solutions

Write each relationship separately even if an entity happens to participate in more than one relationship - this way you don't need to do cumbersome "vertical" plain text drawings.


 

Q3 Design Patterns
12 Points

You are developing an application for an online travel agency. The agency manages several packages to various tourist sites. Each package consists of transport to site (day 1), followed by activities for each of the next 3 days, and finally transport back home (day 5). The transport details and the actual daily activities differ depending on the package.

  Show how your would use the template method design pattern to implement the various tour packages. Write the appropriate classes and method headers (no logic needed inside method bodies) to clearly show the design pattern code structure.

 

Q4 Streams
14 Points

All answers must start with a way to source a stream, followed by a single sequence of stream operations (including collect) ONLY.

For each answer, the result MUST be assigned to a named and typed variable:  

e.g. Integer res = ... sequence of stream operations ... Here res (name) is the result variable of Integer (type) You don't need to write import statements for any of the classes and interfaces you use.

Q4.1
9 Points

In a document named doc.txt, count unique words (case insensitive) by length, i.e. how many unique words of what length. (For instance, 3 words of length 4, 5 words of length 3, etc.) Assume that a word is a sequence of non-space characters, and each line of the document has one or more words.

 

Q4.2
  5 Points

 Consider an nx2 array, double[][] arr , in which each of the 2 columns represents a vector. Write code to get the dot product of the column vectors, i.e. sum of products of corresponding items.

 

Q5 Streams
21 Points

All answers to this question must start with a way to source a stream, followed by a single sequence of stream operations (including collect) ONLY.

For each answer, the result MUST be assigned to a named and typed variable:  

e.g. Integer res = ... sequence of stream operations ... Here res (name) is the result variable of Integer (type)

You don't need to write import statements for any of the classes and interfaces you use.

For this question, you are given an enumeration and a class as follows:

public enum Language {JAVA, PYTHON, C, RUBY, SCALA, PHP} 

public class Coder { 

  ... 

   public String getName() { ... }   public String getCity() { ... }   public int getYear() { ... }   public int getLOC() { ... }   // LOC => Lines Of Code   public Language getLanguage() { ... } 

}

  Assume a pre-populated list of coders, List<Coder> coders . From this list, extract the data required in each of the following questions.

Q5.1
7 Points

Get names of all coders for year 2019, sorted from most prolific

(greatest LOC) to least

 

Q5.2
7 Points

For each language, which cities had coders that wrote at least 50,000 lines of code in that language?

 

Q5.3
7 Points

Which coder (get the name of any one coder) wrote the most lines of code in Python in 2020? If no coder is found, the result should be "No coder found"

 

Q6
20 Points

  Consider the following BankAccount class:

public class BankAccount { 

                                                             private float balance; 

                                                             public BankAccount(float money) { balance = money; } 

                               public void deposit(float money) {         balance = balance + money;

   }                   

    public void withdraw(float money) throws Exception {       if (balance >= money) balance = balance - money;       else throw new Exception(money +  

                               " is more than balance");  

    

   public float getBalance() { return balance;} }                   

Q6.1
8 Points

Detail one scenario in which concurrent use of a BankAccount object by two threads results in an incorrect bank balance when the threads are done executing. Show the exact interleaved sequence of operations executed by the threads. No partial credit - either the scenario is correct, or it isn't.

 

Q6.2
4 Points

  Show how you would modify the BankAccount class to make it thread safe, i.e. multiple threads can concurrently access an instance without the above problem. Keep in mind that you should not overdo it by forcing unnecessary sequentiality among concurrent threads.

 

Q6.3
8 Points

Would your thread safe class of 6.2 above offer sufficient protection against concurrently running threads, one of which is transferring money from one account to another, and another is accessing one or both accounts for withdrawal or deposit or balance check? If so, describe how thread safety is ensured. If not, give an example of an error/unsafe situation, and briefly explain how it could be corrected (no need to write code for the fix, a clear and concise explanation will do).

 

Q7 Multithreading
15 Points

  Class X has synchronized methods S1 and S2. Consider an instance  xinstance of class X, consumer threads T1 and T2, and supplier thread T3, and the following sequence of actions on xinstance :

  Thread T1 enters method S1, and at some point issues a wait() , because T3 hasn't supplied yet

Subsequently, thread T2 enters method S1, and at some point issues a wait() , because T3 hasn't supplied yet

 Finally, thread T3 enters method S2, fills in the supply, issues a  notifyAll() , finishes and exits S2

  Assume that T3's supply is sufficient for only one of the consumers, and that once T3 has finished executing S2 as above, it is terminated (i.e. it will not return to S2 again).

But T1 and T2 are insatiable and will want to keep coming back to method S1 for more. Assume that there is no attempt by the application to safely terminate either T1 or T2, i.e. they are allowed to keep running.

 List ONE plausible sequence of thread states that T1 and T2 might go through, starting from the time they issue a wait() , as described above, up to the time when there will not be any more state changes.

For the purpose of this question, assume that there is an additional state, RUNNING, for when a thread is actually executing on the CPU.

For each state change of each of T1 and T2, specify the cause of change of state.

 

More products