Practical 1. Write a program that prints “Hello world”.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
public class Hello
{
public static void main(String args[])
{
System.out.println("Hello world");
}
}
Practical 2. Write a program for command-line argument in java.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class CommandLineExample
{
public static void main(String args[])
{
System.out.println("Your first argument is: "+args[0]);
}
}
Practical 3 Write a program that uses single dimensional array.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
public class SingleDimArray
{
public static void main(String args[])
{
int a[]={1,2,3,4,5,6};
System.out.println(a[2]);
System.out.println(a[5]);
}
}
Practical 4. Write a program that perform addition of Matrix using Two Dimension array.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
public class TwoDArray
{
public static void main(String args[])
{
int myarray[][]={{1,2,3},{4,1,2},{3,4,1}};
int myarray1[][]={{4,1,2},{1,2,3},{5,2,1}};
for(int i=0; i<myarray.length; i++)
{
for(int j=0; j<myarray.length; j++)
{
System.out.print(myarray[i][j]);
}
System.out.println();
}
System.out.println(" ");
for(int i=0; i<myarray1.length; i++)
{
for(int j=0; j<myarray1.length; j++)
{
System.out.print(myarray1[i][j]);
}
System.out.println();
}
System.out.println(" ");
for(int i=0; i<myarray.length; i++)
{
for(int j=0; j<myarray1.length; j++)
{
System.out.print(myarray[i][j]+myarray1[i][j]);
}
System.out.println();
}
System.out.println(" ");
}
}
Practical 5: Write a program that uses if-else loop for finding odd and even numbers.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
public class If_Else_Example
{
public static void main(String[] args)
{
//create an array of 10 numbers
int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10};
for(int i=0; i < numbers.length; i++)
{
/*
* use modulus operator to check if the number is even or odd.
* If we divide any number by 2 and reminder is 0 then the number is
* even, otherwise it is odd.
*/
if(numbers[i]%2 == 0)
System.out.println(numbers[i] + " is even number.");
else
System.out.println(numbers[i] + " is odd number.");
}
}
}
Practical 6 Write a program that uses switch statement.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class Switch_Demo
{
public static void main(String[] args)
{
int week = 3;
switch (week)
{
case 1:
System.out.println("monday");
break;
case 2:
System.out.println("tuesday");
break;
case 3:
System.out.println("wednesday");
break;
case 4:
System.out.println("thursday");
break;
case 5:
System.out.println("friday");
break;
case 6:
System.out.println("saturday");
break;
case 7:
System.out.println("sunday");
break;
default:
System.out.println("Invalid week");
break;
}
}
}
Practical 7: Write a Program that uses Assignment and Arithmetic operator.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class AssignmentAndArithmetic
{
public static void main (String[] args)
{
int num1 = 10;
int num2 = 20;
int num3 = num1 + num2;
System.out.println(num3);
}
}
Practical 8: Write a Program that performs each Arithmetic operation.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class ArithmeticOperationsDemo
{
public static void main (String[] args)
{
// answer is now 3
int answer = 1 + 2;
System.out.println(answer);
// answer is now 2
answer = answer - 1;
System.out.println(answer);
// answer is now 4
answer = answer * 2;
System.out.println(answer);
// answer is now 2
answer = answer / 2;
System.out.println(answer);
// answer is now 10
answer = answer + 8;
// answer is now 3
answer = answer % 7;
System.out.println(answer);
}
}
Practical 9: Write a program that displays use of pre-increment operator.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class PreIncrOptDemo
{
public static void main(String args[])
{
int i = 5, j = 5, sum = 0;
System.out.println("Value of i is " + i + " j is " +j +" and sum is " + sum);
sum = i + ++j;
System.out.println("Value of i is " + i + " j is " +j +" and sum is " + sum);
}
}
Practical 10: Write a program that displays use of post-decrement operator.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class PostDecrOptDemo
{
public static void main(String args[])
{
int i = 5, j = 5, sum = 0;
System.out.println("Value of i is " + i + " j is " + j +" and sum is " + sum);
sum = i + j--;
System.out.println("Value of i is " + i + " j is " + j +" and sum is " + sum);
}
}
Practical 11: Write a program that uses unary operator.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class UnaryDemo
{
public static void main(String[] args)
{
// result is now 1
int result = +1;
System.out.println(result);
// result is now -1
result = -result;
System.out.println(result);
// false
boolean success = false;
System.out.println(success);
// true
System.out.println(!success);
}
}
Practical 12: Write a program that uses Bitwise operator.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class BitwiseOptrDemo
{
public static void main(String args[])
{
int x = 5;
int y = 2;
System.out.println("x & y : " + (x & y));
System.out.println("x | y : " + (x | y));
System.out.println("x ^ y : " + (x ^ y));
System.out.println("~x : " + (~x));
System.out.println("x << y : " + (x << y));
System.out.println("x >> y : " + (x >> y));
System.out.println("x >>> y : " + (x >>> y));
}
}
Practical 13: Write a program that uses Relational Operator.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class RelationalDemo
{
public static void main(String[] args){
int ivar1 = 1;
int ivar2 = 2;
if(ivar1 == ivar2)
System.out.println("ivar1 == ivar2");
if(ivar1 != ivar2)
System.out.println("ivar1 != ivar2");
if(ivar1 > ivar2)
System.out.println("ivar1 > ivar2");
if(ivar1 < ivar2)
System.out.println("ivar1 < ivar2");
if(ivar1 <= ivar2)
System.out.println("ivar1 <= ivar2");
}
}
Practical 14. Write a program that maintains the records of students using Object and class.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class Obj_Class_Example
{
int rollno;
String name;
void insertRecord(int r, String n)
{
//method
rollno=r;
name=n;
}
void displayInformation()
{
System.out.println(rollno+" "+name);
}//method
public static void main(String args[])
{
Obj_Class_Example s1=new Obj_Class_Example();
Obj_Class_Example s2=new Obj_Class_Example();
s1.insertRecord(111,"Urvashi");
s2.insertRecord(222,"Khushi");
s1.displayInformation();
s2.displayInformation();
}
}
Practical 15: Write a program to take different number of parameters in argument list using function overloading
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class Overloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c+" "+num);
}
}
class Overloading_Demo
{
public static void main(String args[])
{
Overloading d=new Overloading();
d.disp('a');
d.disp('a',20);
}
}
Practical 16: Define a class called product each product has a name, product code and manufacture name. Define variables, methods and constructor for the product class. Write a class called test_product with the main method to test methods and constructor of product class.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class product
{
int product_id;
String product_name;
String manufacture_name;
product(int c,String nm,String mnm)
{
product_id=c;
product_name=nm;
manufacture_name=mnm;
}
public String getname()
{
return product_name;
}
public int getprodcode()
{
return product_id;
}
public String getmnm()
{
return manufacture_name;
}
}
class test_product
{
public static void main(String args[])
{
product p=new product(1,"Mobile","Sony");
System.out.println("Product code :"+p.getprodcode());
System.out.println("Product name :"+p.getname());
System.out.println("Product manufacture name :"+p.getmnm());
}
}
17. Write a class called statistics which has static method called average which takes one dimensional array for double array for double type as parameter and printing the average for values in array. Now write a class with mean method and create two dimensional array for the four of months using average method of static class to compute and print the average.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
public class Practical_17
{
public static void average()
{
double[] iarray=new double[]{4,5,6,7,8};
double average1=0;
for(int i=0;i<iarray.length;i++)
{
average1=average1+iarray[i];
}
System.out.println("Average of the double type array :"+average1);
}
public static void main(String args[])
{
double average2=0;
double[][] month=new double[][]{{1,2,3,4,5,6,7},{2,3,4,5,6,7,8},{3,4,5,6,7,8,9},{4,5,6,7,8,9,1}};
for(int i=0;i<month.length;i++)
{
for(int j=0;j<=month.length;j++)
{
average2=average2+month[i][j];
}
}
System.out.println("Average of the two dimensional array :"+average2);
Practical_17 p1=new Practical_17();
p1.average();
}
}
Practical 18. Write a class called constructor demo which uses default constructor, parameterized constructor which having int as argument and double as argument.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class Constructordemo
{
Constructordemo()
{
System.out.println("Default constructor");
}
Constructordemo(int a)
{
System.out.println("Parameterized constructor : "+a);
}
Constructordemo(int a,double b)
{
System.out.println("Parameterized constructor : "+a+" "+b);
}
Constructordemo(double c,int d)
{
System.out.println("Parameterized constructor : "+c+" "+d);
}
}
public class Constructor_Example
{
public static void main(String args[])
{
Constructordemo c1=new Constructordemo();
Constructordemo c2=new Constructordemo(5);
Constructordemo c3=new Constructordemo(5,10.5);
Constructordemo c4=new Constructordemo(10.5,5);
}
}
19. Write a program to design a String class that perform String method(Equal, Reverse the string, change case, trim etc.)
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
public class String_Demo
{
public static void main(String args[])
{
String str="This is some sample string with some words that have been repeated some times";
System.out.println("Total no. of characters :"+str.length());
System.out.println("To upper case :"+str.toUpperCase());
System.out.println("To lower case :"+str.toLowerCase());
System.out.println("Original String :"+str);
System.out.println(str.substring(8));
System.out.println(str.substring(8,19));
System.out.println(str.indexOf("some"));
String s=" "+str+" ";
System.out.println(s);
System.out.println("["+s.trim()+"]");
System.out.println(str.replace("s","$$##"));
String sh="Parth is a good boy";
System.out.println(sh+"->"+new StringBuffer(sh).reverse());
}
}
Practical 20. Write a program that release the memory from the object that are not longer used.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
public class Practical_20
{
public void finalize()
{
System.out.println("Object is garbage collected");
}
public static void main(String args[])
{
Practical_20 p1=new Practical_20();
Practical_20 p2=new Practical_20();
p1=null;
p2=null;
System.gc();
}
}
Practical 21. . Write a program that uses StringBuilder class. Create a loop and append five strings to it then convert the data into a String object.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
import java.lang.StringBuilder;
public class String_Buider_Demo
{
public static void main(String args[])
{
//Create a new StringBuilder....
StringBuilder sb=new StringBuilder();
//Loop and append values....
for(int i=0;i<5;i++)
{
sb.append("abc");
}
//Convert to string....
String result=sb.toString();
//Print result....
System.out.println("Print result :"+result);
}
}
Practical 22. Write a program to handle the exception using try and multiple catch block.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
public class Try_Catch_Demo
{
public static void main(String args[])
{
String num[]={"123","456","abc","789"};
int sum=0;
int i;
for(i=0;i<=num.length;i++)
{
try
{
sum+=Integer.parseInt(num[i]);
}
catch(NumberFormatException e)
{
System.out.println("NUMBER FORMAT ERROR");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ARRAY ERROR");
}
finally
{
System.out.println("I :"+i);
}
}
System.out.println("Sum is :"+sum);
}
}
Practical 23. Write a program to create a package that access the member of external class as well as same package.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
//save by Hero.java
package Bike;
public class Hero
{
public void msg()
{
System.out.println("Hello");
}
}
//save by Maruti.java
package Car;
import Bike.Hero;
public class Maruti
{
public static void main(String args[])
{
Hero obj = new Hero();
obj.msg();
}
}
Practical 24: Write a program that checks that given try block throws ArithmeticException, ArrayIndexOutOfBoundsException using nested try catch block.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class Exception_Demo
{
public static void main(String args[])
{
//Parent try block
try
{
//Child try block1
try
{
System.out.println("Inside block1");
int b=45/0;
System.out.println(b);
}
catch(ArithmeticException e1)
{
System.out.println("Exception: Arithmetic");
}
//Chiled try block2
try
{
System.out.println("Inside block2");
int b=Integer.parseInt(args[0]);
System.out.println(b);
}
catch(ArrayIndexOutOfBoundsException e2)
{
System.out.println("Exception:Array");
}
System.out.println("Just other statement");
}
catch(ArithmeticException e3)
{
System.out.println("Arithmetic Exception");
System.out.println("Inside parent try catch block");
}
catch(ArrayIndexOutOfBoundsException e4)
{
System.out.println("ArrayIndexOutOfBoundsException");
System.out.println("Inside parent try catch block");
}
catch(Exception e5)
{
System.out.println("Exception");
System.out.println("Inside parent try catch block");
}
System.out.println("Next satatement");
}
}
Practical 25. Define a class called GregorianCalendarDemo which display current date and time and also checks that the current year is leap year or not.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
import java.util.*;
public class Calender_Demo
{
public static void main(String args[])
{
String months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
int year;
//Create a Gregorian calendar initialized with the current date and time in the default locale & timezone.
GregorianCalendar gc=new GregorianCalendar();
//Display current time and date information.
System.out.print("Date :");
System.out.print(months[gc.get(Calendar.MONTH)]);
System.out.print(" "+gc.get(Calendar.DATE)+" ");
System.out.println(year=gc.get(Calendar.YEAR));
System.out.print("Time :");
System.out.print(gc.get(Calendar.HOUR)+":");
System.out.print(gc.get(Calendar.MINUTE)+":");
System.out.println(gc.get(Calendar.SECOND));
//Test if the current year is a leap year
if(gc.isLeapYear(year))
{
System.out.print("The current year is a leap year");
}
else
{
System.out.print("The current year is not a leap year");
}
}
}
Practical 26: Write a program that use super() implicitly and use constructor of super class as well as to call super() method.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class Super_Demo
{
Super_Demo(int a)
{
System.out.println(a);
}
}
class Super_Example extends Super_Demo
{
Super_Example(int a,int b)
{
super(a);
double area=0;
area=a*b;
System.out.println(area);
}
public static void main(String arg[])
{
Super_Example s=new Super_Example(5,5);
}
}
Practical 27: Write a program to create a thread that implements the runnable interface.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
public class CreateThreadRunnableExample implements Runnable
{
/*
* A class must implement run method to implement Runnable
* interface. Signature of the run method is,
*
* public void run()
*
* Code written inside run method will constite a new thread.
* New thread will end when run method returns.
*/
public void run(){
for(int i=0; i < 5; i++){
System.out.println("Child Thread : " + i);
try{
Thread.sleep(50);
}
catch(InterruptedException ie){
System.out.println("Child thread interrupted! " + ie);
}
}
System.out.println("Child thread finished!");
}
public static void main(String[] args)
{
/*
* To create new thread, use
* Thread(Runnable thread, String threadName)
* constructor.*/
Thread t = new Thread(new CreateThreadRunnableExample(), "My Thread");
/*
* To start a particular thread, use
* void start() method of Thread class.
*/
t.start();
for(int i=0; i < 5; i++){
System.out.println("Main thread : " + i);
try{
Thread.sleep(100);
}
catch(InterruptedException ie){
System.out.println("Child thread interrupted! " + ie);
}
}
System.out.println("Main thread finished!");
}
}
Practical 28: Write a program by using BufferedReader class a user to input his/her name and then the output will be shown.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
import java.io.*;
class BufferedReaderStringDemo
{
public static void main(String args[]) throws IOException
{
// create a BufferedReader using System.in
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str;
System.out.println("Enter your name:");
System.out.println("Enter 'stop' to quit.");
do
{
str = br.readLine();
System.out.println(str);
} while(!str.equals("stop"));
}
}
Practical 29: Write a program to create simple applet.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
import java.awt.*;
import java.applet.*;
/*
<APPLET CODE=simpleapplet_22.class
WIDTH=250
HEIGHT=250>
</APPLET>
*/
public class Simple_Applet extends Applet
{
public void paint(Graphics g)
{
g.drawString("wel-come to java applet :",10,210);
Color c1=new Color(255,250,230);
setBackground(c1);
setForeground(Color.red);
}
}
Practical 30: Write a program to implement smiley face using applet.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class SmileExample extends Applet
{
public static void main(String[] args)
{
Frame AppletSmileExample = new Frame("Draw Smiley in Applet Example");
AppletSmileExample.setSize(350, 250);
Applet SmileExample = new SmileExample();
AppletSmileExample.add(SmileExample);
AppletSmileExample.setVisible(true);
AppletSmileExample.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void paint(Graphics g)
{
g.setColor(Color.darkGray);
g.setFont(new Font("Arial",Font.BOLD,14));
g.drawString("Draw Smiley In Applet Example ", 50, 40);
g.setFont(new Font("Arial",Font.BOLD,10));
//The Syntax for drawOval(int xTopLeft, int yTopLeft, int width, int height);
g.drawOval(70, 50, 150, 150);
//The Syntax for fillOval(int xTopLeft, int yTopLeft, int width, int height);
g.fillOval(80, 100, 50, 20);
g.fillOval(160, 100, 50, 20);
//Syntax For:- drawString(String str, int xBaselineLeft, int yBaselineLeft);
g.drawLine(145, 125, 145, 155);
//Syntax For:- drawArc(int xTopLeft, int yTopLeft, int width, int height, int startAngle, int arcAngle);
g.drawArc(100, 90, 95, 95, 0, -180);
}
}
Practical 31: Write a program to implement simple layout manager.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
import java.awt.*;
import javax.swing.*;
public class MyFlowLayout
{
JFrame f;
MyFlowLayout()
{
f=new JFrame();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);
f.setLayout(new FlowLayout(FlowLayout.RIGHT));
//setting flow layout of right alignment
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args)
{
new MyFlowLayout();
}
}
Practical 32. Write a program that create AWT Button using applet and AWT.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
import java.awt.*;
class AWTButton extends Frame
{
Button b1,b2;
public AWTButton()
{
// Set frame properties
setTitle("AWT Frame"); // Set the title
setSize(200,200); // Set size to the frame
setLayout(new FlowLayout()); // Set the layout
setVisible(true); // Make the frame visible
setLocationRelativeTo(null); // Center the frame
// Create buttons
b1=new Button(); // Create a button with default constructor
b1.setLabel("I am button 1"); // Set the text for button
b2=new Button("Button 2"); // Create a button with sample text
b2.setBackground(Color.lightGray); // Set the background to the button
add(b1);
add(b2);
}
public static void main(String args[])
{
new AWTButton();
}
}
Practical 33 Write a program that convert a collection to an array by using list.add(). And list.toArray() method of java util class.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
import java.util.*;
class list_33
{
public static void main(String arg[])
{
List<String> l=new ArrayList<String>();
l.add("this");
l.add("is");
l.add("a good");
l.add("program");
String[]s=l.toArray(new String[0]);
for(int i=0;i<s.length;i++)
{
String c=s[i];
System.out.println(c);
}
}
}
Practical 34 Create AWT Radio button using CheckboxGroup example.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
/* <APPLET CODE ="CBGroup_Example.class" WIDTH=300 HEIGHT=200> </APPLET> */
public class CBGroup_Example extends Applet implements ItemListener
{
Checkbox cb1,cb2,cb3,cb4,cb5,c1,c2,c3;
CheckboxGroup cbg1,cbg2;
TextField t;
Label l;
public void init()
{
setLayout(new GridLayout(3,2,1,1));
Panel p1= new Panel();
Panel p2= new Panel();
Panel p3= new Panel();
Panel p4= new Panel();
p1.setLayout(new GridLayout(3,1));
p2.setLayout(new GridLayout(2,1));
p3.setLayout(new GridLayout(3,1));
p4.setLayout(new GridLayout(1,2));
cbg1= new CheckboxGroup();
cbg2= new CheckboxGroup();
cb1= new Checkbox("Breakfast...Rs. 35",cbg1,true);
cb2= new Checkbox("Lunch...Rs. 60",cbg1,false);
cb3= new Checkbox("Dinner...Rs. 70",cbg1,false);
cb4= new Checkbox("Cold COffee...Rs. 12",cbg2,false);
cb5= new Checkbox("Hot Coffee...Rs. 10",cbg2,false);
cbg2.setSelectedCheckbox(cb5);
p1.add(cb1);
p1.add(cb2);
p1.add(cb3);
p2.add(cb4);
p2.add(cb5);
c1= new Checkbox("Green salad...Rs. 15");
p3.add(c1);
c1.addItemListener(this);
c2= new Checkbox("Soup...Rs. 25");
p3.add(c2);
c2.addItemListener(this);
c3= new Checkbox("Curd...Rs. 10");
p3.add(c3);
c3.addItemListener(this);
l= new Label("Bill is");
p4.add(l);
t= new TextField(10);
p4.add(t);
add(p1);
add(p2);
add(p3);
add(p4);
cb1.addItemListener(this);
cb2.addItemListener(this);
cb3.addItemListener(this);
cb4.addItemListener(this);
cb5.addItemListener(this);
}
public void itemStateChanged(ItemEvent e)
{
int amt=0;
if(cbg1.getSelectedCheckbox()==cb1)
{
amt=amt+35;
}
if(cbg1.getSelectedCheckbox()==cb2)
{
amt=amt+60;
}
if(cbg1.getSelectedCheckbox()==cb3)
{
amt=amt+70;
}
if(cbg2.getSelectedCheckbox()==cb4)
{
amt=amt+12;
}
if(cbg2.getSelectedCheckbox()==cb5)
{
amt=amt+10;
}
if(c1.getState())
{
amt=amt+15;
}
if(c2.getState())
{
amt=amt+25;
}
if(c3.getState())
{
amt=amt+10;
}
t.setText(String.valueOf(amt)); }
}
Practical 35. Write a program to implement Grid layout.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TryGridLayout
{
public static void main(String[] args)
{
JFrame aWindow = new JFrame("This is a Grid Layout");
aWindow.setBounds(30, 30, 300, 300);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout grid = new GridLayout(3, 4, 30, 20);
Container content = aWindow.getContentPane();
content.setLayout(grid);
JButton button = null;
for (int i = 1; i <= 10; i++)
{
content.add(button = new JButton(" Press " + i));
}
aWindow.pack();
aWindow.setVisible(true);
}
}
Practical 36. Write a program to implement card layout.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TryCardLayout extends JPanel implements ActionListener
{
CardLayout card = new CardLayout(50, 50); // Create layout
public TryCardLayout()
{
setLayout(card);
JButton button;
for (int i = 1; i <= 6; i++)
{
add(button = new JButton(" Press " + i), "Card" + i); // Add a button
button.addActionListener(this); // Add listener for button
}
}
// Handle button events
public void actionPerformed(ActionEvent e)
{
card.next(this); // Switch to the next card
}
public static void main(String[] args)
{
JFrame aWindow = new JFrame();
aWindow.setBounds(30, 30, 300, 300);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aWindow.getContentPane().add(new TryCardLayout());
aWindow.setVisible(true);
}
}
Practical 37: Write a program to implement border layout.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
import java.awt.*;
import javax.swing.*;
public class Border
{
JFrame f;
Border()
{
f=new JFrame();
JButton b1=new JButton("NORTH");;
JButton b2=new JButton("SOUTH");;
JButton b3=new JButton("EAST");;
JButton b4=new JButton("WEST");;
JButton b5=new JButton("CENTER");;
f.add(b1,BorderLayout.NORTH);
f.add(b2,BorderLayout.SOUTH);
f.add(b3,BorderLayout.EAST);
f.add(b4,BorderLayout.WEST);
f.add(b5,BorderLayout.CENTER);
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args)
{
new Border();
}
}
Practical 38. Write a program to implement box layout.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
import java.awt.*;
import javax.swing.*;
public class BoxLayoutExample1 extends Frame
{
Button buttons[];
public BoxLayoutExample1 ()
{
buttons = new Button [5];
for (int i = 0;i<5;i++)
{
buttons[i] = new Button ("Button " + (i + 1));
add (buttons[i]);
}
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
setSize(400,400);
setVisible(true);
}
public static void main(String args[])
{
BoxLayoutExample1 b=new BoxLayoutExample1();
}
}
Practical 39. Write a program to find palindrome number using for loop.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
public class Palindrome_Example
{
public static void main(String[] args)
{
//array of numbers to be checked
int numbers[] = new int[]{121,13,34,11,22,54};
//iterate through the numbers
for(int i=0; i < numbers.length; i++)
{
int number = numbers[i];
int reversedNumber = 0;
int temp=0;
/*
* If the number is equal to it's reversed number, then
* the given number is a palindrome number.
*
* For example, 121 is a palindrome number while 12 is not.
*/
//reverse the number
while(number > 0)
{
temp = number % 10;
number = number / 10;
reversedNumber = reversedNumber * 10 + temp;
}
if(numbers[i] == reversedNumber)
System.out.println(numbers[i] + " is a palindrome number");
else
System.out.println(numbers[i] + " is not a palindrome number");
}
}
}
Practicaln 41. Write a program that perform method overloading
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class Calculations
{
void sum(int a,int b)
{
System.out.println(a+b);
}
void sum(double a,double b)
{
System.out.println(a+b);
}
public static void main(String args[])
{
Calculations obj=new Calculations();
obj.sum(10.5,10.5);
obj.sum(20,20);
}
}
Practical 42. Write a program that uses interface to perform multiple inheritance.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
import java.lang.*;
import java.io.*;
interface Exam
{
void percent_cal();
}
class Student
{
String name;
int roll_no,mark1,mark2;
Student(String n, int r, int m1, int m2)
{
name=n;
roll_no=r;
mark1=m1;
mark2=m2;
}
void display()
{
System.out.println ("Name of Student: "+name);
System.out.println ("Roll No. of Student: "+roll_no);
System.out.println ("Marks of Subject 1: "+mark1);
System.out.println ("Marks of Subject 2: "+mark2);
}
}
class Result extends Student implements Exam
{
Result(String n, int r, int m1, int m2)
{
super(n,r,m1,m2);
}
public void percent_cal()
{
int total=(mark1+mark2);
float percent=total*100/200;
System.out.println ("Percentage: "+percent+"%");
}
void display()
{
super.display();
}
}
class Multiple_Inhe_Interface
{
public static void main(String args[])
{
Result R = new Result("Ra.one",12,93,84);
R.display();
R.percent_cal();
}
}
Practical 43 Write a program that uses Abstract class.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
abstract class Vehicle
{
public abstract void engine();
}
public class car_43 extends Vehicle
{
public void engine()
{
System.out.println("Car engine");
//car engine implementation
}
public static void main(String[] args)
{
Vehicle v = new car_43();
v.engine();
}
}
Practical 44. Write a program that uses this keyword.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class This_Demo
{
int id;
String name;
This_Demo(int id,String name)
{
this.id = id;
this.name = name;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
This_Demo s1 = new This_Demo(111,"urvashi");
This_Demo s2 = new This_Demo(222,"khushi");
s1.display();
s2.display();
}
}
Practical 45. Write a program that uses special operator.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class Parent
{ }
public class Child extends Parent
{
public void check()
{
System.out.println("Sucessfull Casting");
}
public static void show(Parent p)
{
if(p instanceof Child)
{
Child b1=(Child)p;
b1.check();
}
}
public static void main(String[] args)
{
Parent p=new Child();
Child.show(p);
}
}
Practical 46. Write a program that perform constructor overloading.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class Language
{
String name;
Language()
{
System.out.println("Constructor method called.");
}
Language(String t)
{
name = t;
}
public static void main(String[] args)
{
Language cpp = new Language();
Language java = new Language("Java");
cpp.setName("C++");
java.getName();
cpp.getName();
}
void setName(String t)
{
name = t;
}
void getName()
{
System.out.println("Language name: " + name);
}
}
Practical 47. Write a program that perform recursion using for loop.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
import java.util.Scanner;
public class Factorial
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number whose factorial is to be found: ");
int n = scanner.nextInt();
int result = factorial(n);
System.out.println("The factorial of " + n + " is " + result);
}
public static int factorial(int n)
{
int result = 1;
for (int i = 1; i <= n; i++)
{
result = result * i;
}
return result;
}
}
Practical 48. Write a program that uses try, catch and finally block.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
public class MyFinallyBlock
{
public static void main(String[] a)
{
/**
* Exception will occur here, after catch block
* the contol will goto finally block.
*/
try
{
int i = 10/0;
}
catch(Exception ex)
{
System.out.println("Inside 1st catch Block");
}
finally
{
System.out.println("Inside 1st finally block");
}
/**
* In this case exception won't, after executing try block
* the contol will goto finally block.
*/
try
{
int i = 10/10;
}
catch(Exception ex)
{
System.out.println("Inside 2nd catch Block");
}
finally
{
System.out.println("Inside 2nd finally block");
}
}
}
Practical 49. Write a program that input ten numbers if number found 5 then skip that number.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
class Jump_Example
{
public static void main(String args[])
{
System.out.println("continue when i is 5 skip:");
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
System.out.print("[continue]");
continue;
}
System.out.println("i=" + i );
}
}
}
Practical 50. Write a program to read a number print table of that number.
/*
* Created by Er. Hardik Chavda at Geetanjali College
* For more info : https://t.me/hardikchavda
*/
public class MultiplicationTable
{
public static void main(String[] args)
{
int number = 7;
printMultiplicationTable(number);
}
private static void printMultiplicationTable(int n)
{
System.out.println("Multiplication table for "+n);
System.out.println("---------------------------");
for(int i = 1; i<=10;i++)
{
System.out.format("%2d x %d = %3d\n", i,n,i*n);
}
}
}