0% found this document useful (0 votes)
6 views31 pages

Arpita Java Programss

The document contains a series of Java programming exercises authored by Arpita, each demonstrating different programming concepts such as calculating prime numbers, string manipulation, array operations, and object-oriented programming principles. Each exercise includes code snippets that illustrate the implementation of the specified functionality, along with comments indicating the author's name and roll number. The exercises cover a range of topics including data types, constructors, access modifiers, abstract classes, and package creation.

Uploaded by

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

Arpita Java Programss

The document contains a series of Java programming exercises authored by Arpita, each demonstrating different programming concepts such as calculating prime numbers, string manipulation, array operations, and object-oriented programming principles. Each exercise includes code snippets that illustrate the implementation of the specified functionality, along with comments indicating the author's name and roll number. The exercises cover a range of topics including data types, constructors, access modifiers, abstract classes, and package creation.

Uploaded by

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

[Link] a program to calculate the prime number from n1 to n2.

n1
and n2 are entered through keyboard.
// Name : Arpita
// [Link]: 24008041008
import [Link];
public class PrimeRange {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n1, n2;
[Link]("Enter n1: ");
n1 = [Link]();
[Link]("Enter n2: ");
n2 = [Link]();
for (int i = n1; i<= n2; i++) {
int count = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0) {
count++;
}
}
if (count == 2) {
[Link](i);
}
}
}
}

Page 1 of 31
[Link] a program to concatenate two strings. One string is
obtained from command line and other from keyboard.
// Name:Arpita
// Roll no.:24008041008
import [Link];
class ConcatString {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first string: ");
String str1 = [Link]();
[Link]("Enter second string: ");
String str2 = [Link]();
String result = str1 + str2;
[Link]("Concatenated String: " + result);
}
}

Page 2 of 31
[Link] a program to convert string into integer and integer into
string.
//Name:Arpita
//Roll no.:24008041008
import [Link];
public class ConvertExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// String to Integer
[Link]("Enter a number (in String): ");
String str = [Link]();
int num1 = [Link](str);
[Link]("String to Integer: " + num1);
// Integer to String
[Link]("Enter a number (in Integer): ");
int num2 = [Link]();
String str2 = [Link](num2);
[Link]("Integer to String: " + str2);
[Link]();
}
}

Page 3 of 31
[Link] a program to find the 2 nd maximum number from an
integer array.

// Name: Arpita
// [Link]: 24008041008
import [Link];
public class SecondMax {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n;
[Link]("Enter size of array: ");
n = [Link]();
int arr[] = new int[n];
[Link]("Enter elements:");
for (int i = 0; i< n; i++) {
arr[i] = [Link]();
}
int max = arr[0];
int secondMax = arr[0];
// Find max
for (int i = 0; i< n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}

Page 4 of 31
[Link] a program to accept the input in variables of all data type
from keyboard and display these on the screen.
// Name: Arpita
// [Link]
import [Link];
public class AllDataTypesInput {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a;
float b;
char c;
String d;
[Link]("Enter integer: ");
a = [Link]();
[Link]("Enter float: ");
b = [Link]();
[Link]("Enter character: ");
c = [Link]().charAt(0);
[Link]("Enter string: ");
d = [Link]();
[Link]("\nValues are:");
[Link]("Integer: " + a);
[Link]("Float: " + b);
[Link]("Character: " + c);
[Link]("String: " + d);
[Link]();

Page 5 of 31
}
}

Page 6 of 31
[Link] a program to check whether a string is empty, length of
string, remove the white spaces, compare two string, and convert
the upper case string to lower case.
// Name:Arpita
// [Link]: 24008041008
import [Link];
public class StringFunctions {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Input strings
[Link]("Enter first string: ");
String s1 = [Link]();
[Link]("Enter second string: ");
String s2 = [Link]();
// Check empty
if ([Link]())
[Link]("First string is empty");
else
[Link]("First string is not empty");
// Length
[Link]("Length of first string: " + [Link]());
// Remove white spaces
[Link]("Without spaces: " + [Link](" ", ""));

// Compare strings
if ([Link](s2))

Page 7 of 31
[Link]("Strings are equal");
else
[Link]("Strings are not equal");

// Upper to lower case


[Link]("Lower case: " + [Link]());
[Link]();
}
}

Page 8 of 31
[Link] a program to multiply two matrix of 3 X 3 and print the
result in matrix format
//Name: Arpita
//Roll no.: 24008041008
import [Link];
public class MatrixMultiply {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

int a[][] = new int[3][3];


int b[][] = new int[3][3];
int c[][] = new int[3][3];

// First matrix input


[Link]("Enter first matrix:");
for(int i = 0; i< 3; i++) {
for(int j = 0; j < 3; j++) {
a[i][j] = [Link]();
}
}

// Second matrix input


[Link]("Enter second matrix:");
for(int i = 0; i< 3; i++) {
for(int j = 0; j < 3; j++) {
b[i][j] = [Link]();

Page 9 of 31
}
}

// Matrix multiplication
for(int i = 0; i< 3; i++) {
for(int j = 0; j < 3; j++) {
c[i][j] = 0;
for(int k = 0; k < 3; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}

// Result display
[Link]("Result matrix:");
for(int i = 0; i< 3; i++) {
for(int j = 0; j < 3; j++) {
[Link](c[i][j] + " ");
}
[Link]();
}

[Link]();
}
}

Page 10 of 31
[Link] a program to reverse the value of an array using function.
Target Array must be pass from calling the function and resultant
array return from function.
// Name : Arpita
// [Link]: 24008041008
import [Link];
public class ReverseArray {
// Function to reverse array
static int[] reverse(int arr[]) {
int rev[] = new int[[Link]];
for(int i = 0; i<[Link]; i++) {
rev[i] = arr[[Link] - 1 - i];
}
return rev; // returning array
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a[] = new int[5];
// Input array
[Link]("Enter 5 elements:");
for(int i = 0; i< 5; i++) {
a[i] = [Link]();
}
// Call function
int result[] = reverse(a);
// Print reversed array
[Link]("Reversed array:");

Page 11 of 31
for(int i = 0; i<[Link]; i++) {
[Link](result[i] + " ");
}

[Link]();
}
}

Page 12 of 31
[Link] a program to calculate the area of various shapes with
the function name ‘Area’ in class name ‘CalculateArea’.
// Name: Arpita
// [Link]: 24008041008
class CalculateArea {
// Area of square
void Area(int side) {
[Link]("Area of Square: " + (side * side));
}
// Area of rectangle
void Area(int length, int breadth) {
[Link]("Area of Rectangle: " + (length * breadth));
}
// Area of circle
void Area(double radius) {
[Link]("Area of Circle: " + (3.14 * radius * radius));
}

public static void main(String[] args) {


CalculateAreaobj = new CalculateArea();

[Link](5); // square
[Link](4, 6); // rectangle
[Link](3.0); // circle
}
}

Page 13 of 31
[Link] a program to initialize the private integer variable of a
class with the value enter through keyboard and print the value of
this variable.
// Name: Arpita
//Roll no.:24008041008
import [Link].*;
class main {
private int num;
public void setValue(int n) {
num = n;
}
public void display() {
[Link](num);
}
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
main t = new main();

int x = [Link]();
[Link](x);
[Link]();
}

Page 14 of 31
[Link] a program to initialize the private variable of a class
without the use of public functions. Print the value of the variable
// Name: Arpita
// [Link]: 24008041008
class Demo {
private int value// Constructor to initialize private variable
Demo(int v) {
value = v;
}

// Method to display value


void show() {
[Link]("Value is: " + value);
}
}

public class Riya {


public static void main(String[] args) {
Demo obj = new Demo(10); // value passed through constructor
[Link]();
}
}

Page 15 of 31
[Link] a program in which there is a display function in class
Test. Display function print the value of private variable i and j of
Test class. Value of i and j should be 0 when we call display by
object obj1 of Test class, Value of i should be 10 and j remain 0
when we call it by object obj2 of Test class, Value of i and j should
be 20 and 30 when we call it by object obj3 of Test class.
// Name: Arpita
// [Link]: 24008041008
class Test {
private int i, j;
// Default constructor
Test() {
i = 0;
j = 0;
}

// One parameter constructor


Test(int x) {
i = x;
j = 0;
}

// Two parameter constructor


Test(int x, int y) {
i = x;
j = y;
}

Page 16 of 31
// Display function
void display() {
[Link]("i = " + i + ", j = " + j);
}

public static void main(String[] args) {


Test obj1 = new Test(); // i=0, j=0
Test obj2 = new Test(10); // i=10, j=0
Test obj3 = new Test(20, 30); // i=20, j=30

[Link]();
[Link]();
[Link]();
}
}

Page 17 of 31
[Link] a program to count the numberof object created of a
class.
Hint : No use of loop.

//Name: Arpita
Roll no.:24008041008
class Counter {
static int count = 0;

// Constructor
Counter() {
count++;
}
}

public class ObjectDemo {


public static void main(String[] args) {

Counter obj1 = new Counter();


Counter obj2 = new Counter();
Counter obj3 = new Counter();

[Link]("Total objects created: " + [Link]);


}
}

Page 18 of 31
[Link] a program to add the value of variable i& j of two object
obj1 and obj2 respectively and store the addition of both variable
in object obj3. All three objects are part of a same class and i& j are
private variables
// Name:Arpita
// [Link]: 24008041008
class Sample {
private int i, j;

void setData(int a, int b) {


i = a;
j = b;
}

Sample add(Sample s) {
Sample obj = new Sample();
obj.i = i + s.i;
obj.j = j + s.j;
return obj;
}

void display() {
[Link](i + " " + j);
}
}

public class TestProgram {

Page 19 of 31
public static void main(String[] args) {

Sample obj1 = new Sample();


Sample obj2 = new Sample();

[Link](10, 20);
[Link](5, 15);

Sample obj3 = [Link](obj2);

[Link]();
}
}

Page 20 of 31
[Link] is a class A which have one default, one private, one
protected and one public variable and there is a public method
AccessAll() which store and print the value in all variable. Derive
class A into another class B. Also write here a public function in
class B which access all 4 variable of class A. Make the object of
Class A and Class B in main and see the result. Write the output and
discuss the reasons
// Name:Arpita
// Roll no.:24008041008
class A {
int a = 10; // default
private int b = 20; // private
protected int c = 30; // protected
public int d = 40; // public

public void AccessAll() {


[Link]("Class A:");
[Link](a + " " + b + " " + c + " " + d);
}

public int getB() {


return b;
}
}

class B extends A {

Page 21 of 31
public void show() {
[Link]("Class B:");
[Link](a + " " + getB() + " " + c + " " + d);
}
}

public class DemoProgram {

public static void main(String[] args) {

A obj1 = new A();


B obj2 = new B();

[Link]();
[Link]();
}
}

Page 22 of 31
[Link] a program which have an abstract class Abst with
abstract function, calculate with two integers as parameter.
Define this calculate function, in the derived class Divide of Abst,
which divide two numbers. Also define calculate function, in
another derived class Multiply of Abst, which multiply two
numbers. Object name should be Calculator. No other object is
required.
// Name: Arpita
// [Link]: 24008041008

abstract class Abst {


abstract void calculate(int a, int b);
}

class Divide extends Abst {


void calculate(int a, int b) {
[Link]("Division: " + (a / b));
}
}

class Multiply extends Abst {


void calculate(int a, int b) {
[Link]("Multiplication: " + (a * b));
}
}

public class Calculator {

Page 23 of 31
public static void main(String[] args) {

Abst obj;

obj = new Divide();


[Link](10, 2);

obj = new Multiply();


[Link](10, 2);
}
}

Page 24 of 31
[Link] a package pkg1, which contain a public class Pkg1Class,
which have integer variables of type private, default, public,
protected each one for each. Write another package pkg2, which
have a class Pkg2Class. Pkg2Class extends the Pkg1Class of pkg1
package. Pkg2Class also have main function. Try to access all the
variables declared in Pkg1Class in the main function of Pkg2Class
with the object of Pkg1Class and Pkg2Class and observe the result.
Write down your findings
// Name: Arpita
//Roll no.:24008041008
// Package 1
package pkg1;

public class Pkg1Class {


private int a = 10; // private
int b = 20; // default
public int c = 30; // public
protected int d = 40; // protected
}

// Package 2
package pkg2;

import pkg1.Pkg1Class;

public class Pkg2Class extends Pkg1Class {

Page 25 of 31
public static void main(String[] args) {

// Object of Pkg1Class
Pkg1Class obj1 = new Pkg1Class();

// Object of Pkg2Class
Pkg2Class obj2 = new Pkg2Class();

[Link]("Access using Pkg1Class object:");

[Link]("\nAccess using Pkg2Class object:");

[Link]("Public c: " + obj2.c);


[Link]("Protected d: " + obj2.d);
}
}

Page 26 of 31
[Link] a program which try to store 6 th value in an array. Handle the
exception through by the program.
// Name:Arpita
// Roll no.:24008041008
public class ArrayExceptionDemo {

public static void main(String[] args) {

int arr[] = {10, 20, 30, 40, 50}; // size = 5

try {
// Trying to access 6th element (index 5)
[Link]("Value: " + arr[5]);
}
catch (ArrayIndexOutOfBoundsException e) {
[Link]("Exception caught: Invalid array index!");
}

[Link]("Program continues normally...");


}
}

Page 27 of 31
[Link] a program of to read from and write to a text file stored
in hard disk.
// Name: Arpita
// Roll no.:24008041008
import [Link].*;

public class FileReadWrite {

public static void main(String[] args) {

try {
// Writing to file
FileWriterfw = new FileWriter("[Link]");
[Link]("Hello, this is file handling in Java.");
[Link]();

[Link]("Data written successfully.");

// Reading from file


FileReaderfr = new FileReader("[Link]");
int ch;

[Link]("File content:");

while ((ch = [Link]()) != -1) {


[Link]((char) ch);

Page 28 of 31
}

[Link]();

} catch (IOException e) {
[Link]("Exception occurred: " + e);
}
}
}

Page 29 of 31
[Link] a program for multithreading using Thread class and
another program using Runnable interface.
// Name: Arpita
// Roll no.:24008041008
class MyThread extends Thread {
public void run() {
for (int i = 1; i<= 5; i++) {
[Link]("Thread Class: " + i);
}
}
}
public class ThreadDemo1 {
public static void main(String[] args) {
MyThread t1 = new MyThread();
[Link]();
}
}
// Second part of 20th
class MyRunnable implements Runnable {
public void run() {
for (int i = 1; i<= 5; i++) {
[Link]("Runnable Interface: " + i);
}
}
}
public class ThreadDemo2 {

Page 30 of 31
public static void main(String[] args) {
MyRunnable r = new MyRunnable();
Thread t1 = new Thread(r);
[Link]();
}
}

Page 31 of 31

You might also like