0% found this document useful (0 votes)
72 views

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

RAJ PRAKASH SHRIVASTAV

18BCE2463

JAVA PROGRAMMING

LAB ASSESSMENT 2

1)

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

class sort extends element{


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

public class sorting {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
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 java.util.*;
interface arrayInsert{
public static int[] insert(int arr[], int val, int n)
{
int arr1[]=new int[arr.length+1];
arr1[n]=val;
int i,j;
for(i=0;i<arr.length;i++)
{
arr1[i]=arr[i];
}
for(i=0;i<arr1.length;i++)
{
for(j=0;j<arr1.length-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[])
{
System.out.println("The array sorted after insertion is ");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}
public class arrayInsertion implements arrayInsert, displayArray{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int i,n,x;
System.out.println("Enter the length of the array:");
n=sc.nextInt();
int arr[]=new int[n];
System.out.println("Enter the array in sorted order:");
for(i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Enter the value to be inserted:");
x=sc.nextInt();
int arr2[]=arrayInsert.insert(arr,x,n);
displayArray.display(arr2);
}
}
Output:

3)

Code:
import java.util.*;
abstract class Animal{
abstract void makeSound();
String name;
public void s() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Any word: ");
String st = sc.nextLine();
this.name=st;
sc.close();
}
}
class Dog extends Animal{
public void makeSound() {
System.out.println("TeroBau "+this.name);
}
}

class Cat extends Animal{


public void makeSound() {
System.out.println("meow");
}
}

public class astclass {


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

Output:
4)

Code:

Add.java
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;
}
}

Calculate.java
package raj;
import java.util.*;
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);
}
}

Mul.java
package raj.subraj;
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;

}
}

packageDriver.java
import raj.*;
import raj.subraj.*;
import java.util.*;
public class packageDriver {
public static void main(String[] args) {
raj.Add obj1;
obj1 = new raj.Add();
raj.Calculate obj2 = new raj.Calculate();
raj.subraj.Mul obj3;
obj3 = new raj.subraj.Mul();
Scanner sc=new Scanner(System.in);
System.out.println("Enter first number:");
int x=sc.nextInt();
System.out.println("Enter second number:");
int y=sc.nextInt();
int sum=obj1.add(x,y);
int pow=obj2.powerFour(x);
int prod=obj3.mul(x, y);
System.out.println("Sum="+sum);
System.out.println("Fourth power of first number is"+pow);
System.out.println("Product is "+prod);
}
}
Output:

You might also like