0% found this document useful (0 votes)
572 views26 pages

Core Java Practical Assignments

The document is a certificate certifying that Mr. Saiyed Tabrejalam of Class S.Y.B.Sc.IT, Roll Number 47, 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 as a partial requirement to fulfill the curriculum for a Bachelor of Science in Information Technology from the University of Mumbai.

Uploaded by

tabrej saiyed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
572 views26 pages

Core Java Practical Assignments

The document is a certificate certifying that Mr. Saiyed Tabrejalam of Class S.Y.B.Sc.IT, Roll Number 47, 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 as a partial requirement to fulfill the curriculum for a Bachelor of Science in Information Technology from the University of Mumbai.

Uploaded by

tabrej saiyed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd

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 [Link] 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. [Link] 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 [Link];

public class Test

public static void main(String[] args)

{ Scanner in = new Scanner([Link]);

[Link]("Input a number: "); int

num1 = [Link]();

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

[Link](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++)
{
[Link](“*”);
}
[Link]();
}
}
}

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;


[Link]( [Link]("Perimeter is = " + perimeter);

"Area is = " + area);

Output:-

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

import [Link].*;

class test2

public static void main(String args[])

Scanner in=new Scanner([Link]);

[Link]("Enter decimal no"); int

d=[Link]();

[Link]("Binary no of "+d+"is");

[Link]([Link](d));
[Link]("Enter binary no"); String

b=[Link](); [Link]("decimal no

of"+d+"is");

[Link]([Link](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=[Link]();

String rev="";

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

rev=rev+[Link](i);

}
[Link]("Reverse of "+name+"is "+rev);

Output:-

PRACTICAL: - 3
A] Write a java program to implement method overriding: class
Vehicle
{
void run()
{
[Link]("Vehicle is running");
}
}

class Bike2 extends Vehicle


{
void run()
{
[Link]("Bike is running safely");
}
public static void main(String args[])
{
Bike2 obj = new Bike2();
[Link]();
}
}
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();
[Link]("Programmer salary is:"+[Link]);
[Link]("Bonus of Programmer is:"+[Link]);
}
}

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(){[Link]("Hello");}
public void show(){[Link]("Welcome");
}
public static void main(String args[])
{
A7 obj = new A7();
[Link]();
[Link]();
}
}
Output:-
PRACTICAL: - 4
A. Designed a class that demonstrates the use of constructor and destructor.
class cons
{
cons()
{
[Link]("Hello");
}

public static void main(String args[])


{
cons c1=new cons();
c1=null; [Link]();
}

public void finalize()


{
[Link]("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()
{
[Link]("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();
[Link]([Link](333));
[Link]([Link](444));
[Link]();
}
}
Output:-
C. Designed a class SortData that contains the method asec() and desc().
import [Link];
import [Link];
public class SortData
{
void asec()
{
// sample int[] array
Integer[] intArray = {1975, 2003, 1979, 1992, 1983, 1999, 1987};
// before sorting
[Link]("Integer[] Array - before sorting : ");
for(Integer iValue : intArray) { [Link](iValue);
}
// sorting int[] array in ascending order.
[Link](intArray);
// after sorting
[Link]("\nInteger[] Array - after sorting in ascending order : ");
for(Integer iValue : intArray) {
[Link](iValue);
}
}
void desc()
{
// sample int[] array
Integer[] intArray = {1975, 2003, 1979, 1992, 1983, 1999, 1987};
// before sorting
[Link]("Integer[] Array - before sorting : ");
for(Integer iValue : intArray) { [Link](iValue);
}
// sorting int[] array in descending order
[Link](intArray, [Link]());
// after sorting
[Link]("\nInteger[] Array - after sorting in descending order : ");
for(Integer iValue : intArray) {
[Link](iValue);
}
}

public static void main(String[] args)


{
SortData ai=new SortData();
SortData a2=new SortData();
[Link]();
[Link]();
}
}

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()
{
[Link]("SYIT");
}
}

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

Output:

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

Output:

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

OUTPUT:-

B. Write
a java
program
to

implement thread life cycle.

public class ThreadDemo extends Thread


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

OUTPUT:-

C. Write
a java
program
to

implement multithreading.

class A extends Thread


{
public void run()
{
[Link]("Thread A");
[Link]("i in Thread A "); for(int
i=1;i<=5;i++)
{
[Link]("i = " + i);
}
[Link]("Thread A Completed.");
}
}

class B extends Thread


{

public void run()


{
[Link]("Thread B");
[Link]("i in Thread B "); for(int
i=1;i<=5;i++)
{
[Link]("i = " + i);
}
[Link]("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();
[Link]();
[Link](); [Link]("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 [Link];
public class Exercise38

public static void main(String[] args)

String test = "core java";

count(test);

public static void count(String x)

char[] ch = [Link](); int

letter = 0;

int space = 0;

int num = 0;

int other = 0;

for(int i = 0; i < [Link](); i++)

if([Link](ch[i]))

letter ++ ;

else if([Link](ch[i]))
{

num ++ ;

else if([Link](ch[i]))

{
space ++ ;

else

other ++;

[Link]("The string is : core java");

[Link]("letter: " + letter); [Link]("space: " + space);


[Link]("number: " + num); [Link]("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 [Link];

public class SumOfDigits

public static void main(String args[])

{
Scanner sc = new Scanner([Link]);

[Link]("Please enter a number to calculate sum of digits"); int number =

[Link]();

int sum = 0;

int input = number;

while (input != 0)

int lastdigit = input % 10;

sum += lastdigit;

input /= 10;

[Link]("Sum of digits of number %d is %d", number, sum); [Link]();

}}

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 <


[Link]; i++)

if (a[i] > max)

max = a[i];

if (a[i] < min)

{ min = a[i];

[Link]("Largest Number in a given array is : " + max);


[Link]("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 [Link].*;

import [Link]; class

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

{
FileInputStream fin=new FileInputStream("E:\\PRACT8\\[Link]"); int i=0;

while((i=[Link]())!=-1)

[Link]((char)i);

[Link]();

Output:

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

[Link].*;

public class B

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

FileInputStream fin=new FileInputStream("E:\\[Link]");

FileOutputStream fout=new FileOutputStream("E:\\[Link]"); int c;

while((c=[Link]())!=-1)

[Link](c);
[Link]((char)c);

[Link]();

[Link]();

OUTPUT:-

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

import [Link].*;

import [Link].*;

public class C

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

Scanner scan=new Scanner([Link]);

[Link]("Enter roll no");

int roll=[Link](); [Link]("Enter

student name"); String studName=[Link]();

[Link]("Enter marks"); Double

studMarks=[Link](); FileWriter f=new

FileWriter("[Link]",true); [Link]("Roll no"+roll);

[Link]("\n");
[Link]("student name"+studName);

[Link]("\n");

[Link]("student marks:"+studMarks);

[Link]();

[Link]();

}}
Output:-

Common questions

Powered by AI

Method overriding in the Java program is demonstrated by creating a `Bike2` class that extends the `Vehicle` class. The `run` method in the `Vehicle` class is overridden in the `Bike2` class to provide specific functionality - printing "Bike is running safely" instead of the generic message from the `Vehicle` class. The significance of this process lies in polymorphism, allowing an object of the derived class, `Bike2`, when invoking `run`, to use its own method implementation rather than that of the base class. This demonstration shows how behavior of inherited classes can be customized, enhancing flexibility and robustness in object-oriented design .

The implementation of threads in the Java programs demonstrates basic thread behavior and state management without explicit synchronization. Threads are created by extending the `Thread` class, and their lifecycle—from 'New' to 'Runnable' and 'Terminated'—is showcased using console output of the thread states. The program lacks synchronization mechanisms, implying threads run independently, potentially leading to race conditions if shared resources were involved. This highlights the need for synchronization primitives like `synchronized` blocks or `java.util.concurrent` utilities in more complex threading scenarios to ensure thread safety and data consistency .

The Java program demonstrates file handling by enabling reading user-entered student data and writing it into a file with `FileWriter`. This involves collecting input, such as roll number, name, and marks, and storing it in a file using `write()`. Java’s extensive I/O functionalities facilitate handling character and byte streams effectively. The integration of `Scanner` and `FileWriter` and handling of exceptions show how Java manages both input capture and persistent storage. This highlights Java’s robust I/O capabilities which are crucial for applications requiring data storage and retrieval .

Inheritance is leveraged in the Java program to achieve code reuse by allowing a `Programmer` class to inherit properties from an `Employee` class. The `Employee` class contains a `salary` field, while `Programmer` inherits this field and adds an additional `bonus` field. By inheriting from `Employee`, `Programmer` avoids redundant declaration of `salary`, demonstrating effective code reuse. The program calculates and prints both fields, illustrating inheritance's role in reducing duplicate code and enhancing maintainability .

The Java program utilizes nested loops to print a right-aligned triangle pattern of asterisks by using two loops: an outer loop to iterate over each row and an inner loop to manage the number of asterisks per row. The outer loop runs in decreasing order from 5 to 1, representing the number of asterisks in each row, while the inner loop prints asterisks equal to the current value of the outer loop's counter. After printing the asterisks for a row, the program moves to the next line .

The principles demonstrated by the Java class on object construction and destruction include resource management and cleanup operations. The class `cons` implements a constructor to initialize objects and print "Hello" when an instance is created. Additionally, it includes a `finalize` method (destructor) to be called by the garbage collector before an object is destroyed, executing `System.gc()`, which explicitly calls the garbage collector and prints "Destroyed" when the object is null and collected. This highlights resource allocation during object creation and necessary cleanup, ensuring efficient memory usage by releasing resources no longer needed .

The Java program converts a decimal number to binary using `Integer.toBinaryString(d)`, which transforms an integer `d` into a binary string representation. Conversely, it converts a binary string back to a decimal by passing it to `Integer.parseInt(b,2)`, which interprets string `b` as a binary number and converts it to a decimal integer. This method is effective due to its simplicity and reliance on built-in methods that handle conversions efficiently, abstracting the complexity of manual binary calculations and reducing potential errors in conversion logic .

The Java program employs file input and output operations using `FileInputStream` and `FileOutputStream`. The `FileInputStream` reads data from a specified file by iteratively using the `read()` method in a loop until it reaches the end of the file. For output, `FileOutputStream` writes data to another file within a loop that reads from the input stream and writes each byte to the output stream using `write()`. The program closes both streams after operations to ensure resource management and prevent memory leaks. These strategies ensure efficient handling and manipulation of file data .

The Java implementation demonstrates abstract classes using the `calc` class, which declares abstract methods `sqr` and `cube` without providing implementations. The subclass `pract4` implements these methods, defining how they operate. The use of abstract classes is significant because it allows for defining a common interface and deferring implementation details to subclasses, promoting a design pattern that encourages abstraction and modularity, enabling diverse implementations of methods defined in the abstract class .

The class `SortData` manipulates array data by employing Java's `Arrays` class methods to sort integer arrays. Two functions, `asec()` and `desc()`, demonstrate sorting operations: `asec()` uses `Arrays.sort()` to sort the array in ascending order, while `desc()` utilizes `Arrays.sort()` combined with `Collections.reverseOrder()` to sort the same array in descending order. These showcase how Java's collection APIs provide efficient mechanisms for data manipulation, offering both ascending and descending sort capabilities in a straightforward manner .

You might also like