Question 1

Consider your university/campus library and imagine about the workflow of the library. A student can borrow/rent items from the library. Once the items are selected, a one-time checkout session is created to issue the invoice. Now consider the following class diagram with important entities of the system. Based on the properties of the system, the relationship between the classes can be aggregation, multiplicity, composition, association, generalization, etc.

Diagram: see image.

Based on the above description, find out the appropriate values for:

A (relation between Student and Library Invoice)
B (relation between Checkout Session and Library invoice)
C (relation between Library Item and Library Invoice)
D (relation between actual items and Library Item class)

In addition, you have to write down the reason in brief for selecting that property

Question 2

The following PhoneDiary class has three public data members:

  • ContactName: a String type data member
  • Setter method: setName(string)
  • Getter method: getName

The setName sets the name to ContactName after checking that the input parameter is not null. On the other hand, the getName method return ContactName if it is not null, otherwise returns "Not Initialized"

Identify the coupling behavior for the following program and justify your answer. If you think that the following program is an example of a bad application design, write down the modified code that satisfies the properties of a good design.

public class PhoneDiary {
public String ContactName;
public String getName() {
if(ContactName == null)
return "Not initialized";
else
return ContactName;
}

public void setName(String Nm) {
if(Nm != null)
this.ContactName = Nm;
else
System.out.println("Name cannot be initialized to a null");
}
}

public class Process {
public static void main(String[] args) {
PhoneDiary pd = new PhoneDiary();
pd.ContactName = null;
System.out.println("Name is " + pd.ContactName);
}
}

Question 3

a. Identify the cohesive behavior for the following program and justify your answer

b. If you think that the program is an example of bad application design, write down the modified code that satisfies the properties of a good design.

public class Purchase {
public void ConnectToSite() {
System.out.println("Inside ConnectToSite");
}

public void SelectItems() {
System.out.println("Inside SelectItems");
}

public void ProceedToPayment() {
System.out.println("Inside ProceedToPayment");
}

public void CloseConnection() {
System.out.println("Inside CloseConnection");
}

public static void main(String[] args) {
Purchase myPurchase = new Purchase();
myPurchase.ConnectToSite();
myPurchase.SelectItems();
myPurchase.ProceedToPayment();
myPurchase.CloseConnection();
}
}

Question 4

Consider the following Sort class:

public class Sort {
public double[] Sort3(double value1, double value2, double value3) {
double[] sorted = new double[3];

if(value1 >= value2 && value1 >= value3) {
sorted[2] = value1; // stores the largest value in the final index
if(value2 <= value3) {
sorted[0] = value2; // stores the smallest value in the first index
sorted[1] = value3; // stores the middle value in the middle index
}
else {
sorted[0] = value3;
sorted[1] = value;
}
}
else if(value2 >= value1 && value2 >= value3) {
sorted[2] = value2;
if(value1 <= value3) {
sorted[0] = value1;
sorted[1] = value3;
}
else {
sorted[0] = value3;
sorted[1] = value1;
}
}
else {
sorted[2] = value3;
if(value2 <= value1) {
sorted[0] = value2;
sorted[1] = value1;
}
else {
sorted[0] = value1;
sorted[1] = value2;
}
}

return sorted; // returns the sorted number in an array of 3 doubles
}
}

The method Sort3 of the class Sort takes 3 (three) numbers as input and returns the numbers in an array in the sorted. For example, if the inputs are 5, 3, 8 the output will be [3, 5, 8].

Write 5 (five) Unit Tests for the above class to test Calculate method. Note that all the Unit Tests should address distinct scenarios.

Question 5

The following questions relate to the Student class provided below:

(a) Identify one (1) bad smell in the Student class and explain why this is an issue. How can this bad smell be removed?

(b) Rewrite the Student class to demonstrate the removal of the bad smell identified in part (a). You only need to rewrite the section that requires modification.

public class Student {
private String name;
private String phone;
private int id;
public Student() {
this.name = null;
this.phone = null;
this.id = 0;
}

public Student(String name, String phone, int id) {
this.name = name;
this.phone = phone;
this.id = id;
}

public void showEmployeeDetail() {
if((this.name) == null || (this.phone) == null || (this.id) < 1)) {
System.out.println("Missing Data from this object");
}
else {
System.out.println("Name = " + name + ", Phone = " + phone + ", Age = " + id);
}
}

public static void main(String[] args) {
Student s1 = new Student();
s1.showEmployeeDetail();

Student s2 = new Student("James", "0431794657", 41849012);
s2.showEmployeeDetail();

Student s3 = new Student();
s3.setData("Moly", "0496970808", 402020202);
s3.showEmployeeDetail();
}
}

Question 6

a) Explain what is meant by an Interface. Identify three (3) features of an Interface that differ from those of a class.

b) The below code defines the Subjects interface. Write a class named "Lesson" that uses the Subjects interface. This outline must include the class definition and any methods that are mandatory to allow the use of Lesson. The code does not need to contain any functionality.

public interface Subjects {
public void reading();
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.