Operating Systems
Lab 1: Java Skills


Purpose of Assignment

This assignment is to make you become familiar with the subset of Java you will need for this course, if you do not have it yet. Anyone who cannot finish this assignment on time does not have the necessary programming skills to complete this course successfully, and may want to consider dropping the course (or at least come by my office to talk about it). 

 


The assignment(s) : note that you can choose between two assignments.

Assignment1 (below) or Assignment2


In this assignment (Assignment1), you will analyse a textual input file determining the frequency and location of words in the file.The information of words is stored in an output file, which lists the distinct words (in alphabetical order is optional) and makes reference to each line on which the word is used. The main Java classes you need to use for this assignment can be found in java.io.*, java.util.*, and java.lang.* packages. The easiest perhaps is to use a hash-table to store the records. Note, however, that if alphabetical order is implemented (it is optional) then it is better to use a map class that guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class. If the key is chosen to be the word itself in such a map, then it is very easy to generate an output in alphabetical order (as this is also the order in which the records are stored in the map).


Input file (create a text file with this content):


Peter Piper picked a peck of pickled peppers. A peck of pickled
peppers Peter Piper picked. If Peter Piper picked a peck of pickled peppers,
where is the peck that Peter Piper picked?

Action: Define a record that consister of a word, its frequency count, and a list that hold the line numbers for each occurrence. In a line, extract each word of text. For the first occurrence of a word in the input file, create a record and insert it in
a hash-table or a tree-map. if the word is already in the hash-table or tree-map, update the frequency and line number list.

Output: After reading the input file, print an alphabetized list of words, the frequency count, and the ordered list of lines on which the word occurred.

Sample output on the console: The output should look like this (this if alphabetical order is implemented):

a..........................3: 1 2
if..........................1: 2
is..........................1: 3
of.........................3: 1 2
peck....................4: 1 2 3
peppers...............3: 1 2
peter...................4: 1 2 3
picked.................4: 1 2 3
pickled................3: 1 2
piper...................4: 1 2 3
that.....................1: 3
the......................1: 3
where..................1: 3

 


Assignment2

Implement a simple elevator simulation in Java.

1.Possible Design

Mechanism:
All passengers board on the first floor. The elevator moves up and down (even if empty),
a couple of times reverts and announces its direction when it reaches top/bottom floor.
If the elevator is full no more passengers can board, "full" message is sent to output.
At the requested floors it will stop and passengers get off. Elevators state is updated
after every stop.

Variables:
The number of floors, top floor, ground floor, maximum number of passengers,
moves between floors should be set to constants.
One integer array can be used to keep track of the number of passengers going
to the different floors. A boolean array can be used to keep track of the different
floors where the elevator has to stop.

Methods:
A constructor method to initialize the variables; a move method to simulate the elevator
going up and down; a board method to board passengers one by one to their destination floor,
and check everytime if the elevator is at capacity; a stop method to process the stopping
process and update the elevators state, such as number of passangers onboard, current floor;
a toString method to summarize the elevators state; a main method with calls to the board method
to board passengers.

2.Classes created
Elevator


A skeleton code (some of the implementation is missing) it is provided below for the design described above,
however, it is not necessary to follow it exactly:


/**
* Class Elevator implements an elevator simulation program.
* It simulates the movements of one elevator operating in
* an 8 storey building with a maximum capacity of 10 people.
* It has a move mechanism that makes it move in a certain
* direction, and stop on certain floors. When it reaches top
* or bottom floor it will change its direction. A board
* mechanism keeps track of the boarding passengers, their
* destination floor, the number of passengers to a particular
* floor, and the total passengers in the elevator. A stop
* mechanism keeps track of the passengers getting of at a
* certain floor and updates the state of the elevator.
*
*
*/

public class Elevator {

//declaration of constants
private final static int DIR_UP = 1;
private final static int DIR_DOWN = -1;
private final static int DIR_IDLE = 0;
private final static int GROUND_FLOOR = 1;
private final static int MAX_FLOORS = 8;
private final static int MAX_PASSENGERS = 10;
private final static int MOVES_BETWEEN_FLOORS = 4;

//total number of passenger in elevator
private int numPassengers;

private int currentDirection;
private int currentFloor;

//counter that keeps track of moves between two floors
private int moveCounter;

//array indicates whether elevator should stop on a certain floor
private boolean[] stopAtFloor;

//number of passengers with destination to a certain floor
private int[] passToFloor;

/**
* Class constructor.
* Initializes the variables.
*/

public Elevator(){

// fill in code

// number of passengers is 0
// direction is UP
//ground floor is floor 1
//assigns values to the boolean array elements
//assigns values to the integer array elements

}

 

/**
* Method that registers the boarding passangers and
* their destination, updates the number of passengers
* to that floor and the number of passengers in the
* elevator. Checks if number of passengers exceed MAX_PASSENGERS
* @param floor the destination of the boarding passenger
* @return <code>true</code> if elevator is not at capacity;
* <code>false</code> otherwise.
*/

public boolean boardPassenger(int floor){
// fill in code that is missing
if (numPassengers < MAX_PASSENGERS){
//increments number of passengers going to the same floor
//signals to stop at destination floor
//increments total number of passengers in elevator
}

 

/**
* This main method executes the Elevator simulation.
* Instantiates an instance of class Elevator, and
* triggers several passenger-boardings to different
* floors.
* @param args Command line argument array
*/

public static void main(String args[]){

Elevator myElevator = new Elevator();

// fill in this method

//board passengers one by one and check full capacity
//update of elevator's initial state
//make it go up and down a couple of times
}
//update of elevator's final state
}

/**
* Method that puts the elevator into action,
* move up or down or remain idle. It also
* makes elevator reverse direction when reached
* top/ground floor.
*/

public void move(){
// fill in code

}

/**
* Simulates the stopping of the elevator.
* Updates the number of passengers left in the
* elevator, and it announces its stop and updates
* its state. Full implementation is given below.
* @param f the floor where elevator stopped
*/

public void stop(int f){

numPassengers = numPassengers - passToFloor[f];

//announce its stopping
System.out.println("Now stopping on floor " + f + ".");

//announce its post-stop-state
System.out.println(this);
System.out.println();
}

 

public String toString(){
//summary of elevator's state
return "Elevator currently on floor " + currentFloor + ".\n" +
"Currently " + numPassengers + " passengers on board";
}
}







Output of your simulation should be something like this:

Elevator currently on floor 1.
Currently 3 passengers on board

Now stopping on floor 2.
Elevator currently on floor 2.
Currently 1 passengers on board

Now stopping on floor 3.
Elevator currently on floor 3.
Currently 0 passengers on board

Elevator going down!

Elevator going up!

Elevator going down!

Elevator going up!

Elevator currently on floor 1.
Currently 0 passengers on board

Process Elevator finished


 


 

Csaba Andras Moritz