CORE Java Practical Journal

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 26

Oriental Education Society’s

SANPADA COLLEGE OF COMMERCE & TECHNOLOGY


SECTOR - 2, PLOT - 3/4/5, ADJACENT SANPADA RAILWAY
STATION,
SANPADA (W), NAVI MUMBAI - 400 705.
DEPARTMENT OF INFORMATION TECHNOLOGY

CERTIFICATE

This is to certify that Mr/Ms. _____Saiyed Tabrejalam____ of


Class S.Y.B.Sc.IT bearing Roll No. ___47___ of Semester IV has
successfully completed the Assignment / Practical work in the
subject of “Core Java” during the academic year 2021 - 2022
under the guidance of Prof. Neha Shaikh being the partial
requirement for the fulfillment of the curriculum of Degree of
Bachelor of Science in Information Technology, University of
Mumbai.
Place : Sanpada
Date :
SANPADA COLLEE OF COMMERCE & TECHNOLOGY

Department of Information Technology


S.Y. B.SC IT. (SEMESTER IV) INFORMATION TECHNOLOGY
PRACTICAL JOURNAL 2021-2022

INDEX
SR.
AIM DATE SIGN REMARKS
NO.
1 Practical 1

2 Practical 2

3 Practical 3

4 Practical 4

5 Practical 5

6 Practical 6

7 Practical 7

8 Practical 8
PRACTICAL: - 1
A] Write a Java program that takes a number as input and prints its
multiplication table up to 10.

import java.util.Scanner;

public class Test

public static void main(String[] args)

{ Scanner in = new Scanner(System.in);

System.out.print("Input a number: "); int

num1 = in.nextInt();

for (int i=0; i< 10; i++)

System.out.println(num1 + " x " + (i+1) + " = " + (num1

* (i+1)));

Output :-
B] Write a java program to display the following pattern.

*****

****

***

**

public class Test1


{
public static void main(String[] args)
{
int i, j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(“*”);
}
System.out.println();
}
}
}

Output:-

C]
Write a
java
program
to print
the area
and

perimeter of a circle.

class Test2

public static void main(String[] args)

{ double radius = 7.5;

double PI=3.14;

double perimeter = 2 * PI * radius;

double area = PI * radius * radius;


System.out.println( System.out.println("Perimeter is = " + perimeter);

"Area is = " + area);

Output:-

PRACTICAL: - 2
A] Write a java program to convert decimal number into binary and vice versa.

import java.util.*;

class test2

public static void main(String args[])

Scanner in=new Scanner(System.in);

System.out.println("Enter decimal no"); int

d=in.nextInt();

System.out.println("Binary no of "+d+"is");

System.out.println(Integer.toBinaryString(d));
System.out.println("Enter binary no"); String

b=in.next(); System.out.println("decimal no

of"+d+"is");

System.out.println(Integer.parseInt(b,2));

OUTPUT:-

B] Write a java program to reverse a string.

class test3

public static void main(String args[])

String name="SYIT";

int l=name.length();

String rev="";

for(int i=l-1;i>=0;i--)

rev=rev+name.charAt(i);

}
System.out.println("Reverse of "+name+"is "+rev);

Output:-

PRACTICAL: - 3
A] Write a java program to implement 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:-

B]
B]
B]
B]
B]
B]
B]
B]
Write a java program to implement single level inheritance.

class Employee
{
float salary=40000;
}

class Programmer extends Employee


{
int bonus=10000;
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:-
C] Write a java program to implement multiple inheritance:

interface Printable
{
void print();
}

interface Showable
{
void show();
}

class A7 implements Printable,Showable


{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");
}
public static void main(String args[])
{
A7 obj = new A7();
obj.print();
obj.show();
}
}
Output:-
PRACTICAL: - 4
A. Designed a class that demonstrates the use of constructor and destructor.
class cons
{
cons()
{
System.out.println("Hello");
}

public static void main(String args[])


{
cons c1=new cons();
c1=null; System.gc();
}

public void finalize()


{
System.out.println("Destroyed");
}
}
Output:-
B. Write a java program to demonstrate the implementation of abstract class.
abstract class calc
{
abstract int sqr(int n1);
abstract int cube(int n1);
void show()
{
System.out.println("Hello");
}
}
class pract4 extends calc
{
int sqr(int n1)
{
return n1*n1;
}
int cube(int n1)
{
return n1*n1*n1;
}
public static void main(String args[])
{
pract4 p1=new pract4();
System.out.println(p1.sqr(333));
System.out.println(p1.cube(444));
p1.show();
}
}
Output:-
C. Designed a class SortData that contains the method asec() and desc().
import java.util.Arrays;
import java.util.Collections;
public class SortData
{
void asec()
{
// sample int[] array
Integer[] intArray = {1975, 2003, 1979, 1992, 1983, 1999, 1987};
// before sorting
System.out.println("Integer[] Array - before sorting : ");
for(Integer iValue : intArray) { System.out.println(iValue);
}
// sorting int[] array in ascending order.
Arrays.sort(intArray);
// after sorting
System.out.println("\nInteger[] Array - after sorting in ascending order : ");
for(Integer iValue : intArray) {
System.out.println(iValue);
}
}
void desc()
{
// sample int[] array
Integer[] intArray = {1975, 2003, 1979, 1992, 1983, 1999, 1987};
// before sorting
System.out.println("Integer[] Array - before sorting : ");
for(Integer iValue : intArray) { System.out.println(iValue);
}
// sorting int[] array in descending order
Arrays.sort(intArray, Collections.reverseOrder());
// after sorting
System.out.println("\nInteger[] Array - after sorting in descending order : ");
for(Integer iValue : intArray) {
System.out.println(iValue);
}
}

public static void main(String[] args)


{
SortData ai=new SortData();
SortData a2=new SortData();
ai.asec();
a2.desc();
}
}

Output:-
PRACTICAL: - 5
A. Create a package, add the necessary classes and import the package in
java class.
\\PACKAGE :
package mypack;
public class a
{
public void display()
{
System.out.println("SYIT");
}
}

\\CLASS:
import mypack.*;
class pack
{
Public static void main(String args[])
{
a x= new a();
x.display();
}
}

Output:

B. Write a java program to add two matrices and print the resultant matrix.
import java.util.Scanner; class
AddTwoMatrix
{
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in); System.out.println("Enter the number of
rows and columns of matrix");
m = in.nextInt(); n =
in.nextInt();
int first[][] = new int[m][n];
int second[][] = new int[m][n];
int sum[][] = new int[m][n];
System.out.println("Enter the elements of first matrix"); for ( c = 0 ; c <
m ; c++ )
for ( d = 0 ; d < n ; d++ ) first[c]
[d] = in.nextInt();
System.out.println("Enter the elements of second matrix"); for ( c = 0 ; c <
m ; c++ )
for ( d = 0 ; d < n ; d++ ) second[c]
[d] = in.nextInt(); for ( c = 0 ; c <
m ; c++ ) for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d]; System.out.println("Sum
of entered matrices:-"); for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
System.out.print(sum[c][d]+"\t");
System.out.println();
}
}
}

Output:

PRACTICAL :- 6
A) Write a java program to implement the vectors. import
java.util.Iterator;
import java.util.Vector;
public class SimpleVectorExample
{
public static void main(String[] args)
{
//create a Vector object Vector
v = new Vector(); v.add("1");
v.add("2");
v.add("3");
System.out.println("Getting elements of Vector");
System.out.println(v.get(0)); System.out.println(v.get(1));
System.out.println(v.get(2));
}
}

OUTPUT:-

B. Write
a java
program
to

implement thread life cycle.

public class ThreadDemo extends Thread


{
public void run()
{
System.out.println("Thread is running !!");
}
public static void main(String[] args)
{
ThreadDemo t1 = new ThreadDemo(); ThreadDemo t2 =
new ThreadDemo(); System.out.println("T1 ==> " +
t1.getState()); System.out.println("T2 ==> " +
t2.getState()); t1.start();
System.out.println("T1 ==> " + t1.getState());
System.out.println("T2 ==> " + t2.getState()); t2.start();
System.out.println("T1 ==> " + t1.getState());
System.out.println("T2 ==> " + t2.getState());
}
}

OUTPUT:-

C. Write
a java
program
to

implement multithreading.

class A extends Thread


{
public void run()
{
System.out.println("Thread A");
System.out.println("i in Thread A "); for(int
i=1;i<=5;i++)
{
System.out.println("i = " + i);
}
System.out.println("Thread A Completed.");
}
}

class B extends Thread


{

public void run()


{
System.out.println("Thread B");
System.out.println("i in Thread B "); for(int
i=1;i<=5;i++)
{
System.out.println("i = " + i);
}
System.out.println("Thread B Completed.");
}
}
public class Main1
{
public static void main(String[] args)
{
//life cycle of Thread
// Thread's New State
A threadA = new A(); B
threadB = new B();
threadA.start();
threadB.start(); System.out.println("Main
Thread End");
}
}

OUTPUT:-

PRACTICAL-7
A. Write a Java program to count the letters, spaces, numbers and other
characters of an input string.

import java.util.Scanner;
public class Exercise38

public static void main(String[] args)

String test = "core java";

count(test);

public static void count(String x)

char[] ch = x.toCharArray(); int

letter = 0;

int space = 0;

int num = 0;

int other = 0;

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

if(Character.isLetter(ch[i]))

letter ++ ;

else if(Character.isDigit(ch[i]))
{

num ++ ;

else if(Character.isSpaceChar(ch[i]))

{
space ++ ;

else

other ++;

System.out.println("The string is : core java");

System.out.println("letter: " + letter); System.out.println("space: " + space);


System.out.println("number: " + num); System.out.println("other: " + other);

OUTPUT:

B. Implement a Java function that calculates the sum of digits for a given char array
consisting of the digits '0' to '9'. The function should return the digit sum as a long
value.

import java.util.Scanner;

public class SumOfDigits

public static void main(String args[])

{
Scanner sc = new Scanner(System.in);

System.out.println("Please enter a number to calculate sum of digits"); int number =

sc.nextInt();

int sum = 0;

int input = number;

while (input != 0)

int lastdigit = input % 10;

sum += lastdigit;

input /= 10;

System.out.printf("Sum of digits of number %d is %d", number, sum); sc.close();

}}

OUTPUT:

C. Find the smallest and largest element from the array.

public class LargestSmallest

public static void main(String[] args)

int a[] = new int[] { 23, 34, 13, 64, 72, 90, 10, 15, 9, 27 };

int min = a[0]; // assume first elements as smallest number int max =

a[0]; // assume first elements as largest number for (int i = 1; i <


a.length; i++)

if (a[i] > max)

max = a[i];

if (a[i] < min)

{ min = a[i];

System.out.println("Largest Number in a given array is : " + max);


System.out.println("Smallest Number in a given array is : " + min);

}}

OUTPUT:

PRACTICAL NO: - 8
A) Write a java program to open a file and display the contents in the console
window.

import java.io.*;

import java.io.FileInputStream; class

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

{
FileInputStream fin=new FileInputStream("E:\\PRACT8\\a.txt"); int i=0;

while((i=fin.read())!=-1)

System.out.print((char)i);

fin.close();

Output:

B) Write a java program to copy the contents from one file to other file. import

java.io.*;

public class B

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

FileInputStream fin=new FileInputStream("E:\\b.txt");

FileOutputStream fout=new FileOutputStream("E:\\b1.txt"); int c;

while((c=fin.read())!=-1)

fout.write(c);
System.out.print((char)c);

fout.close();

fin.close();

OUTPUT:-

C) Write a java program to read the student data from user and store it in the file.

import java.io.*;

import java.util.*;

public class C

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

Scanner scan=new Scanner(System.in);

System.out.println("Enter roll no");

int roll=scan.nextInt(); System.out.println("Enter

student name"); String studName=scan.next();

System.out.println("Enter marks"); Double

studMarks=scan.nextDouble(); FileWriter f=new

FileWriter("n.txt",true); f.write("Roll no"+roll);

f.write("\n");
f.write("student name"+studName);

f.write("\n");

f.write("student marks:"+studMarks);

scan.close();

f.close();

}}
Output:-

You might also like