Practical No 1
Aim of Practical – Write Java applications to print the given patterns
A
. 10101 B. 1
0101 232
101 34543
01 4567654
1
567898765
Objectives –Familiarity
with looping structure and conditional
statements in java
Code:
package
samyak;
public
class patternA {
public
static void main(String[] args) {
for (int i = 5; i >= 1; i--) {
for (int j = i; j >= 1; j--) {
if ((i + j) % 2 == 0) {
System.out.print("0");
} else {
System.out.print("1");
}
}
System.out.println();
}
}
}
Program 2-
package
samyak;
public
class patternB {
public
static void main(String[] args) {
int n = 5;
int i, j, num = 1, gap;
gap = n - 1;
for (j = 1; j <= n; j++) {
num = j;
for (i = 1; i <= gap; i++) {
System.out.print(" ");
}
gap--;
for (i = 1; i <= j; i++) {
System.out.print(num);
num++;
}
num--;
num--;
for (i = 1; i < j; i++) {
System.out.print(num);
num--;
}
System.out.println();
}
}
}
}
Output1-
Conclusion – –I learn looping structure
and conditional statement and print pattern.
Practical No 2
Aim
of Practical: Write a program that accepts integer input and
convert the given integer number to Binary or Hexadecimal.
If 0 is
passed from the command line then convert the given integer number to binary
and if 1 is passed from the command line then convert the given integer to
hexadecimal.
Command
Line Input: 1 Input : 90 Output: 5A
Here, 1
is passed from the command line and 90 is given as input to the program. Since command
line input is 1, the given number 90 is converted to hexadecimal 5A
Code:
package
samyak;
import
java.util.Scanner;
public
class BinaryHexa {
public static void main(String[] args) {
try (Scanner XY = new Scanner(System.in)) {
System.out.print("Enter
the Number : ");
int INT= XY.nextInt();
System.out.print("Coversion to the
Binary is : ");
binary(INT);
System.out.print(" \nCoversion to the
Hexadecimal is : ");
hexa(INT);
}
}
static void binary (int num) {
int a=0;
int abc[]=new int[100];
abc[0]=0;
while(num>0) {
abc[a++]=num%2;
num=num/2;
}
for(int b=a-1; b>=0; b--) {
System.out.print(abc[b]);
}
}
static void hexa (int num) {
int a=0;
int abc[]=new int[100];
abc[0]=0;
while(num>0) {
abc[a++]=num%16;
num=num/16;
}
for(int b=a-1; b>=0; b--) {
if(abc[b]==10)
{
System.out.print('A');
}
else if(abc[b]==11)
{
System.out.print('B');
}
else if(abc[b]==12)
{
System.out.print('C');
}
else if(abc[b]==13)
{
System.out.print('D');
}
else if(abc[b]==14)
{
System.out.print('E');
}
else if(abc[b]==15)
{
System.out.print('F');
}
else
System.out.print(abc[b]);
}
}
}
Observation /Output –
Conclusion –I
learn the concept of convert integer to binary and hexadecimal.
Practical No 3
Name of Practical Write an application in Java which
reads a string from user as a command line argument and checks the string for vowels
and prints the string without the vowels. Ex:Input:
Note:Use
your name as input
Code:
package samyak;
public class vowels {
public
static void main(String args[]) {
String
s=args[0];
for(int
i=0;i<s.length();i++)
{
if(s.charAt(i)=='a'||s.charAt(i)=='e'||s.charAt(i)=='i'||s.charAt(i)=='o'||s.charAt(i)=='u'||
s.charAt(i)=='A'||s.charAt(i)=='E'||s.charAt(i)=='I'||s.charAt(i)=='O'||s.charAt(i)=='U')
{
continue;
}
else
{ System.out.print(s.charAt(i));
}
}
}
}
Observation /Output
–
Conclusion – I learn the concept of command line
argument and print string without vowels.
Practical No 4
Aim of Practical – WAP
that has a class with overloaded member functions(add). One add takes double arguments and the other takes int
arguments. The add member function
should display all the arguments it
takes and also display their sumRun
the program by providing different number of arguments(NOTE: use varargs). Run the program atleast
10 times with different number of arguments and take 10 outputs.
Objectives – Understand the var args
in Java .
Code:
package samyak;
public class overloading {
int
a, b, sum = 0;
double
a1, b1, sum1 = 0;
public
int add(int... a) {
for
(int i : a) {
sum
= sum + i;
}
return
sum;
}
public
double add(double... b) {
for
(double i : b) {
sum1
= sum1 + i;
}
return
sum1;
}
public
static void main(String[] args) {
overloading
obj = new overloading();
//
overloding obj2 = new overloadig();
System.out.println("Addition
of double argument = " + obj.add(2,5,3));
System.out.println("Addition
of double argument = " + obj.add(4,1,4));
System.out.println("Addition
of double argument = " + obj.add(12.5, 15.5));
System.out.println("Addition
of double argument = " + obj.add(12.5,15));
System.out.println("Addition
of double argument = " + obj.add(19.5,17.8,19));
System.out.println("Addition
of double argument = " + obj.add(12.5, 15.5,17,18,12.5));
System.out.println("Addition
of double argument = " + obj.add(12.5, 15.5,13.2,12.4));
System.out.println("Addition
of double argument = " + obj.add(12.5, 15.5));
System.out.println("Addition
of double argument = " + obj.add(14.5, 15.5));
System.out.println("Addition
of double argument = " + obj.add(12.5, 15.5));
}
}
Observation /Output
–
Conclusion – Program of overloading add() function is successfully
executed
Practical No 5
Aim of Practical-Create an abstract class Figure3d
with a data member dim1 and an abstract function vol(). Create 2 classes sphere
and cylinder inherit Figure3d. These classes should implement the vol()
function. Add this program to a package. Execute it from within and outside the
package. .(Hint: Volume of sphere=4/3*pi*r*r*r, volume of cylinder=pi*r*r*h,
volume of cone=1/3*pi*r*r*h).
Objectives –Understand inheritance
Understand abstract classes
Understand
packages
Understand
classpath
Code:
package abstractprac1;
abstract public class figure3d {
public
abstract void vol();
}
package abstractprac1;
public class Cylinder extends figure3d {
final double pi=3.14;
double dim1,h;
public Cylinder (double dim1,double h) {
this.dim1=dim1;
this.h=h;
}
public void vol() {
double
v = pi*dim1*dim1*h;
System.out.println("volum
of cylinder : "+v);
}
}
package samyak;
import abstractprac1.*;
public class Javaprac3 {
public
static void main(String[] args) {
//
TODO Auto-generated method stub
Sphere
vk= new Sphere(2.5);
Cylinder
cy=new Cylinder(4.5,5.4);
vk.vol();
cy.vol();
}
}
Observation /Output
–
Conclusion– Program
of abstraction is successfully executed.
Practical No. 6
Name of Practical
– WAP in java that creates an
interface figure2d with two data members dim1 and dim2 and member function
area().Write two classes named "rectangle" and "triangle" that implement the above
interface and display area of the figure.
Objectives –Understand interfaces
classes
Understand method
overriding
Code:
package samyak;
public interface figure2d {
public
void area();
}
package samyak;
import java.util.Scanner;
public class rectangle implements figure2d{
public
void area() {
Scanner
sc = new Scanner(System.in);
int
l,b,area;
System.out.println("Enter
length = ");
l
= sc.nextInt();
System.out.println("Enter
width = ");
b
= sc.nextInt();
area
= l*b;
System.out.println("Area
of rectangle = "+area);
}
public
static void main(String [] args) {
rectangle
rc = new rectangle();
rc.area();
}
}
package samyak;
import java.util.Scanner;
public class triangle implements figure2d{
@Override
public
void area() {
int
b,h;
float
area;
try
(Scanner sc = new Scanner(System.in)) {
System.out.println("Enter
base : ");
b
= sc.nextInt();
System.out.println("Enter
height : ");
h
= sc.nextInt();
}
area
= (float) (0.5*b*h);
System.out.println("Area
of triangle = "+area);
}
public static void main(String [] args) {
triangle
t = new triangle();
t.area();
}
}
Observation /Output
–
Triangle:-
Rectangle:-
Conclusion – Program of interface executed successfully
Practical No 7
Name of Practical
– Write a program in java
that generates 10 random numbers and divides them.Anticipate the kind of
exception that will be generated and catch it.
Objectives –Understand exception
handling in java
Use the Random class of java.util
Code:
package samyak;
import java.util.Random;
public class RandomNumber {
public
static void main(String [] args) {
int
[] arr= new int [10];
int
[] brr= new int [10];
int
[] crr= new int [10];
Random no = new Random();
for(int i=0;i<arr.length;i++)
arr[i] = no.nextInt(10);
for(int i=0;i<arr.length;i++)
brr[i] = no.nextInt(10);
try {
for(int i=0;i<crr.length;i++) {
crr[i] = arr[i]/brr[i];
}
}catch(ArithmeticException e) {
System.out.println("Number cannot devide
by zero...");
}
System.out.println("random nuumber
in first array ...");
for(int i=0;i<crr.length;i++) {
System.out.print(" "+arr[i]);
}
System.out.println();
System.out.println("random nuumber
in second array ...");
for(int i=0;i<crr.length;i++)
System.out.print(" "+brr[i]);
System.out.println();
System.out.println("divison of two array
... ");
for(int i=0;i<crr.length;i++)
System.out.print(" "+crr[i]);
}
}
Observation /Output
–
Conclusion –
Program of exception handling is successfully executed.
Name of Practical –WAP
in java that creates two threads, sets their priorities(high and low) and shows
the number of cpu cycles alloted to each thread. Make use of join() method.
Objectives –Understand priority
scheduling of threads
Understand join method of Thread class
Code:
package
samyak;
class
Thread1 extends Thread{
private
volatile boolean running=true;
long
count=0;
Thread1(String
name,int priority){
super(name);
setPriority(priority);
start();
}
public
void run(){
//thread
run infinitely
while(running){
count++;
}//while
}
void
pause(){
running=false;
}
}
public
class javathread {
public static void main(String
args[]) throws InterruptedException{
Thread1 th1=new
Thread1("LowPriorityThread",1);
Thread1 th2=new
Thread1("HighPriorityThread",10);
Thread.sleep(2000);
th2.pause();
th1.pause();
th1.join();
th2.join();
System.out.println("Cycles for Low
priority thread are "+ th1.count);
System.out.println("Cycles for High
priority thread are "+ th2.count);
}
}
Observation /Output –
Conclusion
– The program of threading is successfully executed.
Practical No 9
Name of PracticalWAP
in java to display the use of
a.
synchronized
method
b.
synchronized
block
Objectives – understand synchronized
method and synchronized block
Code:
package
samyak;
class
Printer {
void print(String name) {
System.out.print("Name
is [");
System.out.println(name
+ "]");
}
}//
Printer
class
YourThread extends Thread {
Printer p;
YourThread(String name, Printer p) {
super(name);
this.p = p;
start();
}
public void run() { // hread is
running
synchronized (p) {
p.print(getName());
}
}
}
public
class SynchronizedDemo {
public static void main(String[]
args) {
Printer p = new
Printer(); // shared resource/object
YourThread t1 = new
YourThread("Samyak", p);
YourThread t2 = new
YourThread("Nagdive", p);
}
}
Observation/Output-
Conclusion- The program of synchronization block and method is successfully execute.
Practical No 10
Name of Practical- WAP in Java to copy the contents of
one file to another without using any looping statements. Read the names of the
files from the command line.
Objectives –Understand I/O in java
Understand file
handling in java
Use command line
arguments
Code:
package
samyak;
import
java.io.*;
public
class copyfile {
public static void main(String args[]) throws
IOException,FileNotFoundException {
FileInputStream fin=new FileInputStream
("C:\\Users\\lenovo\\Dropbox\\PC\\Desktop\\samyak1.txt");
FileOutputStream fout=new
FileOutputStream ("C:\\Users\\lenovo\\Dropbox\\PC\\Desktop\\New
Text Document (3).txt");
int size =fin.available();
byte b[]=new byte[size];
fin.read(b);
fout.write(b);
}
}
Observation/
Output-
Conclusion- The
program of file transfer without using loop is successfully exrcuted.
Practical
No 11
Name
of Practical – WAP in
Java that reads and displays its own contents.
Objectives – Understand File IO in
java
Code:
package samyak;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public
static void main(String [ ] args) throws IOException {
FileInputStream
fin=new
FileInputStream("C:\\Users\\lenovo\\eclipse-workspace\\samyak\\src\\samyak\\figure2d.java");
int
c;
while((c=fin.read())!=-1)
{
System.out.write(c);
}
}
}
Output-
Conclusion – The
program of copy its own content is successfully executed.