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

Last_two_programs_java_lab

The document provides multiple Java programming examples demonstrating various concepts such as calculating student grades, displaying student details, managing bank account balances, function overloading, using final and super keywords, abstract classes, handling multiple threads, exception handling, and GUI components with ActionListeners. Each example includes code snippets illustrating the implementation of these concepts. The document serves as a comprehensive guide for understanding Java programming fundamentals.

Uploaded by

Shilpa Abhang
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 views29 pages

Last_two_programs_java_lab

The document provides multiple Java programming examples demonstrating various concepts such as calculating student grades, displaying student details, managing bank account balances, function overloading, using final and super keywords, abstract classes, handling multiple threads, exception handling, and GUI components with ActionListeners. Each example includes code snippets illustrating the implementation of these concepts. The document serves as a comprehensive guide for understanding Java programming fundamentals.

Uploaded by

Shilpa Abhang
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

Java AWT TextField Example with ActionListener

1
Expected output

2
3
Output

4
1. To calculate grade of the student using classes and object
package p01;
public class grade
{
public static void main(String[] args)
{
student obj=new student(25,25,25);
[Link]();
}
}

class student
{
int sub1,sub2,sub3,total,avg;
student(int s1,int s2,int s3)
{
sub1=s1;
sub2=s2;
sub3=s3;
}
void show()
{
total=sub1+sub2+sub3;
avg=total/3;
if(avg>=70)
{
[Link]("Grade is A,Excellent");
}
else if(avg>=50&&avg<70)
5
{
[Link]("Grade is B,Good");
}
else if(avg>=30&&avg<50)
{
[Link]("Grade is C,Average");
}
else
{
[Link]("Grade is F,Fail");
}

}
}

6
2. To display details of students with the help of default, parameterized and copy
constructors
package p01;
public class stud_details {
public static void main(String[] args) {
stud s1=new stud();
stud s2=new stud("joy",201,21,"MCA");
stud s3=new stud("meena",202,22,"MCA");
stud s4=new stud(s3);
[Link]();
s2.display1();
s4.display1();
}
}
class stud{
int year, rollno, age;
String name, course;
stud()
{
year=2020;

}
stud(String n,int r,int a,String c) {
name=n;
rollno=r;
age=a;
course=c;
}
stud(stud obj) {
name=[Link];

7
rollno=[Link];
age=[Link];
course=[Link];
}
void display()
{
[Link]("STUDENT DETAILS OF YEAR :"+year);
[Link]("_____________________________");
}
void display1()
{
[Link]("Name: "+name);
[Link]("Rollno: "+rollno);
[Link]("Age: "+age);
[Link]("Course: "+course);
[Link]("");
} }

8
3. To find balance of a customer in a bank account
package p01;
import [Link];
public class bank_bal
{
public static void main(String[] args)
{
balance b1=new balance(101,"pavithra",222887,600);
[Link]();
[Link]();
[Link]();
}
}
class balance
{
Scanner c=new Scanner([Link]);
int cust_id, acc_no, bal,amt;
String cust_name;
balance(int id,String name,int no,int b)
{
cust_id=id;
cust_name=name;
9
acc_no=no;
bal=b;
}
void credit()
{
[Link]("Enter amount you want to deposit: ");
amt=[Link]();
[Link]("");
bal=bal+amt;
}
void debit()
{
[Link]("Enter amount you want to withdraw: ");
amt=[Link]();
[Link]("");
if(bal>=amt)
{
bal=bal-amt;
}
else
{
[Link]("Transaction failed: low
balance!!!");
}
}
void show()
{
[Link]("");
[Link]("CUSTOMER BANK DETAILS");
[Link]("***********************");
[Link]("CUSTOMER ID= "+cust_id);
[Link]("CUSTOMER NAME= "+cust_name);
[Link]("CUSTOMER ACCOUNT_NO=
"+cust_id);
[Link]("CUSTOMER BALANCE= "+bal);
10
}
}

or

package p01;
class balance0{
int cust_id,acc_no,bal,amt,credit,debit;
String cust_name;
balance0(int id,String name,int no,int b)
{
cust_id=id;
cust_name=name;
acc_no=no;
bal=b;
11
}
void credit(int c) {
credit=c;
bal=bal+credit;
[Link]("after credit");
[Link]("customer balance"+bal);
}
void debit(int d) {
debit=d;
if(bal>=debit)
{
bal=bal-debit;
[Link]("after debit");
[Link]("customer balance"+bal);
[Link]("");
[Link]("CUSTOMER BANK DETAILS");
[Link]("***********************");
[Link]("CUSTOMER ID= "+cust_id);
[Link]("CUSTOMER NAME= "+cust_name);
[Link]("CUSTOMER ACCOUNT_NO= "+cust_id);
[Link]("CUSTOMER BALANCE= "+bal);
}
else
{
[Link]("Transaction failed:low balance!!!");
}
}
}
public class bank_bal2 {
public static void main(String[] args) {
balance0 s=new balance0(101,"pavithra",222887,600);
[Link](100);
[Link](500);
}
}

12
13
4. To demonstrate function overloading

package p01;
public class function_overload
{
public static void main(String[] args)
{

[Link]("WELCOME TO UNIVERSITY");
[Link]("******************************");
[Link]("STUDENT DETAILS OF YEAR :
"+2020);

[Link]("___________________________");
[Link]("Student name is: "+”Preeta”);
[Link]("Student Id is: "+202);
[Link]("Student course is: "+”MCA”);

}
}

class student0
{
void display()
{
[Link]("WELCOME TO UNIVERSITY");
14
[Link]("******************************");
}
void display(int year)
{
[Link]("STUDENT DETAILS OF YEAR : "+year);
}
void display(String name,int id,String course)
{
[Link]("___________________________");
[Link]("Student name is: "+name);
[Link]("Student Id is: "+id);
[Link]("Student course is: "+course);
}
}

Public static void main()


15
(
display(8);
}

void display(int x)()


void display(int x)()
5. To demonstrate usage of final keyword

package p01;
class main{
final void display() {
[Link]("this is a final method");
}
}
public class final_keyword extends main{
void display() {
[Link]("final method is overridden");
}
public static void main(String[] args) {
final_keyword o=new final_keyword();
[Link]();
}
}

16
or

package p01;
public class final_variable
{
final int x=100;
void test()
{
[Link]("value of x ="+x);
}

public static void main(String[] args)


{
final_variable s=new final_variable();
//s.x=300;This line will give an exception
since we are trying to change the value of the final
variable.
17
[Link]();
}
}

18
6. To demonstrate usage of super keyword

package p01;
public class super_key
{
public static void main(String[] args)
{
B obj=new B(192,100);
[Link]();
}
}
class A {
int height;
}
class B extends A
{
int height;
B(int h,int d)
{
[Link]=h;
height=d;
}
void show()
{
[Link]("Height of red box:
"+[Link]);
[Link]("Height of blue box: "+height);
}
}

19
Class B
{
int height;
int height;
}

A(x)->B(a,b) B(10,20,30)
{
super(10);
a=20;
b=30;
}->C(h,j)->D->E

20
try
{
}
catch
{
}

finally
{
fclose()
}

7. To demonstrate abstract class

21
package p01;
abstract class ab
{
abstract void callme();
void callmetoo()
{
[Link]("This is an concrete method");
}
}

class bc extends ab
{
void callme()
{
[Link]("B’s implementation of callme");
}
}
public class abstract_class
{
public static void main(String[] args) {
bc obj1=new bc();
[Link]();
[Link]();
}
}

8. To handle multiple threads in java


package p01;

22
class newthread implements Runnable
{
String name;
Thread t;
newthread(String threadname) {
name=threadname;
t=new Thread(this,name);
[Link]("New thread " + t);
[Link]();
}
public void run(){
try{
for(int i=5;i>0;i--)
{
[Link]("child thread"+i);
[Link](1000);
}
}
catch(InterruptedException e)
{
[Link](name+ "interrupted");
}
[Link](name + "Exiting ");
} }

public class multiple_thread {


public static void main(String[] args) {
new newthread("one");
new newthread("two");
new newthread("three");
try
{
[Link](10000);
}
catch(InterruptedException e)
{
[Link]("Main thread interrupted");
}
[Link]("Main thread exiting");
}
}

23
9. To handle exception
package p01;

24
public class multiple_catch {
public static void main(String[] args)
{
try{
int a[]=new int[5];
[Link](a[10]);
}
catch(ArithmeticException e) {
[Link]("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e){
[Link]("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
[Link]("Parent Exception occurs");
}
[Link]("rest of the code");
}
}

[Link] demonstrate java awt text field with action listener

import [Link].*;

25
import [Link].*;
public class textfield extends Frame implements ActionListener {
TextField tf1,tf2,tf3;
Button b1,b2;
textfield(){
tf1=new TextField();
[Link](50,50,150,20);
tf2=new TextField();
[Link](50,100,150,20);
tf3=new TextField();
[Link](50,150,150,20);
[Link](false);
b1=new Button("+");
[Link](50,200,50,50);
b2=new Button("-");
[Link](120,200,50,50);
[Link](this);
[Link](this);
add(tf1);add(tf2);add(tf3);add(b1);add(b2);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1=[Link]();
String s2=[Link]();
int a=[Link](s1);
int b=[Link](s2);
int c=0;
if([Link]()==b1){
c=a+b;
}
else if([Link]()==b2){
c=a-b;
}
String result=[Link](c);
[Link](result);
}
public static void main(String[] args) {
new textfield();
}
}

26
11. Java JButton with action listener

import [Link].*;
import [Link].*;
public class buttonex
{
public static void main(String[] args)
{
JFrame f=new JFrame("JButton Example");
final JTextField tf=new JTextField();
[Link](50,50, 150,20);
JButton b=new JButton("Click Here");
[Link](50,100,95,30);
[Link](new ActionListener(){
public void actionPerformed(ActionEvent e)
{
[Link]("Welcome to Java programming.");
}
});
[Link](b);[Link](tf);
[Link](400,400);
[Link](null);
[Link](true);
}
}

27
12. Inheritance program in java

28
package p01;
class X
{
void root() {
[Link]("parent of Y");
}
}
class Y extends X
{
void parent() {
[Link]("parent of Z");
}
}
class Z extends Y
{
void child() {
[Link]("child of Y && grandchild of X");
}
}
public class inheritance
{
public static void main(String[] args) {
Z p= new Z();
[Link]();
[Link]();
[Link]();
}
}

29

You might also like