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);
}
}