Wednesday, November 23, 2016

Final Assignment

JAVA LAB FINAL ASSIGNMENTS


Note: Archive every assignment problem's class & java files individually (i.e. assigment1.zip contain UofKPayrol.class and UofKPayrol.java)

Submit to : sdlabuofk@gmail.com with your name and index as email's subject.

Deadline 26/11/2016 23:59:59

Assignment 1: [4 Marks]
University of Khartoum needs a program to calculate how much to pay their hourly employees. The Sudanese Department of Labor requires that employees get paid time and a half for any hours over 40 that they work in a single week. For example, if an employee works 45 hours, they get 5 hours of overtime, at 1.5 times their base pay. The Khartoum Locality requires that hourly employees be paid at least 8.00  SDG an hour. The University Human resources requires that an employee not work more than 60 hours in a week.

These rules can be summarized in:
- An employee gets paid (hours worked) × (base pay), for each hour up to 40 hours.
- For every hour over 40, they get overtime = (base pay) × 1.5.
- The base pay must not be less than the minimum wage (8.00 SDG an hour). If it is, print an error. If the number of hours is greater than 60, print an error message.

Create a new class called
UofKPayrol

Write a method that takes the base pay and hours worked as parameters, and prints the total pay or an error. Write a
main method that calls this method for each of these employees:
                Base Pay      Hours Worked
Employee 1 SDG 7.50                       35
Employee 2 SDG 8.20                       47
Employee 3 SDG 10.00                     73
  
Assignment 2: [4 Marks]

A group of UofK friends decide to run a Marathon . Their names and times (in minutes) are below:
Name    Time (minutes)
Ahmed  341
Ali           273
Lena      278
Suzie      329
Shadi     445
Omar     402
Yassin    388
Essra      275
John       243
Tariq      334
Yahia     412
Lelian    393
Alaa       299
Noor      343
Yassir     317
Katy       265

Write a method that takes as input an array of integers and returns the index corresponding to the person with the lowest time. Run this method on the array of times. Print out the name and time corresponding to the returned index.
Write a second method to find the second-best runner. The second method should use the first method to determine the best runner, and then loop through all values to find the second-best (second lowest) time.
Here is a program skeleton to get started:
class UOFKMarathon { public static void main (String[] arguments) {
        String[] names = {
“……”,””
};
int[] times = {
341, 273, …………….
}; for (int i = 0; i < names.length; i++) {     
……………………………………        System.out.println(names[i] +
": " + times[i]);
}
}
}
Assignment 3: [4 Marks]
Define a class Complex that can be used to represent complex numbers. The class should provide support to the following operations:
a. Addition, subtraction, multiplication, and division of two complex numbers.
b. Absolute value and angle (argument) of the complex number.
c. String representation of the complex number in the form r + im (r= real part, m = imaginary part).
Usage example:
 Complex num1 = new Complex(3, 4); // 3 + 4i
Complex num2 = new Complex(1, -2); // 1 -2i
Complex sum = Complex.add(num1, num2);
double absVal = sum.abs();
System.out.println(sum); // Displays 4 + 2i

Assignment 4: [4 Marks]
5.1. Write a program that takes a file name and a word from the user. It then searches the file for the occurrences of the word and then prints the number of times the word has appeared in the file. [2 Marks]
5.2. Add to the previous program the ability to replace each instance of the word with another word given by the user. [2 Marks]

Bonus:
Write a menu-driven program that manages employee’s records. The program should provide two basic functionalities:
1. Display current employee’s data.
2. Add a new employee record and store it on disk.
Usage example (Text in green represents user input):
Employees management software. Press 1 to display current employees, 2 to add a new employee, or 3 to exit: 1
Current employees:
Name              Salary              Age
Abbas              1200                22
Ali                    2200                21
Employees management software. Press 1 to display current employees, 2 to add a new employee, or 3 to exit: 2
Adding a new employee
Name: Amina
Salary: 2300
Age: 24
Employee Amina has been added.

Employees management software. Press 1 to display current employees, 2 to add a new employee, or 3 to exit: 3
Bye.

Saturday, October 8, 2016

Homework #1 - Java Basics


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
    }

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;
    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