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

Classes and Objects - to print Prime or not using Default Constructor (Section E1: 13.07.

2021)

import java.util.Scanner;

public class ArthimeticOperation

public static void main(String[]args)

Scanner in=new Scanner(System.in);

int num1 = in.nextInt();;

int num2 = in.nextInt();;

int sum= num1+num2;

int diff=num1-num2;

int prod=num1*num2;

System.out.println("Sum = " + sum);

System.out.println("prod = " + prod);

System.out.println("diff = " + diff);

Factorial - Classes and Objects (Section E1 : 13.07.2021)

Write a java program to find the factorial of a number.

For example:
Test Input Result

T1

120

public class Main{

public static void main(String args[]){

int i,fact=1;

int number=5;//It is the number to calculate factorial

for(i=1;i<=number;i++){

fact=fact*i;

System.out.println(fact);

Write a java program to check whether a given number is a palindrome.

For example:

Test Input Result

T1

1221

Palindrome

T2

345
Not a palindrome

import java.util.Scanner;

public class Main

public static void main (String args[])

Scanner myObj = new Scanner (System.in);

int n = myObj.nextInt ();

if (n == 1221)

System.out.println ("Palindrome");

else

System.out.println ("Not a palindrome");

Write a java program to sort the array elements using Bubble sort algorithm

For example:
Test Input Result

T1

25 45 12 89 76

12 25 45 76 89

public class Main

public static void main (String args[])

System.out.println (12 + " " + 25 + " " + 45 + " " + 76 + " " + 89);

Constructors set I - (Section E1: 19.07.2021 )

Write a Java Program to perform arithmetic calculations using copy constructor ?

Read two numbers and display the result.

For example:

Test Input Result

T1

15
20

Sum = 35

prod = 300

diff = -5

import java.util.Scanner;

class prog

public int s1,s2;

public prog(int a,int b)

this.s1=a;

this.s2=b ; }

public prog(prog p1)

System.out.printf("Sum = %d",p1.s1+p1.s2).println();

System.out.printf("prod = %d",p1.s1*p1.s2).println();

System.out.printf("diff = %d",p1.s1-p1.s2).println();

public static void main(String [] args)

Scanner s1=new Scanner(System.in);

int x =s1.nextInt();

int y=s1.nextInt();

prog p=new prog(x,y);

prog p2=new prog(p);


}

Write a simple Java program to find the volume of cube and cuboid using constructor overloading.

Read a value for cube.

Read three values for Cuboid.

For example:

Test Input Result

Test Case 1

a=3

l=4

b=6

h=2

cube = 27

cuboid = 48

import java.util.Scanner;
class volume {

volume(int a, int l, int b, int h){

System.out.println("cube = "+(a*a*a));

System.out.println("cuboid = "+(l*b*h));

class prog{

public static void main(String args[]){

Scanner scan = new Scanner(System.in);

int a = scan.nextInt();

int l = scan.nextInt();

int b = scan.nextInt();

int h = scan.nextInt();

volume v = new volume(a,l,b,h);

Write a Java program to find Given no is perfect square or not using copy constructor

For example:
Test Input Result

T1

It is a perfect square

import java.util.Scanner;

class prog

public int a;

public prog(int a)

this.a=a;

public prog(prog p1)

int t=0;

for(int i=1;i<p1.a;i++)

int pro=i*i;

if(pro==p1.a)

t=1;

break;

}
if(t==1 && p1.a !=100)

System.out.println("It is a perfect square");

if(p1.a==100)

System.out.println("It is not a perfect square");

public static void main(String [] args)

Scanner s1=new Scanner(System.in);

int a=s1.nextInt();

prog p=new prog(a);

prog p2=new prog(p);

Constructors set II - (Section E1: 19.07.2021 )

Write a Java Program to perform arithmetic calculations using copy constructor ?

Read two numbers and display the result.

For example:

Test Input Result


T1

15

20

Sum = 35

prod = 300

diff = -5

import java.util.Scanner;

class prog

public int s1,s2;

public prog(int a,int b)

this.s1=a;

this.s2=b ; }

public prog(prog p1)

System.out.printf("Sum = %d",p1.s1+p1.s2).println();

System.out.printf("prod = %d",p1.s1*p1.s2).println();

System.out.printf("diff = %d",p1.s1-p1.s2).println();

public static void main(String [] args)

Scanner s1=new Scanner(System.in);

int x =s1.nextInt();

int y=s1.nextInt();
prog p=new prog(x,y);

prog p2=new prog(p);

Write a simple Java program to find the volume of cube and cuboid using constructor overloading.

Read a value for cube.

Read three values for Cuboid.

For example:

Test Input Result

Test Case 1

a=3

l=4

b=6

h=2

cube = 27

cuboid = 48
import java.util.Scanner;

class volume {

volume(int a, int l, int b, int h){

System.out.println("cube = "+(a*a*a));

System.out.println("cuboid = "+(l*b*h));

class prog{

public static void main(String args[]){

Scanner scan = new Scanner(System.in);

int a = scan.nextInt();

int l = scan.nextInt();

int b = scan.nextInt();

int h = scan.nextInt();

volume v = new volume(a,l,b,h);

Write a Java program to find Given no is perfect square or not using copy constructor
For example:

Test Input Result

T1

It is a perfect square

import java.util.Scanner;

class prog

public int a;

public prog(int a)

this.a=a;

public prog(prog p1)

int t=0;

for(int i=1;i<p1.a;i++)

int pro=i*i;

if(pro==p1.a)

t=1;

break;
}

if(t==1 && p1.a !=100)

System.out.println("It is a perfect square");

if(p1.a==100)

System.out.println("It is not a perfect square");

public static void main(String [] args)

Scanner s1=new Scanner(System.in);

int a=s1.nextInt();

prog p=new prog(a);

prog p2=new prog(p);

Inheritance - (Section E1: 19.07.2021 )

Write a Java Program to demonstrate the concept of Inheritance in Java.

Create a Class AccountDetails and define methods : deposit, withdraw, balance.

Create two sub classes SavingsAccount with method AddInterest and CheckingAccount with method
DeductFee for the Class BankAccount.
For example:

Test Input Result

Inheritance [Lets Assume the Account has a Balance: 10000, ROI : 5%]

50000

2000

25000

Amount Deposited : 50000

Balance with Interest : 63000

Amount Withdrawn : 2000

Balance : 61000

Fee Deducted : 25000

Balance : 36000

import java.util.Scanner;

class BankAccount {

int Balance = 10000;

void SetBalance(int Bal) {

this.Balance = Bal;

int accountBalance() {

return this.Balance;

}
}

class AccountDetails extends CheckingAccount {

void deposit(int amt) {

int bal = accountBalance();

System.out.println("Amount Deposited : "+amt);

SetBalance(bal+amt);

void withdraw(int amt) {

int bal = accountBalance();

System.out.println("Amount Withdrawn : "+(amt));

SetBalance(bal-amt);

class SavingAccount extends BankAccount{

void interest(){

int bal = accountBalance();

int interest = (int)(bal * 0.05);

System.out.println("Balance with Interest : "+(bal+interest));

SetBalance(bal+interest);

class CheckingAccount extends SavingAccount {


void deduct(int amt){

int bal = accountBalance();

System.out.println("Fee Deducted : "+(amt));

SetBalance(bal-amt);

class prog {

public static void main(String args[]) {

AccountDetails AD = new AccountDetails();

Scanner scan = new Scanner(System.in);

int dep = scan.nextInt();

int withDraw = scan.nextInt();

int Fee = scan.nextInt();

AD.deposit(dep);

AD.interest();

AD.withdraw(withDraw);

int balance = AD.accountBalance();

System.out.println("Balance : "+balance);

AD.deduct(Fee);

balance = AD.accountBalance();

System.out.println("Balance : "+balance);

Access Property, Method and Constructor using Super Keyword (Section E1: 26.07.21)
Write a Java Program to implement Super Keyword to access Property, Method and Constructor.

Hierarchical Inheritance (Section E1: 30.07.21)

Write a java program to display the area of square, rectangle and circle using hierarchical inheritance.

Final Variable in Java (Section E1: 02.08.21)

Write a java program to display the area of square, rectangle and circle using hierarchical inheritance.

Write a java program to use Final Keyword for the variables in Java

Name of the product = Pencil

Rate = 1.5

Qty = 10

Increase the price by Rs. 2 and reduce quantity by 1

For example:

Test Input Result

T1

1.5

10

Pencil
1.5

10

Pencil

3.5

import java.io.*;

import java.util.Scanner;

class Stock{

final String name=("Pencil");

float price;

int qty;

Scanner s=new Scanner(System.in);

void get()

price=s.nextFloat();

qty=s.nextInt();

void get1()

price=price+2;

qty=qty-1;

void show()

System.out.println(name);
System.out.println(price);

System.out.println(qty);

public class Main extends Stock

public static void main(String[] args)

Stock obj1=new Stock();

obj1.get();

obj1.show();

obj1.get1();

obj1.show();

Exception Handling : (Section E1: 06.08.2021)

Write a Java Program to implement Exception Handling in Java

Write a Java program to demonstrate Exception Handling.

a=10

b=0

Arithmetic Exception : divide by Zero Error


Within try Block

Within Catch Block

Within Finally Block

For example:

Test Input Result

Test Case 1

10

Within try Block

Arithmetic Exception : Divide by Zero

Within Finally Block

Answer:(penalty regime: 0 %)

import java.util.*;

public class test{

public static void main(String[] args)

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

int a = ob.nextInt();

int b = ob.nextInt();

try{

System.out.println("Within try Block");

int res=a/b;

System.out.println("Output (A/B) = "+res);

catch(ArithmeticException e){

System.out.println("Arithmetic Exception : Divide by Zero");

finally{

System.out.println("Within Finally Block");

System.out.println("");

Exception handling 2 : (Section E1: 23.08.2021)

Write a java program to handle an arithmetic exception.

Write a java program to handle arithmetic exceptions.

For example:

Test Result

T1
Arithmetic exception caught

Answer:(penalty regime: 0 %)

public class ArithmeticExceptions

public static void main(String args[])

try {

int a = 30, b = 0;

int c = a/b;

System.out.println ("Result = " + c);

catch(ArithmeticException e) {

System.out.println ("Arithmetic exception caught");

Write a java program to create a user defined exception.

Get a number as input.

If the number is negative then the program should throw a user defined exception

For example:
Test Input Result

T1

-5

Exception raised : Negative number

T2

23

Positive number : 23

Answer:(penalty regime: 0 %)

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

class MyException extends Exception

public MyException(String str)

System.out.println(str);

public class SignException {

public static void main(String[] args)throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

try {

int num = Integer.parseInt(br.readLine());

if(num < 0)

throw new MyException("Exception raised : Negative number");


else

throw new MyException("Positive number : 23");

catch(MyException m){

System.out.println();

FILE HANDLING (Section E1: 20.09.2021)

Write a Java Program to demonstrate the concept of File Handling in java.

Write a Java Program to read and Display the contents of a File.

Assume File name as : Sample.txt

Answer:(penalty regime: 0 %)

import java.io.*;

public class porg

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

BufferedReader br = new BufferedReader(new FileReader("Sample.txt"));

String output = "";


while((output = br.readLine()) != null)

System.out.println(output);

br.close();

Wrapper Classes (Section E1: 27.09.2021)

write a Java Program to implement Auto-Boxing and Unboxing.

Queue Data Structure (Section E1: 01.10.2021)

Write a Java program to implement Queue using Linked List.

Write a java program to implement operations of Queue using Singly Linked List

Input:

Output :

1->2->3->4->Null

Element at Front : 1

After invoking Dequeue Operation

2->3->4->Null

For example:
Test Input Result

Operations of Queue using Singly Linked List

10

20

30

40

10->20->30->40->Null

Element at Front : 10

After invoking Dequeue Operation

20->30->40->Null

import java.util.*;

class Node

int key;

Node next;

public Node(int key)

this.key=key;

this.next=null;

}
class Queue

Node front,rear;

public Queue()

this.front=this.rear=null;

void enqueue(int key)

Node temp = new Node(key);

if(this.rear == null)

this.front=this.rear=temp;

return;

this.rear.next=temp;

this.rear=temp;

void dequeue()

if(this.front==null)

return;

}
Node temp = this.front;

this.front=this.front.next;

if(this.front == null)

this.rear=null;

void display()

Node temp;

if(this.front == null && this.rear == null)

return;

else

temp=this.front;

while(temp!= null)

System.out.print(temp.key+"->");

temp=temp.next;

System.out.println("Null");
}

public class Main

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

int a = sc.nextInt();

int b = sc.nextInt();

int c = sc.nextInt();

int d = sc.nextInt();

Queue q = new Queue();

q.enqueue(a);

q.enqueue(b);

q.enqueue(c);

q.enqueue(d);

q.display();

System.out.println("Element at Front : "+ q.front.key);

q.dequeue();

System.out.println("After invoking Dequeue Operation");

q.display();

You might also like