You are on page 1of 91

JAVA

MASTER OF COMMERCE

(COMPUTER ORIENTED BUSINESS APPLICATION)

SUBMITTED BY:

SONY.S

Reg.No: 2101722084017

DEPARTMENT OF COMMERCE (SHIFT II)

MADRAS CHRISTIAN COLLEGE (AUTONOMOUS)

TAMBARAM,CHENNAI - 600 059

NOVEMBER - 2022
DEPARTMENT OF COMMERCE
Madras Christian College (Autonomous)
Chennai-600063

Certified to be a bonafide record of JAVA submitted by SONY.S


REG.NO :2101722084017 of II M.COM (COMPUTER
ORIENTED BUSINESS APPLICATION),Department of
Commerce(Shift- II),Madras Christian
College(Autonomous)Tambaram Chennai-600059 in the
academic year November, 2022.

Name: SONY.S Reg.no:21017220840

Class: II M.com (COBA) Semester: III

INTERNAL EXAMINER EXTERNAL EXAMINER


DATE: DATE:
S.NO . PAGE.NO
INDEX CONTENT
1. To find addition of three numbers 4-5
2. To find the biggest number 6-7
3. To find the average of given numbers 8-9
4. To find if given number is odd or even 10-11
5. To find if a person is eligible for voting 12-13
6. To find the largest number 14-15
7. To find the smallest number 16-17
8. Age calculation(Give dob as input) 18-19
9. To find the total and grade of a student 20-22
10. To calculate discount amount 23-24
11. To check the type of the animal using switch case 25-26
12. Airthmetic operations using switch case 27-29
13. Perform the following calculations 30-31
14. To check whether a number is prime or not 32-33
15. To list all prime numbers within the given limit 34-35
16. To find the biggest number in an array 36-37
17. To find the reverse of the number 38-39
18. To find an element in an array 40-42
19. Sorting numbers in an array 43-45
20. Sorting string in an array 46-48
21. Matrix additions 49-51
22. String Operations 52-54
23. Class & objects 55-56
24. Polymorphism-method overloading 57-59
25. Single inheritance 60-62
26. Polymorphism-method overriding 63-64
27. Multiinheritance 65-67
28. Exceptions 68-70
29. Interface 71-72
30. Package 73-75

Output:
1.Write a java program to find the addition of three numbers
Aim:
To find the addition of three numbers
Coding:
import java.util.*;
public class add
{
public static void main(String a[])
{
System.out.println("Enter any three number");
int x=10;
int y=34;
int z=65;
int sum=x+y+z;
System.out.println(x);
System.out.println(y);
System.out.println(z);
System.out.println("Addition of three numbers is"+sum);
}
}
Output:

2. Write a java program to find the biggest numbers


AIM:
To find the biggest numbers
Coding:
Import java.util.*;
Public class biggest
{
Public static void main(String arg[])
{
int x,y,z;
Scanner i=new Scanner (System.in);
System.out.println ("Enter three numbers");
x=i.nextInt ();
y=i.nextInt ();
z=i.nextInt();
if(x>y && x>z)
{
System.out.println("Biggest no is:"+x);
}
else if(y>x && y>z)
{
System.out.println("The biggest numbers is:"+y);
}
else
{
System.out.println("The biggest numbers is:"+z);
}
}
}

Output:
3. Write a java program to find the average of given numbers:
Aim:
To find the average of given numbers
Coding:
Import java.util.*;
Public class Average
{
public static float calAvg(float a, float b, float c, float d, float e)
{
float avg;
avg=(a+b+c+d+e)/5;
Return Avg
Public static void main(String a[])
{
float c,d,e,f,g;
Float avg;
System.out.println("Enter five numbers:");
Scanner b= new Scanner(System.in);
c=b.nextFloat();
d=b.nextFloat();
e=b.nextFloat();
f=b.nextFloat();
g=b.nextFloat();
avg=(c+d+e+f+g)/5;
System.out.println("the average of the numbers
is:"+calAvg(c,d,e,f,g));
}
}
Output:
4. Write a java program to find odd or even
Aim:
To find if given numbers is odd or even
Coding:
import java.util.*;
public class Oddoreven
{
public static void main(String a[])
{
Float x;
Scanner o=new Scanner (System.in);
x=o.nextFloat ();
if (x%2==0)
{
System.out.println ("the given numbers is odd");
}
else
{
System.out.println ("the given numbers is even");
}
}
}
Output:
5. Write a java program to find if a person is eligible for voting
(DOB)
AIM:
To find if a program if a person is eligible for voting (DOB)
Coding:
import java.time.*;
import java.util.*;
public class voting
{
public static void main(String a[])
{
Scanner in= new Scanner(System.in);
System.out.println("enter your DOB as year, Month and Date");
int yr=in.nextInt();
int month=in.nextInt();
int date=in.nextInt();
LocalDate dob= LocalDate.of(yr,month,date);
LocalDate current=LocalDate.now();
Period period= Period.between(dob,current);
int age=period.getYears();
if(age>=18)
{
System.out.println ("You are Eligible for voting");
}
else
{
System.out.println("You are not Eligible for voting");
}
}
}
Output:
6. Write a java program to find the smallest numbers of given 3
numbers
Aim:
To find the smallest numbers of given 3 numbers
Coding:
import java.util.*;
public class Smallest
{
public static void main(String a[])
{
Scanner i=new Scanner(System.in);
int x,y,z,small;
System.out.println("Enter 3 numbers of your choice:");
x=i.nextInt();
y=i.nextInt();
z=i.nextInt();
small=(x<y)?x:y;
small=(small<z)?small:z;
System.out.println(" the Smallest numbers is:"+small
}
}
Output:
7. Write a java program for age calculation (given DOB as input)
Aim:
To find the age calculation (given DOB as input)
Coding:
import java.time.*;
import java.util.*;
public class AgeCalculation
{
public static void main(String a[])
{
Scanner in= new Scanner(System.in);
System.out.println("enter your DOB as year,Month and Date");
int yr=in.nextInt();
int month=in.nextInt();
int date=in.nextInt();
LocalDate dob= LocalDate.of(yr,month,date);
LocalDate current=LocalDate.now();
Period period= Period.between(dob,current);
System.out.println("You are:"+period.getYears()
+"years,"+period.getMonths()+"months,"+period.getDays()+"Days");
}
}
Output:

8. Write a java program to find the total and grade of a student


Aim:
To find the total and grade of a student
Coding:
Public class Grade
{
Public float findTotal(float x1,float x2,float x3,float x4,float x5)
{
float total;
total=x1+x2+x3+x4+x5;
return total;
}
public String findGrade(float avg)
{
String grade;
if(avg>=90)
{
grade="O";
}
else if(avg>=85)
{
grade="A+"
}
else if(avg>=70)
{
grade="A";
}
else if(avg>=60)
{
grade="B";
}
else if (avg>=50)
{
grade="C";
}
else
{
grade="Fail";
}
return grade;
}
}

import java.util.*;
public class CalcGrade
{
public static void main(String arg[])
{
Scanner s=new Scanner(System.in);
Grade g=new Grade();
float m1,m2,m3,m4,m5,total,mean;
String grade;
System.out.println("Enter 5 marks");
m1= s.nextFloat();
m2= s.nextFloat();
m3= s.nextFloat();
m4= s.nextFloat();
m5= s.nextFloat();
total=g.findTotal(m1,m2,m3,m4,m5);
System.out.println("total of your marks is:"+total);
mean= total/5;
grade=g.findGrade(mean);
System.out.println("Your Grade is:"+grade+"grade");
}
}

Output:
9. Write a java program to find the discount amount in a purchase, if
purchase is than 5000 then 5% discount
Aim:
To find java program to discount amount example: if purchase
is>10000 10% discount if purchase is> 5000 5% discount
Coding:
import java.util.*;
public class Discount
{
public static void main(String arg[])
{
Scanner in =new Scanner(System.in);
System.out.println("Enter the amount");
double x=in.nextDouble();
double y;
if(x>10000);
{
y=x*10/100;
x=x-y;
}
if(x>5000)
{
y=x*5/100;
x=x-y;
}
System.out.println("total amount to be paid:"+x);
}
}

Output:
10. Write a java program to check the type of the animal
(domestic/wild) using switch case
Aim:
To check the type of the animal (domestic/wild) using switch case
Coding:
import java.util.*;
public class DomesticAnimal
{
public static void main(String arg[])
{
String animal;
Scanner s=new Scanner(System.in);
System.out.println("enter an animal name:");
animal= s.nextLine();
animal=animal.toLowerCase();
switch(animal)
{
case "dog":
case "cat":
case "cow":
case "horse":
case "rabbit":
case "donkey":
System.out.println("It is an domestic animal");
break;
default:
System.out.println("It is a wild animal");
}
}
}
Output:
11. Write a java program to do arithmetic operation using switch case
Aim:
To find arithmetic operation using switch case
Coding:
import java.util.*;
public class SwitchExample
{
public static void main(String arg[])
{
int choice;
int x,y,z, flag=0;
Scanner s=new Scanner(System.in);
while(flag==0)
{
System.out.println("enter your choice");
System.out.println("1.addition");
System.out.println("2.subtraction");
System.out.println("3.multiplication");
System.out.println("4.division");
System.out.println("exit");
choice=s.nextInt();
switch(choice)

{
case 1:
System.out.println("enter two numbers");
x=s.nextInt();
y=s.nextInt();
z=x+y;
System.out.println("x+y="+z);
break;
case 2:
System.out.println("enter two numbers");
x=s.nextInt();
y=s.nextInt();
z=x-y;
System.out.println("x-y="+z);
break;
case 3:
System.out.println("enter two numbers");
x=s.nextInt();
y=s.nextInt();
z=x*y;
System.out.println("x*y="+z);
break;
case 4:
System.out.println("enter two numbers");
x=s.nextInt();
y=s.nextInt();
z=x/y;
System.out.println("x/y="+z);
break;
case 5:
flag=1;
break;
default:
System.out.println("wrong input");
}
}
}
}
Output:
12. Write a java program to perform the following calculation
1^2+2^2+……n^2
Aim:
To find perform the following calculation 1^2+2^2…n^2
Coding:
import java.util.*;
public class Calculation
{
public static void main(String a[])
{
Float m1,m2,m3,m4,m5,sum,avg;
String Grade;
Scanner s=new Scanner(System.in);
grade g=new grade();
System.out.println("enter five marks");
m1=s.nextFloat();
m2=s.nextFloat();
m3=s.nextFloat();
m4=s.nextFloat();
m5=s.nextFloat();
sum=g.findTotal(m1,m2,m3,m4,m5);
System.out.println("total is:"+sum);
avg=sum/5;
Grade=G.findGrade(avg);
System.out.println("Grade is :"+Grade+"grade");
}
}
}
Output:
13. Write a java program to check if the given numbers is prime or
not (Using loop)
Aim:
To check if the given numbers is prime or not (using loop)
Coding:
import java.util.*;
public class prime_numbers
{
public static void main(String arg[])
{
Scanner s=new Scanner(System.in);
int n,i,flag=0;
System.out.println("Enter the numbers:");
n=s.nextInt();
if(n==1)
{
System.out.println("1 is neither prime nor composite");
}
else
{
For (i=2;i<n;i++)
{
if (n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
System.out.println("the given number is an prime numbers");
}
else
{
System.out.println("the given numbers is an composite numbers");
}
}
Output:
14. Write a java program all the prime numbers within the given
numbers
Aim:
To find all the prime numbers within the given numbers
Coding:
Import java.util.*;
public class Prime_range
{
public static void main(String arg[])
{
int n,i,j,flag=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter any number");
n=sc.nextInt();
for(i=2;i<=n;i++)
{
flag=0;
for(j=2;j<i;j++)
{
if (i%j==0)
{
flag=1;
break;
}
}
if (flag==0)
{
System.out.println(i);
}
}
}
}
Output:
15. Write a java program to generate multiplication table using while
loop
Aim:
To find generate multiplication table using while loop
Coding:
import java.util.*;
public class Multiplication
{
public static void main(String a[])
{
int n,i,mul;
System.out.println("Enter the numbers:");
Scanner s= new Scanner(System.in);
n=s.nextInt();
for(i=1;i<=10;i++)
{
i=1;
while(i<=10)
{
mul=i*n;
System.out.println(n+"*"+i+"="+n*i);
i++;
}
}
Output:
16. Write a java program to check if the given numbers is a
palindrome numbers or not (if the given numbers and reverse of the
numbers is special numbers
Aim:
To check If the given numbers is a palindrome numbers or and
reverse of the numbers is special numbers
Coding:
import java.util.*;
public class reverse
{
public static void main(String arg[])
{
int n,m,sum=0,r;
Scanner s= new Scanner(System.in);
System.out.println("Enter the numbers of reverse:");
n=s.nextInt();
m=n;
while(n>0)
{
R=(n%10);
sum=(sum*10)+r;
n= n/10;
}
System.out.println("The reverse numbers is:"+sum);
if(sum==m)
{
System.out.println("It is an palindrome numbers");
}
else
{
System.out.println("It is not palindrome numbers");
}
}
}
Output:
17. Write a java program to find the biggest numbers in the array
Aim:
Biggest numbers in the array
Coding:
import java.util.*;
public class bigArray
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
int n[]=new int[10];
int i,big;
System.out.println("Enter 10 numbers");
for(i=0;i<10;i++)
{
n[i]=sc.nextInt();
}
big=n[0];
for(i=1;i<10;i++)
{
if(big<n[1])
{
big=n[i];
}
}
System.out.println("The biggest numbers is:"+big);
}
}
}
Output:
18. Write a java program for finding an element in the array.
Aim:
To find an element in the array
Coding:
import java.util.*;
public class findarray
{
public static void main(String arg[])
{
int num[]=new int[10];
int i,n,count=0;
Scanner sc=new Scanner(System.in);
System.out.println("enter 10 number");
for(i=0;i<10;i++)
{
num[i]=sc.nextInt();
}
System.out.println("enter a searching element");
n=sc.nextInt();
for(i=0;i<10;i++)
{
if(n==num[i])
{
count++;
System.out.println("it is available at position;"+(i+1));
}
if(count==0)
{
System.out.println("the given element is not available in position;"+
(i+1));
}
else
{
System.out.println("count is:"+count);
}
}
}
Output:
19. Write a java program for sorting numbers in an array
Aim:
To find storing in an array
Coding:
import java.util.*;
public class sortingnumbers
{
public static void main(String arg[])
{
int i,j,temp;
int num[]=new int[10];
Scanner sc=new Scanner(System.in);
System.out.println("Enter 10 numbers");
for(i=0;i<10;i++)
{
num[i]=sc.nextInt();
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
System.out.println("The numbers after sorting is:");
for(i=0;i<10;i++)
{
System.out.println(num[i]);
}
}
}
Output:
20. write a java program storing string in a array.
Aim:
To find the storing string in an array
Coding:
import java.util.*;
public class SortingNames
{
public static void main(String arg[])
{
String name[] = new String[10];
int i,j;
String temp;
Scanner in = new Scanner(System.in);
System.out.println("Enter 10 names");
for(i=0;i<10;i++)
{
name[i] = in.nextLine();
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(name[i].compareToIgnoreCase(name[j])>0)
{
temp = name[i];
name[i] = name[j];
name[j] = temp;
}
}
}
System.out.println("The names is alphabetical order: ");
for(i=0;i<10;i++)
{
System.out.println(name[i]);
}
}
}
Output:
21. Write a java program to find matrix addition
Aim:
To find matrix addition.
Coding:
import java.util.*;
public class sortingnumbers
{
public static void main(String arg[])
{
int i,j,temp;
int num[]=new int[10];
Scanner sc=new Scanner(System.in);
System.out.println("Enter 10 numbers");
for(i=0;i<10;i++)
{
num[i]=sc.nextInt();
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
System.out.println("The numbers after sorting is:");
for(i=0;i<10;i++)
{
System.out.println(num[i]);
}
}
}
Output:
22. Write a java program for string operations
Aim:
To find string operation
Coding:
import java.util.*;
public class StringEx
{
public static void main(String arg[])
{
String fname,lname,fullname;
Scanner sc=new Scanner(System.in);
System.out.println("Enter your first name & last name:");
fname=sc.nextLine();
lname=sc.nextLine();
fullname=fname.concat(" ");
fullname=fullname.concat(lname);
System.out.println("Welcome" +fullname);
System.out.println("Enter your user id");
String userid=sc.nextLine();
int len=userid.length();
if(len<8 || len>14)
{
System.out.println("Invalid user id");
}
String pwd,cpwd;
System.out.println("Enter the your password:");
pwd=sc.nextLine();
System.out.println("Renter your password for confirmation:");
cpwd=sc.nextLine();
if(pwd.compareTo(cpwd)==0)
{
System.out.println("yourpasswordischanged successfully");
}
else
{
System.out.println("Enter re-enter the password");
}
System.out.println("Enter the your email id:");
String email=sc.nextLine();
email=email.toLowerCase();
System.out.println("Your email in lowercase is:");
System.out.println(email);
String str1="java is a very user friendly programming";
String[] result=str1.split(" ");
System.out.println("no.of.words are:"+result.length);
}
Output:
23. Write a java program for class and objects
Aim:
To find class and objects
Coding:
import java.util.*;
class Person
{
String name, address, gender;
int age;

void getdata(String nm, String a, String g, int n)


{
name = nm;
address = a;
gender = g;
age = n;
}
void display()
{
System.out.println("Name : " + name);
System.out.println("Address : " + address);
System.out.println("Gender : " + gender);
System.out.println("Age : " + age);
System.out.println(" ");
}
}
public class X
{
public static void main(String arg[])
{
Person p1 = new Person();
Person p2 = new Person();
Person p3 = new Person();
p1.getdata("XXX","Chennai","Male",25);
p2.getdata("YYY","Trichy","Female", 25);
p1.display();
p2.display();
}
}
Output:

2101722084017
24. Write a java program for polymorphism-method overloading
Aim:
To find the polymorphism-methods overloading
Coding:
import java.util.*;
public class Shape
{
int findarea(int a)
{
int area;
area=a*a;
return area;
}
int findarea(int l, int b)
{
int area;
area=l*b;
return area;
}
double findarea(double r)
{
double area;
area=3.14*r*r;
return area;
}
public static void main(String arg[])
{
Shape square=new Shape();
int x= square.findarea(5);
System.out.println("Area of the square is:"+x);
Shape rec=new Shape();
int y= rec.findarea(10,20);
System.out.println("Area of the rectangle is:"+y);
Shape cir=new Shape();
double z= cir.findarea(9.2);
System.out.println("Area of the circle is:"+z);
}
}
Output:
25. Write a java program for single Inheritance
Aim:
To find single inheritance
Coding:
import java.util.*;
class Person
{
String name, address, gender;
int age;
void getdata(String nm, String a, String g, int n)
{
name = nm;
address = a;
gender = g;
age = n;
}
void display()
{
System.out.println("Name : " + name);
System.out.println("Address : " + address);
System.out.println("Gender : " + gender);
System.out.println("Age : " + age);
System.out.println(" ");
}
}
public class X
{
public static void main(String arg[])
{
Person p1 = new Person();
Person p2 = new Person();
Person p3 = new Person();
p1.getdata("XXX","Chennai","Male",25);
p2.getdata("YYY","Trichy","Female", 25);
p1.display();
p2.display();
}
}
Output:
26. Write a java program for polymorphism-interface
Aim:
To find polymorphism-interface
Coding:
Import java.util.*;
class RBI
{
int getinterest()
{
int i=4;
return i;
}
}
class IB extends RBI
{

}
class CB extends RBI
{
int getinterest()
{
int i= 7;
return i;
}
}
class SBI extends RBI
{
int getinterest()
{
int i=3;
return i;
}
}
public class Bank
{
public static void main(String arg[])
{
RBI r= new RBI();
CB c= new CB();
SBI s= new SBI();
IB ib= new IB();
int i= s.getinterest();
i= ib.getinterest();
System.out.println(i);
}
}
Output:
27. write a java program for package
Aim:
To find for package
Coding:
package finance;
public class SI
{
public double calculate_si(double p,int n,double r)
{
double si;
si=p*n*r;
return si;
}
}
package finance;
import java.math.*;
public class CI
{
double calculate_CI(double p, int n, double r)
{
double ci;
ci= p*Math.pow((1+r/100),n);
ci=ci=p;
return ci;
}
import finance.*;
public class Cal
{
public static void main(String arg[])
{
SI s=new SI();
double x=s.calculate_si(10000,5,10);
System.out.println("Simple Interest is:"+x);
CI c=new CI();
double y=c.calculate_ci(10000,05,100);
System.out.println("Compound Interest is:"+y);
}
}
}
Output:
28. Write a java program for Exceptions
Aim:
To find Exceptions
Coding:
import java.util.*;
public class JavaExceptionExample
{
public static void main(String arg[])
{
try
{

int x,y;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the two numbers");
x=sc.nextInt();
y=sc.nextInt();
int data=x/y;
System.out.println("Division is:"+data);
}
catch(ArithmeticException e)
{
System.out.println(e);
System.out.println("you cant divide a number by zero");
}
catch(InputMismatchException e1)
{
System.out.println(e1);
System.out.println(" the character not allowed");
}
}
}
Output:
29. write a java program for interface
Aim:
To find interface
Coding:
public class Cat implements Animal
{
public void animalSound()
{
System.out.println("The sound of the Cat is MEOW.....!!!");
}
public void run()
{
System.out.println("Cat runs fast");
}
public static void main(String arg[])
{
Cat c=new Cat();
c.animalSound();
c.run();
}
}
Output:

30. Write a java program for multilevel inheritance


Aim:
To find for multilevel inheritance
Coding:
class Employee extends Person
{
String empid,desig;
double salary;
void
getempdata(Stringe,Stri
ng de,double se)
{
empid=e;
desig=de;
salary=se;
}
void displayempdata()
{
System.out.println("ID:"+empid);
System.out.println("Desig:"+desig);
System.out.println("Salary:"+salary);
}
}
class Faculty extends Employee
{
String dept,subject;
void getfacultydetails(String d,String s)
{
dept=d;
subject =s;
}
void displayfacultydetails()
{
System.out.println("Dept:"+dept);
System.out.println("Subject:"+subject);
}
}
public class F
{
public static void main(String arg[])
{
Faculty e1=new Faculty();
e1.getdata("XXX","Chennai","Male",25);
e1.getempdata("2158","Manager",75000);
e1.getfacultydetails("commerce","HR");
e1.display();
e1.displayempdata();
e1.displayfacultydetails();
}
}

You might also like