Java Practicals
Java Practicals
// 1.Program
import java.lang.*;
import java.util.*;
// 2.Program
import java.util.*;
// 3.Program
import java.util.*;
// 4.Program
import java.util.*;
public class Numbers
{
public static void main(String[] args)
{
int i=1;
System.out.println("Divided by 5 but not 10 : ");
for (i=1;i<100;i++)
{
if (i%5==0 && i%10 !=0)
{
System.out.println(i);
}
}
}
}
// 5.Program
import java.util.*;
for (i=1;i<=num;i++ )
{
if (i%2==0)
{
sum = sum + i;
}
}
System.out.println("The Sum Of Even Number Upto " + num + " =
" + sum);
}
}
//6.Program
import java.util.*;
class Friend1
{
String name,address,email;
long mobileno;
Scanner sc=new Scanner(System.in);
void display()
{
System.out.println("Your Friend Name is "+name+". Address is
"+address+". E-mail is "+email+".");
System.out.println("Mobile No is "+mobileno);
}
f.display();
}
}
//7.Program
import java.util.*;
import java.io.*;
import java.lang.*;
Constructor()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Your Name: ");
name = sc.next();
System.out.println("Enter Your No. ");
no = sc.nextInt();
}
Constructor(String s)
{
name = s;
}
Constructor(String s,int i)
{
name = s;
no = i;
}
Constructor(Constructor c)
{
name = c.name;
}
void display()
{
System.out.println("Name is " + name + " and no is " + no);
}
}
class Student
{
public static void main(String args[])
{
int eno_no,fee;
String name,course;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Enrollment No: ");
eno_no = sc.nextInt();
System.out.print("Enter Name: ");
name = sc.next();
System.out.print("Enter course: ");
course = sc.next();
System.out.print("Enter Fee: ");
fee = sc.nextInt();
// 9.Program
import java.util.*;
class SumOfArray
{
public static void main(String args[])
{
int sum=0;
int []array=new int[3];
Scanner sc=new Scanner(System.in);
System.out.println("Enter a element in a array: ");
for(int i=0;i<array.length;i++)
{
System.out.println("array ["+i+ "]=");
array[i]=sc.nextInt();
sum=sum+array[i];
}
System.out.println("Sum of array is "+sum);
}
}
10.Write a program to display the product of two diagonals
array. [Assume that the array size is 3x3]
//10.Program
import java.util.*;
class SumOfdiagonalArray
{
public static void main(String args[])
{
int i,j,ro,co;
Scanner sc=new Scanner(System.in);
for(i=0;i<ro;i++)
{
for(j=0;j<co;j++)
{
c[i][j] = a[i][j] + b[i][j];
}
//System.out.println();
}
// 11.Program
import java.util.*;
import java.lang.*;
import java.io.*;
class MatrixOperation
{
public static void main(String[] args)
{
int ch;
Scanner sc = new Scanner(System.in);
System.out.print("1. Addition \n");
System.out.print("2. Substraction \n");
System.out.print("3. Multiplication \n");
System.out.print("4. Exit \n \n");
ch = sc.nextInt();
switch(ch)
{
case 1:
addition.add();
break;
case 2:
substraction.subtract();
break;
case 3:
multiplication.multiply();
break;
case 4:
System.out.println();
break;
default:
System.out.println("Invalid choice....");
break;
}
}
}
class addition{
class multiplication{
static void multiply()
{
int i,j;
int a[][],b[][],c[][];
int row,col;
Scanner sc = new Scanner(System.in);
System.out.println("Enter The Number Of Row:- ");
row = sc.nextInt();
System.out.println("Enter The Number Of Columns:- ");
col = sc.nextInt();
}
class prac12{
public static void main(String args[]){
Student1 ob[] = new Student1[2];
ob[0] = new Student1();
ob[1] = new Student1();
System.out.println("Student 1:-\n");
ob[0].insert();
ob[0].percent();
ob[0].grade();
System.out.println("Student 2:-\n");
ob[1].insert();
ob[1].percent();
ob[1].grade();
}
}
// 14.Program
import java.util.*;
import java.io.*;
import java.lang.*;
//program 15.
import java.util.*;
class StringConcatenation
{
public static void main(String args[])
{
String name, fname, surname, MyName;
Scanner sc=new Scanner(System.in);
16.Write a program to input a sentence (with blank space) and display all
characters and their occurrence in inputted string.
i.e. LIRIL -> then output like following
I–2
L–2
R–1
//16 program
import java.util.*;
import java.lang.*;
import java.io.*;
if (find == 1)
{
System.out.println("Number Of Occurence Of " +
str.charAt(i) + " is: " + count[str.charAt(i)]);
}
}
}
}
17.Create class ShapeArea which will calculate area of different
geographical shape like triangle, circle, square, rectangle, etc.
Write a program which will calculate and display the area of shapes using
the concept of method overloading.
//17.program
import java.util.*;
import java.lang.*;
import java.io.*;
void calculate(int a)
{
double s = a * a;
System.out.println("Area of Square is " + s);
}
18.Write a program which will use the concept of method overloading and
calculate volume of a Box. Box class have following properties:
Data Members:
Width
Height Depth Method: volume() having three argument
volume() having two argument
display()
Note: volume = (width*height*depth)
//18.program
import java.util.*;
import java.lang.*;
v = h * w * d;
}
void volume(int h,int w)
{
height = h;
width = w;
m = h * w;
}
void display()
{
System.out.println("Volume of Box Is: " + v);
System.out.println("Volume Is: " + m);
}
}
//program 19.
import java.io.*;
import java.lang.*;
import java.util.*;
class StringComparison
{
static String str1, str2;
else
{
System.out.println("\nBoth String are not Equal..!");
}
}
}
20.Create two class one is Friend and another is Buddy, Friend class has
data member like name and email-id, and Buddy class inherit the
properties of Friend class and it has data member like dob, mobile no and
address and do the following operation.
1. Create three constructor for base and derived class.
2. Insert data through derived class parameterized constructor.
3. Illustrate the use of this reference and Super.
Display all the information through parent class reference variable.
Ans 20)
class Friend{
public String name;
public String email;
public Buddy(String name, String email, String dob, long mno, String
ad){
super(name, email);
this.dob = dob;
this.mno = mno;
this.ad = ad;
}
class VehicleDemo
{
public static void main(String arg[])
{
Twowheeler t1;
Fourwheeler f1;
t1=new Twowheeler("Suzuki", "Access","Non-Gear");
f1=new Fourwheeler("Honda","Amaze","Petrol",4);
t1.display();
f1.display();
}
}
22. Create three class Employee, Teaching,
Nonteaching; In Employee class data members are employee no, name and
there are two abstract methods set and get. Teaching class inherit the
properties of Employee and its data members are course, designation and
salary. Nonteaching class inherits the properties of Employee class and its
data members are department, designation and salary. Write a program to
display employee information according to designation wise.
ANS 22
import java.util.*;
void set(){
Scanner sc = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
System.out.println("Enter eno:- ");
super.eno = sc.nextInt();
System.out.println("Enter name:- ");
super.name = sc1.nextLine();
System.out.println("Enter course:- ");
this.course = sc1.nextLine();
System.out.println("Enter designation:- ");
this.designation = sc1.nextLine();
System.out.println("Enter salary:- ");
this.salary = sc1.nextInt();
void get(){
System.out.println("En no.:- "+super.eno);
System.out.println("Name:- "+super.name);
System.out.println("Course:- "+this.course);
System.out.println("designation:- "+this.designation);
System.out.println("salary:- "+this.salary);
}
}
void set(){
Scanner sc = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
System.out.println("Enter eno:- ");
super.eno = sc.nextInt();
System.out.println("Enter name:- ");
super.name = sc1.nextLine();
System.out.println("Enter department:- ");
this.department = sc1.nextLine();
System.out.println("Enter designation:- ");
this.designation = sc1.nextLine();
System.out.println("Enter salary:- ");
this.salary = sc1.nextInt();
void get(){
System.out.println("En no.:- "+super.eno);
System.out.println("Name:- "+super.name);
System.out.println("Course:- "+this.department);
System.out.println("designation:- "+this.designation);
System.out.println("salary:- "+this.salary);
}
}
class prac22
{
public static void main(String arg[])
{
Teaching t1;
Non_Teaching n;
t1=new Teaching();
n=new Non_Teaching();
System.out.println("Teaching class:- ");
t1.set();
t1.get();
System.out.println("Non Teaching class:- ");
n.set();
n.get();
}
}
24. Create three interfaces and one class called Hockey, Cricket, Tennis
and Player respectively. Player class inherits the properties of Hockey,
Cricket and Tennis.
In Hockey; method is h(), in Cricket;method is c(),and in Tennis method
is t(); all the methods insert no of player required for playing the
particular game.
Player class have data members player_name and type_of_sport and
method insertInfo() and display().
Write a menu driven program which will ask user choice for the sports and
according to user choice call appropriate method to insert no of player
required. After getting the information of sports and no of player required
call insertInfo() which will take players name. After performing all the
necessary operation display information of sports and player in a proper
format.
//25.program
import java.util.*;
import java.lang.*;
System.out.println(str.length());
char c = str.charAt(4);
c = str.charAt(4);
//System.out.println(c);
}
catch(StringIndexOutOfBoundsException e)
{
System.out.println("Can not Enter More Than Four
Character.");
}
}
}
26. Write a program which will take adhaar number from the user, if
adhaar number is not start with „I‟ then throw the exception. (Assume
that Adhaar number starts with „I‟, use the concept of user define
exception)
27. Write a program to create custom exception for bank that contain
fields accountno, and balance throw an exception for the following
condition:
a. If withdrawing amount is greater than actual amount.
If balance become less than 500 after withdrawing amount.
class Number{
public void Factorial(){
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
while(num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
30. Write a java program which will insert data from the console and write
into file.
//30.program
import java.io.*;
import java.lang.*;
import java.util.*;
w.write(str + no);
w.close();
System.out.println("Done.");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
31. Write a java program which will read data from the file and display on
console.
//31.PROGRAM
import java.util.Scanner;
import java.io.*;
public class JavaProgram
{
public static void main(String[] input)
{
String fname;
Scanner scan = new Scanner(System.in);
BufferedReader bufferedReader=new
BufferedReader(fileReader);
bufferedReader.close();
}
catch(IOException ex)
{
System.out.println("Error reading file named '" + fname + "'");
}
}
}
32. Write a java program which will count total number of character, word
and line.
ANS 32
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
int charCount = 0;
int wordCount = 0;
int lineCount = 0;
try
{
lineCount++;
currentLine = reader.readLine();
}
33. Write a java program which will read data from one file and store it
into another file.
ANS 33
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
34. Write a program which accept name from the user and display one by
one character of that name using thread sleep method.
ANS
import java.io.*;
class prac34
{
public static void main(String[] args)throws IOException
{
boolean stop=false;
String str="Sreedhar Practice seassion";
ANS 35
import java.util.concurrent.*;
class GFG {
public static void main(String args[])
{
// Get time to sleep
long timeToSleep = 10L;
try {
catch (InterruptedException e) {
System.out.println("Interrupted "
+ "while Sleeping");
}
}
}
36. Write a program in Java to launch 5 threads. Each thread increments a
counter variable. Display value of thread using the concept of
synchronization.
ANS
public class Counter
{
}
}
}
37. Write a program to create an applet which displays the following
message. Welcome
to Java. Above three line must be displayed in three different colours style.
Ans
import java.awt.Graphics;
import java.applet.Applet;
publicclass prac38 extends Applet
{
publicvoid paint(Graphics g)
{
g.drawString("Welcome",400,200);
g.drawString("to",400,300);
g.drawString("java",400,400);
}
}
38. Write a program to create an applet and draw different shapes like
rectangle, circle, oval and line with different colours.
// 38.Program
//appletviewer FirstApplet.java
import java.applet.Applet;
import java.awt.Graphics;
import java.util.*;
import java.io.*;
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.fillOval(170,200,30,30);
g.drawCircle(90,150,30,30,30,270);
g.fillCircle(270,150,30,30,0,180);
}
}
/*
<applet code="FirstApplet" height="300" width="500"></applet>
*/
39. Write a program to design simple calculator using applet.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
b1=new Button("Add");
b2=new Button("Sub");
b3=new Button("Mul");
b4=new Button("Div");
l3=new Label("Result");
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(b2);
add(b3);
add(b4);
add(l3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}