0% found this document useful (0 votes)
2K views

Java Program: Vishal Kumar

The document contains code snippets for 7 Java programs: 1) Finds the greatest of three numbers 2) Calculates the factorial of a number using command line arguments 3) Reads an array and calculates the sum and average 4) Maintains a student record with functions to input, display, and set data 5) Increments employee salaries based on designation using classes 6) Maintains a bank account with functions to initialize, deposit, withdraw, and display balance 7) Designs classes for student, exam, and result - with exam inheriting from student and adding exam marks, and result inheriting from exam and adding total and average marks.

Uploaded by

vinran
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Java Program: Vishal Kumar

The document contains code snippets for 7 Java programs: 1) Finds the greatest of three numbers 2) Calculates the factorial of a number using command line arguments 3) Reads an array and calculates the sum and average 4) Maintains a student record with functions to input, display, and set data 5) Increments employee salaries based on designation using classes 6) Maintains a bank account with functions to initialize, deposit, withdraw, and display balance 7) Designs classes for student, exam, and result - with exam inheriting from student and adding exam marks, and result inheriting from exam and adding total and average marks.

Uploaded by

vinran
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 25

9/25/2008

VISHAL KUMAR

//1.WAP TO FIND GREATEST OF THREE NUMBERS.

class number
{
public static void main(String args[])
{
int a=15,b=60,c=35;
if(a>b && a>c)
{
System.out.println("Greatest Number is:"+a);
}
else if(b>c)
{
System.out.println("Greatest Number is:"+b);
}
else
SUN JAVA PROGRAM
{
System.out.println("Greatest Number is:"+c);
}
}
}

OUTPUT:
Greatest Number is 60
/*2.WAP TO CALCULATE FACTORIAL OF A NUMBER USING COMMAND
LINE ARGUMENTS.*/

class factorial
{
public static void main(String args[])
{
int num=Integer.parseInt(args[0]);
int sum=1;
for(int i=num;i>=1;i--)
{
sum=sum*i;
}
System.out.println("Factorial of ”+num+” is:"+sum);
}
}

OUTPUT

Factorial of 5 is:120
/*3.WAP TO READ A SET OF ARRAY AND FIND THE SUM AND AVERAGE
OF THEM*/

class array
{
public static void main(String args[])
{
int a[]={10,20,30,40,50};
double sum=0,avg=0;
for(int i=0;i<a.length;i++)
{
sum=sum+a[i];
}
System.out.println("Sum is:"+sum);
avg=sum/a.length;
System.out.println("Average is:"+avg);
}
}

OUTPUT
Sum is:150.0
Average is:30.0
/* 5.WAP TO INCREEMENT THE EMPLOYEE SALARIES ON THE BASIS OF
THEIR DESIGNATION ( MANAGER-5000,GM-10000,CEO-20000,WORKER-
2000).USE EMPLOYEE NAME,ID,DESIGNATION, SALARY AS DATA
MEMBER AND INC_SAL AS MEMBER FUNCTION */

class employee
{
String name,designation;
int id,salary;

int inc_sal(int x)
{
return(15000+x);

}
public static void main(String args[])
{
employee ob1=new employee();

ob1.name="Raj";
ob1.id=53;

ob1.designation=args[0];
int temp=0;
if(ob1.designation.equals("general manager"))
{
temp=10000;
}
else if(ob1.designation.equals("manager"))
{
temp=5000;
}
else if(ob1.designation.equals("ceo"))
{
temp=20000;
}
else if(ob1.designation.equals("worker"))
{
temp=2000;
}
ob1.salary=ob1.inc_sal(temp);
System.out.println("Designation is"+ob1.designation);
System.out.println("Id is"+ob1.id);
System.out.println("Name is"+ob1.name);
System.out.println("Salary is"+ob1.salary);
}
}

OUTPUT:

c:\jdk1.3\bin>java employee ceo

Designation is ceo
Id is 53
Name is Raj
Salary is 35000
/*4.WAP TO MAINTAIN THE STUDENT RECORD CONTAINING ROLL
NUMBER, NAME, MARKS1, MARKS2, MARKS3 AS DATA MEMBER AND
GETDATA(), DISPLAY() AND SETDATA() AS MEMBER FUNCTIONS. */

import java.io.*;
class Student
{
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
String name = "NULL";
int rollnumber = 0;
int marks1 = 0;
int marks2 = 0;
int marks3 = 0;
void getdata() throws Exception
{
System.out.print("ENTER THE NAME OF STUDENT :- ");
name = br.readLine();
System.out.print("ENTER marks1 :- ");
marks1 = Integer.parseInt(br.readLine());
System.out.print("ENTER marks2 :- ");
marks2 = Integer.parseInt(br.readLine());
System.out.print("ENTER marks3 :- ");
marks3 = Integer.parseInt(br.readLine());
}
void setdata(int x) throws Exception
{

rollnumber=x;
}
void display() throws Exception
{
System.out.println("NAME :- "+name);
System.out.println("ROLL NO :- "+rollnumber);
System.out.println("marks1 :- "+marks1);
System.out.println("marks2 :- "+marks2);
System.out.println("marks3 :- "+marks3);
}
}

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

InputStreamReader ir = new InputStreamReader(System.in);


BufferedReader br = new BufferedReader(ir);
int n;
Student ob1 = new Student();
Student ob2 = new Student();
ob1.getdata();
ob1.setdata(15);
ob1.display();
ob2.getdata();
ob2.setdata(25);
ob2.display();
}
catch(Exception e){}

}
}

OUTPUT:
ENTER THE NAME OF STUDENT :- Raj
ENTER marks1 :-53
ENTER marks2 :-62
ENTER marks3 :-58
NAME OF STUDENT :- Raj
ROLL NO :-15
marks1 :-53
marks2 :-62
marks3 :-58
ENTER THE NAME OF STUDENT :- Ajay
ENTER marks1 :-73
ENTER marks2 :-82
ENTER marks3 :-88
NAME OF STUDENT :- Raj
ROLL NO :-25
marks1 :-73
marks2 :-82
marks3 :-88
/*6.WRITE A CLASS BANK, CONTAINING DATA MEMBER: NAME OF
DEPOSITOR, A/C TYPE, BALANCE AMOUNT. MEMBER FUNCTION: TO
ASSIGN INITIAL VALUE, TO DEPOSIT & WITHDRAW AN AMOUNT(AFTER
CHECKING THE MIN. BALANCE WHICH SHOULD BE GREATER THAN Rs
500/-), TO DIPLAY NAME & BALANCE*/

import java.io.*;
class small
{
String name = "NULL";
String ac_type = "NULL";
int ac_no = 0;
static int initbalance = 0;
static int amt_deposit = 0;
static int amt_withdrawn = 0;
static int balance = 0;
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
void initial() throws Exception
{
System.out.print("ENTER NAME :- ");
name = br.readLine();
System.out.print("ENTER ACCOUNT NUMBER :- ");
ac_no = Integer.parseInt(br.readLine());
System.out.print("ENTER A/C TYPE :- ");
ac_type = br.readLine();
System.out.print("ENTER INITIAL BALANCE :- Rs ");
initbalance = Integer.parseInt(br.readLine());
System.out.print("ENTER AMOUNT TO BE WITHDRAWN :- Rs ");
amt_withdrawn = Integer.parseInt(br.readLine());
int l=initbalance-amt_withdrawn;
if(l<500)
{
balance = initbalance + amt_deposit - amt_withdrawn;
System.out.println("");
System.out.println("U MUST HAVE A MINIMUM BALANCE
OF Rs 500");
throw new Exception();

}
System.out.print("ENTER AMOUNT TO BE DEPOSITED :- Rs ");
amt_deposit = Integer.parseInt(br.readLine());
balance = initbalance + amt_deposit - amt_withdrawn;
}
void display() throws Exception
{
System.out.println("");
System.out.println("NAME OF ACCOUNT HOLDER :- "+name);
System.out.println("A/C NUMBER IS :- "+ac_no);
System.out.println("A/C TYPE IS :- "+ac_type);
System.out.println("INITIAL BALANCE IS :- Rs "+initbalance);
System.out.println("AMOUNT DEPOSITED :- Rs "+amt_deposit);
System.out.println("AMOUNT WITHDRAWN IS :-
Rs"+amt_withdrawn);
System.out.println("THE FINAL AMOUNT IS :- Rs "+balance);
}
}
class Bank
{
public static void main(String args[]) throws java.io.IOException
{
try
{
small obj = new small();
obj.initial();
obj.display();

}
catch(Exception e)
{}
}
}

ENTER NAME -: raj


ENTER ACCOUNT NUMBER :- 123
ENTER A/C TYPE :- savings
ENTER INITIAL BALANCE :- Rs 3500
ENTER AMOUNT TO BE WITHDRAWN :- Rs 1200
ENTER AMOUNT TO BE DEPOSITED :- Rs 0

OUTPUT
NAME OF ACCOUNT HOLDER -: raj
ACCOUNT NUMBER :- 123
A/C TYPE :- savings
INITIAL BALANCE :- Rs 3500
AMOUNT WITHDRAWN :- Rs 1200
AMOUNT DEPOSITED :- Rs 0
FINAL AMOUNT is-: Rs 2300

/*7.WAP TO DESIGN 3 CLASSES: STUDENT, EXAM, RESULT.THE STUDENT


CLASS HAS DATA MEMBERS SUCH AS ROLL NO, NAME, ETC .CREATE A
CLASS EXAM BY INHERITING THE STUDENT CLASS. THE EXAM CLASS
ADDS DATA MEMBERS REPRESENTING THE MARKS SCORED IN 6
SUBJECTS. DERIVE THE RESULT FROM CLASS EXAM AND IT HAS ITS
OWN MEMBERS SUCH AS TOTAL MARKS AND AVERAGE.*/

import java.io.*;
class Student
{
String name = "NULL";
int batch = 0;
int roll_no = 0;
}

class Exam extends Student


{
static int marks1 = 0;
static int marks2 = 0;
static int marks3 = 0;
static int marks4 = 0;
static int marks5 = 0;
static int marks6 = 0;
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
void setdata() throws Exception
{
System.out.println("");
System.out.print("ENTER NAME OF THE STUDENT :- ");
name = br.readLine();
System.out.print("ENTER BATCH :- ");
batch = Integer.parseInt(br.readLine());
System.out.print("ENTER THE ROLL NUMBER :- ");
roll_no = Integer.parseInt(br.readLine());
System.out.print("ENTER MARKS1 :- ");
marks1 = Integer.parseInt(br.readLine());
System.out.print("ENTER MARKS2 :- ");
marks2 = Integer.parseInt(br.readLine());
System.out.print("ENTER MARKS3 :- ");
marks3 = Integer.parseInt(br.readLine());
System.out.print("ENTER MARKS4 :- ");
marks4 = Integer.parseInt(br.readLine());
System.out.print("ENTER MARKS5 :- ");
marks5 = Integer.parseInt(br.readLine());
System.out.print("ENTER MARKS6 :- ");
marks6 = Integer.parseInt(br.readLine());
}
}

class Result extends Exam


{
void display() throws Exception
{
System.out.println("");
System.out.println(" NAME OF THE STUDENT IS :- "+name);
System.out.println(" BATCH IS :- "+batch);
System.out.println(" THE ROLL NUMBER IS :- "+roll_no);
System.out.println("");
}

public static void main(String s[]) throws java.io.IOException


{
int total = 0;
float average = 0;
try
{
Result obj = new Result();
obj.setdata();
obj.display();
total = marks1 + marks2 + marks3 + marks4 + marks5 + marks6;
System.out.println("TOTAL MARKS CALCULATED IS :-
"+total);
average = total/6;
System.out.println("AVERAGE MARKS CALCULATED IS :-
"+average);

}
catch(Exception e)
{}
}
}

ENTER NAME OF THE STUDENT :- Raj


ENTER BATCH :- 2006
ENTER THE ROLL NUMBER :- 324
ENTER MARKS1 :- 67
ENTER MARKS2 :- 68
ENTER MARKS3 :- 87
ENTER MARKS4 :- 87
ENTER MARKS5 :- 89
ENTER MARKS6 :- 78

OUTPUT:
NAME OF THE STUDENT :- Raj
BATCH :- 2006
ROLL NUMBER :- 324
TOTAL MARKS CALCULATED IS :- 476
AVERAGE MARKS CALCULATED IS :- 79.0
/* 8.WAP TO CALCULATE THE AREA OF GEOMETRICAL
FIGURES(CIRCLE,RECTANGLE,SQUARE,TRIANGLE)USING FUNCTION
OVERLOADING*/

class area
{
int area1(int x,int y)
{
return(x*y);
}
int area1(int l)
{
return(l*l);
}
double area2(double j,double k)
{
double d=j*k/2;
return(d);
}
double area2(double s)
{
double m=3.14*s*s;
return(m);
}
public static void main(String args[])
{
area obj1=new area();
int ara;
ara=obj1.area1(15,12);
System.out.println("Area of Rectangle:"+ara);
ara=obj1.area1(11);
System.out.println("Area of Square:"+ara);
double ara2;
ara2=obj1.area2(15,12);
System.out.println("Area of Triangle:"+ara2);
ara2=obj1.area2(11);
System.out.println("Area of Circle:"+ara2);
}
}

OUTPUT:

Area of Rectangle:180
Area of Square:121
Area of Triangle:90.0
Area of Circle:379.94
/*9.WAP TO CREATE A CLASS EMPLOYEE. DERIVE 3 CLASSES FROM
THIS CLASS NAMELY, PROGRAMMER, ANALYST & PROJECT LEADER.
TAKE ATTRIBUTE AND OPERATIONS ON YOUR OWN.*/

class Employee
{
String progname="ABC";
String ananame="XYZ";
String proname="PQR";
int progid=12,proid=15,anaid=24;
int progsalary=15000,prosalary=12000,anasalary=14000;

}
class programmer extends Employee
{
void display()
{
System.out.println("Programmer Name:"+progname);
System.out.println("Programmer ID:"+progid);
System.out.println("Programmer Salary:"+progsalary);
System.out.println("");
}
}
class analyst extends Employee
{
void display()
{
System.out.println("Analyst Name:"+ananame);
System.out.println("Analyst ID:"+anaid);
System.out.println("Analyst Salary:"+anasalary);
System.out.println("");
}
}
class project extends Employee
{
void display()
{
System.out.println("Project Leader Name:"+proname);
System.out.println("Project Leader ID:"+proid);
System.out.println("Project Leader Salary:"+prosalary);
System.out.println("");
}
}
class employees
{
public static void main(String args[])
{
programmer obj1=new programmer();
analyst obj2=new analyst();
project obj3=new project();
obj1.display();
obj2.display();
obj3.display();
}
}

OUTPUT:
Programmer Name="ABC";
Analyst Name="XYZ";
Project Leader Name="PQR"

Programmer Id=12
Analyst Id=24
Project Leader Id=15

Programmer Salary=15000
Analyst Salary=14000
Project Leader Salary=12000
//10.WAP TO IMPLEMENT MULTIPLE INHERITANCE USING INTERFACE

class office
{
int idnum;
void getnumber(int n)
{
idnum=n;
}
void putnumber()
{
System.out.println("ID Number:"+idnum);
}
}
interface sports
{
double sportwt= 6.0;
void putwt();
}
class sport extends office implements sports
{
public void putwt()
{
System.out.println("Sports Wt:"+sportwt);
}
public static void main(String args[])
{
sport a=new sport();
a.putwt();
a.getnumber(15);
a.putnumber();

}
}

OUTPUT:

Sports Wt:6.0
ID Number:15
/*11.WAP TO CERATE STUDENT CLASS IN PACKAGE1 AND MARKS CLASS
IN PACKAGE2 WHICH INHERIT STUDENT CLASS. CALCULATE THE
TOTAL AND AVERAGE MARKS IN RESULT CLASS.*/

package package1;
public class student
{
public
int id;
public String name;
public void getdat()
{
name="Raj";
id=104;
}

}
package package2;
import package1.*;

public class marks


{
int math,hindi;
void getdata()
{
math=54;
hindi=63;
}
}
import package2.marks;
class result2
{
public static void main(String args[])
{
marks obj1=new marks();
obj1.getdata();
obj1.getdat();
System.out.println("Name is"+obj1.name);
System.out.println("Id is"+obj1.id);
System.out.println("Hindi:"+obj1.hindi);
System.out.println("maths:"+obj1.maths);

}
}

OUTPUT:

Name is:Raj
Id is104
Hindi:63
Maths:54
/*12.WAP to handle ArithmeticException and
ArrayIndexOutOfBoundsException*/

class handling
{
public static void main(String args[])
{
int a[]={5,10};
int c=5;
int b=5;
try
{
int x=a[1]/(b-c);

}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
try
{
int y=a[2]/5;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Index Error");
}
}

OUTPUT:

Division by zero
Array Index Error
//13.WAP TO CREATE AND HANDLE YOUR OWN EXCEPTIONS

import java.lang.Exception;
class MyException extends Exception
{
String me;
MyException(String message)
{
super(message);
me=message;
}
String getmessage()
{
return(me);
}

}
class throw2
{
public static void main(String args[])
{
int x=5,y=1000;
try
{
float z=(float)x/(float)y;
if(z<0.01)
{
throw new MyException("Number is too small");
}
}
catch(MyException e)
{
System.out.println("Caught my exception");
System.out.println(e.getmessage());
}

}
}

OUTPUT:
Caught my exception
Number is too small
/*14.WAP TO CALCULATE AREA OF DIFFERENT FIGURES USING
ABSTRACT CLASS*/

abstract class figure


{
double d1,d2;
figure(double x,double y)
{
d1=x;
d2=y;
}
abstract double area();
}
class rectangle extends figure
{
rectangle(double i,double j)
{
super(i,j);
}
double area()
{
System.out.print("Rectangle Area:");
return(d1*d2);
}
}
class triangle extends figure
{
triangle(double m,double n)
{
super(m,n);
}
double area()
{
System.out.print("Triangle Area:");
return(d1*d2/2);
}
}
class abstractdemo
{
public static void main(String args[])
{
rectangle rect1=new rectangle(20,30);
System.out.println(rect1.area());
triangle tri1=new triangle(20,30);
System.out.println(tri1.area());
}
}

OUTPUT:

Rectangle Area:600.0
Triangle Area:300.0
CONTENTS

TITLE OF THE PROGRAM

1. WAP TO FIND GREATEST OF THREE NUMBERS.

2. WAP TO CALCULATE FACTORIAL OF A NUMBER USING COMMAND


LINE ARGUMENTS.

3. WAP TO READ A SET OF ARRAY AND FIND THE SUM AND AVERAGE
OF THEM

4. WAP TO MAINTAIN THE STUDENT RECORD CONTAINING ROLL


NUMBER, NAME, MARKS1, MARKS2, MARKS3 AS DATA MEMBER AND
GETDATA(), DISPLAY() AND SETDATA() AS MEMBER FUNCTIONS.

5. WAP TO INCREEMENT THE EMPLOYEE SALARIES ON THE BASIS OF


THEIR DESIGNATION ( MANAGER-5000,GM-10000,CEO-20000,WORKER-
2000).USE EMPLOYEE NAME,ID,DESIGNATION, SALARY AS DATA
MEMBER AND INC_SAL AS MEMBER FUNCTION

6. WRITE A CLASS BANK, CONTAINING DATA MEMBER: NAME OF


DEPOSITOR, A/C TYPE, BALANCE AMOUNT. MEMBER FUNCTION: TO
ASSIGN INITIAL VALUE, TO DEPOSIT & WITHDRAW AN
AMOUNT(AFTER CHECKING THE MIN. BALANCE WHICH SHOULD BE
GREATER THAN Rs 500/-), TO DIPLAY NAME & BALANCE

7. WAP TO DESIGN 3 CLASSES: STUDENT, EXAM, RESULT.THE STUDENT


CLASS HAS DATA MEMBERS SUCH AS ROLL NO, NAME, ETC. CREATE
A CLASS EXAM BY INHERITING THE STUDENT CLASS. THE EXAM
CLASS ADDS DATA MEMBERS REPRESENTING THE MARKS SCORED
IN 6 SUBJECTS. DERIVE THE RESULT FROM CLASS EXAM AND IT HAS
ITS OWN MEMBERS SUCH AS TOTAL MARKS AND AVERAGE.

8. WAP TO CALCULATE THE AREA OF GEOMETRICAL


FIGURES(CIRCLE,RECTANGLE,SQUARE,TRIANGLE)USING FUNCTION
OVERLOADING

9. WAP TO CREATE A CLASS EMPLOYEE. DERIVE 3 CLASSES FROM THIS


CLASS NAMELY, PROGRAMMER, ANALYST & PROJECT LEADER.
TAKE ATTRIBUTE AND OPERATIONS ON YOUR OWN.
10. WAP TO IMPLEMENT MULTIPLE INHERITANCE USING
INTERFACE

11. WAP TO CERATE STUDENT CLASS IN PACKAGE1 AND MARKS CLASS


IN PACKAGE2 WHICH INHERIT STUDENT CLASS. CALCULATE THE
TOTAL AND AVERAGE MARKS IN RESULT CLASS.

12. WAP TO HANDLE ARITHMETIC EXCEPTION AND ARRAY INDEX


OUT OF BOUND EXCEPTION

13. WAP TO CREATE AND HANDLE YOUR OWN EXCEPTIONS

14. WAP TO CALCULATE AREA OF DIFFERENT FIGURES USING


ABSTRACT CLASS

You might also like