 ## Java project class IX Maria Day School

Q1. Write a program to find net pay where basic  salary taken as input.
da= 25% of basic salary
ta= 2% basic salary
pf= 15% basic salary
gross pay= basic salary+da+ta
net pay=gross pay – pf
Solution:
import java.util.*;
//Start of clas Employee
public class Employee
{
public static void main(String args[])
{
//Local variables
double bs,da,ta,pf,gp,np;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Basic salary:");
bs=sc.nextDouble();
da=bs*25.0/100.0;
ta=bs*2.0/100.0;
pf=bs*15.0/100.0;
gp=bs+da+ta;
np=gp-pf;
System.out.println("Gross payment:"+gp);
System.out.println("Net Payment :"+np);
}//end of main
}//end of class

Q2. Write a program to input radius and find volume and surface area of a Sphere.
[ sa=4  π r2      v=4/3 π r3 ]
Solution
import java.util.*;
//Start of clas Sphere
public class Sphere
{
public static void main(String args[])
{
//Local variables
double v,sa,r,pi=314;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Radius of Sphere:");
r=sc.nextDouble();
sa=4*pi*r*r;
v=(double)4/3*pi*r*r*r;
System.out.println("Surface area="+sa);
System.out.println("Volume  ="+v);
}
}end of the class

Q3. Write a program to accept a year and check whether the year is Leap year or not. If the year in century then it is divisible by 400. If it is non-century year then it is divisible by 4 but not 100.
Solution:
import java.util.*;
//Start of clas Leap Year
public class LeapYear
{
public static void main(String args[])
{
//Local variables
int year;
Scanner sc=new Scanner(System.in);
System.out.println("Enter year :");
year=sc.nextInt();
if ((year % 4 == 0) && year % 100 != 0)
{
System.out.println(year + " is a leap year.");
}
else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
{
System.out.println(year + " is a leap year.");
}
else
{
System.out.println(year + " is not a leap year.");
}
}

} end of the class

## School Computer Project for class X

#### Calculation of Area using Function overloading

import java.util.*;
public class AreaCalculator
{
double s,ar;
//Area of Scalant Triangle
double area(double a,double b,double c)
{
s=(a+b+c)/2.0;
ar=Math.sqrt(s*(s-a)*(s-b)*(s-c));
return ar;
}
//Area of Trapezium
double area(int h,int a,int b)
{
ar=(1.0)/2*h*(a+b);
return ar;
}
double area(double d1,double d2)
{
ar=(1.0/2)*d1*d2;
return ar;
}
public static void main(String args[])
{
double a,b,c,d1,d2,ar;
int h,a1,b1;
Scanner sc=new Scanner(System.in);
AreaCalculator ob=new AreaCalculator();
System.out.println("Enter three sides of triangle:");
a=sc.nextDouble();
b=sc.nextDouble();
c=sc.nextDouble();
ar=ob.area(a,b,c);
System.out.println("Area of Scalant Triangle is="+ar);
System.out.println("Enter Height and two sides:");
h=sc.nextInt();
a1=sc.nextInt();
b1=sc.nextInt();
ar=ob.area(h,a1,b1);
System.out.println("Area of Trapezium is="+ar);
System.out.println("Enter two diagonal:");
d1=sc.nextDouble();
d2=sc.nextDouble();
ar=ob.area(d1,d2);
System.out.println("Area of Rhombus is="+ar);
}
}

#### Movie Magic Program

import java.util.*;
public class MovieMagic
{
// instance variables
int year;
String title;
float rating;
/**
* Constructor for objects of class MovieMagic
*/
public MovieMagic()
{
year=0;
title="";
rating=0.0f;
}
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Year, Title of movie and its rating(0.0 to 5.0) :");
year=sc.nextInt();
title=sc.next();
rating=sc.nextFloat();
}
void display()
{
String msg="";
if(rating>=0.0f && rating<=2.0f)
msg="Flop";
else if(rating>2.0f && rating<=3.4f)
msg="Semi-Hit";
else if(rating>3.4f && rating<=4.5f)
msg="Hit";
else if(rating>4.5f && rating<=5.0)
msg="Super-Hit";

System.out.println("Movie Title\t"+title);
System.out.println("Year\t\t"+year);
System.out.println("Movie Massage\t"+msg);
}
public static void main(String args[])
{
MovieMagic ob=new MovieMagic();
ob.accept();
ob.display();
}

}

### Armstrong number in java

public class ArmstronNumber
{
public static void main(String args[])
{
int n, m, a, arm=0;
n=153;
m=n;
while(n!=0)
{
a=n%10;
arm  = arm + a * a * a;
n = n/10;
}
if(m==arm)
System.out.println("Armstrong Number");
else
System.out.println("Not Armstrong number");
}
}

#### Series through Menu using switch-case

import java.util.*;
public class Menu
{
public static void main(String args[])
{
double s=0.0;
int ch,f,i,j,n,m=4;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1. s=1/4+1/8+1/12+.......upto n terms");
System.out.println("Enter 2. s=1!/1+2!/2+3!/3+.....upto n terms");
System.out.println("Enter your choice:");
ch=sc.nextInt();
System.out.println("Enter term :");
n=sc.nextInt();
switch(ch)
{
case 1:
for(i=0;i<n;i++)
{
s=s+(1.0/m);
m=m+4;
}
System.out.println("Sum="+s);
break;
case 2:
for(i=1;i<=n;i++)
{
f=1;
for(j=1;j<=i;j++)
{
f=f*j;
}
s=s+(double)f/i;
}
System.out.println("Sum="+s);
default:
System.out.println("Wrong choice");
}
}

}

#### A shop will give a discount of 10% if the cost of the purchased quantity is more than 1000.Ask the user for quantity, Suppose, one unit will cost 100. Judge and print total cost for the user.

import java.util.*;
public class Discount
{
public static void main(String args[])
{
double cost=0.0, tqty,dis, qty;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Quantity:");
qty=sc.nextDouble();
tqty=qty*100;
if(tqty>=1000)
{
dis=tqty*10/100.0; // Percentage
cost=tqty-dis;
System.out.println("Cost="+cost);
}
else
{
cost=qty*100;
System.out.println("Cost="+cost);
}
}
}

#### Menu-driven program. Press 1 for For Prime Number Press 2 For Sum of digits of a Given number

import java.util.*;
public class MenuDrivenProgram
{
public static void main(String args[])
{
int ch, num, m, sum=0, f, i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number:");
num=sc.nextInt();
System.out.println("1. For Prime Number");
System.out.println("2. For Sum of digits of a Givem number");
System.out.println("Enter your choice----->");
ch=sc.nextInt();
switch(ch)
{
case 1:
f=1;
for(i=2;i<num;i++)
{
if(num%i==0)
{
f=0; //Flag Variable
break;
}
}
if(f==1)
System.out.println("Prime Number");
else
System.out.println("Not Prime Number");

break;
case 2:
while(num!=0)
{
m=num%10;
sum=sum+m;
num=num/10;
}
System.out.println("Sum="+sum);
break;
default:
System.out.println("Wrong Choice");
}
}
}

#### A student will not be allowed to sit in the exam if his/her attendance is less than 75%.Take the following input from the user Number of classes held, Number of classes attended. And the print percentage of class attended Is student is allowed to sit in the exam or not.

import java.util.*;
public class Attendence
{
public static void main(String[] args)
{
int noclheld, noclatt, per;
Scanner sc=new Scanner(System.in);
System.out.println("Enter No Of class Held and No of Class Attend:");
noclheld=sc.nextInt();
noclatt=sc.nextInt();
per=(noclatt*100)/noclheld;
System.out.println("Percentage attendence:"+per);
if(per>=75)
{
System.out.println("Allowed to sit in exam");
}
else
{
System.out.println("Not Allowed to sit in exam");
}
}
}

## Math library method in java

public class MathLibraryinJava
{
public static void main(String[] args)
{

double rint;
double x = 62.5;
double y = 69.7;
int i, j;
double a, b, c, num;
a = 9.2;
b = 9.9;
c = -5;
System.out.println("Math Function in Java");
System.out.println("=====================");
// Math.abs() will return absolute value(without any sign)
System.out.println(c + " Absolute value is " + Math.abs(c));

/*
Math.round() method returns the value in rounded form.
It will return the same value if the number is
below ½ or .5 otherwise it return the next higher integer value.
*/

System.out.println(x + " is approximately " + Math.round(x));
System.out.println(y + " is approximately " + Math.round(y));

/*The Math.ceil() return the rounded value to the
* next higher integer and it return double type value */
System.out.println("The ceiling of " + a + " is " + Math.ceil(a));
System.out.println("The ceiling of " + b + " is " + Math.ceil(b));
System.out.println("The ceiling of " + x + " is " + Math.ceil(x));
System.out.println("The ceiling of " + y + " is " + Math.ceil(y));

// Math.floor() returns a number down to the nearest integer
// it can round off a floating point number
System.out.println("The floor of " + a + " is " + Math.floor(a));
System.out.println("The floor of " + b + " is " + Math.floor(b));
System.out.println("The floor of " + x + " is " + Math.floor(x));
System.out.println("The floor of " + y + " is " + Math.floor(y));

// Math.rint() will returns the truncated value of the given number
// that is, the integer part of the value by removing fractional part
rint = 8.5;
System.out.println("The rint of " + rint + " is " + Math.rint(rint));
rint = 8.84;
System.out.println("The rint of " + rint + " is " + Math.rint(rint));

// Comparison operators
// min() returns the smaller of the two numbers.
// The value return's depending upon the number used as the
// arguments to the method
System.out.println("min(" + a + "," + b + ") is " + Math.min(a,b));
System.out.println("min(" + x + "," + y + ") is " + Math.min(x,y));
System.out.println("min(" + a + "," + b + ") is " + Math.min(a,b));
System.out.println("min(" + y + "," + b + ") is " + Math.min(y,b));
// There's a corresponding max() method
// that returns the larger of two numbers
// The value return's depending upon the number used as the
// arguments to the method
System.out.println("max(" + a + "," + b + ") is " + Math.max(a,b));
System.out.println("max(" + x + "," + y + ") is " + Math.max(x,y));
System.out.println("max(" + a + "," + x + ") is " + Math.max(a,x));
System.out.println("max(" + a + "," + b + ") is " + Math.max(a,b));

// The Math library Math.PI and Math.E defines
// constant value
System.out.println("Pi value is " + Math.PI);
System.out.println("e value is " + Math.E);

// The trigonometric methods in Java
// parameter or arguments / parameters are given in radians
// radiant = (22/(7*180))*Degree
// Convert a 30 degree angle to radians
double angle = 30.0 * 2.0 * Math.PI/360.0;
System.out.println("cos(" + angle + ") is " + Math.cos(angle));
System.out.println("sin(" + angle + ") is " + Math.sin(angle));

// Inverse Trigonometric methods
// All values are returned as radians

double radians = 1.5d;
double sine = Math.sin(radians);
double cosine = Math.cos(radians);
double tan = Math.tan(radians);

double asine = Math.asin(sine);
double acosine = Math.acos(cosine);
double atan = Math.atan(tan);

System.out.println("The value Sine of       " + radians + " = " + sine);
System.out.println("The value Cosine of     " + radians + " = " + cosine);
System.out.println("The value Tangent of    " + radians + " = " + tan);
System.out.println("The value Arcsine of    " + sine + " = " + asine);
System.out.println("The value Arccosine of  " + cosine + " = " + acosine);
System.out.println("The value Arctangent of " + tan + " = " + atan);

// Exponential and Logarithmic Methods

// Math.exp(2.0) returns e (7.38905609893065) raised
// to the power of a.
System.out.println("exp(3.0) is " + Math.exp(3.0));
System.out.println("exp(20.0) is " + Math.exp(20.0));
System.out.println("exp(0.0) is " + Math.exp(0.0));

// Math.log(a) returns the natural
// logarithm (base e) of a.
System.out.println("log(2.0) is " + Math.log(1.0));
System.out.println("log(10.0) is " + Math.log(10.0));
System.out.println("log(Math.E) is " + Math.log(Math.E));

// Math.pow(x, y) returns the x raised
// to the yth power.
System.out.println("pow(10, 3) is " + Math.pow(10,3));
System.out.println("pow(5.0, 4.5) is " + Math.pow(5.0,4.5));
System.out.println("pow(10, -1) is " + Math.pow(10,-1));

// Math.sqrt(num) returns the square root of num
num = 25;
System.out.println("The square root of " +  num + " is " + Math.sqrt(num));

// Math.cbrt() return the cube root of number
num = 125;
System.out.println("The cube root of " +  num + " is " + Math.cbrt(num));

// Random method Math.random() will retrun
// pseudo-random number between 0.0 and 1.0;

System.out.println("Random number genetated  : " + Math.random());
System.out.println("Here's 5 random number is: " + Math.random()*5);
}
}

# Bubble sort in Java

Bubble sort
is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted. It compare each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. The algorithm for bubble sort requires a pair of nested loops. The outer loop must iterate once for each element in the data set (of size n) while the inner loop iterates n times the first time it is entered, n-1 times the second, and so on.  Bubble sort belongs to O(n2) sorting algorithms, which makes it quite inefficient for sorting large data volumes. Bubble sort is stable and adaptive.

First Version
import java.util.*;
public class BubbleSort
{
public static void main(String args[])
{
int n[]=new int;
int i, j, t;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Elements in Array");
for(i=0; i<5; i++)
{
n[i]=sc.nextInt();
}
System.out.println("Before Sorted Order");
for(i=0;i<5;i++)
{
System.out.print(n[i] + " ");
}
System.out.println();
for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
if(n[j]>n[j+1])
{
t=n[j];
n[j]=n[j+1];
n[j+1]=t;
}
}
}
System.out.println("Sorted Order");
for(i=0;i<5;i++)
{
System.out.print(n[i] + " ");
}
}
}

Process defines pictorially

second version
public class BubbleSortProject
{

int n[]={8,18,20,6,1,4,19,16,15,3};
int l,i,j,t;

void Process()
{
l=n.length;
for(i=0;i<l;i++)
{
System.out.print(n[i]+" ");
}
System.out.println();
}
void BubbleSort()
{
l=n.length;
for(i=0;i<l-1;i++)
{
for(j=0;j<(l-1)-i;j++)
{
if(n[j]<n[j+1])
{
t=n[j];
n[j]=n[j+1];
n[j+1]=t;
}
}
}
}

public static void main(String arg[])
{
BubbleSortProject ob=new BubbleSortProject();
System.out.println("Before Sort");
ob.Process();
ob.BubbleSort();
System.out.println("After Sort");
ob.Process();
}

}

Output:
Before Sort
8 18 20 6 1 4 19 16 15 3
After Sort

20 19 18 16 15 8 6 4 3 1