Friday, 26 February 2016


java.net lesson 3 : 

creating a server and three clients and make the server receive a message and echo it back to all the clients connected to the server in the chat room application. 


In the MultithreadedChatClient.java file we create three clients and connect them to the server in the file MultithreadedChatServer.java to make a simple chat application. The three clients have separate GUIs of their own and each client can send message to the server but the server echoes back the meassage of any client to all the clients. Therefore, the message sent by any client is seen by all th eclients in the chatroom i.e,. a JTextArea. 

To see the code please click here.

Thursday, 25 February 2016

Corrupting data by not locking the object- Multithreading in Java Lesson 1

Corrupting data by not locking the object- Multithreading in Java


This program shows how the data is get corrupted when two threads share a single object. We have one object account of class BankAccount1 which is shared by two threads threadNamedJack and threadNamedJill. Both of these threads try to modify the object account and consequenctly corrupts the data. In the utility method makeWithdrawal(int amountToWithdraw) we have a condition

 if (account.getBankBalance() >= amountToWithdraw) 

to make sure that the account is not overdrawn but if we run the program and see the results we see that the account has been overdrawn. The solution is to lock the object when one of the threads is using it and relase the lock after this thread has finished its job and make it available for other threads. We do not use locks in this program but we will use it in the next version of this program.

To see the source code please click here.

Wednesday, 24 February 2016

java.net lesson 2 : creating a server and two clients and make the server receive a message one client only


Creating a Server and Two clients using java.net package


Here we write to code to highlight the problem in the code of lesson 1. The problem in that code was that the server sits in a loop ( Socket socket = serverSocket.accept(); ) and can serve one client at a time. It can not make a connection with another client untill it finishes the client in hand. When the current iteration of the infinte loop is over then the server can accept a request from a new client and can serve that client by making a socket and repeating the whole process again for this client. 

In this lesson we have a server that gets messages from two clients but displays the messages of the first client only on its console although we can see on the console of the clients that both the clients have successfully made a socket (connection) with the server and that botht are displaying the messages that they are sending to the server each time the user types a message in the two GUIs and click at the send button. In the next lesson we use multithreading to solve this problem.

The code given below displays how to send messages to a Server using Swing GUIs and how to make clients make connection to the server and send messages to them but not receive any messages from the Server. 


For the source code click here.


java.net lesson 1: Creating a server and a client and make the client receive a message from the server


  Creating a server and a client and make the client receive a message from the server 


 In this program we make a server on the same machine as the one on which the client is running but you can make the client run on another machine also. Then we make a client and send a request to the server on port 5555 which is accepted by the server. The server is running in a loop waiting for connection requests from the clients.

 When the server gets a request from the client, the client is connected on some anonymous port and a message is sent to the client which is displayed on the console also in this case. Then the server continues it wait for the other clients.

 Here we make seven client instances and all of them repeat the process given above and get a message from the server which is displayed on the console.

To see the code of the program please click

here.

Friday, 19 February 2016

Music Beat box to make/play/save patterns version 1 - ObjectOuputStrem, ObjectInputStream, FileOutputStream, FileInputStream along with JFileChooser and use of Inner classes.


ObjectOuputStrem, ObjectInputStream, FileOutputStream, FileInputStream along with JFileChooser and use of Inner classes.


Making a music beat box where you can make beats using 16 different instruments. You can save the created patteren to disk and load it again to play it in the music box. ObjectOuputStrem, ObjectInputStream, FileOutputStream, FileInputStream along with JFileChooser has been used to save and load the music pattern to and from the heard desk. You also learn the use of inner classes as event handles as well as making GUIs (Graphical User Interfaces) in Swing.

To see the source code please click here.

Objects serialization and deserialization in java with java I/O API and Swing example to make flash card maker and displayer

Objects serialization and deserialization, Java I/O API and Swing


In the program below we show how to serialize objects and save them as text file to disk. Then we get the file and load it back in to memory using java I/O API and deserialize the objects. 

We use Swing to make the GUI portion of the program to make a flash card maker and displayer. The user can make flash cards with questions and answers on flash cards and save a deck of flash cards as a text file to the hard disk by serializing the FlashCard object. Then the user can load a text file previously saved to the disk and deserialize it back in to FlashCard objects in order to use the flash cards.

To see the code please click here.

Wednesday, 17 February 2016

Object serialization and deserilaization. Saving file to desk. File input output.

Object Serialization and Deserialization


 Serialization helps save the state of the objects. To be serialized the class of that object must implement the Serializable interface. If a class implements the interface Serialization then objects of  all of its subclasses are serializable. When an object is serialized, its entire object graph is serialized, i.e., if the object has references to other objects then they are also serialized. 

If you don't want to serialize some of the instance variables then make them "transient". Then that variable will be skipped during the process of serialization and during the deserialization that object will get null as value if it is a reference object or default value if it is of type primitive. 

Deserialized objects are read in the same order in which they were serialized. Since the type of the object that is deserialized is of class Object therefore it must be casted to their real type.  

Static variables are not deserialized as all instances of a class share a static variable. A static variable is per class and not per instance. So it doesn't make sense to save a static variable value as part of a specific objects state since all objects of that type share only a single value - the one in the class.


Example program:


 In this program we write a class that makes game characters. We make three instances/objects and save them to desk after serialization. Then we set those three references to null so that we can not access them on the heap. Next, we have code for deserializatin of those characters to inflate them again and give them life on the heap. We print the values of the instance variables of these three objects both before serialization and after deserialization to show that it works.

To see the code please click

here

Thursday, 11 February 2016

Event handling with inner classes and Swing components Example 1

We can use inner classes for event handling. Here is this program we use Swing components for the GUI (Graphical User Interface) of the program and we use inner classes for event handling when the user clicks at the buttons given. We also use JLabel, Label and MIDI class along with a few others to make a music beat box. Using the buttons the user can increase or decrease the tempo. Using the checkboxes (JCheckBox) the user can select beats and instruments.

To see the code please click

here.

Monday, 8 February 2016

Inner classes example 3 - Displaying music notes as squares on a GUI to demonstrate event handling, and use of inner classes.

Inner classes - Displaying music notes as squares on a GUI to demonstrate event handling, and use of inner classes.

To see the code please click

here

Saturday, 6 February 2016

Making animation using inner classes

Making animation using inner classes

To see the code please click

here.

Demonstrating the use of inner classes and how to make a relationship between an object of outer class with an object of inner class.

Demonstrating the use of inner classes and how to make a relationship between an object of outer class with an object of inner class.

To see the code please click

here