0% found this document useful (0 votes)
2 views69 pages

Java Manual

The document contains a collection of programming exercises divided into two parts. Part A includes programs for calculating student grades, summing products of digits, generating Fibonacci numbers, and creating classes for payroll, distance, and matrix operations. Part B focuses on class hierarchies, interfaces, abstract classes, and multi-threading, along with user interface design for basic operations.

Uploaded by

ayshaazmiya66
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views69 pages

Java Manual

The document contains a collection of programming exercises divided into two parts. Part A includes programs for calculating student grades, summing products of digits, generating Fibonacci numbers, and creating classes for payroll, distance, and matrix operations. Part B focuses on class hierarchies, interfaces, abstract classes, and multi-threading, along with user interface design for basic operations.

Uploaded by

ayshaazmiya66
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

INDEX

PART-A

SI.N program Page


O No

1. Program to accept student name and marks in three 4-6


subjects. Find the total marks, average and grade
(depending on the average marks).

2. Program, which reads two numbers having same number 7-9


of digits. The program outputs the sum of product of
corresponding digits.(Hint Input 327 and 539 output
3x5+2x3+7x9=84).

3. Program to input Start and End limits and print all 9-12
Fibonacci numbers between the ranges.( Use for loop).

4. Define a class named Pay with data members String 13-16


name, double salary, double da, double hra, double pf,
double grossSal, double netSal and methods: Pay(String
n, double s) - Parameterized constructor to initialize the
data members, void calculate() - to calculate the
following salary components, and void display() - to
display the employee name, salary and all salary
components.

5. Program to create a class DISTANCE with the data 16-20


members feet and inches. Use a constructor to read the
data and a member function Sum ( ) to add two distances
by using objects as method arguments and show the
result. (Input and output of inches should be less than
12.).

6. Program to create a class “Matrix” that would contain 20-24


integer values having varied numbers of columns for
each row. Print row-wise sum.

7. Program to extract portion of character string and print 25-27


extracted string. Assume that ‘n’ characters extracted

1
starting from math character position.

8. Program to add, remove and display elements of a 27-31


Vector.

PART-B

SI.N program Page


O No

1. Create a class named ‘Member’ having data 33-37


members:Name,Age,PhoneNumber,Place and [Link]
also has a method named ‘printSalary’ which prints the
salary of the members.

2. Program to implement the following class hierarchy: 38-43


Student:id,name StudentExam(derived from
student):Marks of 3subjects,total marks
StudentResult(derived from
StudentExam):percentage,grade

3. Program to calculate marks of a student using multiple 44-49


inheritance implemented through [Link] Student
with data members rollNo,name,String class and
methods to set and put data

4. Program to create an abstract class named shape that 50-54


contains two integers and an empty method named print
Area().

5. Create a package to convert temperature in centigrade 55-57


into Fahrenhit,and one more package to calculate the
simple Interest.

6. Program that implements a multi-threaded program has 58-61

2
three [Link] thread generates a random integer
every second,and if the value is even,second thread
computes the square of the number and prints.

7. Program that creates a user interface to perform basic 62-66


integer [Link] user enters two numbers in the
TextFields-Num1 and Num2.

8. Using the swing components,design the frame for 67-


shopping a book that accepts book code,book name,and 72
[Link] the discount on code as follows.

3
PART-A

4
PROGRAM: 1

Program to accept student name and marks in three subjects.


Find the total marks, average and grade (depending on the
average marks).

import [Link].*;

class Prog1

public static void main(String args[]) throws IOException

int total,m1,m2,m3;

String name;

double avg;

BufferedReader br=new BufferedReader(new


InputStreamReader([Link]));

[Link]("Enter the name of the student:");

name=[Link]();

[Link]("Enter marks in three subjects:");

m1=[Link]([Link]());

m2=[Link]([Link]());

m3=[Link]([Link]());

total=m1+m2+m3;

avg=total/3.0;

5
[Link]("Total Marks:"+total);

[Link]("Average Marks:"+avg);

if(m1<35 || m2<35 || m3<35)

[Link]("Grade=FAIL");

else if(avg>=75)

[Link]("Grade=DISTINCTION");

else if(avg>=70)

[Link]("Grade=FIRST CLASS");

else if(avg>=60)

[Link]("Grade=SECOND CLASS");

else if(avg>=40)

[Link]("Grade=THIRD CLASS");

else

[Link]("Grade=PASS CLASS");

OUTPUT:

6
PROGRAM: 2

Program, which reads two numbers having same number of


digits. The program outputs the sum of product of corresponding
digits.(Hint Input 327 and 539 output 3x5+2x3+7x9=84).

7
import [Link].*;

import [Link].*;

class Zoor

public static void main(String[]args)throws IOException

BufferedReader br=new BufferedReader(new


InputStreamReader([Link]));

int n1,n2;

[Link]("enter two numbers");

n1=[Link]([Link]());

n2=[Link]([Link]());

sumOf(n1,n2);

static void sumOf(int n1,int n2)

int sum=0;

while(n1>0&&n2>0)

sum+=(n1%10)*(n2%10);

n1/=10;

n2/=10;

[Link]("sumis"+sum);

8
}

OUTPUT:

9
PROGRAM: 3

Program to input Start and End limits and print all Fibonacci
numbers between the ranges.( Use for loop).

10
import [Link].*;

import [Link].*;

class Prog3

public static void main(String args[]) throws IOException

int start,end,f0=0,f1=1,f2;

BufferedReader br=new BufferedReader(new


InputStreamReader([Link]));

[Link]("enter the start and end limits:");

start=[Link]([Link]());

end=[Link]([Link]());

[Link]("the fibonacci series b/w the range:");

for( ;f0<=end;)

if(f0>=start)

[Link](f0+"\t");

f2=f0+f1;

f0=f1;

f1=f2;

11
OUTPUT:

12
PROGRAM: 4

Define a class named Pay with data members String name, double salary,
double da, double hra, double pf, double grossSal, double netSal and
methods: Pay(String n, double s) - Parameterized constructor to initialize
the data members, void calculate() - to calculate the following salary

13
components, and void display() - to display the employee name, salary
and all salary components. Dearness Allowance = 15% of salary House
Rent Allowance = 10% of salary Provident Fund = 12% of salary Gross
Salary = Salary + Dearness Allowance + House Rent Allowance Net
Salary = Gross Salary - Provident Fund Write a main method to create
object of the class and call the methods to compute and display the
salary details. [class basics].

import [Link].*;

import [Link].*;

class Progg4

public static void main(String args[]) throws IOException

String name;

double sal;

BufferedReader br=new BufferedReader(new


InputStreamReader([Link]));

[Link]("enter the employee name:");

name=[Link]();

[Link]("enter the employee salary:");

sal=[Link]([Link]());

Pay obj=new Pay(name,sal);

[Link]();

[Link]();

class Pay

14
{

String name;

double sal,da,hra,pf,gross,net;

Pay(String n,double s)

name=n;

sal=s;

void cal()

da=sal*0.15;

hra=sal*0.10;

pf=sal*0.12;

gross=sal+da+hra;

net=gross-pf;

void display()

[Link]("**********PAYSLIP**********");

[Link]("EMPLOYEE NAME:" + name);

[Link]("EMPLOYRR SALARY:" + sal);

[Link]("DA:" + da);

[Link]("HRA:" + hra);

[Link]("PF:" + pf);

[Link]("GROSS PAY:" + gross);

[Link]("NET SALARY:" + net);


15
[Link]("*****************");

OUTPUT:

16
PROGRAM: 5

Program to create a class DISTANCE with the data members feet


and inches. Use a constructor to read the data and a member
function Sum ( ) to add two distances by using objects as

17
method arguments and show the result. (Input and output of
inches should be less than 12.).

import [Link].*;

import [Link].*;

class Prog5

public static void main(String args[])throws IOException

int f1,i1,f2,i2;

BufferedReader br=new BufferedReader(new


InputStreamReader([Link]));

[Link]("enter the first distance:");

f1=[Link]([Link]());

do

i1=[Link]([Link]());

}while(i1>=12);

Distance d1=new Distance(f1,i1);

[Link]("enter the second distance:");

f2=[Link]([Link]());

do

i2=[Link]([Link]());

}while(i2>=12);

Distance d2=new Distance(f2,i2);

18
[Link](d1);

class Distance

int feet,inch;

Distance(int f,int i)

feet=f;

inch=i;

void sum(Distance ob1)

int feetsum,inchsum,feet_con;

inchsum=([Link]+[Link])%12;

feet_con=([Link]+[Link])/12;

feetsum=[Link]+[Link]+feet_con;

[Link]("the sum is:"+feetsum +"feet"+inchsum+"inches");

19
0UTPUT:

20
PROGRAM: 6

Program to create a class “Matrix” that would contain integer


values having varied numbers of columns for each row. Print
row-wise sum.

import [Link].*;

import [Link].*;

class Prog6

public static void main(String args[])throws IOException

int i,j,m,n,sum;

21
BufferedReader br=new BufferedReader(new
InputStreamReader([Link]));

[Link]("enter the number of rows:");

int rows=[Link]([Link]());

[Link]("enter the number of columns:");

int cols=[Link]([Link]());

int arr[][]=new int[rows][cols];

[Link]("enter the element of the arrays:");

for(i=0;i<rows;i++)

for(j=0;j<cols;j++)

arr[i][j]=[Link]([Link]());

[Link]();

[Link]("input matrix");

for(i=0;i<rows;i++)

for(j=0;j<cols;j++)

[Link](arr[i][j]+ " ");

[Link]();

[Link]();

22
sum=0;

for(i=0;i<rows;i++)

int rSum=0;

for(j=0;j<cols;j++)

sum+=arr[i][j];

rSum+=arr[i][j];

[Link]("row"+(i+1)+"sum="+rSum);

23
OUTPUT:

24
PROGRAM: 7

Program to extract portion of character string and print


extracted string. Assume that ‘n’ characters extracted starting
from math character position.

import [Link].*;

import [Link].*;

class Program7

public static void main(String args[])throws IOException

String str,res=" ";

int start,end,m,n,len,i;

BufferedReader br=new BufferedReader(new


InputStreamReader([Link]));

[Link]("enter the string:");

str=[Link]();

len=[Link]();

[Link]("enter the start and end position of extraction:");

start=[Link]([Link]());

end=[Link]([Link]());

if(start<0||start>=len||end>=len||start>end)

25
{

[Link]("invalid position");

else

for(i=start;i<=end;i++)

res=res+[Link](i);

[Link]("the extracted string is:"+res);

OUTPUT:
26
PROGRAM: 8

Program to add,remove and display elements of a Vector.

27
import [Link].*;

import [Link].*;

import [Link].*;

class program8

public static void main(String args[])throws IOException

int ch,i;

String item;

BufferedReader br=new BufferedReader(new


InputStreamReader([Link]));

Vector<String> vect = new Vector<>();

do

[Link]("\n*******menu option*******");

[Link]("[Link] element\[Link] element\[Link]


element");

[Link]("enter your choice:");

ch=[Link]([Link]());

switch(ch)

case 1:

[Link]("\nenter the element to be added:");

item=[Link]();

[Link](item);

break;

28
case 2:

[Link]("\nenter the element to be removed:");

item=[Link]();

[Link](item);

break;

case 3:

if([Link]()==0)

[Link]("vector is empty!");

else

[Link]("vector contents are:");

for(i=0;i<[Link]();i++)

[Link]([Link](i));

break;

}while(ch>0&&ch<4);

29
OUTPUT:

30
31
PART-B

PROGRAM: 1
Create a class named ‘Member’ having data
members:Name,Age,PhoneNumber,Place and [Link] also has a
method named ‘printSalary’ which prints the salary of the
32
[Link] classes ‘Employee’ and ‘Manager’ classes have
data members ‘specialization’ and ‘department’
[Link],assign name,age,phone number,address and
salary to an employee and a manager by making an object of
both of these classes and print the same.

import [Link].*;

import [Link].*;

class Member

String name,phone,place;

int age,sal;

void getdetail()throws IOException

BufferedReader br=new BufferedReader(new


InputStreamReader([Link]));

[Link]("ENTER THE NAME OF MEMBER:");

name=[Link]();

[Link]("ENTER THE AGE OF MEMBER:");

age=[Link]([Link]());

[Link]("ENTER THE PHONE NUMBER OF MEMBER:");

phone=[Link]();

[Link]("ENTER THE PLACE OF MEMBER:");

place=[Link]();

[Link]("ENTER THE SALARY OF MEMBER:");

sal=[Link]([Link]());

33
void printsal()

[Link]("SALARY EARNED:"+sal);

void printdetail()

[Link]("MEMBER NAME:"+name);

[Link]("MEMBER AGE:"+age);

[Link]("MEMBER PHONE NUMBER:"+phone);

[Link]("MEMBER PLACE:"+place);

printsal();

class Employee extends Member

String special;

void getemp()throws IOException

getdetail();

BufferedReader br=new BufferedReader(new


InputStreamReader([Link]));

[Link]("ENTER THE SPECIALIZATION OF EMPLOYEE:");

special=[Link]();

void printemp()

34
printdetail();

[Link]("EMPLOYEE SPECIALIZATION:"+special);

class Manager extends Member

String depart;

void getmgr()throws IOException

getdetail();

BufferedReader br=new BufferedReader(new


InputStreamReader([Link]));

[Link]("ENTER THE DEPARTMENT OF MANAGER:");

depart=[Link]();

void printmgr()

printdetail();

[Link]("MANAGER OF THE DEPARTMENT:"+depart);

class PB1

public static void main(String args[])throws IOException

[Link]("~~~ENTER THE DETAILS OF EMPLOYEE~~~");

35
Employee emp=new Employee();

[Link]();

[Link]("~~~~ENTER THE DETAILS OF MANAGER~~~~");

Manager mgr=new Manager();

[Link]();

[Link]("*****THE DETAILS OF EMPLOYEE*****");

[Link]("**********************************");

[Link]();

[Link]("********THE DETAILS OF MANAGER*******");

[Link]("*************************************");

[Link]();

OUTPUT:

36
PROGRAM: 2

Program to implement the following class hierarchy:


Student:id,name StudentExam(derived from student):Marks of
3subjects,total marks StudentResult(derived from

37
StudentExam):percentage,grade Define appropriate methods to
accept and calculate grade based on existing criteria and display
details of N students.

import [Link].*;

import [Link].*;

class Student

int id;

String name;

Student(int sid,String sname)

id=sid;

name=sname;

void display1()

[Link](id+"\t"+name+"\t");

class Exam extends Student

int m1,m2,m3;

double tot;

Exam(int sid,String sname,int mk1,int mk2,int mk3)

38
super(sid,sname);

m1=mk1;

m2=mk2;

m3=mk3;

void display2()

display1();

tot=m1+m2+m3;

[Link](m1+"\t"+m2+"\t"+m3+"\t"+tot+"\t");

class Result extends Exam

double per;

String grade;

Result(int sid,String sname,int mk1,int mk2,int mk3)

super(sid,sname,mk1,mk2,mk3);

void display3()

display2();

per=tot/3.0;

if(m1>=35&&m2>=35&&m3>=35)
39
{

if(per>70)

grade="DISTINTION";

else if(per>60)

grade="FIRST CLASS";

else if(per>50)

grade="SECOND CLASS";

else

grade="PASS CLASS";

else

grade="FAIL";

[Link](per+"\t"+grade+"\n");

class PB2

public static void main(String args[])throws IOException

int n,i;

BufferedReader br=new BufferedReader(new


InputStreamReader([Link]));

[Link]("ENTER THE NUMBER OF STUDENTS:");

n=[Link]([Link]());

int sid[]=new int[n];

40
String sname[]=new String[n];

int m1[]=new int[n];

int m2[]=new int[n];

int m3[]=new int[n];

for(i=0;i<n;i++)

[Link]("ENTER STUDENT ID:");

sid[i]=[Link]([Link]());

[Link]("ENTER STUDENT NAME:");

sname[i]=[Link]();

[Link]("MARK1:");

m1[i]=[Link]([Link]());

[Link]("MARK2:");

m2[i]=[Link]([Link]());

[Link]("MARK3:");

m3[i]=[Link]([Link]());

[Link]("**************STUDENT MARKS LIST***********");

[Link]("____________________________________");

[Link](" SID SNAME MARK1 MARK2 MARK3 TOTAL PERCENT


GRADE");

[Link]("_______________________________________________");

for(i=0;i<n;i++)

41
Result obj=new Result(sid[i],sname[i],m1[i],m2[i],m3[i]);

obj.display3();

OUTPUT:

42
PROGRAM: 3
Program to calculate marks of a student using multiple
inheritance implemented through [Link] Student with

43
data members rollNo,name,String class and methods to set and
put [Link] another class test extended by class Student
with data membersmark1,mark2,mark3 and methods to set and
put [Link] interface sports with members sports Wt=5 and
put Wt().Now let the class results extends class test and
implements interface [Link] a Java program to read
required data and display details in a neat format.

import [Link].*;

import [Link].*;

class Student

int rollno;

String sname,sclass;

void setdata(int rn,String sn,String cls)

rollno=rn;

sname=sn;

sclass=cls;

void putdata()

[Link]("STUDENT ROLL NUMBER:"+rollno);

[Link]("STUDENT NAME:"+sname);

[Link]("STUDENT CLASS:"+sclass);

class Test extends Student

44
{

int mark1,mark2,mark3;

void setmarks(int m1,int m2,int m3)

mark1=m1;

mark2=m2;

mark3=m3;

void putmarks()

[Link]("SUBJECT1 MARKS:"+mark1);

[Link]("SUBJECT2 MARKS:"+mark2);

[Link]("SUBJECT3 MARKS:"+mark3);

interface Sports

int swt=5;

void putwt();

class Result extends Test implements Sports

public void putwt()

[Link]("SPORTS WEIGHTAGE:"+swt);
45
}

void cal()

int total;

total=mark1+mark2+mark3+swt;

[Link]("TOTAL MARKS:"+total);

class PB33

public static void main(String args[])throws IOException

int rn,m1,m2,m3;

String sn,cls;

BufferedReader br=new BufferedReader(new


InputStreamReader([Link]));

[Link]("ENTER ROLLNO OF THE STUDENT");

rn=[Link]([Link]());

[Link]("ENTER THE NAME OF THE STUDENT:");

sn=[Link]();

[Link]("ENTER CLASS");

cls=[Link]();

Result obj=new Result();

[Link]("ENTER SUBJECT1 MARKS");

m1=[Link]([Link]());

46
[Link]("ENTER SUBJECT2 MARKS");

m2=[Link]([Link]());

[Link]("ENTER SUBJECT3 MARKS");

m3=[Link]([Link]());

[Link](rn,sn,cls);

[Link](m1,m2,m3);

[Link]("************STUDENT MARKS DETAILS************");

[Link]("*********************************************");

[Link]();

[Link]();

[Link]();

[Link]();

[Link]("*************************************");

47
OUTPUT:

48
PROGRAM: 4

Program to create an abstract class named shape that


contains two integers and an empty method named print
Area().Provide three classes named Rectangle, Triangle
and Ellipse such that each one of the classes extends the
class [Link] one of the class contains only the
method print Area() that print the area of the given
shape.

import [Link].*;

import [Link].*;

abstract class Shape

int m,n;

abstract void print_Area();

class Rectangle extends Shape

Rectangle(int a,int b)

49
{

m=a;

n=b;

void print_Area()

double area;

area=m*n;

[Link]("AREA OF RECTANGLE IS:"+area);

class Triangle extends Shape

Triangle(int a,int b)

m=a;

n=b;

void print_Area()

double area;

area=0.5*m*n;

[Link]("AREA OF TRIANGLE IS:"+area);

class Ellipse extends Shape


50
{

Ellipse(int a,int b)

m=a;

n=b;

void print_Area()

double area;

area=3.14*m*n;

[Link]("AREA OF ELLIPSE IS:"+area);

class PB4

public static void main(String args[])throws IOException

int p,q;

BufferedReader br=new BufferedReader(new


InputStreamReader([Link]));

[Link]("Enter the first parameter:");

p=[Link]([Link]());

[Link]("Enter the second parameter:");

q=[Link]([Link]());

Rectangle obj1=new Rectangle(p,q);

obj1.print_Area();

51
Triangle obj2=new Triangle(p,q);

obj2.print_Area();

Ellipse obj3=new Ellipse(p,q);

obj3.print_Area();

52
OUTPUT:

PROGRAM: 5

53
Create a package to convert temperature in centigrade
into Fahrenhit,and one more package to calculate the
simple [Link] both package in the Main() by
accepting the required inputs for each application.

import [Link];

import [Link];

import [Link].*;

public class B5 {

public static void main(String[] args) throws IOException {

double temp, p, t, r;

int ch;

BufferedReader br = new BufferedReader(new


InputStreamReader([Link]));

do {

[Link]("************MENU**************");

[Link]("1. Temperature Conversion");

[Link]("2. Simple Interest");

[Link]("Enter your choice:");

ch = [Link]([Link]());

switch (ch) {

case 1:

[Link]("Enter temperature in degree Celsius:");

temp = [Link]([Link]());

Convert obj = new Convert();

54
[Link]("Temp in Fahrenheit is " +
[Link](temp));

break;

case 2:

[Link]("Enter value for P, T and R:");

p = [Link]([Link]());

t = [Link]([Link]());

r = [Link]([Link]());

Simple obj1 = new Simple();

[Link]("Simple Interest is " + [Link](p, t, r));

break;

default:

[Link]("Invalid choice! Exiting.");

ch = 0; // to exit loop

break;

} while (ch >= 1 && ch <= 2);

OUTPUT:

55
PROGRAM:6

Program that implements a multi-threaded program has three


[Link] thread generates a random integer every
56
second,and if the value is even,second thread computes the
square of the number and [Link] the value is odd the thord
thread will print the value of cube of the number.

import [Link].*;

import [Link].*;

class T1 extends Thread

public void run()

for(;;)

int n;

try

[Link](1000);

n=(int)([Link]([Link]()*10));

[Link]("GENERATED NUMBERS IS:"+n);

if(n%2==0)

T2 obj1=new T2(n);

[Link]();

else

T3 obj2=new T3(n);

57
[Link]();

catch(InterruptedException e)

class T2 extends Thread

int n;

T2(int r)

n=r;

public void run()

[Link]("THE SQUARE OF THE NUMBER IS:"+n*n);

class T3 extends Thread

int n;

T3(int r)

{
58
n=r;

public void run()

[Link]("THE CUBE OF THE NUMBER IS:"+n*n*n);

class PB6

public static void main(String args[])throws IOException

T1 obj=new T1();

[Link]();

OUTPUT:

59
PROGRAM: 7

Program that creates a user interface to perform basic integer


[Link] user enters two numbers in the TextFields-Num1
and [Link] result of operations must be displayed in the
Result TextField when the “=” button is [Link]

60
Exception handling message to be displayed in the Result
TextField Num1 or Num2 is not an integer or Num2 is Zero when
division operation is applied.

import [Link].*;

import [Link].*;

import [Link].*;

class PB7 extends JFrame implements ActionListener

JTextField txt1,txt2,txt3,txt4;

JLabel lbl1,lbl2,lbl3,lbl4;

JButton btn;

PB7()

getContentPane().setLayout(new FlowLayout());

setSize(800,600);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

txt1=new JTextField(10);

txt2=new JTextField(5);

txt3=new JTextField(10);

txt4=new JTextField(10);

lbl1=new JLabel("Number 1");

lbl2=new JLabel("Operation");

lbl3=new JLabel("Number 2");

lbl4=new JLabel("Result");

61
btn=new JButton("=");

getContentPane().add(lbl1);

getContentPane().add(txt1);

getContentPane().add(lbl2);

getContentPane().add(txt2);

getContentPane().add(lbl3);

getContentPane().add(txt3);

getContentPane().add(btn);

getContentPane().add(lbl4);

getContentPane().add(txt4);

[Link](this);

public void actionPerformed(ActionEvent e)

int num1,num2,res=0;

char op;

try

if([Link]()==btn)

num1=[Link]([Link]().trim());

num2=[Link]([Link]().trim());

op=[Link]().trim().charAt(0);

switch(op)

case '+':
62
res=num1+num2;

break;

case '-' :

res=num1-num2;

break;

case '*':

res=num1*num2;

break;

case '/':

res=num1/num2;

break;

[Link](" "+res);

catch(ArithmeticException e1)

[Link]("");

[Link](null,"Divide by Zero");

catch(NumberFormatException e2)

[Link](" ");

[Link](null,"Field can't be Empty");

}
63
}

public static void main(String args[])

new PB7();

OUTPUT:

64
PROGRAM:8

Using the swing components,design the frame for shopping a


book that accepts book code,book name,and [Link] the
discount on code as follows.
Code Discount Rate
101 15%
102 20%
103 25%
Any other 5%
Find the discount amount and Net bill [Link] the bill.

import [Link].*;

import [Link].*;

import [Link].*;

import [Link].*;

65
class PB8 extends JFrame implements ActionListener

JLabel l1,l2,l3,l4,l5;

JTextField t1,t2,t3,t4,t5;

JButton b1,b2;

PB8()

getContentPane().setLayout(new GridLayout(6,2));

setSize(400,500);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

t1=new JTextField(20);

t2=new JTextField(20);

t3=new JTextField(20);

t4=new JTextField(20);

t5=new JTextField(20);

l1=new JLabel("Book Code");

l2=new JLabel("Book Name");

l3=new JLabel("Price");

l4=new JLabel("Discount");

l5=new JLabel("NetBill");

b1=new JButton("Calculate");

b2=new JButton("Clear");

getContentPane().add(l1);

getContentPane().add(t1);

getContentPane().add(l2);
66
getContentPane().add(t2);

getContentPane().add(l3);

getContentPane().add(t3);

getContentPane().add(l4);

getContentPane().add(t4);

getContentPane().add(l5);

getContentPane().add(t5);

getContentPane().add(b1);

getContentPane().add(b2);

[Link](this);

[Link](this);

public void actionPerformed(ActionEvent e)

int bcode;

double price=0.0,discount,net;

try

if([Link]()==b1)

bcode=[Link]([Link]().trim());

price=[Link]([Link]().trim());

if(bcode==101)

discount=0.15*price;

else if(bcode==102)

discount=0.2*price;
67
else if(bcode==103)

discount=0.25*price;

else

discount=0.05*price;

net=price-discount;

[Link](" "+discount);

[Link](" "+net);

if([Link]()==b2)

[Link](" ");

[Link](" ");

[Link](" ");

[Link](" ");

[Link](" ");

}}

catch(Exception a)

[Link]("Invalid entry");

public static void main(String argd[])

new PB8();

}
68
OUTPUT:

69

You might also like