Alek Modi

Alek Modi
Follow You Heart!

Sunday, July 22, 2012

Tic - Tac - Toe Source Code in JAVA


import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class tictac {

static int turn = 0;
static int board[][] = {{0,0,0},{0,0,0},{0,0,0}};
static ImageIcon blank = new ImageIcon("res/blank.png");
static JButton button[][] = new JButton[3][3];
static ImageIcon opic = new ImageIcon("res/o.png");
static ImageIcon xpic = new ImageIcon("res/x.png");

public static void main(String[] args) {
// Alek Modi
// 3/22/11
// Tic Tac Toe board
// Import custom images
//USE YOUR OWN IMAGES HERE if needed.
ImageIcon turtle = new ImageIcon("res/turtle.png");
ImageIcon jiraffe = new ImageIcon("res/jiraffe.png");
ImageIcon elephant = new ImageIcon("res/elephant.png");
ImageIcon frog = new ImageIcon("res/frog.png");
ImageIcon hippo = new ImageIcon("res/hippo.png");
ImageIcon owl = new ImageIcon("res/owl.png");
ImageIcon penguin = new ImageIcon("res/penguin.png");
ImageIcon seal = new ImageIcon("res/seal.png");

String promt1 = "Choose your icon:\n" + "1 - Turtle\n" + "2 - Giraffe\n" + "3 - Elephant\n" + "4 - Frog";
String promt2 = "Choose your icon:\n" + "1 - Hippo\n" + "2 - Owl\n" + "3 - Penguin\n" + "4 - Seal";

// Enter Player's names and choose pictures
final String player1 = JOptionPane.showInputDialog("Enter Player 1's name:");
if (player1==null || player1.equals(""))
System.exit(0);
String entry1 = JOptionPane.showInputDialog(player1 + "\n" + promt1);
if (entry1.equalsIgnoreCase("1") | entry1.equalsIgnoreCase("turtle"))
opic = turtle;
else if (entry1.equalsIgnoreCase("2") | entry1.equalsIgnoreCase("giraffe"))
opic = jiraffe;
else if (entry1.equalsIgnoreCase("3") | entry1.equalsIgnoreCase("elephant"))
opic = elephant;
else if (entry1.equalsIgnoreCase("4") | entry1.equalsIgnoreCase("frog"))
opic = frog;
else
JOptionPane.showMessageDialog(null," Incorrect pick.\n" + "You'll be using O's");

final String player2 = JOptionPane.showInputDialog("Enter Player 2's name:");
if (player2==null || player2.equals(""))
System.exit(0);
String entry2 = JOptionPane.showInputDialog(player2 + "\n" + promt2);
if (entry2.equalsIgnoreCase("1") | entry2.equalsIgnoreCase("hippo"))
xpic = hippo;
else if (entry2.equalsIgnoreCase("2") | entry2.equalsIgnoreCase("owl"))
xpic = owl;
else if (entry2.equalsIgnoreCase("3") | entry2.equalsIgnoreCase("penguin"))
xpic = penguin;
else if (entry2.equalsIgnoreCase("4") | entry2.equalsIgnoreCase("seal"))
xpic = seal;
else
JOptionPane.showMessageDialog(null," Incorrect pick.\n" + "You'll be using X's");

// Top image
BufferedImage bi = null;
try {
File imagefile = new File("res/name.png");
bi = ImageIO.read(imagefile);
}
catch (IOException e){
System.out.println("cannot open file");
System.exit(0);
}

// Main frame construction
JFrame myframe = new JFrame ("Tic Tac Toe");

// Set grid layout for board with buttons and images
//ImageIcon blank = new ImageIcon("./blank.png");

Container box = myframe.getContentPane();
box.setLayout(new GridLayout(3,3));

for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
button[i][j] = new JButton(blank);
box.add(button[i][j]);
button[i][j].setBackground(Color.white);
button[i][j].addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JButton jb = (JButton)arg0.getSource();
int row = -1;
int col = -1;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (jb == button[i][j]) {
row = i;
col = j;
}
}
}
if (board[row][col] == 0) {

// Player 1
if (turn % 2 == 0){
jb.setIcon(opic);
turn++;
board[row][col]=1;

//Message announcing winner in row
if (board[0][0] + board[0][1] + board[0][2] == 3){
button[0][0].setBackground(Color.red);
button[0][1].setBackground(Color.red);
button[0][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player1 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[1][0] + board[1][1] + board[1][2] == 3){
button[1][0].setBackground(Color.red);
button[1][1].setBackground(Color.red);
button[1][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player1 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[2][0] + board[2][1] + board[2][2] == 3){
button[2][0].setBackground(Color.red);
button[2][1].setBackground(Color.red);
button[2][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player1 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

//Message announcing winner in column
if (board[0][0] + board[1][0] + board[2][0] == 3){
button[0][0].setBackground(Color.red);
button[1][0].setBackground(Color.red);
button[2][0].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player1 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[0][1] + board[1][1] + board[2][1] == 3){
button[0][1].setBackground(Color.red);
button[1][1].setBackground(Color.red);
button[2][1].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player1 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[0][2] + board[1][2] + board[2][2] == 3){
button[0][2].setBackground(Color.red);
button[1][2].setBackground(Color.red);
button[2][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player1 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

//Message announcing winner in verticals
if (board[0][0] + board[1][1] + board[2][2] == 3){
button[0][0].setBackground(Color.red);
button[1][1].setBackground(Color.red);
button[2][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player1 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[0][2] + board[1][1] + board[2][0] == 3){
button[0][2].setBackground(Color.red);
button[1][1].setBackground(Color.red);
button[2][0].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player1 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}
}

// Player 2
else{
jb.setIcon(xpic);
turn++;
board[row][col]=-1;

//Message announcing winner in row
if (board[0][0] + board[0][1] + board[0][2] == -3){
button[0][0].setBackground(Color.red);
button[0][1].setBackground(Color.red);
button[0][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player2 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[1][0] + board[1][1] + board[1][2] == -3){
button[1][0].setBackground(Color.red);
button[1][1].setBackground(Color.red);
button[1][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player2 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[2][0] + board[2][1] + board[2][2] == -3){
button[2][0].setBackground(Color.red);
button[2][1].setBackground(Color.red);
button[2][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player2 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

//Message announcing winner in column
if (board[0][0] + board[1][0] + board[2][0] == -3){
button[0][0].setBackground(Color.red);
button[1][0].setBackground(Color.red);
button[2][0].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player2 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[0][1] + board[1][1] + board[2][1] == -3){
button[0][1].setBackground(Color.red);
button[1][1].setBackground(Color.red);
button[2][1].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player2 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[0][2] + board[1][2] + board[2][2] == -3){
button[0][2].setBackground(Color.red);
button[1][2].setBackground(Color.red);
button[2][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player2 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

//Message announcing winner in verticals
if (board[0][0] + board[1][1] + board[2][2] == -3){
button[0][0].setBackground(Color.red);
button[1][1].setBackground(Color.red);
button[2][2].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player2 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}

if (board[0][2] + board[1][1] + board[2][0] == -3){
button[0][2].setBackground(Color.red);
button[1][1].setBackground(Color.red);
button[2][0].setBackground(Color.red);
JOptionPane.showMessageDialog(null,player2 + " Wins!");
for (int a = 0; a < 3; ++a) {
for (int j = 0; j < 3; ++j) {
button[a][j].setIcon(blank);
button[a][j].setBackground(Color.white);
turn=0;
board[a][j]=0;}}}
}

// In the event of a tie
if (turn == 9){
JOptionPane.showMessageDialog(null,"Tied!");
for (int s = 0; s < 3; ++s) {
for (int r = 0; r < 3; ++r) {
button[s][r].setIcon(blank);
button[s][r].setBackground(Color.white);
turn=0;
board[s][r]=0;}}}
}}});

// Create frame
myframe.setVisible(true);
myframe.setSize(510, 510);
myframe.setIconImage(bi);
myframe.setLocationRelativeTo(null);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}}}

Thursday, June 7, 2012

Operating System Process Scheduling Simulation Project


SOURCE CODE FOR PROCESS SCHEDULING SIMULATOR    


 Download source code


Process/Job Characteristics
1. Two ways to input jobs file. 
a. Import Files
b. User Input

2. Scheduling can be done in two techniques 
a. SJF
Imported Data
Processed Data (Sorted in ascending order)
b. FCFS
Imported Data
Processed Data (First Come First Served basis)
C. Round Robin Scheduling
Process ID : 1 - 10 (unique)
Process Arrival Time : Jobs should arrive to the job queue one after another with intervals of 1 to 10 time units (unique)
Process Type : System
Length of Each CPU Burst : 1-12

CPU Scheduling
· Round Robin (Quantum 4)
Simulation Model Parameters

Simulate the Execution of the processes . The simulation program should be able to compute for the average turnaround time, waiting time, job throughput in order to evaluate the performance of the operating system.

Display/ Output 
· Average turnaround time
· Waiting time
· Job throughput
· CPU Gantt chart


Object Oriented Software Development Concepts:


1. Class:
A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of an object. A class is the blueprint from which the individual objects are created. Basically, OOPs encapsulates names, attributes (data), and operations (behavior) into packages called Classes, where data and member functions of the class are immediately tied together.
Syntax of a class in C++;



2. Object:
In pure OOP terms an object is an instance of a class. Generally, an object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs can be called as object's behavior. For instance from example 1.1,
Let’s create one object, person student = new person(“Sam”,21)
So here, student (object) can hold all the characteristic of class person.


3. Encapsulation:
The encapsulation is the inclusion within a program object of all the resources need for the object to function - basically, the methods and the data. In OOP the encapsulation is mainly achieved by creating classes, the classes expose public methods and properties. The class is kind of a container or capsule or a cell, which encapsulate the set of methods, attribute and properties to provide its indented functionalities to other classes. In example 1.1, class person {...} has encapsulated variables, name & age and methods, print(); and person(); this is encapsulation.

4. Message passing & 5. Message receiver
A method implements behavior, which is defined by most of the OOP authors as: “Behavior is how an object acts and reacts, in terms of its state changes and message passing.” Means, a program consists of a network of interconnected objects that call upon each other to solve a part of the puzzle. Each object has a specific role to play in the overall design of the program and is able to communicate with other objects. Objects communicate through passing messages, requests to execute a code.


For example 1.2,
Student.person(“Matt”,26);
Student person : “matt” param1: 26 param2
Student.print();
This object ‘student’ is called the receiver, which is the object the method operates on. An exception exists with C++'s static member functions which do not have a receiver, or "this" pointer. The example 1.2 is some common notations for invoking a method, and this invocation can be called a message (or message passing).


6. Instantiation:
Instantiation is one of the most important mechanisms for code reuse in object-oriented programming languages. Instantiation is the use of object classes. (An object class is a set of objects that share a common structure and a common behavior). Instantiation is fundamentally a word for the use of inheritance. Programmers can define object classes, define their own kinds of objects, and instantiate them as needed. This is one of the most important mechanisms for code reuse in object-oriented programming languages.
To create an instance of the nested class, use the name of the container class followed by the dot and then followed by the name of the nested class:
Container Instance = new Container()
After instantiating a class, now you can assign values to the instance's properties and fields and invoke class methods.


7. Polymorphism:
Poly – morphism is a generic term that means 'many shapes'. More precisely Polymorphism means the ability to request that the same operations be performed by a wide range of different types of things. In OOP the polymorphism is achieved by using many different techniques named method overloading, operator overloading and method overriding.
For example, let’s say there is a main class vehicle with a method called wheels(); and there are two subclasses, class car – 4 wheels and class bike – 2 wheels. Here one method was used by two different classes


8. Overloading
a. Method Overloading:
The method overloading is the ability to define several methods all with the same name. Method overloading should not be confused with forms of polymorphism where the correct method is chosen at runtime.
 


b. Operator Overloading:
The operator overloading is a specific case of polymorphisms in which some or all of operators like +, - or == are treated as polymorphic functions and as such have different behaviors depending on the types of its arguments. For example ‘+’ operator can be used for adding integer or for concatenating two strings or for incrementing values.


9. Shadowing:
A member can shadow another member in a parent class if it has the same name and parameters, if any. In that case, the new member supersedes the old version. This is very similar to the way a class can override a parent class member, but shadowing changes the way polymorphism works.



10. Member:
A class can have both data members and functions members associated with it. Unlike the built-in types, the class can encapsulate several variables and functions and those are called members.
In example 1.2, variables and methods - name, age, print (), person () are the members of the class person.


11. Instance:
If you look at the word instantiation you can see that it is derived from the word instance. An instance of a class is an object that is described by that class. Instantiation of a class is the creation of a new instance of that class.
When a new instance of an object class is created, it has its own set of instance variables, and it shares the method implementations with other instances of its class.


12. Inheritance:
Ability of a new class to be created, from an existing class by extending it, is called inheritance.

According to the above example the new classes (Bike and car), which are called the derived classes or subclasses, inherits the members of an existing class (Vehicle), which is called the base class or super-class. The class Bike and Car can extend the functionality of the class Vehicle by adding new types and methods and by overriding existing ones.


13. Composition:
Composition is about expressing relationships between objects. Let’s say about the vehicle example in example 1.3. A vehicle has a gas tank. A vehicle has a pair of wheels. A vehicle has an engine. The phrase "has a" implies a relationship where the vehicle owns, or at minimum, uses, another object. It is this "has a" relationship which is the basis for composition.


14. Data hiding:
The objects of a class have members which can be integers, characters, arrays, functions or even structures. Now these members can be either Private, Public or Protected.
Public allows you to use all the members from outside the class.
Private allows you to access all the members from inside the class only.
Protected allows only the friend functions to access the members.
Data hiding is an important characteristic of OOP for securing data, so an object can only be associated with data in predefined classes or templates, the object can only "know" about the data it needs to know about.
In example 1.1, any object of the class person cannot access the private variables – name and age, in the class. This restriction is called data hiding.


15. Principle of substitution:
In class hierarchies, it should be possible to treat a specialized object as if it were a base class object.
The basic idea of substitution principle is very simple. All code operating with a pointer or reference to the base class should be completely transparent to the type of the inherited object. It should be possible to substitute an object of one type with another within the same class hierarchy. Inheriting classes should not perform any actions that will invalidate the assumptions made by the base class.


16. Attribute:
Attributes are the properties that specify the information associated with a component, such as its name, description, and color. Attributes determine how a component will look and behave. For example, the Push Button Control has an attribute named label that specifies the text displayed on the button. You can create two instances of the Push Button Control on your frame and have one display "OK" and the other display "Cancel," simply by specifying a different value for the label attribute of each instance.
You can define attributes with attribute statements in CLASS blocks:
scope data-type attribute-name/(attribute-options);
Attribute names can be up to 256 characters long.


17. Behavior:
Here, I assume, behavior means the scope of the data members. If a variable is declared globally then it will be access by any method in the whole program. The lifespan of the global variable is till the program is terminated. On other hand there are local variables which declared inside the method or a class. Thus the behavior of that local variable is used only inside the class. Thus the behavior of any data/method depends by the way it is declared in the program.


18. Interface:
The Interface separates the implementation and defines the structure, and this concept is very useful in cases where you need the implementation to be interchangeable. Apart from that an interface is very useful when the implementation changes frequently.
Interface can be used to define a generic template and then one or more abstract classes to define partial implementations of the interface.
public interface ILogger{
bool IsThisLogError { get; }
}


19. Constructor:
It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created. It is called a constructor because it constructs the values of data members of the class.
In example 1.1 (see above), the function -> Person(sting str, int n){ … } is a construction which is used to initialize the private member variables – name and age in the class.



20. Abstraction:
Abstraction is the process by which data and programs are defined with a representation similar in form to its meaning, while hiding away the implementation details. Abstraction captures only those details about an object that are relevant to the current perspective.
Abstraction can apply to control or to data: Control abstraction is the abstraction of actions while data abstraction is that of data structures.
 Control abstraction involves the use of subprograms and related concepts control flows
 Data abstraction allows handling data bits in meaningful ways. For example, it is the basic motivation behind datatype.
Abstraction is essential in the construction of programs. It places the emphasis on what an object is or does rather than how it is represented or how it works. Thus, it is the primary means of managing complexity in large programs.