JAVA Assignments
JAVA Assignments
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:-
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:-
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.
int i, j, k;
a[i] = s.nextInt();
k = a[i];
a[i] = a[j];
a[j] = k;
}}}
}}}
OUTPUT:-
2.Write a java program to calculate a average value of array element.
int i, sum = 0, p;
a[i] = s.nextInt();
sum += a[i];
p = sum / 5;
}}
OUTPUT:-
3.Write a java program to find the maximum and minimum value of an array.
int i,k=a[0];
for(i=1;i<n;i++){
if(k>a[i]){
k=a[i];
}}
return k;
int i,j,k=a[0];
for(i=1;i<n;i++){
if(k<a[i]){
k=a[i];
}}
return k;
int i,p,r;
int n=s.nextInt();
for(i=0;i<n;i++){
a[i]=s.nextInt();
p=l.max(a,n);
r=l.min(a,n);
}}
OUTPUT:-
import java.util.Scanner;
int i, j, n;
n = s.nextInt();
a[i] = s.nextInt();
}}}}}
OUTPUT:-
5.Write a java program to find common elements between two arrays(String values).
import java.util.*;
for(int i=0;i<4;i++){
a[i]=s.next();
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.
String name;
salary = 60000;
int p = a * n;
return p;
}
int l, work;
System.out.println("Enter name:");
n.name = s.next();
n.jobid = s.nextInt();
work = s.nextInt();
l = n.calculate(work);
}}
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.
int roll;
int n;
String p = "";
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;
System.out.println("Name is '" + s + "' roll no. '" + r + "' course '" + c + "'");
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();
}}
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
int area;
area = a * b;
return area;
int area;
area = a * a;
return area;
int a, b, c;
b = s.nextInt();
if (a == b) {
c = n.calculate(a);
} else {
c = calculate(a, b);
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.
public class SI {
double r = 4.5;
double i = (p * t * r) / 100;
return i;
int p = 10000;
double i = (p * t * r) / 100;
return i;
SI n = new SI();
int p, o;
double a, b, t, r;
o = s.nextInt();
switch (o) {
case 1:
p = s.nextInt();
t = s.nextDouble();
a = n.interest(p, t);
break;
case 2:
r = s.nextDouble();
t = s.nextDouble();
b = n.interest(r, t);
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.
System.out.println("book is Added");
int p = s.nextInt();
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");
int i;
int x = s.nextInt();
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.
String name;
int age;
Person(String a, int b) {
name = a;
age = b;
System.out.println("Enter name:");
String a = s.next();
System.out.println("Enter age:");
int b = s.nextInt();
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.
Rectangle(int w, int h) {
width = w;
height = h;
int w = s.nextInt();
int h = s.nextInt();
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.
String name,job;
int year;
name=n;
job=j;
year=2023-y;
System.out.println(" Name is: "+name+" JOB is: "+job+" year of service "+year);
System.out.println("Enter name:");
String a=s.next();
String b=s.next();
System.out.println("Enter hire_year:");
int c= s.nextInt();
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.
System.out.println("Review added");
System.out.println(a[j]);
}}
int j;
int i = s.nextInt();
title = s.next();
director = s.next();
actor = s.next();
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.
int radius;
Circle(int r) {
radius = r;
int p=s.nextInt();
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".
void drive() {
System.out.println("Repairing.");
void drive() {
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.
class BankAccount {
BankAccount(int y) {
amt = y;
}
void deposite(int x) {
amt = amt + x;
void withdraw(int v) {
System.out.println("Amount withdrawed.");
int ac;
String name;
super(b);
ac = x;
name = p;
void withdraw(int v) {
System.out.println(amt);
} else {
amt = amt - v;
int f;
int ac = s.nextInt();
System.out.println("Enter Name:");
String a = s.next();
int b = s.nextInt();
while (b >= 0) {
System.out.println("enter a no.:");
System.out.println("Enter a no.:");
int n = s.nextInt();
switch (n) {
case 1:
f = s.nextInt();
sa.deposite(f);
break;
case 2:
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.
class Person {
void getFirstName(String a) {
void getLastName(String a) {
System.out.println(a);
void getEmployeeId(int a) {
void getLastName(String a) {
String a = s.next();
int c = s.nextInt();
String d = s.next();
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.
class Shape {
void getPerimeter(int r) {
System.out.println(r);
void getArea(int r) {
System.out.println(r);
void getPerimeter(int r) {
double a = 2 * 3.14 * r;
void getArea(int r) {
System.out.println("Enter radius:");
int r = s.nextInt();
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.
class Vehicle {
System.out.println("model:"+a+"year"+b+"fueltype"+c+"company"+d);
double eff=d/f;
System.out.println("distance travelled"+d+"km");
double spd=d/t;
System.out.println("speed is:"+spd+"km/h");
}
System.out.println("model:"+a+"year"+b+"fueltype"+c+"company"+d);
double eff=d/f;
System.out.println("distance travelled"+d+"km");
double spd=d/t;
System.out.println("speed is:"+spd+"km/h");
System.out.println("model:"+a+"year"+b+"fueltype"+c+"company"+d);
double eff=d/f;
System.out.println("distance travelled"+d+"km");
double spd=d/t;
System.out.println("speed is:"+spd+"km/h");
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();
int di=s.nextInt();
int ti=s.nextInt();
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();
di=s.nextInt();
ti=s.nextInt();
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();
di=s.nextInt();
ti=s.nextInt();
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.)
int p=100000;
int t=1;
float getrate(){
return 0;
float getrate(){
float r=8.4f;
float i=(p*r*t)/100;
return i;
float getrate(){
float r=7.3f;
float i=(p*r*t)/100;
return i;
float getrate(){
float r=9.7f;
float i=(p*r*t)/100;
return i;
}
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:-
void print();
System.out.println("Printing default");
interface Showable{
void show();
default void shows(){
System.out.println("Showing default");
System.out.println("Printing");
System.out.println("Showing");
m.print();
m.show();
m.prints();
m.shows();
OUTPUT:-
3.Write a program to demonstrate the use of this keyword.
Arith(){
System.out.println("Adding...");
Arith(int x){
this();
int a,b;
this.a=a;
this.b=b;
this.input(c,d);
return a+b;
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.
static {
System.out.println("Eating.........");
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...");
System.out.println("barking...");
void sleep(){
System.out.println("sleeping...");
void roar(){
System.out.println("roaring...");
class Mul{
c.eat();
c.bark();
c.sleep();
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.
int a = str1.compareTo(str2);
if (a < 0) {
} else if (a == 0) {
} else {
OUTPUT:-
2. Write a java program to check whether two String objects contain the same data or not without
using any comparison method.
int i,flag=1,len1,len2;
String s1,s2;
s1=sc.nextLine();
s2=sc.nextLine();
len1=s1.length();
len2=s2.length();
if(len1==len2){
for(i=0;i<len1;i++){
if(str1[i]!=str2[i]){
flag=0;
break;
}
}else{
flag=0;
if(flag==1){
}else{
OUTPUT:-
3. Write a java program to get the index of all the characters of the alphabet in a given string.
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.
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{
OUTPUT:-
5. Write a java program to find the second most frequent character in a given string.
int n=256;
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;
System.out.println("Enter a string:");
String a=s.next();
char c=f.smfreq(a);
if(c!='\0'){
else{
OUTPUT:-
****Thank You****