Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

java solution

java solutions

Uploaded by

MD FARHAN 3070
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

java solution

java solutions

Uploaded by

MD FARHAN 3070
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

WAP to find the area of Rectangle, Square and Circle

Code:
import java.util.*;

class Program{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

int breadth,width;
System.out.print("Enter breadth and width of Rectangle:");
breadth = sc.nextInt();
width = sc.nextInt();
System.out.println("Area of the Rectangle
is:"+(breadth*width));

int side;
System.out.print("Enter Side of Square:");
side= sc.nextInt();
System.out.println("Area of the Side is:"+(side*side));

double radius;
System.out.print("Enter Radius of Circle:");
radius= sc.nextDouble();
System.out.println("Area of the Circle
is:"+(3.1415*radius*radius));
}
}

Output:
WAP to calculate the Simple Interest

Code:
import java.util.*;

class Program{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

double amount,interest_rate,time;
System.out.print("Enter Amount:");
amount = sc.nextDouble();
System.out.print("Enter Interest Rate(per Year):");
interest_rate = sc.nextDouble();
System.out.print("Enter Time(in Year):");
time = sc.nextDouble();

System.out.println("Total Interest
is:"+(amount*interest_rate*time)/100);
}
}

Output:
WAP to convert Fahrenheit to Celsius Temperature

Code:
import java.util.*;

class Program{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

double fahrenheit,celsius;
System.out.print("Enter Temperature in fahrenheit:");
fahrenheit = sc.nextDouble();

System.out.println("Temperature in celsius
is:"+((fahrenheit-32)/9)*5);
}
}

Output:
WAP to solve a given equation

Code:
import java.util.*;

class Program{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

int a,b,c;
System.out.print("Enter a,b,c Value:");
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();

int x = (b*b)-(4*a*c);

if(x<0){
double y = Math.sqrt(Math.abs(x));
System.out.printf("Root 1 is: (%d + %.2fi)/%d\n",-
b,y,2*a);
System.out.printf("Root 2 is: (%d - %.2fi)/%d",-
b,y,2*a);
}else if(x==0) {
double z = (double)-b/(2*a);
System.out.printf("Roots are: %.2f",z);
}else{
double y = Math.sqrt(Math.abs(x));
System.out.printf("Root 1 is: (%d + %.2f)/%d\n",-
b,y,2*a);
System.out.printf("Root 2 is: (%d - %.2f)%d",-b,y,2*a);
}
}
}

Output:
WAP to find average of 3 Nos

Code:
import java.util.*;

class Program{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

int num1,num2,num3;
System.out.print("Enter three numbers:");
num1 = sc.nextInt();
num2 = sc.nextInt();
num3 = sc.nextInt();

System.out.println("The average of 3 Numbers


is:"+(double)(num1+num2+num3)/3);
}
}

Output:
WAP to swap 2 numbers with and without using third
variable

Code: (with third variable)


import java.util.*;

class Program{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

int num1,num2,temp;
System.out.print("Enter two numbers:");
num1 = sc.nextInt();
num2 = sc.nextInt();

System.out.println("Before swapping Num1 is:"+num1+"\nBefore


swapping Num2 is:"+num2);

temp = num2;
num2=num1;
num1=temp;

System.out.print("After swapping Num1 is:"+num1+"\nAfter


swapping Num2 is:"+num2);
}
}

(Without third variable)


import java.util.*;

class Program{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

int num1,num2;
System.out.print("Enter two numbers:");
num1 = sc.nextInt();
num2 = sc.nextInt();

System.out.println("Before swapping Num1 is:"+num1+"\nBefore


swapping Num2 is:"+num2);
num1+=num2;
num2=num1-num2;
num1=num1-num2;

System.out.print("After swapping Num1 is:"+num1+"\nAfter


swapping Num2 is:"+num2);
}
}

Output:
WAP to find the greatest of 3 Nos using Ternary
Operator

Code:
import java.util.*;

class Program{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

int num1,num2,num3;
System.out.print("Enter three numbers:");
num1 = sc.nextInt();
num2 = sc.nextInt();
num3 = sc.nextInt();

int result =
num1>=num2?num1>=num3?num1:num3:num2>=num3?num2:num3;

System.out.print("The greatest number is:"+result);


}
}

Output:
WAP to calculate billing of call

Code:
import java.util.*;

class Program{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

int call;
double bill;
System.out.print("Enter number of calls:");
call = sc.nextInt();

if(call<100){
bill = call*0.02;
}else if(call>=100&&call<200){
bill = call*0.05;
}else if(call>=200&&call<300){
bill = call*0.1;
}else if(call>=300&&call<400){
bill = call*0.15;
}else if(call>=400&&call<500){
bill = call*0.25;
}else{
bill = call*0.38;
}

System.out.print("The bill amount is:"+bill);


}
}

Output:
WAP to add all digits of a number

Code:
import java.util.*;

class Program{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

int number,sum=0;

System.out.print("Enter a number:");
number = sc.nextInt();

while(number>0){
sum+=number%10;
number/=10;
}

System.out.print("The sum of digits is:"+sum);


}
}

Output:
WAP to check whether a number is Palindrome or not

Code:
import java.util.*;

class Program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int number, temp, reverse = 0;

System.out.print("Enter a number:");
number = sc.nextInt();
temp = number;

while (number > 0) {


reverse = reverse * 10 + (number % 10);
number /= 10;
}

if (reverse == temp) {
System.out.println("The number is Palindrome!");
} else {
System.out.println("The number is not Palindrome!");
}
}
}

Output:
WAP to calculate the Fibonacci sequence of a given
range

Code:
import java.util.*;

class Program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int number,a1=0,a2=1,temp;

System.out.print("Enter the range:");


number = sc.nextInt();

if(number==1){
System.out.println(a1+" ");
}else if(number==2){
System.out.print(a1+" "+a2);
}else{
System.out.print(a1+" ");
System.out.print(a2+" ");
for(int i=2;i<number;i++){
temp = a1+a2;
System.out.print(temp+" ");
a1=a2;
a2=temp;
}
}
}
}

Output:
WAP to create a calculator functionality

Code:
import java.util.*;

class Program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

double number1,number2;
int choice;

System.out.print("Enter number1:");
number1 = sc.nextDouble();
System.out.print("Enter number2:");
number2 = sc.nextDouble();
System.out.println("1.Addition | 2.Subtraction |
3.Multiplication | 4.Division");
System.out.print("Enter option:");
choice = sc.nextInt();

switch(choice){
case 1:
System.out.println("Addition result
is:"+(number1+number2));
break;
case 2:
System.out.println("Subtraction result
is:"+(number1-number2));
break;
case 3:
System.out.println("Multiplication result
is:"+(number1*number2));
break;
case 4:
if(number2!=0){
System.out.println("Division result
is:"+number1/number2);
}else{
System.out.println("Divisor cannot be 0!");
}
break;
default:
System.out.println("Invalid option selected!");
}
}
}
Output:
WAP to print the prime numbers within given range

Code:
import java.util.*;

class Program {
public static boolean isPrime(int num){
if(num==1) return false;
boolean flag = true;
for(int i=2;i<=Math.sqrt(num);i++){
if(num%i==0){
flag = false;
break;
}
}
return flag;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int lowerLimit,upperLimit;

System.out.print("Enter lower Limit:");


lowerLimit = sc.nextInt();
System.out.print("Enter upper Limit:");
upperLimit = sc.nextInt();

if(lowerLimit<=0||upperLimit<lowerLimit){
System.out.println("Invalid Limits Entered!");
return;
}
for(int i=lowerLimit;i<=upperLimit;i++){
if(isPrime(i)){
System.out.print(i+" ");
}
}
}
}

Output:
WAP to print if given number is buzz number or not

Code:
import java.util.*;

class Program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int number;
System.out.print("Enter a number:");
number = sc.nextInt();

if(number%7==0||number%10==7){
System.out.println(number+" is a buzz number!");
}else{
System.out.println(number+" is not buzz number!");
}
}
}

Output:
WAP to calculate area of rectangle by initializing the
object by reference variable and method

Code:
import java.util.*;

class Rectangle{
double breadth,width;
public double area(double b,double w){
return b*w;
}
public void display(){
System.out.println("The area of Rectangle is:"+breadth*width);
}
}
class Program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

//By reference variable


Rectangle r1 = new Rectangle();
System.out.print("Enter breadth and width of Rectangle:");
r1.breadth = sc.nextDouble();
r1.width = sc.nextDouble();
r1.display();

//By method
double b1,w1;
Rectangle r2 = new Rectangle();
System.out.print("Enter breadth and width of Rectangle:");
b1 = sc.nextDouble();
w1 = sc.nextDouble();
System.out.println("The area of Rectangle is:"+r2.area(b1,w1));
}
}

Output:
WAP to implement banking system

Code:
import java.util.*;

class Bank{
double amount;
public void deposit(double a){
amount+=a;
System.out.println("Amount Deposited Successfully!");
}
public void withdraw(double b){
if(amount<b){
System.out.println("Invalid Withdraw request!");
}else{
amount-=b;
System.out.println("Withdraw Successful!");
}
}
public void display(){
System.out.println("Amount is:"+amount);
}
}
class Program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

double x;
Bank bn = new Bank();
System.out.print("Enter the amount to Deposit:");
x = sc.nextDouble();
bn.deposit(x);

System.out.print("Enter amount to withdraw:");


x = sc.nextDouble();
bn.withdraw(x);

bn.display();
}
}

Output:
WAP to implement Library system

Code:
class LM {
String member_category, dept;
int member_id, no_books;

void getter() {
System.out.println("Member Id:" + member_id);
System.out.println("Member Category:" +
member_category);
System.out.println("Department:" + dept);
System.out.println("Number of Books:" + no_books);
}

void setter(String a, String b, int c, int d) {


member_category = a;
dept = b;
member_id = c;
no_books = d;
}

void borrowBook(int no) {


if (no > no_books) {
System.out.println("Invalid Request!");
return;
}
no_books -= no;
System.out.println("Current No of Books:" + no_books);
}

void returnBook(int no) {


no_books += no;
System.out.println("Current No of Books:" + no_books);
}
}

class Library {
public static void main(String[] args) {
LM obj1 = new LM();
obj1.setter("Std1", "Cse", 1, 100);
obj1.getter();
obj1.borrowBook(20);
obj1.returnBook(30);

}
}

Output:
WAP to calculate bonus of Employee

Code:
class Employee {
String name;
int eid;
double salary;
double rating = 1.0;

Employee(String name, int eid, double salary, double


rating) {
this.eid = eid;
this.name = name;
this.salary = salary;
this.rating = rating;
}

int bonus() {
if (rating > 0 && rating <= 2) {
return 500;
} else if (rating > 2 && rating <= 4) {
return 1000;
} else if (rating > 4 && rating <= 5) {
return 1500;
}
return 0;
}

void display() {
System.out.println(
"Employee Name:" + name + "\nEmployee Id:" +
eid + "\nSalary:" + salary + "\nBonus:" + bonus());
}
}

public class Test {


public static void main(String[] args) {
Employee obj = new Employee("Emp1", 1, 5000, 3.2);
obj.display();
}
}

Output:
WAP to create 2 objects of a class

Code:
class Student {
int roll;
String name;
float fees;

Student(String name, int roll, float fees) {


this.roll = roll;
this.name = name;
this.fees = fees;
}

void display() {
System.out.println("Student Name: " + name + "\nRoll: " +
roll + "\nFees: " + fees);
}
}

class Test {
public static void main(String[] args) {
Student s1 = new Student("Student1", 1, 2000.0f);
Student s2 = new Student("Student2", 2, 2500.0f);
s1.display();
s2.display();
}
}
Output:
WAP to invoke methods of same class by ‘this’

Code:
class A {
void m() {
System.out.println("hello m");
}

void n() {
System.out.println("Hello n");
this.m();
}
}

class Test {
public static void main(String[] args) {
A obj1 = new A();
obj1.n();
}
}

Output:
WAP to invoke default constructor by ‘this’

Code:
class A {
A() {
System.out.println("hello A");
}

A(int x) {
this();
System.out.println(x);
}
}

class Test {
public static void main(String[] args) {
A obj1 = new A(10);
}
}

Output:
WAP to invoke parameterized constructor by ‘this’

Code:
class A {
A(int x) {
System.out.println(x);
}

A() {
this(5);
System.out.println("Hello A");
}
}

class Test {
public static void main(String[] args) {
A obj1 = new A();
}
}

Output:
WAP to pass ‘this’ as parameter

Code:
class A {
void m(A obj) {
System.out.println("Method is Invoked!");
}

void p() {
m(this);
}
}

class Test {
public static void main(String[] args) {
A obj1 = new A();
obj1.p();
}
}

Output:
WAP to pass other class ‘this’ to another class

Code:
class A {
B obj;

A(B obj) {
this.obj = obj;
}

void display() {
System.out.println(obj.data);
}
}

class B {
int data = 10;

B() {
A obj = new A(this);
obj.display();
}
}

class Test {
public static void main(String[] args) {
B obj = new B();
}
}

Output:
WAP to perform linear Search
Code:
import java.util.Scanner;
class Test {
public static void linearSearch(int arr[], int target) {
boolean found = false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
found = true;
System.out.println("Target found at Index: " + i);
}
}
if (!found) {
System.out.println("Target nopt found!");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, target;
System.out.print("Enter the number of elements:");
n = sc.nextInt();
int arr[] = new int[n];
System.out.print("Enter the elements:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.print("Enter the target number:");
target = sc.nextInt();
linearSearch(arr, target);
}
}

Output:
WAP to perform bubble sort
Code:
import java.util.Scanner;
class Test {
public static void bubbleSort(int arr[]) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j + 1] < arr[j]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;}}}}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
System.out.print("Enter the number of elements:");
n = sc.nextInt();
int arr[] = new int[n];
System.out.print("Enter the elements:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
bubbleSort(arr);
System.out.println("Sorted Array is:");
for (int i = 0; i < n; i++) {
System.out.println(arr[i] + " ");}}
}

Output:
WAP to find min and max element of array
Code:
import java.util.Scanner;

class Test {
public static void minmax(int arr[]) {
int min = arr[0], max = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
System.out.println("Max: " + max + "\nMin: " + min);
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n;
System.out.print("Enter the number of elements:");
n = sc.nextInt();
int arr[] = new int[n];
System.out.print("Enter the elements:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
minmax(arr);
}
}
Output:
WAP to implement Binary Search
Code:
import java.util.Scanner;
class Test {
public static int binarySearch(int arr[], int target) {
int low = 0, high = arr.length - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] < target) low = mid + 1;
if (arr[mid] > target) high = mid - 1;
}
return -1;
}
public static void main(String[] args) {
int n, target;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the numbr of elements:");
n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.print("Enter the target:");
target = sc.nextInt();
int result = binarySearch(arr, target);
System.out.println("Target " + (result == -1 ? ("not found!") :
("found at" + result + "!")));
}
}
Output:
WAP to add two matrices
Code:
import java.util.Scanner;

class Test {
public static void matrixAddition(int a[][], int b[][]) {
int res[][] = new int[a[0].length][a[0].length];
for (int i = 0; i < a[0].length; i++) {
for (int j = 0; j < a[0].length; j++) {
res[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("Resultant matrix is:");
for (int i = 0; i < a[0].length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(res[i][j] + "\t");
}
System.out.print("\n");
}
}

public static void main(String[] args) {


int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the numbr of elements:");
n = sc.nextInt();
int arr1[][] = new int[n][n];
int arr2[][] = new int[n][n];
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr1[i][j] = sc.nextInt();
}
}
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr2[i][j] = sc.nextInt();
}
}
matrixAddition(arr1, arr2);
}
}

Output:
WAP to find transpose of matrix
Code:
import java.util.Scanner;

class Test {
public static void matrixTranspose(int a[][]) {
System.out.println("Transpose matrix is:");
for (int i = 0; i < a[0].length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(a[j][i] + "\t");
}
System.out.print("\n");
}
}
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the numbr of elements:");
n = sc.nextInt();
int arr[][] = new int[n][n];
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
matrixTranspose(arr);
}
}
Output:
WAP to find sum of principal and secondary diagonal
Code:
import java.util.Scanner;
class Test {
public static void diagonalAddtion(int a[][]) {
int primary = 0, secondary = 0;
for (int i = 0; i < a[0].length; i++) {
for (int j = 0; j < a[0].length; j++) {
if (i == j) primary += a[i][j];
if (i + j == a[0].length - 1) secondary += a[i][j];
}
}
System.out.print("Primary Diagonal: " + primary +
"\nSecondary Diagonal: " + secondary);
}
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the numbr of elements:");
n = sc.nextInt();
int arr[][] = new int[n][n];
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
diagonalAddtion(arr);
}
}
Output:
WAP to implement matrix multiplication
Code:
import java.util.Scanner;

class Test {
public static void matrixMultiplication(int a[][], int b[][]) {
int res[][] = new int[a[0].length][a[0].length];
for (int i = 0; i < a[0].length; i++) {
for (int j = 0; j < a[0].length; j++) {
res[i][j] = 0;
for (int k = 0; k < a[0].length; k++) {
res[i][j] += (a[i][k] * b[k][j]);
}
System.out.print(res[i][j] + "\t");
}
System.out.print("\n");
}
}
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the numbr of elements:");
n = sc.nextInt();
int arr1[][] = new int[n][n];
int arr2[][] = new int[n][n];
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr1[i][j] = sc.nextInt();
}
}
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr2[i][j] = sc.nextInt();
}
}
matrixMultiplication(arr1, arr2);
}
}
Output:
WAP to perform row addition and column addition
Code:
import java.util.Scanner;
class Test {
public static void rowColumnAddition(int a[][]) {
int r = 0, c = 0, i, j;
for (i = 0; i < a[0].length - 1; i++) {
for (j = 0; j < a[0].length - 1; j++) {
r += a[i][j];
c += a[j][i];}
System.out.println("row " + i + " sum is: " + r);
System.out.println("Column " + i + " sum is: " + c); }
}
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the numbr of elements:");
n = sc.nextInt();
int arr[][] = new int[n + 1][n + 1];
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();}}
rowColumnAddition(arr);}
}
Output:
WAP to show boundary elements of a matrix
Code:
import java.util.Scanner;
class Test {
public static void main(String[] args) {
int row, column;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the numbers of row:");
row = sc.nextInt();
System.out.print("Enter the numbers of column:");
column = sc.nextInt();
int arr[][] = new int[row][column];
System.out.println("Enter the values of matrix:");
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
arr[i][j] = sc.nextInt();}}
System.out.println("Boundary elements are:");
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
if (i == 0 || j == 0 || i == row - 1 || j ==
column - 1) {
System.out.print(arr[i][j] + "\t");
} else {System.out.print(" " + "\t");}
}
System.out.print("\n");}}}
Output:
WAP to calculate box volume
Code:
import java.util.Scanner;

class Box {
int length, breadth, height;
Box() {
length = breadth = height = 20;
}
Box(int l, int b, int h) {
length = l;
breadth = b;
height = h;
}
int calculate(){
return length*breadth*height;
}
}
class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Box obj1 = new Box();
Box obj2 = new Box(10, 5, 20);
System.out.println("The area of obj1(dafault) is:" +
obj1.calculate());
System.out.println("The area of obj2 is:" +
obj2.calculate());
}}
Output:
WAP to implement various constructor types
Code:
class Employee {
String name, designation, department;
double salary;

Employee() {
name = "default name";
designation = "default designation";
department = "default department";
salary = 0;
}

Employee(String n, String d, String dep) {


name = n;
designation = d;
department = dep;

if (designation == "m") {
salary = 1000;
} else if (designation == "n") {
salary = 2000;
} else {
salary = 3000;
}
}

Employee(String n, String d, String dep, double s) {


name = n;
designation = d;
department = dep;
salary = s;
}

void print() {
System.out.print("Name: " + name + "\nDesignation: " +
designation + "\nDepartment: " + department + "\nSalary: "
+ salary + "\n");
}
}
class test {
public static void main(String[] args) {
Employee obj[] = { new Employee(), new
Employee("emp1", "n", "dep1"),
new Employee("emp2", "desg1", "dep2", 5000) };

for (int i = 0; i < obj.length; i++) {


obj[i].print();
System.out.print("________________\n");

}
}

Output:
WAP to find area, perimeter of shapes by fn
overriding
Code:
import java.util.*;
class Shape {
public void area() {
System.out.println("Method2");}
public void perimeter() {
System.out.println("Overriding Method1");}}
class Rectangle extends Shape {
Scanner sc = new Scanner(System.in);
int a, b;
public void area() {
System.out.println("Enter the Dimension of Rectangle
");
a = sc.nextInt();
b = sc.nextInt();
System.out.println("Area is " + (a * b));
}
public void perimeter() {
System.out.println("Enter the Dimension of Rectangle
");
a = sc.nextInt();
b = sc.nextInt();
System.out.println("Perimeter is " + 2 * (a + b));}}
class Square extends Shape {
Scanner sc = new Scanner(System.in);
int a;
public void area() {
System.out.println("Enter the Dimension of Square ");
a = sc.nextInt();
System.out.println("Area is " + (a * a));}
public void perimeter() {
System.out.println("Enter the Dimension of Square ");
a = sc.nextInt();
System.out.println("Perimeter is " + 2 * (a + a));}}
class Circle extends Shape {
Scanner sc = new Scanner(System.in);
int a;
public void area() {
System.out.println("Enter the Dimension of Circle ");
a = sc.nextInt();
System.out.println("Area is " + (22 / 7) * (a * a));}
public void perimeter() {
System.out.println("Enter the Dimension of Circle ");
a = sc.nextInt();
System.out.println("Perimeter is " + 2 * (22 / 7) *
a);}}
class test {
public static void main(String args[]) {
Rectangle rectangle = new Rectangle();
Square square = new Square();
Circle circle = new Circle();
rectangle.area();
rectangle.perimeter();
square.area();
square.perimeter();
circle.area();
circle.perimeter();}}
Output:
WAP to round off Km,m,Mm
Code:
class Measurement {
double km, m, mm;
Measurement(double km, double m, double mm) {
this.km = km;
this.m = m;
this.mm = mm;}
double getkm() {
return Math.round(km);}
double getm() {
return Math.round(m);}
double getmm() {
return Math.round(mm);}
}
class test {
public static void main(String args[]) {
Measurement measurement = new Measurement(5.8, 15.2,
13.5);
System.out.println(measurement.getkm());
System.out.println(measurement.getm());
System.out.println(measurement.getmm());}
}
Output:
WAP to implement ticket counter and show ticket
price and passenger count
Code:
import java.util.Scanner;
class ticketCounter {
int nop;
double money;
ticketCounter() {this.nop = 0;this.money = 0.0;}
void payingPassenger() {nop++;money += 50.0;}
void childPassenger() {nop++;}
void display() {
System.out.println("PASSENGERS="+nop +"\nREVENUE
COLLECTED=" + money);}}
class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ticketCounter t = new ticketCounter();
int choice = 0;
while (choice <= 3) {System.out.println("1 For Adult,
2 For Child, 3 for Display\nENTER YOUR CHOICE:");
choice = sc.nextInt();
if (choice == 1) {t.payingPassenger();}
if (choice == 2) {t.childPassenger();}
if (choice == 3) {t.display();}}}}
Output:
WAP to implement inheritance with
member,manager & employee class using super
Code:
import java.util.*;
class member {
String name, address, ph;
int age, salary;
public static final String ins_name = "My Organisation";
public member(String name, String address, int age, String
ph, int salary) {
this.name = name;
this.address = address;
this.age = age;
this.address = address;
this.ph = ph;
this.salary = salary;
}
void display1() {
System.out.println("from member class name=" + name);
System.out.println("from member class address=" +
address);
System.out.println("from member class age=" + age);
System.out.println("from member class phone=" + ph);
System.out.println("from member class salary=" +
salary);
System.out.println("from member class institute name="
+ ins_name);}}
class employee extends member {
String spl;
public employee(String name, String address, int age,
String ph, int salary, String spl) {
super(name, address, age, ph, salary);
this.spl = spl;}
void display1() {
super.display1();
System.out.println("Speciatzation inside employee
class is" + spl);}}
class manager extends member {
String dept;
public manager(String name, String address, int age,
String ph, int salary, String dept) {
super(name, address, age, ph, salary);
this.dept = dept;}
void display1() {
super.display1();
System.out.println("inside Manager class Depertment is
" + dept);}}
public class test4 {
public static void main(String args[]) {
employee e[] = new employee[2];
e[0] = new employee("asa", "garia", 22, "894512",
50000, "Al");
e[1] = new employee("MINA", "SALTLAKE", 23, "215487",
60000, "Block chain");
e[0].display1();
e[1].display1();
manager m = new manager("megha", "jadavpur", 27,
"9845123", 70000, "MCA");
m.display1(); }}
Output:
WAP to implement inheritance with
Rectangle,square class
Code:
import java.util.*;

class Rectangle {
int length, breadth;
double area, perimeter;

public Rectangle(int length, int breadth) {


this.length = length;
this.breadth = breadth;
}
void area() {
System.out.println("Area of the reactangle is = " +
length * breadth);
}

void perimeter() {
System.out.println("Perimeter of the rectangle is = "
+ 2 * (length + breadth));
}
}
class Square extends Rectangle {

int s;
public Square(int s) {
super(s, s);
this.s = s;}
void area() {
super.area();
System.out.println("The area of the Square having
side: " + s + " is : " + s * s);
}
void perimeter() {
super.perimeter();
System.out.println("The perimeter of the Square having
side: " + s + " is: " + (4 * s));
}
}
public class test4 {
public static void main(String[] args) {
Rectangle r = new Rectangle(2, 4);
Square s = new Square(2);
r.area();
r.perimeter();
s.area();
s.perimeter();}}
Output:
WAP to calculate marks using abstract class
Code:
abstract class Marks {
public abstract double getPercentage();
}
class A extends Marks {
double m1, m2, m3;
A(double a, double b, double c) {
m1 = a;m2 = b;m3 = c;}
public double getPercentage() {
return ((m1 + m2 + m3) / 300.00) * 100;}}
class B extends Marks {
double m1, m2, m3, m4;
B(double a, double b, double c, double d) {
m1 = a;m2 = b;m3 = c;m4 = d;}
public double getPercentage() {
return ((m1 + m2 + m3 + m4) / 400.00) * 100; }}
public class test3 {
public static void main(String[] args) {
A a = new A(70, 80, 90);
B b = new B(50, 100, 75, 90);
System.out.println("Percentage of Student A is:" +
a.getPercentage());
System.out.println("Percentage of Student B is:" +
b.getPercentage());
}
}

Output:
WAP to implement varargs in Java
Code(multiple args):
class test4 {
static void fun(String s, int... a) {
System.out.println("sentence " + s + " ");
System.out.println("Hi " + a.length + " ");
for (int i : a) {System.out.print(i + " ");}
System.out.println("");}
public static void main(String[] args) {
fun("Abcd", 100);fun("Java", 1, 2, 3, 4, 5);fun("c++");}}
Output:

Code(One args):
class test4 {
static void fun(int... a) {
System.out.print("Hi " + a.length + " ");
for (int i : a) {System.out.print(i + " ");}
System.out.println("");}
public static void main(String[] args) {fun(100);fun(1, 2,
3, 4, 5);fun();}}
Output:
WAP to implement abstract class with
BankAccount,SavingsAccount,CurrentAccount
Code:
import java.util.*;

abstract class BankAccount {


public abstract void deposit(int amount);

public abstract void withdraw(int amount);


}

class SavingsAccount extends BankAccount {


int amount;

SavingsAccount() {
amount = 0;
}

public void deposit(int amount) {


this.amount += amount;
System.out.println("Current Amount in Savings
Account:" + this.amount);
}

public void withdraw(int amount) {


if (this.amount < amount) {
System.out.println("Insufficient Balance in
Savings Account!");
return;
}
this.amount -= amount;
System.out.println("Current Amount in Savings
Account:" + this.amount);}}
class CurrentAccount extends BankAccount {
int amount;
CurrentAccount() {
amount = 0;
}
public void deposit(int amount) {
this.amount += amount;
System.out.println("Current Amount in Current
Account:" + this.amount);
}

public void withdraw(int amount) {


if (this.amount < amount) {
System.out.println("Insufficient Balance in
Current Account!");
return;
}
this.amount -= amount;
System.out.println("Current Amount in Current
Account:" + this.amount);}}
class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
SavingsAccount sa = new SavingsAccount();
CurrentAccount ca = new CurrentAccount();
sa.deposit(1000);sa.withdraw(1000);ca.deposit(500);ca.
withdraw(1000);}}

Output:
WAP to implement abstract class with
Animal,Lion,Tiger,Deer
Code:
abstract class Animal {
public abstract void sleep();public abstract void eat();}
class Lion extends Animal {
public void sleep() {
System.out.println("Lion sleeping!");}
public void eat() {
System.out.println("Lion eating!");}}
class Tiger extends Animal {
public void sleep() {
System.out.println("Tiger sleeping!")}
public void eat() {
System.out.println("Tiger eating!");}}
class Deer extends Animal {
public void sleep() {
System.out.println("Deer sleeping!");}
public void eat() {
System.out.println("Deer eating!");}}
public class test1 {
public static void main(String[] args) {
Lion l = new Lion();Tiger t = new Tiger();
Deer d = new Deer();
l.eat();l.sleep();d.sleep();
d.eat();t.eat();t.sleep()}
}

Output:
WAP to implement abstract class with
Manager,Programmer,Employee
Code:
abstract class Employee {
public abstract void calculateSalary();
public abstract void displayInfo();
}
class Manager extends Employee {
String name, department;
int age;
double salary = 0.0;

Manager(String name, int age) {


this.name = name;
this.age = age;
}

public void calculateSalary() {


if (age > 25 && age < 40) {
salary = 10000.00;
} else if (age >= 40 && age < 60) {
salary = 20000.00;
}
}

public void displayInfo() {


System.out.println("Name:" + name + "\nAge:" + age +
"\nSalary:" + salary);
}
}

class Programmer extends Employee {


String name, department;
int age;
double salary = 0.0;

Programmer(String name, int age) {


this.name = name;
this.age = age;
}

public void calculateSalary() {


if (age > 25 && age < 40) {
salary = 8000.00;
} else if (age >= 40 && age < 60) {
salary = 18000.00;
}
}

public void displayInfo() {


System.out.println("Name:" + name + "\nAge:" + age +
"\nSalary:" + salary);
}
}

public class test2 {


public static void main(String[] args) {
Programmer p = new Programmer("Prog1", 30);
Manager m = new Manager("Mang1", 50);
p.calculateSalary();
p.displayInfo();
m.calculateSalary();
m.displayInfo();
}
}
Output:
Index
No. Program Name Page Date Signature
WAP to implement multiple inheritance using
interface in java
Code:
interface printable {
void print();
}

interface showable {
void show();
}

class A implements printable, showable {


public void print() {
System.out.println("Print called");
}

public void show() {


System.out.println("Show called");
}
}

class test {
public static void main(String[] args) {
A obj = new A();
obj.show();
obj.print();
}
}
Output:
WAP to implement interface with circle & rectangle
Code:
interface Shape {
void input();void area();
}
class Circle implements Shape {
int r = 0;double pi = 3.14, ar = 0;
public void input() {
r = 5;}
public void area() {
ar = pi * r * r;
System.out.println("Area of circle:" + ar);}}
class Rectangle extends Circle {
int l = 0, b = 0;double ar;
public void input() {
super.input();
l = 6;b = 4;}
public void area() {
super.area();
ar = l * b;
System.out.println("Area of rectangle:" + ar);}}
public class test {
public static void main(String[] args) {
Rectangle obj = new Rectangle();
obj.input();
obj.area();}}

Output:
WAP to implement threading using thread class
Code:
public class test extends Thread {
public static void main(String[] args) {
test obj = new test();
obj.start();
System.out.println("Outside the thread");
}

public void run() {


System.out.println("Running inside the thread");
}
}

Output:
WAP to implement runnable interface
Code:
public class test implements Runnable {
public static void main(String[] args) {
class1 obj = new class1();
Thread t = new Thread(obj);
t.start();
System.out.println("Outside the thread");
}

public void run() {


System.out.println("Running inside the thread");
}
}

Output:
WAP to set priority of thread
Code:
public class test extends Thread {
public static void main(String[] args) {
test m1=new test();
test m2=new test();
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();
}
public void run(){
System.out.println("Running thread priority
"+Thread.currentThread().getPriority());
}
}

Output:
WAP to implement hollow square pattern
Code:
import java.util.*;

class test {
static void print_rectangle(int n, int m) {
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (i == 1 || i == n ||
j == 1 || j == m)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);
int row, col;
System.out.println("Enter the number of rows: ");
row = sc.nextInt();
System.out.println("Enter the number of columns: ");
col = sc.nextInt();
print_rectangle(row, col);
}
}

Output:
WAP to print pyramid pattern with star symbol
Code:
public class test {
public static void PyramidStar(int n) {
int a, b;
for (a = 0; a < n; a++) {
for (b = 0; b <= a; b++) {
System.out.print("* ");
}
System.out.println();
}
}

public static void main(String args[]) {


int k = 5;
PyramidStar(k);
}
}

Output:
WAP to print hollow pyramid pattern
Code:
class test {
public static void main(String args[]) {
int n = 6;
printPattern(n);
}
static void printPattern(int n) {
int i, j, k = 0;
for (i = 1; i <= n; i++) {
for (j = i; j < n; j++) {
System.out.print(" ");
}
while (k != (2 * i - 1)) {
if (k == 0 || k == 2 * i - 2)
System.out.print("*");
else
System.out.print(" ");
k++;}
k = 0;
System.out.println();}
for (i = 0; i < 2 * n - 1; i++) {
System.out.print("*");}}}

Output:
WAP to implement concurrency problem
Code:
public class test extends Thread {
public static int x = 0;

public static void main(String[] args) {


test obj = new test();
obj.start();
System.out.println("The value is " + x);
x++;
System.out.println("The value is " + x);
}

public void run() {


x++;
}
}

Output:
WAP to implement concurrency control with isAlive
Code:
public class test extends Thread {
public static int x = 0;

public static void main(String[] args) {


test obj = new test();
obj.start();
while (obj.isAlive()) {
System.out.println("Waiting ");
}
System.out.println("The value is " + x);
x++;
System.out.println("The value is " + x);
}

public void run(){


x++;
}
}

Output:
WAP to implement concurrency control by
synchronize method
Code:
class table {
synchronized void print(int n) {
for (int i = 1; i <= 5; i++) {
System.out.println(n * i);try {
Thread.sleep(400);} catch (Exception e) {
System.out.println(e);}}}}
class thd1 extends Thread {
table t;
thd1(table t) {this.t = t;}
public void run() {t.print(5);}}
class thd2 extends Thread {
table t;
thd2(table t) {this.t = t;}
public void run() {t.print(100);}}
public class test {
public static void main(String[] args) {
table obj = new table();
thd1 obj1 = new thd1(obj);
thd2 obj2 = new thd2(obj);
obj1.start();
obj2.start();
}
}

Output:

You might also like