Professional Documents
Culture Documents
ASSIGNMENT COVER
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.
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 {
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; }
}
}
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) {
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; }
}
}
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);
}
return super.getName();
}
}
QUESTION 2
b) Implement an Account Class using Polymorphism.
Answer:
class Account {
balance = 50000;
this.balance = balance;
}
@Override
public void Withdraw(double amount) {
this.balance = finalBalance;
}
@Override
public void Deposit(double amount) {
this.balance = finalBalance;
}
@Override
public void Withdraw(double amount) {
this.balance = finalBalance;
}
@Override
public void Deposit(double amount) {
this.balance = finalBalance;
}
@Override
public void printBalance() {
System.out.println("Balance is: " + this.balance);
}
QUESTION 3
a) Given the following declaration
Show using sample code how you can add String objects into the list.
Answer:
import java.util.LinkedList;
import java.util.List;
try {
}
catch(NullPointerException error) {
}
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.*;
try {
System.out.println("Beginning execution...");
Thread.sleep(5000);
//System.out.println("Terminating execution!");
}
finally { return; }