Professional Documents
Culture Documents
Java e 1 Codes
Java e 1 Codes
2021)
import java.util.Scanner;
int diff=num1-num2;
int prod=num1*num2;
For example:
Test Input Result
T1
120
int i,fact=1;
for(i=1;i<=number;i++){
fact=fact*i;
System.out.println(fact);
For example:
T1
1221
Palindrome
T2
345
Not a palindrome
import java.util.Scanner;
if (n == 1221)
System.out.println ("Palindrome");
else
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
System.out.println (12 + " " + 25 + " " + 45 + " " + 76 + " " + 89);
For example:
T1
15
20
Sum = 35
prod = 300
diff = -5
import java.util.Scanner;
class prog
this.s1=a;
this.s2=b ; }
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();
int x =s1.nextInt();
int y=s1.nextInt();
Write a simple Java program to find the volume of cube and cuboid using constructor overloading.
For example:
Test Case 1
a=3
l=4
b=6
h=2
cube = 27
cuboid = 48
import java.util.Scanner;
class volume {
System.out.println("cube = "+(a*a*a));
System.out.println("cuboid = "+(l*b*h));
class prog{
int a = scan.nextInt();
int l = scan.nextInt();
int b = scan.nextInt();
int h = scan.nextInt();
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;
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)
if(p1.a==100)
int a=s1.nextInt();
For example:
15
20
Sum = 35
prod = 300
diff = -5
import java.util.Scanner;
class prog
this.s1=a;
this.s2=b ; }
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();
int x =s1.nextInt();
int y=s1.nextInt();
prog p=new prog(x,y);
Write a simple Java program to find the volume of cube and cuboid using constructor overloading.
For example:
Test Case 1
a=3
l=4
b=6
h=2
cube = 27
cuboid = 48
import java.util.Scanner;
class volume {
System.out.println("cube = "+(a*a*a));
System.out.println("cuboid = "+(l*b*h));
class prog{
int a = scan.nextInt();
int l = scan.nextInt();
int b = scan.nextInt();
int h = scan.nextInt();
Write a Java program to find Given no is perfect square or not using copy constructor
For example:
T1
It is a perfect square
import java.util.Scanner;
class prog
public int a;
public prog(int a)
this.a=a;
int t=0;
for(int i=1;i<p1.a;i++)
int pro=i*i;
if(pro==p1.a)
t=1;
break;
}
if(p1.a==100)
int a=s1.nextInt();
Create two sub classes SavingsAccount with method AddInterest and CheckingAccount with method
DeductFee for the Class BankAccount.
For example:
Inheritance [Lets Assume the Account has a Balance: 10000, ROI : 5%]
50000
2000
25000
Balance : 61000
Balance : 36000
import java.util.Scanner;
class BankAccount {
this.Balance = Bal;
int accountBalance() {
return this.Balance;
}
}
SetBalance(bal+amt);
SetBalance(bal-amt);
void interest(){
SetBalance(bal+interest);
SetBalance(bal-amt);
class prog {
AD.deposit(dep);
AD.interest();
AD.withdraw(withDraw);
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.
Write a java program to display the area of square, rectangle and circle using hierarchical inheritance.
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
Rate = 1.5
Qty = 10
For example:
T1
1.5
10
Pencil
1.5
10
Pencil
3.5
import java.io.*;
import java.util.Scanner;
class Stock{
float price;
int qty;
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);
obj1.get();
obj1.show();
obj1.get1();
obj1.show();
a=10
b=0
For example:
Test Case 1
10
Answer:(penalty regime: 0 %)
import java.util.*;
{
Scanner ob = new Scanner(System.in);
int a = ob.nextInt();
int b = ob.nextInt();
try{
int res=a/b;
catch(ArithmeticException e){
finally{
System.out.println("");
For example:
Test Result
T1
Arithmetic exception caught
Answer:(penalty regime: 0 %)
try {
int a = 30, b = 0;
int c = a/b;
catch(ArithmeticException e) {
If the number is negative then the program should throw a user defined exception
For example:
Test Input Result
T1
-5
T2
23
Positive number : 23
Answer:(penalty regime: 0 %)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
System.out.println(str);
try {
if(num < 0)
catch(MyException m){
System.out.println();
Answer:(penalty regime: 0 %)
import java.io.*;
System.out.println(output);
br.close();
Write a java program to implement operations of Queue using Singly Linked List
Input:
Output :
1->2->3->4->Null
Element at Front : 1
2->3->4->Null
For example:
Test Input Result
10
20
30
40
10->20->30->40->Null
Element at Front : 10
20->30->40->Null
import java.util.*;
class Node
int key;
Node next;
this.key=key;
this.next=null;
}
class Queue
Node front,rear;
public Queue()
this.front=this.rear=null;
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;
return;
else
temp=this.front;
while(temp!= null)
System.out.print(temp.key+"->");
temp=temp.next;
System.out.println("Null");
}
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
q.enqueue(a);
q.enqueue(b);
q.enqueue(c);
q.enqueue(d);
q.display();
q.dequeue();
q.display();