JAVA ASSIGNMENT PROGRAMS 2025-26
Q1. Define a class ElectricBill with the following details:
data members:
String n--> to store the customer name
int units --> to store the no of units consumed
double bill--> to calculate the final bill amount
member functions:
a) ElectricBill()- To store the default values to the data members
b) void accept() --> to accept the values for customer name and no of units
consumed
c) void calculate() --> to claculate the bill according to the table and add the
surcharge
of 2.5% of the bill if the units exceed more than 300 units.
units Rs\perunit
first 100 units 5
next 200 units 7
above 300 units 9
d) void display()--> to display all the details
Write a main method to create an object and call the obove methods.
Program
import [Link].*;
class ElectricBill
{
String n;
int units;
double bill;
public void accept()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the name and units consumed");
n=[Link]();
units=[Link]();
}
public void calculate()
{
if(units<=100)
bill=units*5;
else if(units>100&&units<=300)
bill=500+(units-100)*7;
else if(units>300)
{
bill=500+1400+(units-300)*9;
double surcharge=(bill*(2.5/100));
bill=bill+ surcharge;
[Link](surcharge);
}
}
public void Print()
{
[Link]("Name of the customer is "+n);
[Link]("No of units consumed is "+ units);
[Link]("Total bill is "+bill);
}
public static void main()
{
ElectricBill obj=new ElectricBill();
[Link]();
[Link]();
[Link]();
}
}
Q2. /*Design a class Railway_Ticket with the following specifications
class name : Railway_Ticket
Instance variables/ Data Members
String name : to store the name of the customer
String coach : to store the type of coach customer wants to travel
long mobno : to store the customer's mobile no
int amt : to store basic amount of ticket
int totalamt : to store the amount to be paid after updating the original amount
Member methods
i) Railway_Ticket() : to initialize the default values to the data members
ii) void accept() : to accept the customer's name, mobile no and type of coach
and amount
iii) void update() : to update the amount as per the type of coach selected (
extra amount to be added to the amount as follows
Type of coach Amount
First_AC Rs700
Second_AC Rs 500
Third _AC Rs 250
Sleeper None
iv) void display() : to display all the details of the customer
Write a main method to create an object and call the above methods.*/
Program
import [Link].*;
class Railway_Ticket
{
String name, coach; long mobno;
int amt, totalamt;
public Railway_Ticket()//default or non-parameterised constructor
{
}
void accept()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the details name, coach, mobno,amt");
name=[Link]();
coach=[Link]();
mobno=[Link]();
amt=[Link]();
}
public void update()
{
if([Link]("First_AC"))
totalamt=amt+700;
else if([Link]("Second_AC"))
totalamt=amt+500;
else if([Link]("Third_AC"))
totalamt=amt+250;
else if([Link]("Sleeper"))
totalamt=amt;
}
public void display()
{
[Link]("Name = " + name);
[Link]("Coach = " + coach);
[Link]("Mobile no = " + mobno);
[Link]("Total amount = " + totalamt);
}
public static void main()
{
Railway_Ticket obj=new Railway_Ticket();
[Link]();
[Link]();
[Link]();
}
}
Q3. /**Define a class named BookFair with the following description:
Instance Variables/ Data Members
String Bname- stores the name of the book
double price - stores the price of the book
Member Methods:
i) BookFair() - Default constructor to initialize data members.
ii) void input() - To input and store the name and price of the book
iii) void calculate() - To calculate the price after discount. Discount is calculated
based on the following criteria.
Price Discount
less than or equal to Rs 1000 2% of price
More than Rs 1000 and less than or 10% of price
equal to Rs 30000
More than Rs 3000 15% of price
iv) void display() - To display the name and price of the book after discount.
Write a main method to create an object of the class and call the above member
methods. */
Program
import [Link].*;
class BookFair
{
String Bname;
double price,amt;
public BookFair()
{
Bname="";
price=0.0;
amt=0.0;
}
void input()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter name and price");
Bname=[Link]();
price=[Link]();
}
void calculate()
{
double dis=0.0;
if(price<=1000)
dis=2/100.0*price;
else if(price>1000&&price<=3000)
dis=10/100.0*price;
else if(price>3000)
dis=15/100.0*price;
amt=price-dis;
}
public void display()
{
[Link]("the book name is "+Bname);
[Link]("Price after discount is "+ amt);
}
public static void main()
{
BookFair obj=new BookFair();
[Link]();
[Link]();
[Link]();
}
}
Q4. /**Agile Network Company charges its customers as per the following
specifications given below:
Class name AgileNet
Members variables
String name: to store the name of the customer.
int speed : to store the speed of the network
double amount : to store the amount payable by the customer
Member functions:
void accept() : to accept the name of the customer, speed chosen from the user.
(using Scanner class)
void calculate() : to calculate the amount to be paid annually based on the following
criteria:
Speed in Mbps Charge per month
200 Mbps Rs. 275 per month
300 Mbps Rs. 500 per month
500 Mbps Rs. 750 per month
In addition to this customers have to pay an annual rent of Rs.1000 for all the
above plans.
void display(): to print the name of the customer, spped chosen and Amount payable
in the given format:
Name Speed Bill Amount
***** **** *****
Define the class with the above-mentioned specifications, create a
main() method, create an object and invoke the given methods.*/
class AgileNet
{
String name;
int speed;
double amount;
public AgileNet(String s, int sp)
{
name=s;
speed = sp;
}
void calculate()
{
if(speed==200)
amount= 275;
else if(speed ==300)
amount = 500;
else if(speed == 500)
amount=750;
}
public void display()
{
[Link]("The customer name is "+name);
[Link]("The opted speed is "+speed);
[Link]("The total amount with annual charges is
"+(amount+1000));
}
public static void main()
{
AgileNet obj=new AgileNet("Anu", 300);
[Link]();
[Link]();
}
}
Q5. /**Define a class Bescom having the following description:
Data members/Instance variables:
String Name Name of the Customer.
int PR1 Previous Meter Reading.
int PR2 Present Meter Reading
int units Number of Units Consumed [Formula: PR2-PR1]
int Bill Amount to be paid by the Customer.
Member Methods/Functions:
(a) A Parameterized constructor to assign Name, PR1, and PR2.
(b) void compute ( ) To compute the Bill amount based on the following criteria:
Unit Consumed Bill Amount (in Rs.)
First 30 units 3.00 per unit
Next 30 units 5.00 per unit
Next 40 units 7.00 per unit
Above 100 units 9.00 per unit
(c) void display ( ) To display the output in the following format:
Name Previous Reading Present Reading Units Bill Amount
--- --- --- --- ---
Write a main ( ) method to create an object for the above class and call the
functions.
*/
Program
import [Link].*;
class Bescom
{
String Name; int PR1,PR2, units,bill;
public Bescom(String s, int p1,int p2)
{
Name=s;
PR1=p1;
PR2=p2;
}
void compute()
{
units=PR2-PR1;
if(units>=1&& units<=30)
bill=units*3;
else if(units>30 && units<60)
bill=(30*3)+(units-30)*5;
else if(units>60&& units<=100)
bill=(30*3)+(30*5)+ (units-60)*7;
else if(units>100)
bill=(30*3)+(30*5)+(40*7)+(units-100)*9;
}
void display()
{
[Link]("[Link] presentreading previous reading units bill");
[Link](Name + "\t"+ PR1 + " \t "+PR2+ " \t "+ units + " \t "+
bill);
}
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Entrer name, Pr1, pr2");
String s=[Link]();
int p1=[Link]();
int p2=[Link]();
Bescom obj=new Bescom(s,p1,p2);
[Link]();
[Link]();
}
}
Q6. /**Wap to accept a string and the word in piglatin format.
A word is said to be in piglatin format when you extract
all the characters from the first occurance of a vowel and
replace it at the end of the word and add "ay" to it.
eg. Java ---> avaJay
program ---> ogrampray.*/
Program
class piglatin
{
public static void main(String s)
{
int i; String s1="",s2="";
char ch;
for(i=0;i<[Link]();i++)
{ o 1
ch=[Link](i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'
||ch=='U')
{
s1=[Link](i);
s2=[Link](0,i);
break;
}
}
[Link](s1+s2+"ay");
}
}
Q7. /** Write a program to accept any two strings and check and display whether
both
* the strings are equal to each other with the same set of characters in them.
* example Wolf and flow are anagram words. */
Program
import [Link].*;
class anagram
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the first string");
String s=[Link](); s=[Link]();
[Link]("Enter the second string");
String s1=[Link](); s1=[Link]();
String a="",b="";
for(int i=65;i<=90;i++)
{
for(int j=0;j<[Link]();j++)
{
if([Link](j)==i)
a+=[Link](j);
}
for(int k=0;k<[Link]();k++)
{
if([Link](k)==i)
b+=[Link](k);
}
}
if([Link](b))
[Link](s + " " + s1 + " are anagram words");
else
[Link]("Not anagram words");
}
}
Q8. WAP to accept a number and display it in alphabetical order
Program
class alphabetical
{
public static void main(String s)
{
int i,j; char ch; String s1="";
[Link]("The String is "+ s);
s=[Link]();
for(i=65 ; i<=90;i++)
{
for(j=0;j<[Link]();j++)
{
ch=[Link](j);
if(ch==i)
s1=s1+(char)ch;
}
}
[Link]("The new String in alphabetical order is "+ s1);
} }
Q9. WAP to accept a number and check if it is a supreme number.
A number is said to supreme when all the digits of that number is odd.
Program
import [Link].*;
class supreme
{
public static void main()
{
Scanner sc=new Scanner ([Link]);
[Link]("enter any no");
int n=[Link]();
int rem, even=0, odd=0;
while(n>0)
{
rem=n%10;
if(rem%2==0)
even ++;
else
odd++;
n=n/10;
}
if(even==0)
[Link]("is a supreme no");
else
[Link]("Not a supreme no");
}
}
Q10. WAp yo accept an number and check if it is a Dudeney no or not.
A number is said to a dudeney no when sum of its digit raised to
the cube of it gives back the input no
Program
import [Link].*;
class dudeney
{
public static void main()
{
Scanner sc=new Scanner ([Link]);
[Link]("enter any no");
int n=[Link]();
int rem, m=n, sum=0, cube=0;
while(n>0)
{
rem = n%10;
sum+=rem;
n=n/10;
}
cube= (int)([Link](sum,3));
if(m==cube)
[Link](m + "is a Dudeney no");
else
[Link](m+ " is not a Dudeney no");
}
}
Q11. /*write a Java program to accept a no and check if it is a Manchausen
number or not.
Manchausen number is --> sum of the digits raised to a power of itself is equal to
the input number
3435--> 33+44+33+55 */
Program
import [Link].*;
class munchhausen
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any no");
int n=[Link]();
int s=0, d, p=0,m=n;
while(n>0)
{
d=n%10;
p=(int)[Link](d,d);
//[Link](p);
s=s+p;
n=n/10;
}
if(s==m)
[Link]("It is munchhausen no");
else
[Link]("Not");
}
}
Q12. /*WAP to accept any currency value {indian currency value] and display the
denominations of it.
for example : currency= 2345
output is : 500 rupee notes of 4
200 rupee notes of 1
100 rupee notes of 1
20 rupee notes of 2
5 rupee notes of 1*/
import [Link].*;
class currency
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the currency value");
int c=[Link]();
int den[]={500,200,100,50,20,10,5,2,1}; int notes=0,i=0;
while(c>0)
{
notes=c/den[i];
if(notes>0)
[Link](den[i]+" rupee notes of "+ notes);
c=c%den[i];
notes=0; i++;
}
}
}
Q13. /* WAP to accept integer elements into the array of 3x3
display the sum of right diagonal and left diagonal of the array
eg
123
456
7 8 9 */
import [Link].*;
class sumdiagonals
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int a[][]=new int [3][3];
int i, j, sumright=0, sumleft=0;
[Link]("enter 9 elements into the array");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=[Link]();
[Link]("Array elements are");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
[Link](a[i][j]+ " ");
}
[Link]();
}
for(i=0, j=2; i<3; i++,j--)
{
sumright+=a[i][i];
sumleft+=a[i][j];
}
[Link]("Sum of right diagonal is "+ sumright);
[Link]("Sum of left diagonal is "+ sumleft);
}
}
Q14. //WAP to sort the given elements into asscending order using bubble sorting
technique.
class bubble
{
public static void main()
{
int a[]={23,56 ,81,91,13};
int i, j,temp;
[Link]("Un Sorted array is ");
for(i=0;i<[Link];i++)
[Link](a[i]+ " ");
for(i=0;i<[Link];i++)
{
for(j=0;j<[Link]-1-i; j++)
{
if(a[j] < a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
[Link]("\nSorted array is ");
for(i=0;i<[Link];i++)
[Link](a[i]+ " ");
}
}
Q15. //WAp to accept any 10 Integers into an array and arrange them in
// ascending order using selection sort
import [Link].*;
class selection
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int a[]=new int[10];
[Link]("Enter any 10 elements into the array");
for(int i=0;i<10;i++)
a[i]=[Link]();
int small=0, pos=0, i,j;
for(i=0;i<10;i++)
{
small=a[i];
for(j=i+1;j<10;j++)
{
if(a[j]>small)
{
small=a[j];
pos=j;
}
}
int temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
[Link]("sorted array is ");
for(i=0;i<10;i++)
[Link](a[i]+ " ");
}
}
Q16. WAP to accept any ten elements into the array and sort the array in ascending
order
using bubble sorting technique.
import [Link].*;
class bubble
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int a[]=new int[10];
[Link]("Enter any 10 elements into the array");
for(int i=0;i<10;i++)
a[i]=[Link]();
int i, j, temp;
for(i=0;i<10;i++)
{
for(j=0;j<10-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
[Link]("sorted array is ");
for(i=0;i<10;i++)
[Link](a[i]+ " ");
}
}
Q17. /**WAP to accept a string with multiple words and display the string in title
case.
Title case is that every word of the string should begin with capital letter.
ex: anu is a good girl
o/p Anu Is A Good Girl*/
import [Link].*;
class title
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter any string");
String s=[Link]();
int i; char ch; String s1="";
s=" "+s;
for(i=0;i<[Link]();i++)
{
ch=[Link](i);
if([Link](ch))
{
ch=[Link](i+1);
ch=[Link](ch);
s1=s1+" " +ch;
i++;
}
else
s1=s1+ch;
}
[Link]("The title case string is "+ s1);
}
}
Q18. /*WAP to intitialize any 10 integer elements in to the array and display all the
factors of every element of that array.
eg - arr[]={ 3, 5, 12, 34, 18};
factors are
3 - 1,3
5 - 1, 5
12 - 1, 2, 3, 4, 6,12
34 - 1, 2, 17,34
18 - 1, 2,3 , 6, 9, 18*/
class factors
{
public static void main()
{
int a[]={12, 34, 15, 17, 27, 45, 56,78, 88, 90};
int i, j;
[Link]("the factors are");
for(i=0;i<[Link];i++)
{
[Link]( "\nthe factor of "+ a[i]+ "are " );
for(j=1; j<=a[i];j++)
{
if(a[i]%j==0)
[Link](j+ " ");
}
}
}
}
Q19. /**WAP to accept the country name and display their wonders of world.*/
import [Link].*;
class wondersofworld
{
public static void main()
{
Scanner sc=new Scanner([Link]);
String wonder[]={"Chichen itza", "christ the redeemer","taj mahal","Great wall
of China","MachuPiccu", "petra", "Colosseum"};
String country[]={"Mexico","Brazil","India","China","Peru","Jordan","Italy"};
[Link]("Enter the country name");
String s=[Link]();
int i,flag=0;
for(i=0;i<[Link];i++)
{
if ([Link](country[i]))
{
[Link]("wonder is "+ wonder[i]) ;
flag=1;
break;
}
}
if(flag==0)
[Link]("not found");
}
}
Q20. WAP to overload the method area to calculate the area of a square, rectangle
and circle.
class overload1
{
public void area(int s)
{
[Link]("area of square is" + s*s);
}
public void area(int l, int b)
{
[Link]("area of rectangle is" + l*b);
}
public void area( double r)
{
[Link]("area of circle is" + 3.14*r*r);
}
}