Homework #1
Note : Submit Emails to : sdlabuofk@gmail.com (The Email in the printed Lab Sheet was wrong ! sorry)
Deadline is : 15 OCT 2016 11:59 PM
Part #1 Coding Exercises (7 Marks)
1. (3 Marks) Write a program that reads in a series of first names and
eliminates duplicates by storing them in a Set.
Allow the user to search for a first name by implementing a proper function for
that
2.
(4 Marks) (This
Exercise is almost solved with below codes given) The code below Defines an integer stack interface:
interface IntStack
{
void push(int item); // store an
item
int pop(); // retrieve an item
int pop(); // retrieve an item
}
Create a class called
FixedStack that implements IntStack above , FixedStack class has two variables
( tos (index to the current top of stack from type int , and integer array stck
as in below ) , Complete the implementation of this class so it can represent a stack with a fixed size
class FixedStack
implements IntStack
{
private int stck[];
private int tos;
private int tos;
FixedStack(int size)
{
/*Add your code here to
implement the constructor that initialize both fixed stck array and tos */
}
public void push(int item)
{
/* Add your code here to implement Push Function that adds an
integer element to the stack (Take in
consideration when user trying to add to a full stack */
}
public int pop()
{
/* Add your code here to implement Pop Function that pops item
from stack
(Take in consideration the this is a fixed size stack */
}
Then
test your class with below code in MAIN as below:
public
static void main(String args[])
{
FixedStack
mystack1 = new FixedStack(5);
FixedStack
mystack2 = new FixedStack(8); // push some numbers onto the stack for(int i=0; i<5; i++) mystack1.push(i); for(int i=0; i<8; i++) mystack2.push(i); // pop those numbers off the stack System.out.println("Stack in
mystack1:") for(int i=0; i<5; i++)
System.out.println(mystack1.pop());
1
System.out.println("Stack in
mystack2:") for(int i=0; i<8; i++)
System.out.println(mystack2.pop());
}
Part #2 Short Questions (3 Marks)
• What
is wrong with the following interface?
public interface
SomethingWrongWithThisInterface {
void aMethod() {
System.out.println("Hi,You’ve Just called aMethod");
}
}
• Fix
the interface above?
• Is
the following interface valid?
public interface Student {
}
Part #3 BONUS “OPTIONAL” (5 Marks)
One of most known and simple design
patterns in industry is the singleton pattern. This design pattern restricts
the instantiation of a class to one object , so it’s useful when exactly one
object is needed to coordinate actions across the system. Knowing the concept
of “static” keyword in java show how can you implement a single class which is
responsible to create an object while making sure that only single object gets
created. This class provides a way to access its only object which can be
accessed directly without need to instantiate the object of the class. (Write
Code Example)
In
what Applications do you think that this design pattern suits, why?
Submission
Guidelines:
• Subject of Email Should be :
Java_HW(No)_(Index) E.g. Java_HW1_104048
• Submit Java Files .java for codes and
document (.doc,.docx,.pdf) for short questions (As we’re using automatic
filters for downloading attachments then answers in email body will not be
received so make sure that all your answers are in attachment)
•
Bonus
must be submitted in Separate file (either .Java or document file )
2