1.
write a simple java program to demonstrate use of command line
arguments in java.
class CommandLineExample{
public static void main(String args[]){
System.out.println("Your first argument is: "+args[0]);
}
}
Output:-
Java kok.java
Java kok Bikram
Your Name is:Bikram
2.write a java program to define a class, describe its constructor,
overload the constructors and instantiate its object.
import java.lang.*;
class student
{
String name;
int regno;
int marks1,marks2,marks3;
student()
{
name="BIKRAM DAS";
regno=192017044;
marks1=82;
marks2=83;
marks3=84;
}
student(String n,int r,int m1,int m2,int m3)
{
name=n;
regno=r;
marks1=m1;
marks2=m2;
marks3=m3;
}
student(student s)
{
name=s.name;
regno=s.regno;
marks1=s.marks1;
marks2=s.marks2;
marks3=s.marks3;
}
void display()
{
System.out.println(name + "\t" +regno+ "\t" +marks1+ "\t" +marks2+ "\t" + marks3);
}
}
class studentdemo
{
public static void main(String arg[])
{
student s1=new student();
student s2=new student("john", 34266, 58, 96, 84);
student s3=new student(s1);
s1.display();
s2.display();
s3.display();
}
}
Output:
BIKRAM DAS 192017044 82 83 84
John 34266 58 96 84
BIKRAM DAS 192017044 82 83 84
3.write a java program to difine a class, define instance methods for
setting and retrieving values of instance variables and instantiate
its object.
package Edureka;
import java.util.Scanner;
public class Student
{
public String name;
private int marks;
public Student (String stuName) {
name = stuName;
}
public void setMarks(int stuMar) {
marks = stuMar;
}
// This method prints the student details.
public void printStu() {
System.out.println("Name: " + name );
System.out.println("Marks:" + marks);
}
public static void main(String args[]) {
Student StuOne = new Student("BIKRAM");
Student StuTwo = new Student("BIKI");
Student StuThree = new Student("PRIYA");
StuOne.setMarks(99);
StuTwo.setMarks(91);
StuThree.setMarks(90);
StuOne.printStu();
StuTwo.printStu();
StuThree.printStu();
}
}
OUTPUT:
Name: BIKRAM
Marks:99
Name: BIKI
Marks:91
Name: PRIYA
Marks:90
4.write a java program to define a class, define instance methods
and overload tham and use them for dynamic method invocation.
import java.lang.*;
class emp
{
String name;
int id;
String address;
void getdata(String name,int id,String address)
{
this.name=name;
this.id=id;
this.address=address;
}
void putdata()
{
System.out.println("Employee details are :");
System.out.println("Name :" +name);
System.out.println("ID :" +id);
System.out.println("Address :" +address);
}
}
class empdemo
{
public static void main(String arg[])
{
emp e=new emp();
e.getdata("BIKRAM DAS",192017044,"JALPAIGURI");
e.putdata();
}
}
Output:
Employee details are :
Name : BIKRAM DAS
ID : 192017044
Address : JALPAIGURI
5.write a java program to demonstrate use of sub class.
Class Employee{
float salary=10000;
}
Class programmer extends Employee{
int bonus=1000;
public static void main(String args[]){
programmer p=new programmer();
System.out.println(“Programmer salary is:”+p.salary);
System.out.println(“Bonus of Programmer is:”+p.bonus);
}
}
OUTPUT:-
Program salary is:10000.0
Bonus of programmer is:1000.0
6.write a java program to demonstrate use of nested class.
Class OuterClass
{
Static int outer_x=12;
int outer_y=20;
private static int outer_private=50;
static class StaticNestedClass
{
Void display()
{
System.out.println(“outer_x=”+outer_x);
System.out.println(“outer_private=”+outer_private);
}
}
}
Public class StaticNestedClassDemo
{
Public static void main(String[])
{
OuterClass.StaticNestedClass NestedObject=new OuterClass.StaticNestedClass();
nestedObject.display();
}
}
Output:-
Outer_x=12
Outer_private=50
7.Write a Java Program to practice
-use of single Dimensional array.
-use of multidimensional array.
Single Dimensional array:-
Class OnedimensionalStandard
{
public static void main(String args[])
{
int[] a=new int[3];
a[0]=20;
a[1]=30;
a[2]=40;
System.out.println("One dimensional array elements are");
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
}
}
Output:-
Javac OnedimensionalStandard.java
Java OnedimensionalStandard.java
One dimensional array elements are
20
30
40
Multidimensional array:-
public class multidimensionalarray {public static void main(String args[]){
int a[][]={{2,2,3},{8,4,5},{9,4,5}};
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}}
Output:
Javac multidimensionalarry.java
Java multidimensionalarry.java
223
845
945
8.Write a java program to implement array of objects.
public class arrayOfObjects
{
public static void main(String args[])
{
//create an array of product object
Product[] obj = new Product[5] ;
//create & initialize actual product objects using constructor
obj[0] = new Product(23908,"Dell Laptop");
obj[1] = new Product(91241,"HP 630");
obj[2] = new Product(29823,"LG TV");
obj[3] = new Product(11909,"VIVO Y20G");
obj[4] = new Product(43591,"SAMSUNG USB");
//display the product object data
System.out.println("Product Object 1:");
obj[0].display();
System.out.println("Product Object 2:");
obj[1].display();
System.out.println("Product Object 3:");
obj[2].display();
System.out.println("Product Object 4:");
obj[3].display();
System.out.println("Product Object 5:");
obj[4].display();
}
}
//Product class with product Id and product name as attributes
class Product
{
int pro_Id;
String pro_name;
//Product class constructor
Product(int pid, String n)
{
pro_Id = pid;
pro_name = n;
}
public void display()
{
System.out.print("Product Id = "+pro_Id + " " + " Product Name = "+pro_name);
System.out.println();
}
}
Output:-
Product Object 1:
Product Id = 23908 Product Name = Dell Laptop
Product Object 2:
Product Id = 91241 Product Name = HP 630
Product Object 3:
Product Id = 29823 Product Name = LG TV
Product Object 4:
Product Id = 11909 Product Name = VIVO Y20G
Product Object 5:
Product Id = 43591 Product Name = SAMSUNG USB
9.Write a java program to practice string class and its methods.
public class Example{
public static void main(String args[]){
//creating a string by java string literal
String str = "Japan";
char arrch[]={'h','e','l','l','o'};
//converting char array arrch[] to string str2
String str2 = new String(arrch);
//creating another java string str3 by using new keyword
String str3 = new String("Java String Example");
//Displaying all the three strings
System.out.println(str);
System.out.println(str2);
System.out.println(str3);
}
}
Output:-
Japan
hello
Java String Example
10.Write a java program to implement Wrapper classes and their
methods.
//Java Program to convert all primitives into its corresponding
//wrapper objects and vice-versa
public class WrapperExample3{
public static void main(String args[]){
byte b=20;
short s=30;
int i=40;
long l=50;
float f=60.0F;
double d=70.0D;
char c='a';
boolean b2=true;
//Autoboxing: Converting primitives into objects
Byte byteobj=b;
Short shortobj=s;
Integer intobj=i;
Long longobj=l;
Float floatobj=f;
Double doubleobj=d;
Character charobj=c;
Boolean boolobj=b2;
//Printing objects
System.out.println("---Printing object values---");
System.out.println("Byte object: "+byteobj);
System.out.println("Short object: "+shortobj);
System.out.println("Integer object: "+intobj);
System.out.println("Long object: "+longobj);
System.out.println("Float object: "+floatobj);
System.out.println("Double object: "+doubleobj);
System.out.println("Character object: "+charobj);
System.out.println("Boolean object: "+boolobj);
//Unboxing: Converting Objects to Primitives
byte bytevalue=byteobj;
short shortvalue=shortobj;
int intvalue=intobj;
long longvalue=longobj;
float floatvalue=floatobj;
double doublevalue=doubleobj;
char charvalue=charobj;
boolean boolvalue=boolobj;
//Printing primitives
System.out.println("---Printing primitive values---");
System.out.println("byte value: "+bytevalue);
System.out.println("short value: "+shortvalue);
System.out.println("int value: "+intvalue);
System.out.println("long value: "+longvalue);
System.out.println("float value: "+floatvalue);
System.out.println("double value: "+doublevalue);
System.out.println("char value: "+charvalue);
System.out.println("boolean value: "+boolvalue);
}}
Output:-
-Printing object values---
Byte object: 20
Short object: 30
Integer object: 40
Long object: 50
Float object: 60.0
Double object: 70.0
Character object: a
Boolean object: true
---Printing primitive values---
byte value: 20
short value: 30
int value: 40
long value: 50
float value: 60.0
double value: 70.0
char value: a
boolean value: true
11.Write a java program to implement single inheritance by applying
various access controls to its data members and methods.
class Employee
{
private String name;
private int Id;
private Double Salary;
private Double Bonus;
public String getName() {
return(name);
}
public void setName(String n) {
name = n;
}
public int getId() {
return(Id);
}
public void setId(int i) {
Id = i;
}
}
class SingleInheritance extends Employee {
Double Salary =325000.00;
public static void main(String args[]) {
SingleInheritance obj = new SingleInheritance();
obj.setName("Bikram");
obj.setId(700);
System.out.println("Employee Name = " +obj.getName());
System.out.println("Employee Id = " +obj.getId());
System.out.println("Employee Salary = " +obj.Salary);
}
}
Output:-
Employee Name =Bikram
Employee Id = 700
Employee Salary = 325000.0
12.Write a java program to implement multilevel inheritance by
applying various access controls to its data members and methods.
import java.io.DataInputStream;
class Student
{
private int rollno;
private String name;
DataInputStream dis=new DataInputStream(System.in);
public void getrollno()
{
try
{
System.out.println("Enter rollno ");
rollno=Integer.parseInt(dis.readLine());
System.out.println("Enter name ");
name=dis.readLine();
}
catch(Exception e){ }
}
void putrollno()
{
System.out.println("Roll No ="+rollno);
System.out.println("Name ="+name);
}
}
class Marks extends Student
{
protected int m1,m2,m3;
void getmarks()
{
try
{
System.out.println("Enter marks :");
m1=Integer.parseInt(dis.readLine());
m2=Integer.parseInt(dis.readLine());
m3=Integer.parseInt(dis.readLine()); }
catch(Exception e)
{
}
}
void putmarks() {
System.out.println("m1="+m1);
System.out.println("m2="+m2);
System.out.println("m3="+m3); }}
class Result extends Marks {
private float total;
void compute_display() {
total=m1+m2+m3;
System.out.println("Total marks :" +total); }}
clas
s MultilevelDemo
{
public static void main(String arg[]) {
Result r=new Result();
r.getrollno();
r.getmarks();
r.putrollno();
r.putmarks();
r.compute_display(); }}
Output:-
-
Enter rollno
111
Enter name
Bikram
Enter marks :
76
77
78
Roll No =110
Name =Bikram
m1=76
m2=77
m3=78
Total marks :231.0
13.Write a java program to implement inheritance and demonstrate
use of method overriding.
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
class Bike2 extends Vehicle{
void run(){System.out.println("Bike is running safely");}
public static void main(String args[]){
Bike2 obj = new Bike2();
obj.run();
}
}
Output:
Bike is running safely
14.Write a program to implement the concept of threading.
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
Output:-
thread is running...
15.Write a program to implement the concept of Exception
Handling.
-using predefined exception.
-by creating user defined exceptions.
Using predefined exception:-
// Java program to demonstrate
// ArithmeticException
class ArithmeticException_Demo {
public static void main(String args[])
{
try {
int a = 30, b = 0;
int c = a / b; // cannot divide by zero
System.out.println("Result = " + c);
}
catch (ArithmeticException e) {
System.out.println("Can't divide a number by 0");
}
}
}
Output:-
Can't divide a number by 0
by creating user defined exceptions:-
import java.util.Scanner;
class NegativeAmtException extends Exception {
String msg;
NegativeAmtException(String msg) {
this.msg = msg;
}
public String toString() {
return msg;
}
}
public class userdefined {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter Amount:");
int a = s.nextInt();
try {
if (a < 0) {
throw new NegativeAmtException("Invalid Amount");
}
System.out.println("Amount Deposited");
} catch (NegativeAmtException e) {
System.out.println(e);
}
}
}
Output :-
Enter Amount:-1100
Invalid Amount
16.Write a program to implement the concept of Synchronization for
-object synchronization.
-Method synchronization.
Object synchronization:-
// A Java program to demonstrate working of
// synchronized.
import java.io.*;
import java.util.*;
// A Class used to send a message
class Sender
{
public void send(String msg)
{
System.out.println("Sending\t" + msg );
try
{
Thread.sleep(1000);
}
catch (Exception e)
{
System.out.println("Thread interrupted.");
}
System.out.println("\n" + msg + "Sent");
}
}
// Class for send a message using Threads
class ThreadedSend extends Thread
{
private String msg;
Sender sender;
// Receives a message object and a string
// message to be sent
ThreadedSend(String m, Sender obj)
{
msg = m;
sender = obj;
}
public void run()
{
// Only one thread can send a message
// at a time.
synchronized(sender)
{
// synchronizing the send object
sender.send(msg);
}
}
}
// Driver class
class SyncDemo
{
public static void main(String args[])
{
Sender send = new Sender();
ThreadedSend S1 =
new ThreadedSend( " Hi " , send );
ThreadedSend S2 =
new ThreadedSend( " Bye " , send );
// Start two threads of ThreadedSend type
S1.start();
S2.start();
// wait for threads to end
try
{
S1.join();
S2.join();
}
catch(Exception e)
{
System.out.println("Interrupted");
}
}
}
Output:-
Sending Hi
Hi Sent
Sending Bye
Bye Sent
Method synchronized:-
class First
{
synchronized public void display(String msg)
{
System.out.print ("["+msg);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println ("]");
}
}
class Second extends Thread
{
String msg;
First fobj;
Second (First fp,String str)
{
fobj = fp;
msg = str;
start();
}
public void run()
{
fobj.display(msg);
}
}
public class MyThread
{
public static void main (String[] args)
{
First fnew = new First();
Second ss = new Second(fnew, "welcome");
Second ss1= new Second(fnew,"my");
Second ss2 = new Second(fnew, "jalpaiguri");
}
}
Output:-
[welcome]
[jalpaiguri]
[my]
17.Write a program using Applet
-To display a message in the Applet.
-For configuring Applets by passing parameters.
To display a message in the Applet:-
public class Greeting {
static void hello() {
System.out.println("Hello.. Happy learning!");
}
public static void main(String[] args) {
hello();
}
}
Output:
Hello.. Happy learning!
For configuring Applets by passing parameters:-
import java.awt.*;
import java.applet.*
/*
<applet code="Applet8" width="400" height="200">
<param name="Name" value="Bikram">
<param name="Age" value="20">
<param name="Sport" value="Tennis">
<param name="Food" value="Pasta">
<param name="Fruit" value="Apple">
<param name="Destination" value="California">
</applet>
*/
public class Applet8 extends Applet
{
String name;
String age;
String sport;
String food;
String fruit;
String destination;
public void init()
{
name = getParameter("Name");
age = getParameter("Age");
food = getParameter("Food");
fruit = getParameter("Fruit");
destination = getParameter("Destination");
sport = getParameter("Sport");
}
public void paint(Graphics g)
{
g.drawString("Reading parameters passed to this applet -", 20, 20);
g.drawString("Name -" + name, 20, 40);
g.drawString("Age -" + age, 20, 60);
g.drawString("Favorite fruit -" + fruit, 20, 80);
g.drawString("Favorite food -" + food, 20, 100);
g.drawString("Favorite destination -" + name, 20, 120);
g.drawString("Favorite sport -" + sport, 20, 140);
}
}
Output:-
parameters passed to this applet-
Name-Bikram
Age-20
Favorite fruit-Apple
Favorite food-Pasta
Favorite sport-Tennis