[Link]. University Programmes Sign.
1 Create a java program to implement stack and
queue concept.
2 Write a java package to show dynamic polymorphism
and interfaces.
3 Write a java program to show multithreaded producer
and consumer application.
4 Write a java program to show multithreaded producer
and consumer application.
5 Convert the content of a given file into the
uppercase content of the same file.
6 Develop an analog clock using applet.
7 Develop a scientific calculator using swings.
8 Create an editor like MS-word using swings.
[Link] a java program to implement stack and queue concept.
Programme to implement Stack:
public class MyStack {
private int maxSize;
private long[] stackArray;
private int top;
public MyStack(int s) {
maxSize = s;
stackArray = new long[maxSize];
top = -1;
}
public void push(long j) {
stackArray[++top] = j;
}
public long pop() {
return stackArray[top--];
}
public long peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
public static void main(String[] args) {
MyStack theStack = new MyStack(10);
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](50);
while (![Link]()) {
long value = [Link]();
[Link](value);
[Link](" ");
}
[Link]("");
}
}
Programme to implement Queue:
import [Link].*;
class Queue
{
private int arr[]; // array to store queue elements
private int front; // front points to front element in the
queue private int rear; // rear points to last element in the
queue private int capacity; // maximum capacity of the queue
private int count; // current size of the queue
Queue(int size)
{
arr = new int[size];
capacity = size;
front = 0;
rear = -1;
count = 0;
}
// Utility function to remove front element from the queue
public void dequeue()
{
// check for queue underflow
if (isEmpty())
{
[Link]("UnderFlow\nProgram Terminated");
[Link](1);
}
[Link]("Removing " + arr[front]);
front = (front + 1) % capacity;
count--;
}
// Utility function to add an item to the queue
public void enqueue(int item)
{
// check for queue overflow
if (isFull())
{
[Link]("OverFlow\nProgram Terminated");
[Link](1);
}
[Link]("Inserting " + item);
rear = (rear + 1) % capacity;
arr[rear] = item;
count++;
}
// Utility function to return front element in the queue
public int peek()
{
if (isEmpty())
{
[Link]("UnderFlow\nProgram Terminated");
[Link](1);
}
return arr[front];
}
// Utility function to return the size of the queue
public int size()
{
return count;
}
// Utility function to check if the queue is empty or not
public Boolean isEmpty()
{
return (size() == 0);
}
// Utility function to check if the queue is empty or not
public Boolean isFull()
{
return (size() == capacity);
}
// Queue implementation in java
public static void main (String[] args)
{
// create a queue of capacity 5
Queue q = new Queue(5);
[Link](1);
[Link](2);
[Link](3);
[Link]("Front element is: " + [Link]());
[Link]();
[Link]("Front element is: " + [Link]());
[Link]("Queue size is " + [Link]());
[Link]();
[Link]();
if ([Link]())
[Link]("Queue Is Empty");
else
[Link]("Queue Is Not Empty");
}
}
2. Write a java package to show dynamic polymorphism and interfaces.
class Animal{
void eat(){[Link]("eating...");}
}
class Dog extends Animal{
void eat(){[Link]("eating bread...");}
}
class Cat extends Animal{
void eat(){[Link]("eating rat...");}
}
class Lion extends Animal{
void eat(){[Link]("eating meat...");}
}
class TestPolymorphism3{
public static void main(String[] args){
Animal a;
a=new Dog();
[Link]();
a=new Cat();
[Link]();
a=new Lion();
[Link]();
}
}
Java program to demonstrate working of Interface
import [Link].*;
interface in1
{
// public, static and final
final int a = 10;
void display();
}
class testClass implements in1
{
// Implementing the capabilities of
// interface.
public void display()
{
[Link]("Geek");
}
// Driver Code
public static void main (String[] args)
{
testClass t = new testClass();
[Link]();
[Link](a);
}
}
3. Write a java program to show multithreaded producer and consumer application.
import [Link];
public class Threadexample
{
public static void main(String[] args)
throws InterruptedException
{
// Object of a class that has both produce()
// and consume() methods
final PC pc = new PC();
// Create producer thread
Thread t1 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
[Link]();
}
catch(InterruptedException e)
{
[Link]();
}
}
});
// Create consumer thread
Thread t2 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
[Link]();
}
catch(InterruptedException e)
{
[Link]();
}
}
});
// Start both threads
[Link]();
[Link]();
// t1 finishes before t2
[Link]();
[Link]();
}
// This class has a list, producer (adds items to list
// and consumber (removes items).
public static class PC
{
// Create a list shared by producer and consumer
// Size of list is 2.
LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;
// Function called by producer thread
public void produce() throws InterruptedException
{
int value = 0;
while (true)
{
synchronized (this)
{
// producer thread waits while list
// is full
while ([Link]()==capacity)
wait();
[Link]("Producer produced-"
+ value);
// to insert the jobs in the list
[Link](value++);
// notifies the consumer thread that
// now it can start consuming
notify();
// makes the working of program easier
// to understand
[Link](1000);
}
}
}
// Function called by consumer thread
public void consume() throws InterruptedException
{
while (true)
{
synchronized (this)
{
// consumer thread waits while list
// is empty
while ([Link]()==0)
wait();
//to retrive the ifrst job in the list
int val = [Link]();
[Link]("Consumer consumed-"
+ val);
// Wake up producer thread
notify();
// and sleep
[Link](1000);
}
}
}
}
}
[Link] a customized exception and also make use of all the 5 exception keywords.
// Java program to demonstrate working of throws
class ThrowsExecp {
// This method throws an exception
// to be handled
// by caller or caller
// of caller and so on.
static void fun() throws IllegalAccessException
{
[Link]("Inside fun(). ");
throw new IllegalAccessException("demo");
}
// This is a caller function
public static void main(String args[])
{
try {
fun();
}
catch (IllegalAccessException e) {
[Link]("caught in main.");
}
}
}
// Java program to demonstrate working of try,
// catch and finally
class Division {
public static void main(String[] args)
{
int a = 10, b = 5, c = 5, result;
try {
result = a / (b - c);
[Link]("result" + result);
}
catch (ArithmeticException e) {
[Link]("Exception caught:Division by zero");
}
finally {
[Link]("I am in final block");
}
}
}
5. Convert the content of a given file into the uppercase content of the same file.
import [Link];
import [Link].*;
public class UppercaseFileConverter2
{
public static void main(String[] args)throws IOException
{
String filename;
String message;
String filename2;
//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner([Link]);
//Get the filename.
[Link]("Enter the filename: ");
filename = [Link]();
//Open the file.
FileWriter fwriter = new FileWriter(filename);
PrintWriter outputFile = new PrintWriter(fwriter);
//Get the message to be written in the file.
[Link]("Enter a message: ");
message = [Link]();
//Write the message to the file.
[Link](message);
//Close the file.
[Link]();
//Get the second file's name.
[Link]("Enter the name of the second file: ");
filename2 = [Link]();
//Open an input file.
FileReader freader = new FileReader(filename2);
BufferedReader inputFile = new BufferedReader(freader);
String str;
//Read the first item.
str = [Link]();
//If the item was read, display it in UpperCase.
while (str != null)
{
[Link](str);
String upper = [Link]();
str = [Link](upper);
}
//Close the file.
[Link]();
}
}
6. Develop an analog clock using applet.
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class MyClock extends Applet implements Runnable {
int width, height;
Thread t = null;
boolean threadSuspended;
int hours=0, minutes=0, seconds=0;
String timeString = "";
public void init() {
width = getSize().width; height
= getSize().height;
setBackground( [Link] );
}
public void start() {
if ( t == null ) {
t = new Thread( this );
[Link]( Thread.MIN_PRIORITY );
threadSuspended = false;
[Link]();
}
else {
if ( threadSuspended ) {
threadSuspended = false;
synchronized( this ) {
notify();
}
}
}
}
public void stop() {
threadSuspended = true;
}
public void run() {
try {
while (true) {
Calendar cal = [Link]();
hours = [Link]( Calendar.HOUR_OF_DAY );
if ( hours > 12 ) hours -= 12;
minutes = [Link]( [Link] );
seconds = [Link]( [Link] );
SimpleDateFormat formatter
= new SimpleDateFormat( "hh:mm:ss", [Link]() );
Date date = [Link]();
timeString = [Link]( date );
// Now the thread checks to see if it should suspend itself
if ( threadSuspended ) {
synchronized( this ) {
while ( threadSuspended ) {
wait();
}
}
}
repaint();
[Link]( 1000 ); // interval specified in milliseconds
}
}
catch (Exception e) { }
}
void drawHand( double angle, int radius, Graphics g ) {
angle -= 0.5 * [Link];
int x = (int)( radius*[Link](angle) );
int y = (int)( radius*[Link](angle) );
[Link]( width/2, height/2, width/2 + x, height/2 + y );
}
void drawWedge( double angle, int radius, Graphics g ) {
angle -= 0.5 * [Link];
int x = (int)( radius*[Link](angle) );
int y = (int)( radius*[Link](angle) );
angle += 2*[Link]/3;
int x2 = (int)( 5*[Link](angle) );
int y2 = (int)( 5*[Link](angle) );
angle += 2*[Link]/3;
int x3 = (int)( 5*[Link](angle) );
int y3 = (int)( 5*[Link](angle) );
[Link]( width/2+x2, height/2+y2, width/2 + x, height/2 + y );
[Link]( width/2+x3, height/2+y3, width/2 + x, height/2 + y );
[Link]( width/2+x2, height/2+y2, width/2 + x3, height/2 + y3 );
}
public void paint( Graphics g ) {
[Link]( [Link] );
drawWedge( 2*[Link] * hours / 12, width/5, g );
drawWedge( 2*[Link] * minutes / 60, width/3, g );
drawHand( 2*[Link] * seconds / 60, width/2, g );
[Link]( [Link] );
[Link]( timeString, 10, height-10 );
}
}
Code for applet
[Link]
<html>
<body>
<applet code="[Link]" width="300" height="300">
</applet>
</body>
</html>
7. Develop a scientific calculator using swings.
// Java program to create a simple calculator
// with basic +, -, /, * using java swing elements
import [Link].*;
import [Link].*;
import [Link].*;
class calculator extends JFrame implements ActionListener {
// create a frame
static JFrame f;
// create a textfield
static JTextField l;
// store oprerator and operands
String s0, s1, s2;
// default constrcutor
calculator()
{
s0 = s1 = s2 = "";
}
// main function
public static void main(String args[])
{
// create a frame
f = new JFrame("calculator");
try {
// set look and feel
[Link]([Link]());
}
catch (Exception e) {
[Link]([Link]());
}
// create a object of class
calculator c = new calculator();
// create a textfield
l = new JTextField(16);
// set the textfield to non editable
[Link](false);
// create number buttons and some operators
JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bs, bd, bm, be, beq, beq1;
// create number buttons
b0 = new JButton("0"); b1
= new JButton("1"); b2 =
new JButton("2"); b3 =
new JButton("3"); b4 =
new JButton("4"); b5 =
new JButton("5"); b6 =
new JButton("6"); b7 =
new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
// equals button
beq1 = new JButton("=");
// create operator buttons
ba = new JButton("+"); bs
= new JButton("-"); bd =
new JButton("/"); bm =
new JButton("*"); beq =
new JButton("C");
// create . button
be = new JButton(".");
// create a panel
JPanel p = new JPanel();
// add action listeners
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
[Link](c);
// add elements to panel
[Link](l);
[Link](ba);
[Link](b1);
[Link](b2);
[Link](b3);
[Link](bs);
[Link](b4);
[Link](b5);
[Link](b6);
[Link](bm);
[Link](b7);
[Link](b8);
[Link](b9);
[Link](bd);
[Link](be);
[Link](b0);
[Link](beq);
[Link](beq1);
// set Background of panel
[Link]([Link]);
// add panel to frame
[Link](p);
[Link](200, 220);
[Link]();
}
public void actionPerformed(ActionEvent e)
{
String s = [Link]();
// if the value is a number
if (([Link](0) >= '0' && [Link](0) <= '9') || [Link](0) == '.') {
// if operand is present then add to second no
if ()
s2 = s2 + s;
else
s0 = s0 + s;
// set the value of text
[Link](s0 + s1 + s2);
}
else if ([Link](0) == 'C') {
// clear the one letter
s0 = s1 = s2 = "";
// set the value of text
[Link](s0 + s1 + s2);
}
else if ([Link](0) == '=') {
double te;
// store the value in 1st
if ([Link]("+"))
te = ([Link](s0) + [Link](s2));
else if ([Link]("-"))
te = ([Link](s0) - [Link](s2));
else if ([Link]("/"))
te = ([Link](s0) / [Link](s2));
else
te = ([Link](s0) * [Link](s2));
// set the value of text
[Link](s0 + s1 + s2 + "=" + te);
// convert it to string
s0 = [Link](te);
s1 = s2 = "";
}
else {
// if there was no operand
if ([Link]("") || [Link](""))
s1 = s;
// else evaluate
else {
double te;
// store the value in 1st
if ([Link]("+"))
te = ([Link](s0) + [Link](s2));
else if ([Link]("-"))
te = ([Link](s0) - [Link](s2));
else if ([Link]("/"))
te = ([Link](s0) / [Link](s2));
else
te = ([Link](s0) * [Link](s2));
// convert it to string
s0 = [Link](te);
// place the operator
s1 = s;
// make the operand blank
s2 = "";
}
// set the value of text
[Link](s0 + s1 + s2);
}
}
}
8. Create an editor like MS-word using swings.
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
class editor extends JFrame implements ActionListener {
// Text component
JTextArea t;
// Frame
JFrame f;
// Constructor
editor()
{
// Create a frame
f = new JFrame("editor");
try {
// Set metl look and feel
[Link]("[Link]");
// Set theme to ocean
[Link](new OceanTheme());
}
catch (Exception e) {
}
// Text component
t = new JTextArea();
// Create a menubar
JMenuBar mb = new JMenuBar();
// Create amenu for menu
JMenu m1 = new JMenu("File");
// Create menu items
JMenuItem mi1 = new JMenuItem("New");
JMenuItem mi2 = new JMenuItem("Open");
JMenuItem mi3 = new JMenuItem("Save");
JMenuItem mi9 = new JMenuItem("Print");
// Add action listener
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](mi1);
[Link](mi2);
[Link](mi3);
[Link](mi9);
// Create amenu for menu
JMenu m2 = new JMenu("Edit");
// Create menu items
JMenuItem mi4 = new JMenuItem("cut");
JMenuItem mi5 = new JMenuItem("copy");
JMenuItem mi6 = new JMenuItem("paste");
// Add action listener
[Link](this);
[Link](this);
[Link](this);
[Link](mi4);
[Link](mi5);
[Link](mi6);
JMenuItem mc = new JMenuItem("close");
[Link](this);
[Link](m1);
[Link](m2);
[Link](mc);
[Link](mb);
[Link](t);
[Link](500, 500);
[Link]();
}
// If a button is pressed
public void actionPerformed(ActionEvent e)
{
String s = [Link]();
if ([Link]("cut")) {
[Link]();
}
else if ([Link]("copy")) {
[Link]();
}
else if ([Link]("paste")) {
[Link]();
}
else if ([Link]("Save")) {
// Create an object of JFileChooser class
JFileChooser j = new JFileChooser("f:");
// Invoke the showsSaveDialog function to show the save dialog
int r = [Link](null);
if (r == JFileChooser.APPROVE_OPTION) {
// Set the label to the path of the selected directory
File fi = new File([Link]().getAbsolutePath());
try {
// Create a file writer
FileWriter wr = new FileWriter(fi, false);
// Create buffered writer to write
BufferedWriter w = new BufferedWriter(wr);
// Write
[Link]([Link]());
[Link]();
[Link]();
}
catch (Exception evt) {
[Link](f, [Link]());
}
}
// If the user cancelled the operation
else
[Link](f, "the user cancelled the operation");
}
else if ([Link]("Print")) {
try {
// print the file
[Link]();
}
catch (Exception evt) {
[Link](f, [Link]());
}
}
else if ([Link]("Open")) {
// Create an object of JFileChooser class
JFileChooser j = new JFileChooser("f:");
// Invoke the showsOpenDialog function to show the save dialog
int r = [Link](null);
// If the user selects a file
if (r == JFileChooser.APPROVE_OPTION) {
// Set the label to the path of the selected directory
File fi = new File([Link]().getAbsolutePath());
try {
// String
String s1 = "", sl = "";
// File reader
FileReader fr = new FileReader(fi);
// Buffered reader
BufferedReader br = new BufferedReader(fr);
// Initilize sl
sl = [Link]();
// Take the input from the file
while ((s1 = [Link]()) != null) {
sl = sl + "\n" + s1;
}
// Set the text
[Link](sl);
}
catch (Exception evt) {
[Link](f, [Link]());
}
}
// If the user cancelled the operation
else
[Link](f, "the user cancelled the operation");
}
else if ([Link]("New")) {
[Link]("");
}
else if ([Link]("close")) {
[Link](false);
}
}
// Main class
public static void main(String args[])
{
editor e = new editor();
}
}