0% found this document useful (0 votes)
2 views69 pages

JAVA Assignments

The document contains a series of Java programming assignments that cover various topics such as calculating volumes, temperature conversion, swapping integers, triangle area calculation, and more. Each assignment includes a code snippet and expected output. The assignments range from basic programming concepts to more complex tasks like sorting algorithms and finding maximum/minimum values in an array.

Uploaded by

dashsandeep100
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)
2 views69 pages

JAVA Assignments

The document contains a series of Java programming assignments that cover various topics such as calculating volumes, temperature conversion, swapping integers, triangle area calculation, and more. Each assignment includes a code snippet and expected output. The assignments range from basic programming concepts to more complex tasks like sorting algorithms and finding maximum/minimum values in an array.

Uploaded by

dashsandeep100
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
You are on page 1/ 69

Assignment-1

1.Write a program to enter length, breadth and height. Calculate and print
the volume of rectangle, square and cube.
Ans) import java.util.Scanner;
public class volume{
public static void main(String[] args){
int l,b,h,rect,sq,cb,n;
Scanner s= new Scanner(System.in);
System.out.println("Enter the length:");
l=s.nextInt();
System.out.println("Enter the breadth:");
b=s.nextInt();
System.out.println("Enter the height:");
h=s.nextInt();
rect=l*b;
System.out.println("The area of rectangle is:"+rect);
sq=l*l;
System.out.println("The area of square is:"+sq);
cb=l*b*h;
System.out.println("The volume of cube is:"+cb);
}}}
OUTPUT:-

2.Write a program to enter a temperature in celsius and convert and display it


in farhenhit equivalent.
Ans) import java.util.Scanner;
public class temp{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
float cel,far;
System.out.println("Enter the temperature in Celsius:");
cel=s.nextFloat();
far=((1.8f)*cel)+32;
System.out.println("Temperature in Farhenhit is:"+far);
}}
OUTPUT:-
3.Write a program to enter the 2 integers and swap them using the 3rd
variable.
Ans) import java.util.Scanner;
public class swap{
public static void main(String[] args){
int a,b,c;
Scanner s=new Scanner(System.in);
System.out.println("Enter the first number:");
a=s.nextInt();
System.out.println("Enter the second number:");
b=s.nextInt();
c=a;
a=b;
b=c;
System.out.println("After swapping first no. is "+a+" and second
no. is "+b);
}}
OUTPUT:-
4.Write a program to enter 2 digit integers and swap them using bitwise operator.
Ans) import java.util.Scanner;
public class Swapit{
public static void main(String[] args){
int a,b;
Scanner s=new Scanner(System.in);
System.out.println("enter the first no.:");
a=s.nextInt();
System.out.println("Enter the second no.:");
b=s.nextInt();

a=a^b;
b=a^b;
a=a^b;
System.out.println("the first no. is "+a+" the second number is "+b);
}}
OUTPUT: -
5.Write a program to enter the 3 sides of a triangle and print the area of the triangle.
Ans) import java.util.Scanner;
public class Area{
public static void main(String[] args){
int a,b,c;
double p,area;
Scanner s=new Scanner(System.in);
System.out.println("Enter the three side:");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
p=(a+b+c)/2;
area=Math.sqrt(p*(p-a)*(p-b)*(p-c));
System.out.println("the area of the triangle is: "+area);
}}
OUTPUT: -

………….
Assignment-2

1.Write a java program that reads an integer between 100 and 999 and adds
all the digits in the integer.
Ans) public class Sum{
public static void main(String[] args){
int i,sum=0;
for(i=101;i<999;i++){
sum=sum+i;
}
System.out.println("the sum of integer between 100 and 999 is:
"+sum);
}}

OUTPUT: -
2.Write a java program to compute the body mass index (BMI).
Ans) import java.util.Scanner;
public class Bmi{
public static void main(String[] args){
double a,b,bmi;
Scanner s=new Scanner(System.in);
System.out.println("Enter your weight in kg:");
a=s.nextDouble();
System.out.println("Enter your height in meter:");
b=s.nextDouble();
bmi=a/Math.pow(b,2);
System.out.println("BMI is "+bmi);
}}
OUTPUT: -
3.Write a program to enter required values and calculate volume of cube and cuboid.
Ans) import java.util.Scanner;
public class Volume2{
public static void main(String[] args){
int a,b,c,vol;
Scanner s=new Scanner(System.in);
System.out.println("Enter the length,breadthand height of cube or cuboid:");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
vol=a*b*c;
System.out.println("the volume of cube or cuboid is "+vol);
}}

OUTPUT: -
4.Write a program to enter a number x and calculate x>>3 and x<<2.
Ans) import java.util.Scanner;
public class I{
public static void main(String[] args){
int a,b,x;
Scanner s= new Scanner(System.in);
System.out.println("Enter the value of x:");
x=s.nextInt();
a=x>>3;
System.out.println("x value after rightshift:"+a);
b=x<<2;
System.out.println("x value after leftshift:"+b);
}}

OUTPUT: -
5.Write a program to convert minutes into years and days.
Ans) import java.util.Scanner;
public class Min{
public static void main(String[] args){
int min,year,mon,day;
double a,b,c;
Scanner s=new Scanner(System.in);
System.out.println("enter the time value in minute:");
min=s.nextInt();
day=(int) min/(60*24);
year=day/365;
day=day%365;
mon=day/30;
day=day%30;
System.out.println("The actual time is:"+year+" year "+mon+" Month
"+day+" day ");
}}

OUTPUT: -
Assignment-3
1.Write a java program to solve a Quadratic equation(use if,elseif else).
Ans) import java.util.Scanner;
public class Eq{
public static void main(String[] args){
int a,b,c;
double d=0.0,p=0.0,q=0.0;
Scanner s=new Scanner(System.in);
System.out.println("Enter the coefficient of x2,x,constant:");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
d=Math.pow(b,2)-(4*a*c);
if(d>0){
p=(-b+Math.sqrt(d))/(2*a);
q=(-b-Math.sqrt(d))/(2*a);
System.out.println("the values of x is:"+p+"and"+q);
}
else if(d==0){
p=q=-b/(2*a);
System.out.println("the values of x is:"+p+"and"+q);
}
else{
System.out.println("imaginary value can't give output");
}
}}
Output:-
2.Write a java program that reads two floating point numbers and test whether they are
same upto three decimal places.
Ans) import java.util.Scanner;
public class Match{
public static void main(String[] args){
float a,b;
int p,q;
Scanner s=new Scanner(System.in);
System.out.println("Enter two floating point number:");
a=s.nextFloat();
b=s.nextFloat();
p=(int)a*1000;
q=(int)b*1000;
if(p==q){
System.out.println(a+" is same number with number "+b);
}
else{
System.out.println(a+" is not same number with number "+b);
}
}}
OUTPUT:-
3.Write a java program to find the no. of days in a month.
Ans) import java.util.Scanner;
public class Month{
public static void main(String[] args){
int n,o;
Scanner s=new Scanner(System.in);
System.out.println("Enter the month number:");
n=s.nextInt();
switch(n){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("No. of days in the "+n+" number month is 31 days.");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("No. of days in the "+n+" number month is 30 days.");
break;
case 2:
System.out.println("Enter the year:");
o=s.nextInt();
if(o%4==0){
System.out.println("No. of days in the "+n+" number month is 29 days.");
}
else{
System.out.println("No. of days in the "+n+" number month is 28 days.");
}
break;
default:
System.out.println("Invalid Number");
}}}
OUTPUT: -
4.Write a java program which reads three numbers from the user and prints the greatest
number.
Ans) import java.util.Scanner;
public class Great{
public static void main(String[] args){
int a,b,c;
Scanner s =new Scanner(System.in);
System.out.println("Enter the three numbers:");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
if(a>b && a>c){
System.out.println(a+" is the greatest number.");
}
else if(a<b && b>c){
System.out.println(b+" is the greatest number.");
}
else{
System.out.println(c+" is the greatest number.");
}
}}
OUTPUT: -
5.Write a java program that requires the user to enter a single character from the alphabet
to print vowel or consonant depending on user input.
Ans) import java.util.Scanner;
public class Alpha{
public static void main(String[] args){
char c;
Scanner s= new Scanner(System.in);
System.out.println("enter a character:");
c=s.next().charAt(0);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||
c=='U'){
System.out.println("It is a vowel.");
}
else{
System.out.println("It is a consonant.");
}}}
OUTPUT:-
Assignment-4
1.Write a java program to print the following pattern:
12345
1234
123
12
1
Ans) public class Pattern{
public static void main(String args[]){
int i,j,n=5;
for(i=0;i<5;i++){
for(j=1;j<=n;j++){
System.out.print(j+" ");
}
n--;
System.out.println();
}}}

Output:-
2.Write a program to calculate mathematical constant 'e' using a formula: e=1+1/2!+1/3!
+.................+upto 5.
Ans) public class Fact{
public static void main(String args[]){
int i;
double f=1.0,e=0.0;
for(i=1;i<=5;i++){
f=f*i;
e+=1/f;
}
System.out.println("The value of e is: "+e);
}}

OUTPUT:-

3.Write a java program to sort the elements using bubble sort.


Ans) import java.util.Scanner;
public class Bubble{
public static void main(String args[]){
int i,j,s,n;
int a[]=new int[5];
Scanner p=new Scanner(System.in);
n=5;
System.out.println("Enter the array numbers:");
for(i=0;i<n;i++){
a[i]=p.nextInt();
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
s=a[i];
a[i]=a[j];
a[j]=s;
}}}
System.out.println("The values of array after sorting:");
for(i=0;i<n;i++){
System.out.print(a[i]+" ");
}}}
OUTPUT:-
4.Write a java program to search an element using binary search.
Ans) import java.util.Scanner;
public class Binary{
public static void main(String args[]){
int i,j,k,l,first,mid,end;
Scanner s=new Scanner(System.in);
int a[]= new int[5];
int n=5;
System.out.println("Enter the value of array:");
for(i=0;i<n;i++){
a[i]=s.nextInt();
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
k=a[i];
a[i]=a[j];
a[j]=k;
}}}
System.out.println("The values of array after sorting:");
for(i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.println("Enter the element to be found:");
l=s.nextInt();
first=0;end=5;
mid=(first+end)/2;
while(first<=end){
if(a[mid]<l){
first=mid+1;
}
else if(a[mid]==l){
System.out.println("the given element present");
break;
}
else{
end=mid-1;
}
mid=(first+end)/2;
}
if(first>end){
System.out.println("Element not found");
}}}

OUTPUT:-
5.Write a java program to create and display unique three-digit number using 1,2,3,4.How
many three digit numbers are there.
Ans) public class Display{
public static void main(String args[]){
int am=0;
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
for(int k=1;k<=4;k++){
if(k!=i && k!=j && i!=j){
am++;
System.out.println(i+" "+j+" "+k);
}}}}
System.out.println("The total no. of three digit no. is: "+am);
}}
OUTPUT:-
Assignment-5
1.Write a java program to short a int array.

Ans) import java.util.Scanner;

public class Short1 {

public static void main(String args[]) {

Scanner s = new Scanner(System.in);

int a[] = new int[5];

int i, j, k;

System.out.println("Enter the values of Array:");

for (i = 0; i < 5; i++) {

a[i] = s.nextInt();

for (i = 0; i < 5; i++) {

for (j = i + 1; j < 5; j++) {

if (a[i] > a[j]) {

k = a[i];

a[i] = a[j];

a[j] = k;

}}}

System.out.println("The values after shorting is:");

for (i = 0; i < 5; i++) {

System.out.print(a[i] + " ");

}}}

OUTPUT:-
2.Write a java program to calculate a average value of array element.

Ans) import java.util.Scanner;

public class Avg {

public static void main(String args[]) {

Scanner s = new Scanner(System.in);

int i, sum = 0, p;

int a[] = new int[5];

System.out.println("Enter the values of array:");

for (i = 0; i < 5; i++) {

a[i] = s.nextInt();

for (i = 0; i < 5; i++) {

sum += a[i];

p = sum / 5;

System.out.println("The average of the array elements is " + p);

}}

OUTPUT:-
3.Write a java program to find the maximum and minimum value of an array.

Ans) import java.util.Scanner;

public class Minmax{

public int min(int a[],int n){

int i,k=a[0];

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

if(k>a[i]){

k=a[i];

}}

return k;

public int max(int a[],int n){

int i,j,k=a[0];

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

if(k<a[i]){

k=a[i];

}}

return k;

public static void main(String args[]){

Minmax l=new Minmax();


Scanner s =new Scanner(System.in);

int i,p,r;

System.out.println("Enter total no. of numbers:");

int n=s.nextInt();

int a[]=new int[n];

System.out.println("Enter the values of array:");

for(i=0;i<n;i++){

a[i]=s.nextInt();

p=l.max(a,n);

r=l.min(a,n);

System.out.println("maximum number of the given array is "+p);

System.out.println("Minimum number of the given array is "+r);

}}

OUTPUT:-

4.Write a java program to find duplicates values in an array of integral values.

Ans) import java.util.Arrays;

import java.util.Scanner;

public class Dup {


public static void main(String args[]) {

int i, j, n;

Scanner s = new Scanner(System.in);

System.out.println("Enter the no. of total elements:");

n = s.nextInt();

int a[] = new int[n];

System.out.println("Enter the values of array:");

for (i = 0; i < n; i++) {

a[i] = s.nextInt();

for (i = 0; i < a.length - 1; i++) {

for (j = i + 1; j < a.length; j++) {

if ((a[i] == a[j]) && (i != j)) {

System.out.println("Duplicate Element is:" + a[j]);

}}}}}

OUTPUT:-

5.Write a java program to find common elements between two arrays(String values).

Ans) import java.util.Scanner;

import java.util.*;

public class Common{


public static void main(String args[]){

Scanner s=new Scanner(System.in);

String a[]=new String[4];

String b[]=new String[4];

System.out.println("Enter the values 1st array:");

for(int i=0;i<4;i++){

a[i]=s.next();

System.out.println("Enter the values 2nd array:");

for(int i=0;i<4;i++){

b[i]=s.next();

System.out.print("Common element:[");

for(int i=0;i<4;i++){

for(int j=0;j<4;j++){

if(a[i].equals(b[j]))

System.out.print(a[i]+",");

}}

System.out.print("]");

}}

OUTPUT:-
Assignment-6
1.Write a program to create a class called employee with name,job and salary as variables.

Create a method that calculate and update the salary according to the number of working days.

Ans) import java.util.Scanner;

public class Employee {

String name;

int salary, jobid;

public int calculate(int a) {

salary = 60000;

int n = salary / 30;

int p = a * n;

return p;
}

public void update(String s, int p, int a) {

System.out.println(s + " " + p + " " + a);

public static void main(String args[]) {

Employee n = new Employee();

Scanner s = new Scanner(System.in);

int l, work;

System.out.println("Enter name:");

n.name = s.next();

System.out.println("Enter the jobid:");

n.jobid = s.nextInt();

System.out.println("Enter the no. of days working:");

work = s.nextInt();

l = n.calculate(work);

n.update(n.name, n.jobid, l);

}}

OUTPUT:-
2. Write a program to create a class called student with name,rollno and course as attribute. Create
one method that provide options for courses and another method that display the name,rollno,
courses selected.

Ans) import java.util.Scanner;

public class Student {

String name, course;

int roll;

public String allcourse() {

int n;

String p = "";

Scanner f = new Scanner(System.in);

System.out.println();

System.out.println("SELECT A COURSE:");

System.out.println("1.DANCE");

System.out.println("2.MUSIC");

System.out.println("3.GAME");

System.out.println("4.COMPUTER");

System.out.println("Enter a no.:");

n = f.nextInt();

switch (n) {

case 1:

p = "DANCE";

break;

case 2:

p = "MUSIC";

break;

case 3:

p = "GAME";

break;

case 4:

p = "COMPUTER";

break;
default:

System.out.println("Invalid input");

return p;

public void display(String s, int r, String c) {

System.out.println("Name is '" + s + "' roll no. '" + r + "' course '" + c + "'");

public static void main(String args[]) {

Scanner s = new Scanner(System.in);

Student n = new Student();

System.out.println("Enter Name:");

n.name = s.next();

System.out.println("Enter Rollno.:");

n.roll = s.nextInt();

System.out.println();

String c = n.allcourse();

n.display(n.name, n.roll, c);

}}

OUTPUT:-
3. Write a program to create a static and non-static method where the static method calculates
area of rectangle and non-static method calculates area of a square

Ans) import java.util.Scanner;

public class Area {

public static int calculate(int a, int b) {

int area;

area = a * b;

return area;

public int calculate(int a) {

int area;

area = a * a;

return area;

public static void main(String args[]) {

Scanner s = new Scanner(System.in);

Area n = new Area();

int a, b, c;

System.out.println("Enter the Sides of Rectangle or Square:");


a = s.nextInt();

b = s.nextInt();

if (a == b) {

c = n.calculate(a);

} else {

c = calculate(a, b);

System.out.println("The area of rectangle or Square is: " + c);

OUTPUT:-

4. Write a program to overload a method that calculates the simple interest according to time and
principal amount provided with default rate and according to rate and time provided with default
principal amount.

Ans) import java.util.Scanner;

public class SI {

public double interest(int p, double t) {

double r = 4.5;

double i = (p * t * r) / 100;

return i;

public double interest(double r, double t) {

int p = 10000;
double i = (p * t * r) / 100;

return i;

public static void main(String args[]) {

Scanner s = new Scanner(System.in);

SI n = new SI();

int p, o;

double a, b, t, r;

System.out.println("To see the interest,What information you have:");

System.out.println("1.principal and time or");

System.out.println("2.rate and Time");

System.out.println("Enter the number accordingly:");

o = s.nextInt();

switch (o) {

case 1:

System.out.println("Enter the principal amount:");

p = s.nextInt();

System.out.println("Enter the time in years:");

t = s.nextDouble();

a = n.interest(p, t);

System.out.println("The interest is:" + a);

break;

case 2:

System.out.println("Enter the rate and time:");

r = s.nextDouble();

t = s.nextDouble();

b = n.interest(r, t);

System.out.println("The interest is :" + b);

break;

default:

System.out.println("Invalid Number!");
}}}

OUTPUT:-

5.Write a program to create a class called library that has methods to add and remove books from
the library.

Ans) import java.util.Scanner;


public class Library {

public void add(String a[], int b[], String c[], int x) {

System.out.println("book is Added");

for (int i = 0; i < x; i++) {

System.out.println(a[i] + " " + b[i] + " " + c[i]);

public void remove(String a[], int b[], String c[], int x) {

Scanner s = new Scanner(System.in);

System.out.println("Enter bookid for remove:");

int p = s.nextInt();

for (int i = 0; i < x; i++) {

if (p == b[i]) {

int j=i+1;

b[i] = b[j];

a[i] = a[j];

c[i] = c[j];

System.out.println("ITEM REMOVED");

for (int i = 0; i < x-1; i++) {

System.out.println(a[i] + " " + b[i] + " " + c[i]);

public static void main(String args[]) {

Scanner s = new Scanner(System.in);

Library n = new Library();

int i;

System.out.println("Enter the no. of books: ");

int x = s.nextInt();

String a[] = new String[x];


int b[] = new int[x];

String c[] = new String[x];

System.out.println("Enter the book details:");

for (i = 0; i < x ; i++) {

a[i] = s.next();

b[i] = s.nextInt();

c[i] = s.next();

n.add(a, b, c, x);

n.remove(a, b, c, x);

OUTPUT:-

Assignment-7
1. Write a java program to create a class called person with a name and age attribute.Create two
instance of the person class,set their attributes using the constructor, and print their name and
age.

Ans) import java.util.Scanner;

public class Person {

String name;

int age;

Person(String a, int b) {

name = a;

age = b;

public void display() {

System.out.println(name + " " + age);

public static void main(String args[]) {

Scanner s = new Scanner(System.in);

System.out.println("Enter name:");

String a = s.next();

System.out.println("Enter age:");

int b = s.nextInt();

Person p1 = new Person(a, b);

p1.display();

OUTPUT:-
2. Write a java program to create a class called rectangle with width and height attribute. Calculate
the area and perimeter of the rectangle using two different methods.

Ans) import java.util.Scanner;

public class Rectangle {

int width, height;

Rectangle(int w, int h) {

width = w;

height = h;

public void perimeter() {

System.out.println("Perimeter of the rectangle is: " + 2 * (height + width));

public void area() {

System.out.println("Area of the rectangle is: " + width * height);

public static void main(String args[]) {

Scanner s = new Scanner(System.in);

System.out.println("Enter the Width and height of the rectangle:");

int w = s.nextInt();

int h = s.nextInt();

Rectangle r = new Rectangle(w, h);

r.perimeter();

r.area();

OUTPUT:-
3. Write a java program to create a class called employee with a name, job and hire date attribute
and a method to calculate year of service.

Ans) import java.util.Scanner;

public class Employee{

String name,job;

int year;

Employee(String n,String j,int y){

name=n;

job=j;

year=2023-y;

public void display(){

System.out.println(" Name is: "+name+" JOB is: "+job+" year of service "+year);

public static void main(String args[]){

Scanner s= new Scanner(System.in);

System.out.println("Enter name:");

String a=s.next();

System.out.println("Enter job name:");

String b=s.next();

System.out.println("Enter hire_year:");

int c= s.nextInt();

Employee e=new Employee(a,b,c);

e.display();

}}

OUTPUT:-
4. Write a java program to create a class called movie with attribute title, director, actor and review
and method for adding and retrieving review.

Ans) import java.util.Scanner;

public class Movie {

public void add(String a[], int i) {

System.out.println("Review added");

public void retrive(String a[], int i) {

for (int j = 0; j < i; j++) {

System.out.println(a[j]);

}}

public static void main(String args[]) {

Movie m = new Movie();

Scanner s = new Scanner(System.in);

String title, director, actor;

int j;

System.out.println("Enter the no. reviews:");

int i = s.nextInt();

String a[] = new String[i];

System.out.println("Enter the title:");

title = s.next();

System.out.println("Enter director name:");

director = s.next();

System.out.println("Enter actor name:");

actor = s.next();

System.out.println("Enter the Review:");

for (j = 0; j < i; j++) {

a[j] = s.next();

m.add(a, i);

m.retrive(a, i);
}

OUTPUT:-

5. Write a java program to create a class called circle with a radius attribute.

You can access and modify these attribute. Calculate the area and circumference of a circle.

Ans) import java.util.Scanner;

public class Circle {

int radius;

Circle(int r) {

radius = r;

public void Area() {

double area = 3.14 * Math.pow(radius, 2);

System.out.println("Area of a circle is " + area);

public void circumference() {

double c = 2 * 3.14 * radius;

System.out.println("Circumference of circle is " + c);


}

public static void main(String args[]){

Scanner s=new Scanner(System.in);

System.out.println("Enter the value of radius: ");

int p=s.nextInt();

Circle c=new Circle(p);

c.Area();

c.circumference();

OUTPUT:-
ASSIGNMENT-8
1.Write a program to create class called Vehicle with a method called drive(). Create a subclass
called Car that overrides the drive method to print "repairing the car".

Ans) class Vehicle {

void drive() {

System.out.println("Repairing.");

class Car extends Vehicle {

void drive() {

System.out.println("Repairing the car.");

public static void main(String args[]) {

Car c = new Car();

c.drive();

Output:-

2.Write a java program to create a class known as "BankAccount" with methods called deposite()
and withdraw(). create a subclass called SavingAccount that overrides the withdraw() method to
prevent withdrawls if the account balance falls below.

Ans) import java.util.Scanner;

class BankAccount {

static int amt;

BankAccount(int y) {

amt = y;

}
void deposite(int x) {

amt = amt + x;

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

void withdraw(int v) {

System.out.println("Amount withdrawed.");

class SavingAccount extends BankAccount {

int ac;

String name;

SavingAccount(int x, int b, String p) {

super(b);

ac = x;

name = p;

void withdraw(int v) {

System.out.println(amt);

if ((amt - v) < 3000) {

System.out.println("Amount can't be Wthdraw");

} else {

amt = amt - v;

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

public static void main(String args[]) {

Scanner s = new Scanner(System.in);

int f;

System.out.println("Enter Account no:");

int ac = s.nextInt();

System.out.println("Enter Name:");
String a = s.next();

System.out.println("Enter the Available Amount:");

int b = s.nextInt();

SavingAccount sa = new SavingAccount(ac, b, a);

while (b >= 0) {

System.out.println("enter a no.:");

System.out.printf("1.Deposite \n2.Wthdraw \n3.exit \n");

System.out.println("Enter a no.:");

int n = s.nextInt();

switch (n) {

case 1:

System.out.println("Enter the amount to be deposited:");

f = s.nextInt();

sa.deposite(f);

break;

case 2:

System.out.println("Enter the amount to Withdraw:");

int v = s.nextInt();

sa.withdraw(v);

break;

case 3:

break;

default:

System.out.println("Invalid Number!");

if (n == 3) {

break;

}}

OUTPUT:-
3.Write a java program to create a class known as Person with methods called getFirstName() and
getLastName(). create a subclass called Employee that adds a new method named getEmployeeId()
and overrides the getLastName() method to include the employee's job title.

Ans) import java.util.Scanner;

class Person {

void getFirstName(String a) {

System.out.println("Name is " + a);

void getLastName(String a) {

System.out.println(a);

class Employee extends Person {

void getEmployeeId(int a) {

System.out.println("EmployeeId is " + a);

void getLastName(String a) {

System.out.println("job title is " + a);

public static void main(String args[]) {

Scanner s = new Scanner(System.in);

System.out.println("Enter your firstname:");

String a = s.next();

System.out.println("Enter your Employeeid:");

int c = s.nextInt();

System.out.println("Enter your job titile:");

String d = s.next();

Employee e = new Employee();

e.getFirstName(a);

e.getLastName(d);

e.getEmployeeId(c);

}}
OUTPUT:-

4.Write a java program to create a class called Shape with methods called getPerimeter() and
getArea(). Create a subclass called Circle that overrides the getPerimeter() and getArea() methods
to calculate the area and perimeter of a circle.

Ans) import java.util.Scanner;

class Shape {

void getPerimeter(int r) {

System.out.println(r);

void getArea(int r) {

System.out.println(r);

class Circle extends Shape {

void getPerimeter(int r) {

double a = 2 * 3.14 * r;

System.out.println("Perimeter is: " + a);

void getArea(int r) {

double a = 3.14 * (r * r);

System.out.println("Area is: " + a);


}

public static void main(String args[]) {

Scanner s = new Scanner(System.in);

System.out.println("Enter radius:");

int r = s.nextInt();

Circle c = new Circle();

System.out.printf("1.Perimeter \n2.Area\nEnter a no.:");

int n = s.nextInt();

switch (n) {

case 1:

c.getPerimeter(r);

break;

case 2:

c.getArea(r);

break;

default:

System.out.println("Invalid Number!");

OUTPUT:-
5.Write a java program to create a vehicle class hierarchy.The base class should be Vehicle ,with
subclass truck , Car and motocycle.Each subclass should have properties such as
make,model,year,and fuel type. Implementation methods for calculating fuel efficiency ,distance
traveled ,and maximum speed.

Ans) import java.util.Scanner;

class Vehicle {

void print(String a,int b,String c,String d){}

void Fuelef(int d,int f){}

void disttvd(int d){}

void maxspd(int d,int t){}

class Truck extends Vehicle {

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

System.out.println("model:"+a+"year"+b+"fueltype"+c+"company"+d);

void Fuelef(int d,int f){

double eff=d/f;

System.out.println("fuel Efficiency is"+eff+"km/li");

void disttvd(int d){

System.out.println("distance travelled"+d+"km");

void maxspd(int d,int t){

double spd=d/t;

System.out.println("speed is:"+spd+"km/h");
}

class Car extends Vehicle {

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

System.out.println("model:"+a+"year"+b+"fueltype"+c+"company"+d);

void Fuelef(int d,int f){

double eff=d/f;

System.out.println("fuel Efficiency is"+eff+"km/li");

void disttvd(int d){

System.out.println("distance travelled"+d+"km");

void maxspd(int d,int t){

double spd=d/t;

System.out.println("speed is:"+spd+"km/h");

class Motorcycle extends Vehicle {

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

System.out.println("model:"+a+"year"+b+"fueltype"+c+"company"+d);

void Fuelef(int d,int f){

double eff=d/f;

System.out.println("fuel Efficiency is"+eff+"km/li");

void disttvd(int d){

System.out.println("distance travelled"+d+"km");

void maxspd(int d,int t){

double spd=d/t;
System.out.println("speed is:"+spd+"km/h");

public class Distance {

public static void main(String args[]) {

Scanner s = new Scanner(System.in);

Motorcycle m = new Motorcycle();

Truck t = new Truck();

Car c = new Car();

System.out.println("Choose a number acc. to vehicle type");

System.out.printf("1.Motorcycle \n2.Car\n3.Truck\n");

System.out.println("Enter a no.:");

int a = s.nextInt();

switch (a) {

case 1:

System.out.println("Enter Model:");

String b = s.next();

System.out.println("Enter year:");

int d = s.nextInt();

System.out.println("Enter Fueltype:");

String e = s.next();

System.out.println("Enter company:");

String f = s.next();

System.out.println("enter distance travelled:");

int di=s.nextInt();

System.out.println("Enter time taken:");

int ti=s.nextInt();

System.out.println("Enter total fuel used in lit.");

int fu=s.nextInt();

m.print(b,d,e,f);

m.Fuelef(di,fu);
m.disttvd(di);

m.maxspd(di,ti);

break;

case 2:

System.out.println("Enter Model:");

b = s.next();

System.out.println("Enter year:");

d = s.nextInt();

System.out.println("Enter Fueltype:");

e = s.next();

System.out.println("Enter company:");

f = s.next();

System.out.println("enter distance travelled:");

di=s.nextInt();

System.out.println("Enter time taken:");

ti=s.nextInt();

System.out.println("Enter total fuel used in lit.");

fu=s.nextInt();

c.print(b,d,e,f);

c.Fuelef(di,fu);

c.disttvd(di);

c.maxspd(di,ti);

break;

case 3:

System.out.println("Enter Model:");

b = s.next();

System.out.println("Enter year:");

d = s.nextInt();

System.out.println("Enter Fueltype:");

e = s.next();

System.out.println("Enter company:");
f = s.next();

System.out.println("enter distance travelled:");

di=s.nextInt();

System.out.println("Enter time taken:");

ti=s.nextInt();

System.out.println("Enter total fuel used in lit.");

fu=s.nextInt();

t.print(b,d,e,f);

t.Fuelef(di,fu);

t.disttvd(di);

t.maxspd(di,ti);

break;

default:

System.out.println("Invalid number!");

}}}

OUTPUT:-
Assignment-9
1.Write a program to implememt polymorphism.

(NOTICE: Consider a scenerio Bank is a class that provides methods to get the rate of interest.but
rate of interest may difer according to banks. for example: SBI,ICICI,AXIS ARE PROVIDING
8.4%,7.3%,9.7% RATE OF INTEREST.)

Ans) class Bank{

int p=100000;

int t=1;

float getrate(){

return 0;

class SBI extends Bank{

float getrate(){

float r=8.4f;

float i=(p*r*t)/100;

return i;

class ICICI extends Bank{

float getrate(){

float r=7.3f;

float i=(p*r*t)/100;

return i;

class AXIS extends Bank{

float getrate(){

float r=9.7f;

float i=(p*r*t)/100;

return i;
}

public class Total{

public static void main(String args[]){

Bank b;

b=new SBI();

float a=b.getrate();

b=new ICICI();

float c=b.getrate();

b=new AXIS();

float d=b.getrate();

System.out.println("Amount in SBI"+a);

System.out.println("Amount in ICICI"+c);

System.out.println("Amount in AXIS"+d);

OUTPUT:-

2.Write a java program to demonstrate multiple inheritance through default method(using


interface).

Ans) interface Printable{

void print();

default void prints(){

System.out.println("Printing default");

interface Showable{

void show();
default void shows(){

System.out.println("Showing default");

class Multi implements Printable,Showable{

public void print(){

System.out.println("Printing");

public void show(){

System.out.println("Showing");

public static void main(String args[]){

Multi m=new Multi();

m.print();

m.show();

m.prints();

m.shows();

OUTPUT:-
3.Write a program to demonstrate the use of this keyword.

Ans) class Arith{

Arith(){

System.out.println("Adding...");

Arith(int x){

this();

int a,b;

void input(int a,int b){

this.a=a;

this.b=b;

int add(int c,int d){

this.input(c,d);

return a+b;

public class Add{

public static void main(String args[]){

Arith a=new Arith(5);

int p=a.add(5,6);

System.out.println(p);

OUTPUT:-
4.write a program to implement the static keyword and represent all its uses.

Ans) public class CheckStatic {

static String college= "GIET";

static {

System.out.println("statis block is created.");

static void eat(){

System.out.println("Eating.........");

public static void main(String[] args) {

System.out.println(college);

eat();

OUTPUT:-

5.Write a program to demonstrate the hybrid inheritance by combing multilevel and single
inheritance.

Ans)class Animal{

void eat(){

System.out.println("Eating...");

class Dog extends Animal{


void bark(){

System.out.println("barking...");

class Cat extends Dog{

void sleep(){

System.out.println("sleeping...");

class Lion extends Animal{

void roar(){

System.out.println("roaring...");

class Mul{

public static void main(String args[]){

Cat c=new Cat();

c.eat();

c.bark();

c.sleep();

Lion l=new Lion();

l.eat();

l.roar();

OUTPUT:-
Assignment-10
1. Write a java program to compare two strings lexicographically. Two strings are lexicographically
equal if they are the same length and contain the same characters in the same positions.

Ans) import java.util.Scanner;

public class Lexicographical {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.print("Enter the String 1: ");

String str1 = s.nextLine();

System.out.print("Enter the String 2: ");

String str2 = s.nextLine();

int a = str1.compareTo(str2);

if (a < 0) {

System.out.println(str1+ " is greater than "+ str2);

} else if (a == 0) {

System.out.println(str1+" is equal to "+str2);

} else {

System.out.println(str1+ " is less than "+str2);

OUTPUT:-
2. Write a java program to check whether two String objects contain the same data or not without
using any comparison method.

Ans) import java.util.Scanner;

public class Compare {

public static void main(String args[])

int i,flag=1,len1,len2;

String s1,s2;

Scanner sc = new Scanner(System.in);

System.out.println("Enter the 1st string");

s1=sc.nextLine();

System.out.println("Enter the 2nd string");

s2=sc.nextLine();

len1=s1.length();

len2=s2.length();

char str1[] = s1.toCharArray();

char str2[] = s2.toCharArray();

if(len1==len2){

for(i=0;i<len1;i++){

if(str1[i]!=str2[i]){

flag=0;

break;

}
}else{

flag=0;

if(flag==1){

System.out.println("The two string are EQUAL!!!");

}else{

System.out.println("The two string are NOT EQUAL!!!");

OUTPUT:-

3. Write a java program to get the index of all the characters of the alphabet in a given string.

Ans) import java.util.Scanner;

public class Ind{

public static void main(String args[]){

Scanner s=new Scanner(System.in);

System.out.println("Enter String:");

String a=s.next();

a=a.toLowerCase();

char b[]=a.toCharArray();

char c[]=a.toCharArray();

char q;

int len1=a.length();

for(int i=0;i<len1;i++){
for(int j=i+1;j<len1;j++){

if(b[i]>b[j]){

q=b[i];

b[i]=b[j];

b[j]=q;

for(int i=0;i<len1;i++){

System.out.println(b[i]+"="+a.indexOf(c[i]));

}}

OUTPUT:-

4. Write a java program to find the given string is Pallindrome or not using String class.

Ans) import java.util.Scanner;

public class Pallindrome{

public static void main(String[] args) {

Scanner s=new Scanner(System.in);

System.out.println("Enter a String:");

String a=s.nextLine();

String b="";
int len1=a.length();

for(int i=len1-1;i>=0;i--){

b=b+a.charAt(i);

if(a.equals(b)){

System.out.println("String is pallindrome");

else{

System.out.println("String is not pallindrome.");

OUTPUT:-

5. Write a java program to find the second most frequent character in a given string.

Ans) import java.util.Scanner;

public class Freq{

char smfreq(String a){

int n=256;

int count[]=new int[n];

int i;

char t;

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

(count[a.charAt(i)])++;

int fir=0,sec=0;
for(i=0;i<n;i++){

if(count[i]>count[fir]){

sec=fir;

fir=i;

else if(count[i]>count[sec]&&count[i]!=count[fir]){

sec=i;

t=(char)sec;

return t;

public static void main(String[] args) {

Freq f= new Freq();

Scanner s=new Scanner(System.in);

System.out.println("Enter a string:");

String a=s.next();

char c=f.smfreq(a);

if(c!='\0'){

System.out.println("Second most frequent is "+c);

else{

System.out.println("No second most frequent character");

OUTPUT:-
****Thank You****

You might also like