You are on page 1of 11

ZQMS-ARC-REC-002

ASSIGNMENT COVER

REGION: Bulawayo SEMESTER: 1 YEAR: 3

FULL NAME OF STUDEN T: Obey Sibanda PIN: P1910721r

EMAIL ADDRESS: obeydlamini@gmail.com


PROGRAMME: Bachelor of Software Engineering Honors INTAKE: 7

CONTACT TELEPHONE/CELL: 0783998634 ID. NO.: 08-805631 M35

COURSE NAME: Object Oriented Programming COURSE CODE: BSEH 341

ASSIGNMENT NO. e.g. 1 or 2: 2 STUDENT’S SIGNATURE O.S

DUE DATE: SUBMISSION DATE:

ASSIGNMENT TITLE: Assignment 2


Instructions
Marks will be awarded for good presentation and thoroughness in your approach.
NO marks will be awarded for the entire assignment if any part of it is found to be copied
directly from printed materials or from another student.
Complete this cover and attach it to your assignment. Insert your scanned signature.

Student
declaration I
declare that:
• I understand what is meant by plagiarism
• The implications of plagiarism have been explained to me by the institution
• This assignment is all my own work and I have acknowledged any use of the published or
unpublished works of other people.

MARK ER’S COMMEN TS:

OVERALL MARK: MARK ER’S NAME:


MARK ER’S SIGNATURE: DATE:

QUESTION 1
a) Write a full java program that contains a method called findMode() that takes a
LinkedList of Integers as a parameter and returns the mode of the LinkedList as an
integer. The method should not assume that the values are sorted in increasing order,
therefore when looking for the mode, the method should not take benefit of the fact
that the numbers are ordered and same numbers are stored on consecutive locations.
You may use any other lists, or other data structures from the java Collections
framework to help you solve this problem. You may assume that the LinkedList passed
is not null.
Answer:
class ModeOfNumbers {

//* Prints the mode from the LinkedList of integer number.


@SuppressWarnings("FinallyDiscardsException")
public static void findMode(java.util.LinkedList <Integer> nums) {

java.util.LinkedList <Integer> modeNumbers= new java.util.LinkedList<>();

// represents the Maximum Count...


int mCnt = 0;

for (int c = 0; c<nums.size(); c++)


{
int counter = 0;

for(int i = 0; i<nums.size(); i++) {

if( ( nums.get(i) != nums.get(c) ) == false )


{
counter++;
}
if((counter < mCnt) == false) {

mCnt = counter;
modeNumbers.clear();
modeNumbers.add(nums.get(c));
}
else if((counter != mCnt) == false) {

modeNumbers.add(nums.get(c));
}
}
}
for(int c = 0; c<nums.size(); c++) {

try{
System.out.println("Mode: " + modeNumbers.get(c));
}
finally { return; }
}
}

public static void main(String[] args) {

java.util.LinkedList<Integer> numbers = new java.util.LinkedList<>();

numbers.add(2);
numbers.add(4);
numbers.add(6);
numbers.add(8);
numbers.add(10);
numbers.add(10);
numbers.add(12);
numbers.add(14);

findMode(numbers);
}
}

QUESTION 1
b) Instead of integers as above this question works with first names of people as Strings.
Write a full java program that contains a method called findMostCommonName() that
takes a LinkedList of names as a parameter and returns the most common name in the
LinkedList as String

Answer:
class ModeOfNames {

//* Prints the most common name from the LinkedList of names.
@SuppressWarnings("FinallyDiscardsException")
public static void findMostCommonName(java.util.LinkedList <String> _names) {

java.util.LinkedList <String> modeNames= new java.util.LinkedList<>();

// represents the Maximum count...


int mCnt = 0;

for (int c = 0; c<_names.size(); c++) {


int counter = 0;

for(int i = 0; i<_names.size(); i++) {

if( (_names.get(i) != _names.get(c)) == false )


counter++;

if( (counter < mCnt) == false ) {


mCnt = counter;
modeNames.clear();
modeNames.add(_names.get(c));
}
else if( (counter != mCnt) == false ) {

modeNames.add(_names.get(c));
}
}
}
for(int c = 0; c<_names.size(); c++) {

try{
System.out.println("Most Common Name: " + modeNames.get(c));
}
finally{ return; }
}
}

public static void main(String[] args) {

java.util.LinkedList<String> names = new java.util.LinkedList<>();

names.add("Delta");
names.add("Echo");
names.add("Foxtrot");
names.add("Golf");
names.add("Juliet");
names.add("Kilo");
names.add("Kilo");

findMostCommonName(names);

}
}

QUESTION 2
a) Override a Method using the Super Keyword

ANSWER
/**
* Shape class as a base class.
*/
class Shape {
private String name;

/**
* Acts as a default constructor for the class.
* @param name
*/
public Shape(String name) {

this.name = name;
}

/**
* Gets the name of the class.
* @return
*/
public String getName() {

return this.name;
}
}

/**
* Rectangle class is a derived class.
*/
class Rectangle extends Shape {

/**
* Acts as a default constructor for the class.
* @param name Name of the class.
*/
public Rectangle(String name) {

super(name);
}

/** Overridding the getName() method.


* Gets the name of the class.
* @return Name.
*/
@Override
public String getName() {

return super.getName();
}
}
QUESTION 2
b) Implement an Account Class using Polymorphism.
Answer:
class Account {

protected double balance;

public void Withdraw(double amount) { }

public void Deposit(double amount) { }

public void printBalance() { }

public static void main(String[] args) {

Account SAccount = new Savings(50000);


SAccount.Deposit(1000);
SAccount.printBalance();

class Savings extends Account {

public Savings(double balance) {

balance = 50000;
this.balance = balance;
}

private double interestRate = 0.8;

@Override
public void Withdraw(double amount) {

double interestBalance = amount / this.interestRate;

// Deducting from the balance with interestRate.


double finalBalance = this.balance - (amount + interestBalance);

this.balance = finalBalance;
}

@Override
public void Deposit(double amount) {

double interestBalance = amount / this.interestRate;

// Adding in the balance with interestRate.


double finalBalance = this.balance + (amount + interestBalance);

this.balance = finalBalance;
}

public void printBalance() {

System.out.println("Balance is: " + this.balance);


}

class Current extends Account {

public Current(double balance) {


balance = 50000;
this.balance = balance;
}

@Override
public void Withdraw(double amount) {

// Deducting from the balance.


double finalBalance = this.balance - amount;

this.balance = finalBalance;
}

@Override
public void Deposit(double amount) {

// Adding in the balance.


double finalBalance = this.balance + amount;

this.balance = finalBalance;
}

@Override
public void printBalance() {
System.out.println("Balance is: " + this.balance);
}

QUESTION 3
a) Given the following declaration

List list = new LinkedList();

 Show using sample code how you can add String objects into the list.

Answer:
import java.util.LinkedList;
import java.util.List;

public class Program {

public static void main(String[] args) throws Exception {

try {

List<String> list = new LinkedList();

// The add method of the LinkedList add elements in the list..


list.add("String to be added here...");
list.add("Another String to be added here...");

}
catch(NullPointerException error) {

System.err.println("Failed to add elements into a List!");


}

}
QUESTION 3
b) When it comes to Threads, very little is guaranteed. Describe behaviors of Threads that
may be guaranteed and those that may not be guaranteed.

Answer:
- A thread is a single unit of execution that operates with other threads
concurrently while sharing resources.
1. A thread is expected executing concurrent operations with other
threads, whilst it is not expected operating in a sequential manner.
2. A thread is expected to share a resource while executing, whilst it is
not expected to interrupt resource allocation, leaving other threads in
the waiting state.
3. A thread is expected to surpass a deadlock, whilst it is not expected to
interrupt another thread accessing a resource.

QUESTION 3
c) Describe the behavior of garbage collection that can be guaranteed and also that
cannot be guaranteed.

Answer:
- Garbage collection is an automatic background program that performs
memory checkup management for every executing program in the Java
Run Time environment.
1. Garbage collection is expected to clean memory space for every
unused memory, whilst it is not expected to clean the memory space
that is still occupied by an object.
2. Garbage collection is expected be defer before every memory cleanup,
whilst it is not expected to randomly run a checkup, as this may cause
runtime errors.
3. Garbage collection is expected to be eligible whenever an object has
no references pointing to it, whilst it is not expected to perform
memory cleanup when there are objects with references.
d) Exceptions can either be caught or declared. Explain using an example how to declare
an exception.
Answer:
- Declaring an exception involve the use of three types of keywords:
1. throw – which throws an exception.
2. new – which invokes the instance of a class containing the exception.
3. Exception name/class – which specifies the type of exception.

import java.lang.*;

public class Program {

public static void main(String[] args) throws Exception


{

try {

System.out.println("Beginning execution...");

Thread.sleep(5000);

// Throwing the exception.


throw new InterruptedException("The terminating process has been
cancelled!");

//System.out.println("Terminating execution!");

}
finally { return; }

You might also like