Sunteți pe pagina 1din 41

PROGRAM 1

AIM
Write a program to calculate the electricity bill by reading the consumer no and no
of units consumed. The charges for different slabs are given below
Up to 50 units
50 100 units
100 200 units
200 300 units
above 300 units

Rs. 1.5 per unit


Rs. 2 per unit
Rs. 2.8 per unit
Rs. 3.5 per unit
Rs. 4.50 per unit

CODE
package bill;
import java.util.Scanner;
public class Bill {
static double calcamt(int unit) {
double billAmt;
if(unit>=300) {
billAmt = (double) unit*4.5;
}
else if (unit>=200) {
billAmt = (double) unit*3.5;
}
else if(unit>=100) {
billAmt = (double) unit*2.8;
}
else if(unit>=50) {
billAmt = (double) unit*2;

}
else {
billAmt = (double) unit*1.5;
}
return billAmt;
}
public static void main(String[] args) {
int unit;
double billAmt;
System.out.println("Enter the usage in Units:");
Scanner scan=new Scanner(System.in);
unit = scan.nextInt();
billAmt = calcamt(unit);
System.out.println("Your bill Amount is "+billAmt);
}
}

OUTPUT

PROGRAM 2

AIM
Write java program to display Fibonacci series up to n.

CODE
package fibonacci.series;
import java.util.Scanner;

public class FibonacciSeries {


public static void main(String[] args) {
int firstNo=0,nextNo=1;
int limit;
System.out.println("Enter the limit:");
try {
Scanner scan=new Scanner(System.in);
limit = scan.nextInt();
System.out.println("Fibonacci Series:");
while(firstNo <= limit) {
System.out.print(firstNo+" ");
firstNo=nextNo-firstNo;
nextNo=firstNo+nextNo;
}
System.out.println("");
}
catch(Exception e) {
System.out.println("Error :" + e);

}
}
}

OUTPUT

PROGRAM 3
AIM

Write java program to check whether a given number is perfect, abundant or


deficient.

CODE
package perabudef;
import java.util.Scanner;

public class PerAbuDef {


static int perfectCalc(int num) {
int i;
int temp = 0;
i = (int) Math.floor(num/2);
while(i != 0){
if(num%i == 0){
temp+=i;
}
i--;
}
return temp;
}
public static void main(String[] args) {
int number;
int temp;
try {
System.out.println("Enter A Number");

Scanner scan=new Scanner(System.in);


number = scan.nextInt();
temp=perfectCalc(number);
if(number == temp) {
System.out.println("Perfect Number ");
}
else if(number <= temp){
System.out.println("Abundant Number ");
}
else {
System.out.println("Deficent Number ");
}
}
catch (Exception e) {
System.out.println("Error !!! :" + e);
}
}
}

OUTPUT

PROGRAM 4

AIM
Write java program to check whether the given sides can form a triangle. If yes,
find the type (isosceles, equilateral, scalene) and area of the triangle.

CODE
package canformtriangle;
import java.util.Scanner;

public class CanFormTriangle {


static boolean triCheck(float s1,float s2, float s3){
boolean check = true;
if(s1+s2>s3 && s2+s3 >s1 && s3+s1 >s2) {
check = true;
}
else {
check= false;
}
return check;
}
static float calArea(float s1,float s2,float s3)
{
float area,s;
s=(s1+s2+s3)/2;
area=(float)Math.sqrt(s*(s-s1)*(s-s2)*(s-s3));
return area;

}
public static void main(String[] args) {
float side1, side2, side3, area;

try {
System.out.println("Enter the Length of 3 Sides:");
Scanner scan=new Scanner(System.in);
side1=scan.nextFloat();
side2= scan.nextFloat();
side3= scan.nextFloat();

if(triCheck(side1,side2,side3)) {
area=calArea(side1,side2,side3);
if(side1==side2&&side2==side3) {
System.out.println("It can form an Equilateral Triangle!");
}
else if(side1==side2||side1==side3||side2==side1||side2==side3) {
System.out.println("It can form an Isosceles Triangle!");
}
else {
System.out.println("It can from a Scalene Triangle!");
}
System.out.println("Area of the given triangle: " + area);

else {
System.out.println("A Triangle can't be Formed");
}
}
catch(Exception e) {
System.out.println("Error !!! xxXxx :"+ e);
}
}
}

OUTPUT

PROGRAM 5
AIM
Write a java program to read an array of 10 elements and to
find the following:
a) Sum of the elements.
b) Average of the element.
c) Maximum of the elements.
d) Minimum of the elements.

CODE
package array;
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
int arr[]=new int [20];
int n,sum,min,max;
sum=0;
try
{
Scanner scan=new Scanner(System.in);
System.out.print("enter the limit:");
n=scan.nextInt();
System.out.println("enter the elements:");
for( int i=0;i<n;i++){
arr[i]=scan.nextInt();
sum+=arr[i];
}
min=max=arr[0];
System.out.println("Sum = " + sum);
System.out.println("Average = " + (float)sum/n);
for( int i=1;i<n;i++){
if(arr[i] < min)
min=arr[i];
else
max=arr[i];
}

System.out.println("Minimum of elements :" + min);


System.out.println("Maximum of elements :" + max);
}
catch(Exception e)
{
System.out.println("ERROR !!! " + e);
System.exit(0);
}
}
}

OUTPUT

PROGRAM 6
AIM
Write a java program to find the sum of the digit and reverse
of given number using class and object.

CODE
package reverse;
import java.util.Scanner;
class number
{
int num;
void input()
{
Scanner scan= new Scanner(System.in);
num=scan.nextInt();
reverseOp();
}
void reverseOp()
{
int dup=num,sum=0,d,rev=0;
while(dup != 0)
{
d=dup%10;
rev=(rev * 10) + d;
sum+=d;
dup/=10;
}
System.out.println("Sum of the digits :"+ sum);
System.out.println("Reverse number :"+ rev);
}
}
public class Reverse {
public static void main(String[] args) {
number N1=new number();
System.out.print("Enter the Number ");
N1.input();
}
}

OUTPUT

PROGRAM 7
AIM
Write a program in java with class rectangle with the data
fields length, breadth. Calculate the area. Create two object of
rectangle and compare their area.

CODE
package rectarea;
import java.util.Scanner;
class rectangle
{
float len,br;
void input()
{
Scanner scan=new Scanner(System.in);
len=scan.nextFloat();
br=scan.nextFloat();
}
double area()
{
return(len*br);
}
}
public class RectArea {
public static void main(String[] args) {
rectangle R1=new rectangle();
rectangle R2= new rectangle();
System.out.println("Enter the length and breadth of 1st rectangle:");
R1.input();
System.out.println("Enter the length and breadth of 2nd rectangle:");
R2.input();
if(R1.area() > R2.area())
System.out.println("Area of 1st rectangle is greater than the 2nd
rectangle!!");
else if(R1.area() < R2.area())

System.out.println("Area of 1st rectangle is less than the 2nd


rectangle!!");
else
System.out.println("Both rectangles have same area!!");
}
}

OUTPUT

PROGRAM 8

AIM
Write a java program to find the volume of cube, rectangular
box and cylinder using function overloading.

CODE
package overloading;
import java.util.*;
public class Overloading {
void volume(int x)
//for cube
{
System.out.println("Volume of cube :"+ x*x*x);
}
void volume(int x,int y,int z) //for rectangular box
{
System.out.println("Volume of rectangular box :"+x*y*z);
}
void volume(int x,int y) //for cylinder
{
System.out.println("Volume of cylinder :"+Math.PI*x*x*y);
}
public static void main(String[] args) {
int a,b,c,ch;
Overloading E1=new Overloading();
Scanner scan=new Scanner(System.in);
System.out.println("Find the area of :\n 1.cube \n 2.Rectangular box \n
3.cylinder");
System.out.print("Enter ur choice :");
ch=scan.nextInt();
switch(ch)
{
case 1: System.out.println("Enter the side of cube:");
a=scan.nextInt();
E1.volume(a);
break;
case 2: System.out.println("enter the length,breadth and height of
the rectangular box:");
a=scan.nextInt();
b=scan.nextInt();
c=scan.nextInt();

E1.volume(a,b,c);
break;
case 3: System.out.println("Enter the height and radius of the
cylinder:");
a=scan.nextInt(); //height of cylinder
b=scan.nextInt(); //radius of cylinder
E1.volume(b,a);
break;
default:System.out.println("Invalid entry");
}
}
}

OUTPUT

PROGRAM 9
AIM
Write a java program to create a class complex. Create two
objects and find the sum of two complex numbers. Read the real
and imaginary part using constructor.

CODE
package complexnum;
import java.util.Scanner;
class complex
{
int real;
int imag;
complex()
{
real=0;
imag=0;
}
complex(int a,int b)
{
real=a;
imag=b;
}
void add(complex c1,complex c2)
{
real=c1.real+c2.real;
imag=c1.imag+c2.imag;
}
void display()
{
System.out.println("Result :" + real + " + "+ "i "+imag );
}
}
public class ComplexNum {
public static void main(String[] args) {
int a,b;
Scanner scan=new Scanner(System.in);

System.out.println("Enter the real and imaginary part of 1st complex


no:");
a=scan.nextInt();
b=scan.nextInt();
complex c1=new complex(a,b);
System.out.println("Enter the real and imaginary part of 2st complex
no:");
a=scan.nextInt();
b=scan.nextInt();
complex c2=new complex(a,b);
complex c3=new complex();
c3.add(c1, c2);
c3.display();
}
}

OUTPUT

PROGRAM 10
AIM
Write a java program to find the area of square and a
rectangle using overloaded constructors.

CODE
package overloadedconst;
import java.util.Scanner;
class area
{
int a;
int b;
area(int a)
{
System.out.println("Area of square :"+ a*a);
}
area(int a,int b)
{
System.out.println("Area of reectangle :"+ a*b);
}
}
public class OverloadedConst {
public static void main(String[] args) {
int a,b,ch;
Scanner scan=new Scanner(System.in);
System.out.println("Find the area of :\n 1.square \n 2.rectangle");
System.out.println("Enter ur choice :");
ch=scan.nextInt();
switch(ch)
{
case 1:System.out.println("Enter the side of square :");
a=scan.nextInt();
area a1=new area(a);
break;
case 2:System.out.println("Enter the length and breadth of
rectangle :");
a=scan.nextInt();
b=scan.nextInt();

area a2=new area(a,b);


break;
default:System.out.println("Invalid entry");
}
}
}

OUTPUT

PROGRAM 11
AIM
Write a java program to create a class student. Write
methods to read and display the student details. Create a derived
class result. Write methods to read mark of 5 subjects and display
the total and grade. Create object to read and display the result of
a student.

CODE
package stud;
import java.util.*;
class student
{
String name;
int rollno;
void read()
{
System.out.println("Enter the name of student:");
Scanner scan = new Scanner(System.in);
name = scan.nextLine();
System.out.println("enter the roll number:");
rollno = scan.nextInt();
}
void display()
{
System.out.println("STUDENTS DETAILS\n
*****************************");
System.out.println("Name: "+ name +"\n" + "Roll No: " + rollno);
}
}
class result extends student
{
int[] mark= new int[5];
int tot,per;
char grade;
result()
{

tot=0;
}
void readMark()
{
System.out.println("Enter the marks for 5 subjects(out of 100)");
Scanner scan= new Scanner(System.in);
for(int i=0; i<5 ;i++)
{
mark[i]=scan.nextInt();
tot+=mark[i];
}
per=tot/5;
if(per>=90)
grade='A';
else if (per>=80)
grade='B';
else if(per>=70)
grade='C';
else if(per>=60)
grade='D';
else if(per>=50)
grade='E';
else if( per>=0 )
grade='F';
else
System.out.println("Invalid marks!!");
}
void disp()
{
System.out.println("Total mark: "+ tot);
System.out.println("Grade: "+ grade);
}
}
public class Stud {
public static void main(String[] args) {
result r1=new result();
r1.read();
r1.readMark();
r1.display();
r1.disp();
}
}

OUTPUT

PROGRAM 12

AIM
Write a java program to use this keyword to prevent
namespace collision.

CODE
package thiskey;
public class ThisKey {
private int x,y;
public void setVar(int x,int y)
{
this.x=x;
this.y=y;
System.out.println(x + " " + y);
}
public static void main(String[] args) {
ThisKey obj= new ThisKey();
obj.setVar(10,20);
System.out.println(obj.x + " " + obj.y);
}
}

OUTPUT

PROGRAM 13

AIM
Write a program to find the area of circle and sphere using
interface.

CODE
package interfacearea;
import java.util.Scanner;
interface Area
{
final static float PI=3.14f;
float compute( float r );
}
class Circle implements Area
{
public float compute( float r)
{
return(PI*r*r);
}
}
class Sphere implements Area
{
public float compute(float r)
{
return(4*PI*r*r);
}
}
public class InterfaceArea {
public static void main(String[] args) {
float r;
Circle cir =new Circle();
Sphere sph= new Sphere();
Scanner scan = new Scanner(System.in);
Area area;
area = cir;
System.out.println("Enter the radius : ");
r=scan.nextFloat();
System.out.println("Area of circle : " + area.compute(r));

area = sph;
System.out.println("Area of sphere : " + area.compute(r));
}
}

OUTPUT

PROGRAM 14

AIM
Write a java program for generating two threads One for odd
numbers and one for even numbers.

CODE
package oddeventhread;
class Even extends Thread
{
public void run()
{
System.out.println("Thread even start:");
for( int i=0 ;i<=10 ; i+=2)
{
System.out.println(" even no: " + i);
}
System.out.println("Thread even stop \n");
}
}
class Odd extends Thread
{
public void run()
{
System.out.println("Thread odd start:");
for( int i=1 ;i<=10; i+=2)
{
System.out.println(" odd no: " + i);
}
System.out.println("Thread even stop \n");
}
}
public class OddEvenThread {
public static void main(String[] args) {
new Even().start();
new Odd().start();
}
}

OUTPUT

PROGRAM 15
AIM
Write a java program to enable arithmetic exceptions using
throw keyword.

CODE
package throwexample;
class ThrowKeyword {
static void divide(int x,int y)
{
int z;
try
{
if(y==0)
throw new ArithmeticException(" error : divide by zero!!");
else
z=x/y;
}
catch(Exception e)
{
System.out.println(e);
}

}
}
public class ThrowExample {
public static void main(String[] args) {
ThrowKeyword obj = new ThrowKeyword();
obj.divide(10,0);
System.out.println("Rest of the code...");
}
}

OUTPUT

PROGRAM 16
AIM
Write a java program to override method greatest () for
finding the greatest of 2 numbers and 3 numbers.

CODE
package overridegreatest;
class Super
{
int x;
int y;
Super(int x, int y)
{
this.x=x;
this.y=y;
}
void greatest()
{
if (x>y)
System.out.println(x + " is greater");
else
System.out.println(y + " is greatest");
}
}
class Sub extends Super
{
int z;
Sub(int x, int y,int z)
{
super(x,y);
this.z=z;
}
void greatest()
{
if(x>y && x>z)
System.out.println(x + " is greater");
else if(y>z)
System.out.println(y + " is greater");

else
System.out.println(z + " is greater");
}
}
public class OverrideGreatest {
public static void main(String[] args) {
Sub s1 = new Sub(3,7,10);
s1.greatest();
}
}

OUTPUT

PROGRAM 17
AIM
Write an applet to display a rectangle with specified
coordinate and color passed as parameter from the HTML
file.
CODE
Rectangle.java
import java.awt.*;
import javax.swing.text.html.StyleSheet;
import java.applet.*;
public class Rectangle extends Applet
{
String a,b,c,d,e;
Color colr;
int x1,y1,x2,y2;
StyleSheet s=new StyleSheet();
public void init()
{
a = getParameter("x1");
b = getParameter("y1");
c = getParameter("x2");
d = getParameter("y2");
e = getParameter("color");
x1 = Integer.parseInt(a);
y1 = Integer.parseInt(b);
x2 = Integer.parseInt(c);
y2 = Integer.parseInt(d);
colr = s.stringToColor(e);
}
public void paint(Graphics g)
{
g.setColor(colr);
g.drawRect(x1,y1,x2,y2);
}
}

Rectangle.html
<html>
<head>
<title> Rectangle </title>
</head>
<body>
<applet code=Rectangle.class
width= 600
height=600 >
<param name="x1" value=100 >
<param name="y1" value=100 >
<param name="x2" value=300 >
<param name="y2" value=300 >
<param name="color" value="red">
</applet>
</body>
</html>

OUTPUT

PROGRAM 18
AIM
Create an AWT application to add, remove items in a listbox.

CODE
import java.applet.*;
import java.awt.*;
public class SampleList extends Applet
{
TextField txt,txt1;
List list;
public void init()
{
list = new List();
txt = new TextField(8);
txt1 = new TextField(10);

list.add("apple");
list.add("orange");
list.add("mango");
list.add("grapes");
add(list); //adding list
list.remove(2); //remove element using index
}
}

OUTPUT

PROGRAM 19
AIM
Create an applet for a displaying smiling face.

CODE
import java.awt.*;
import java.applet.*;
public class Face extends Applet
{
public void paint(Graphics g)
{
g.drawOval(40,40,120,150);
g.drawOval(57,75,30,20);
g.drawOval(110,75,30,20);
g.fillOval(68,81,10,10);
g.fillOval(121,81,10,10);
g.drawOval(85,100,30,30);
g.fillArc(60,125,80,40,180,180);
g.drawOval(25,92,15,30);

g.drawOval(160,92,15,30);
}
}

OUTPUT

PROGRAM 20
AIM
Write a program to display the x and y position of the cursor
movement using mouse.

CODE
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MouseXY extends Applet implements MouseMotionListener
{
int x,y;
public void init()
{
addMouseMotionListener(this);
}

public void mouseMoved(MouseEvent e)


{
x=e.getX();
y=e.getY();
repaint();
}
public void mouseDragged(MouseEvent e)
{
x=e.getX();
y=e.getY();
repaint();
}
public void paint(Graphics g)
{
showStatus(x + " , " + y);
}
}

OUTPUT

S-ar putea să vă placă și