Grid Layout in Java using applet

//program to implement grid layout

Description


This program demonstrate grid layout in Java.  Grid layout is used to place the components in a grid of cells.
The statement setLayout(new GridLayout(5,3)); is used to create a grid layout with five rows and three columns. The program repeatedly places a label, text box and a button in each row using a for loop.


Program



//<applet code=LayGrid height=400 width=400></applet>

import java.awt.*;

import java.applet.*;

public class LayGrid extends Applet

{

public void init()

{

setLayout(new GridLayout(5,3));

for(int i=1;i<6;i++)

{

add(new Label("Lablel "+i));

add(new TextField("TextField "+i));

add(new Button("Button "+i));

}

}

}



Output


Java applet program for grid layout
Grid Layout in Java (Execution) 

Grid layout in Java
Grid Layout in Java applet

C Program to implement stack

What is a stack?


A stack is a data structure in which the items entered last will be the first to be removed. It is also known as LIFO (Last In First Out). The entry of a new item is called as PUSH and the removal of an item is called as POP operations respectively.

Explanation of the program


The program displays a menu from which the user can select one of the operations, which includes insert, delete, display and exit. The choice is read into the variable ch. 

If the user selects the insert operation, the stack is checked to see if it is full, by using the statement if(top==max), if so a message is printed to indicate it and the control will be exited from the switch case. If the stack is not full, the new item will be read into the variable item. Then the item is inserted into stack[top] and the value of top will be incremented.  

If the user selects the delete operation, the stack is first checked to see if it is empty, by using the statement if(top==0),  if so a message will be printed to indicate it and the control will be exited from the switch case. If the stack is not empty, the value of top will be decremented.

If the user selects the display operation, the stack is first checked to see if it is empty, by using the statement if(top==0),  if so a message will be printed to indicate it and the control will be exited from the switch case. If the stack is not empty, the contents of the stack will be displayed by using a for loop.

If the user selects the exit operation, the program will be exited. 

   

Program


// Stack Program in c
#include<stdio.h>

#include<process.h>

#include<conio.h>

#define max 5

void main()

{

int stack[max],top=0,ch,item,i;

printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit\nEnter your choice: ");

scanf("%d",&ch);

while(ch!=4)

{

switch(ch)

{

case 1:

if(top==max)

{

printf("\nStack is Full!!!");

break;

}

else

{

printf("Enter the item: ");

scanf("%d",&item);

stack[top]=item;

top++;

break;

}

case 2:

if(top==0)

{

printf("\nStack is Empty!!!");

break;

}

else

{

item=stack[top-1];

top--;

printf("\nItem deleted is %d",item);

break;

}

case 3:

if(top==0)

{

printf("stack is empty!!!");

break;

}

else

{

for(i=0;i<=top-1;i++)

printf("%d ",stack[i]);

break;

}

case 4:

exit(0);

break;

}

printf("\n\n1.Insert\n2.Delete\n3.Display\n4.Exit\nEnter your choice: ");

scanf("%d",&ch);

}

}


Output





c program for stack operations
C program to implement stack

Java program using package (Importing Package in Java)

Description


This program is used to demonstrate the concept of package in Java. The steps below demonstrates how to create package in Java. Student1.java is the package and Student.java is the program that uses the package. 

Step 1


Create a directory named Mypack under your main directroy, and inside that, save the following code as Student1.java.

// Package program

package Mypack;

public class Student1

{

String name;

int sid;

public Student1(String n,int id)

{

 name=n;

 sid=id;

}

public void display()

{

System.out.println("Name: "+name);

System.out.println("Sid: "+sid);

}

} 


Step2


Save the following code as Student.java under your main directory.
// This program imports the class Student1 under Mypack

//program to demostrate package
import Mypack.Student1;
public class Student extends Student1
{

String name;

int rollno,mark;

Student(String n,int r,int m)

{

super(n,r);

this.mark=m;

this.name=n;

this.rollno=r;

}

public void show()

{

System.out.println("Mark: "+mark);

}

public static void main(String s[])

{

Student s1=new Student("Amal",30,50);

s1.display();

s1.show();

}


Now the program is ready to be executed, compile Student1.java, then compile and execute Student.java


Output


Package program in Java
Package in Java

Java program to print IP address using InetAddress class

//Java program to print IP address
//Java program to print domain name
//Java program using InetAddress class

Description


This program is used to print the IP address of a website or a system by using the domain name. The getByName() method is used to get the ip address of the specified domain name. Some sites inclue multiple IP addresses, the getAllByName() method is used to fetch all the IP addresses associated with a site.


Progarm


import java.net.*;

public class IpClass

{

public static void main(String s[]) throws UnknownHostException

{

InetAddress add;

add=InetAddress.getByName("programs-code.blogspot.com");

System.out.println("\n");

System.out.println(add);

System.out.println("\n");

InetAddress address[]=InetAddress.getAllByName("programs-code.blogspot.com");

for(int i=0;i<address.length;i++)

System.out.println(address[i]);

}

}


Output


IP address and domain using InetAddress
Java IP address using InetAddress


Java program for user defined exception

//user defined exception in Java


Description



This program demonstrates the working of user defined exception in Java. The class which handles the user defined exception must extend the Exception class, here the class is Empid which contains the constructor Empid(). The class Employee contains the constructor Employee() which takes as input name, department and employee id. The display() function calls the user defined exception if employee id less than or equal to 0, otherwise it will print both these values.  


Program



class Empid extends Exception

{

Empid()    //Defining user defined exception

{

System.out.println("Empid Cannot be negative");

}

}

class Employee

{

String name,dept;

int empid;

public Employee(String name,String dept,int empid)

{

this.name=name;

this.dept=dept;

this.empid=empid;

}

public void display() throws Empid

{

if(empid <= 0)

throw new Empid();

else

System.out.println("Emp id= "+empid+"  Name= "+name+"  Dept= "+dept);

}

}

public class Emp

{

public static void main(String s[])

{

Employee e1=new Employee("Jibin","EC",4);

Employee e2=new Employee("Amal","ME",-5);

try

{

e1.display();

e2.display();

}

catch(Exception ex)

{

System.out.println("Error");         

}

}

}


Output


java program for user defined exception
Java:  user defined exception 

Java program to demonstrate multithreading

//Multithreading in Java



Description

Multi threading in Java allows multiple process to run simultaneously. It can be implemented in two ways, by extending the Thread class or imlementing the Runnable interface. The run() method is used to implement multi threading. The sleep() method pauses the execution of the current thread for the specified time. The start() method is used to start the thread by calling the run() method.


Program



class Multi extends Thread

{

int i=0,delay;

String word;

Multi(String word,int delay)

{

this.word=word;

this.delay=delay;

}

public void run()

{

try

{

while(i<5)

{

System.out.println(word);

i++;

Thread.sleep(delay);

}

}

catch(InterruptedException e)

{

return;

}

}

public static void main(String s[]) throws Exception

{

Multi m1=new Multi("S1",100);

m1.start();

Multi m2=new Multi("S2",100);

m2.start();

}

}


Output

multithreading in Java
Multithreading in Java

JavaScript to print browser name and version

// JavaScript to print browser name
// JavaScript to print browser version

Description

This script is used to print the browser name and version. The statement var browser=navigator.appName; fetches the browser name and stores it in the variable browser and bversion=navigator.appVersion; fetches the browser version and stores it in the variable bversion.

Code


<html>

<head>

<title>Name of Browser</title>

<script>

var browser=navigator.appName;

var bversion=navigator.appVersion;

var version=parseFloat(bversion);

document.write("<Font color=RED>Browser Name: </font>"+browser);

document.write("<br>");

document.write("<Font color=RED>Browser Version: </font>"+bversion);

</script>

<body></body>

</head>

</html>


Output

javascript to print browser name and version
Java Script - Browser name and version

Java program to demonstrate FlowLayout

//Program to implement flow layout


Description


Java provides several Layout managers they are:


  • Border Layout
  • Card Layout
  • Grid Layout
  • Flow Layout
  • Box Layout
  • GridBag Layout
  • Group Layout
  • Spring Layout


This program demonstrates the concept of flow layout. Flow Layout is the default layout manager. It arranges each components from left to right in row by row manner. In this program a label, text box and a button is added to an applet window by using Flow Layout.


Program



//<applet code=LayFlow.class height=400 width=400></applet>
import java.awt.*;

import java.applet.*;

public class LayFlow extends Applet

{

public void init()

{

setLayout(new FlowLayout());

Label l=new Label("Label");

add(l);

TextField text=new TextField("Text Field");

add(text);

Button b=new Button("Button");

add(b);

}

}


Output


FlowLayout demonstration in Java
FlowLayout in Java

Java program using ActionListener - how many button clicks

//program to display how many times a user clicked a button, using ActionListener and WindowListener


Description

This program counts the number of times a user clicks a button. The class BClick implements ActionListener interface which contains the actionPerformed() method. The statement b.addActionListener(this); associates ActionListener to the button b, so that when ever a user clicks a button the actionPerformed() method will be called. The value of click is incremented each time the actionPerformed() method is called. The text.setText("Button Clicked "+click+" times"); statement displays the value stored in the variable click to the text box.


Program


import java.awt.*;

import java.awt.event.*;

import java.applet.*;

class BClick extends Frame implements ActionListener,WindowListener

{

int click=0;

TextField text=new TextField(25);

//MenuBar m=new MenuBar();

Button b;

int clicks=0;

public static void main(String s[])

{

BClick b=new BClick("ActionListenerWindow");

b.setSize(350,100);

b.setVisible(true);

}

public BClick(String str)

{

super(str);

setLayout(new FlowLayout());

addWindowListener(this);

b=new Button("Click");

add(b);

add(text);

b.addActionListener(this);

}

public void actionPerformed(ActionEvent e)

{

click++;

text.setText("Button Clicked "+click+" times");

}

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

public void windowOpened(WindowEvent e)

{

}

public void windowActivated(WindowEvent e)

{

}

public void windowIconified(WindowEvent e)

{

}

public void windowDeiconified(WindowEvent e)

{

}

public void windowDeactivated(WindowEvent e)

{

}

public void windowClosed(WindowEvent e)

{

}

}


Output


Java program to find how many times a user clicked a buttion
Java program using ActionListener

JavaScript to display running time in the browser


Description


This script displays the running time in Hours, Minutes and Seconds format. The Date() function is used to fetch the current system date. getHours(), getMinutes and getSeconds() function is used to fetch hours, minutes and seconds respectively from the value of Date() function.

Code

<html>

<head>

<title>Display running time using JavaScript</title>

<script>

function starttime()

{

var today=new Date();

var hours=today.getHours();

var minutes=today.getMinutes();

var seconds=today.getSeconds();

var i;

var x;

x=checkampm(hours);

m=checktime(minutes);

s=checktime(seconds);

document.getElementById('txt').innerHTML=
"<font color=BLACK><h1>"+hours+":"+minutes+":"+seconds+" "+x+"</h1>";

t=setTimeout('starttime()',100);

}

function checktime(i)

{

if(i<10)

i="0"+i;

return(i);

}

function checkampm(i)

{

if(i<12)

return('AM');

else

return('PM');

}

</script>

</head>

<body onload=starttime();>

<span id='txt' style="background_color=WHITE">Time is</span>

</body>

</html>


Output


Display current time in the browser using JavaScript
Display time using JavaScript