0% found this document useful (0 votes)
94 views8 pages

Java Lab 2

The document contains 4 Java programs that demonstrate different concepts: 1) A program that sorts an integer array using a sorting class that extends an element class. 2) A program that inserts a value into a sorted integer array and displays the sorted array. 3) A program that uses abstract classes and inheritance to create animal subclasses that make sounds. 4) A program that demonstrates using packages and importing classes to perform mathematical operations like addition, subtraction, multiplication and division across multiple files.

Uploaded by

Akriti Agrawal
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)
94 views8 pages

Java Lab 2

The document contains 4 Java programs that demonstrate different concepts: 1) A program that sorts an integer array using a sorting class that extends an element class. 2) A program that inserts a value into a sorted integer array and displays the sorted array. 3) A program that uses abstract classes and inheritance to create animal subclasses that make sounds. 4) A program that demonstrates using packages and importing classes to perform mathematical operations like addition, subtraction, multiplication and division across multiple files.

Uploaded by

Akriti Agrawal
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

RAJ PRAKASH SHRIVASTAV

18BCE2463

JAVA PROGRAMMING

LAB ASSESSMENT 2

1)

Code:
import [Link].*;
class element{
private Scanner sc = new Scanner([Link]);
public void e(int a ,int[] b) {
[Link]("Before sorting: ");
for (int i=0;i<a;i++) {
b[i] = sc .nextInt();
[Link](b[i]+" ");
}
[Link](" ");
}
}

class sort extends element{


public void s(int a, int[] b) {
int [] len = b;
for(int i=0;i<[Link];i++) {
for (int j=0;j<[Link]-1;j++) {
if(len[j+1]<len[j]) {
int temp = len[j+1];
len[j+1]=len[j];
len[j]=temp;
}
}
}
[Link]("After Sorting: ");
for(int item: len) {
[Link](item+" ");
}
}

public class sorting {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int[] arr = new int[5];
element obj =new element();
obj.e(n, arr);
sort obj1 =new sort();
obj1.s(n, arr);
}

Output:

2)

Code:
import [Link].*;
interface arrayInsert{
public static int[] insert(int arr[], int val, int n)
{
int arr1[]=new int[[Link]+1];
arr1[n]=val;
int i,j;
for(i=0;i<[Link];i++)
{
arr1[i]=arr[i];
}
for(i=0;i<[Link];i++)
{
for(j=0;j<[Link]-i-1;j++)
{
if(arr1[j]>arr1[j+1])
{
int temp=arr1[j];
arr1[j]=arr1[j+1];
arr1[j+1]=temp;
}
}
}
return arr1;
}
}
interface displayArray{
static void display(int arr[])
{
[Link]("The array sorted after insertion is ");
for(int i=0;i<[Link];i++)
{
[Link](arr[i]);
}
}
}
public class arrayInsertion implements arrayInsert, displayArray{
public static void main(String[] args)
{
Scanner sc=new Scanner([Link]);
int i,n,x;
[Link]("Enter the length of the array:");
n=[Link]();
int arr[]=new int[n];
[Link]("Enter the array in sorted order:");
for(i=0;i<n;i++)
{
arr[i]=[Link]();
}
[Link]("Enter the value to be inserted:");
x=[Link]();
int arr2[]=[Link](arr,x,n);
[Link](arr2);
}
}
Output:

3)

Code:
import [Link].*;
abstract class Animal{
abstract void makeSound();
String name;
public void s() {
Scanner sc = new Scanner([Link]);
[Link]("Enter Any word: ");
String st = [Link]();
[Link]=st;
[Link]();
}
}
class Dog extends Animal{
public void makeSound() {
[Link]("TeroBau "+[Link]);
}
}

class Cat extends Animal{


public void makeSound() {
[Link]("meow");
}
}

public class astclass {


public static void main(String[] args) {
Animal d = new Dog();
d.s();
[Link]();
Animal c = new Cat();
[Link]();
}
}

Output:
4)

Code:

[Link]
package raj;
public class Add
{
public int add(int x, int y)
{
int z=x+y;
return z;
}
}
class Subtract
{
public int diff(int x,int y)
{
int z=x-y;
return z;
}
}

[Link]
package raj;
import [Link].*;
public class Calculate{
public int powerFour(int x)
{
return (x*x*x*x);
}
}
class Remainder {
public int mod(int x, int y) {
return (x % y);
}
}

[Link]
package [Link];
public class Mul {
public int mul(int x, int y)
{
int z=x*y;
return z;
}
}
class Div {
public int div(int x, int y)
{
int z=x/y;
return z;

}
}

[Link]
import raj.*;
import [Link].*;
import [Link].*;
public class packageDriver {
public static void main(String[] args) {
[Link] obj1;
obj1 = new [Link]();
[Link] obj2 = new [Link]();
[Link] obj3;
obj3 = new [Link]();
Scanner sc=new Scanner([Link]);
[Link]("Enter first number:");
int x=[Link]();
[Link]("Enter second number:");
int y=[Link]();
int sum=[Link](x,y);
int pow=[Link](x);
int prod=[Link](x, y);
[Link]("Sum="+sum);
[Link]("Fourth power of first number is"+pow);
[Link]("Product is "+prod);
}
}
Output:

You might also like