Java File
Java File
Sr
Program Name Date Grade Signature
No.
WAP to find total, percentage and
1 grade of a student using class in
Java.
package javafile;
import java.util.Scanner;
Page 1 of 2
Program - 1
else if(percentage>=60){
System.out.println("Grade is B");
}
else if(percentage>=50){
System.out.println("Grade is C");
}
else if(percentage>=33){
System.out.println("Grade is D");
}
else{
System.out.println("Grade is F");
}
}
}
OUTPUT
Page 2 of 2
Program - 2
PROGRAM TO CHECK WHETHER A NO IS PRIME OR NOT
USING FUNCTION
package javafile;
import java.util.Scanner;
Page 1 of 2
Program - 2
OUTPUT
Page 2 of 2
Program - 3
PROGRAM TO CHECK WHETHER A NO IS PALINDROME
OR NOT USING LOOPS IN JAVA
package javafile;
import java.util.Scanner;
class PallindromeOrNot{
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
int sum=0,number, temp;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number : ");
number = sc.nextInt();
temp = number;
while(temp > 0){
int r = temp%10;
sum = (sum*10) + r;
temp /= 10;
}
if(sum == number){
System.out.println(number + " is a pallindrome number");
}
else{
System.out.println(number + " is not a pallindrome number");
}
}
}
Page 1 of 2
Program - 3
OUTPUT
Page 2 of 2
Program - 4
PROGRAM TO DEFINE A CLASS, CREATE OBJECTS AND
CALL THE METHODS OF THE CLASS
package javafile;
OUTPUT
Page 1 of 1
Program - 5
PROGRAM TO EXPLAIN METHOD OVERLOADING
package javafile;
OUTPUT
Page 1 of 1
Program - 6
PROGRAM TO EXPLAIN CONSTRUCTOR OVERLOADING
package javafile;
public class ConstructorOverloading {
int id;
String name;
ConstructorOverloading() {
System.out.println("Default Constructor");
}
ConstructorOverloading(int id, String name) {
this.id = id;
this.name = name;
}
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
ConstructorOverloading St1 = new ConstructorOverloading();
System.out.println("Student 1 Details");
System.out.println("\t id is : " + St1.id);
System.out.println("\t name is : " + St1.name);
Page 1 of 2
Program - 6
OUTPUT
Page 2 of 2
Program - 7
PROGRAM TO EXPLAIN METHOD OVERRIDING
package javafile;
class Bank{
int getRateOfInterest() {
return 0;
}
}
//Creating child classes.
class SBI extends Bank{
int getRateOfInterest() {
return 8;
}
}
class ICICI extends Bank{
int getRateOfInterest() {
return 7;
}
}
class AXIS extends Bank{
int getRateOfInterest() {
return 9;
}
}
public class MethodOverriding {
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
Page 1 of 2
Program - 7
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}
}
OUTPUT
Page 2 of 2
Program - 8
PROGRAM TO EXPLAIN DIFFERENT TYPES OF
INHERITANCE IN JAVA
package javafile;
import java.util.Scanner;
class Student {
int roll;
String name;
Scanner sc = new Scanner(System.in);
public void getData() {
System.out.print("\nEnter Roll no. : ");
roll = sc.nextInt();
System.out.print("\nEnter Name : ");
name = sc.next();
}
public void display(){
System.out.println("\nRoll No. is : " + roll);
System.out.println("\nName is : " + name);
}
}
class BCA extends Student {
int sub1, sub2;
public void readData() {
getData();
System.out.print("\nEnter Subject 1 Marks : ");
sub1 = sc.nextInt();
System.out.print("\nEnter Subject 2 Marks : ");
sub2 = sc.nextInt();
}
public void putData() {
Page 1 of 4
Program - 8
display();
System.out.println("\nSubject 1 marks is : " + sub1);
System.out.println("\nSubject 2 marks is : " + sub2);
}
}
class BcaTotal extends BCA {
int total;
public void findTotal() {
readData();
total = sub1 + sub2;
System.out.println("\nBCA Student data");
putData();
System.out.println("\nTotal Marks are : " + total);
}
}
class BSC extends Student {
int sub1, sub2;
public void readData() {
getData();
System.out.print("\nEnter Subject 1 Marks : ");
sub1 = sc.nextInt();
System.out.print("\nEnter Subject 2 Marks : ");
sub2 = sc.nextInt();
}
public void putData() {
display();
System.out.println("\nSubject 1 marks is : " + sub1);
System.out.println("\nSubject 2 marks is : " + sub2);
}
}
class BscTotal extends BSC {
int total;
Page 2 of 4
Program - 8
public void findTotal() {
readData();
total = sub1 + sub2;
System.out.println("\nBSC Student data");
putData();
System.out.println("\nTotal Marks are : " + total);
}
}
public class UseOfInheritance {
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
BcaTotal b1 = new BcaTotal();
System.out.println("\nEnter BCA Student data");
b1.findTotal();
Page 3 of 4
Program - 8
OUTPUT
Page 4 of 4
Program - 9
PROGRAM TO MULTIPLE INHERITANCE IN JAVA USING
INTERFACE
package javafile;
interface First {
int X = 200;
void display();
}
interface Second{
int X = 300;
void display();
}
class Demo implements First, Second{
@Override
public void display(){
System.out.println("Implemented Multiple Inheritance");
}
}
public class UseOfInterface {
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
Demo d1 = new Demo();
d1.display();
System.out.println("Interface 1 x value is : " + First.X);
System.out.println("Interface 2 x value is : " + Second.X);
}
}
Page 1 of 2
Program - 9
OUTPUT
Page 2 of 2
Program - 10
PROGRAM TO CREATE AND USE PACKAGES IN JAVA
//UseOfPackage.java
package mypackage;
import javafile.CreatePackage;
//CreatePackage.java
package javafile;
OUTPUT
Page 1 of 1
Program - 11
PROGRAM TO EXPLAIN THE GUI PROGRAMMING USING
TEXT FIELD, LABEL AND COMMAND BUTTON AWT
TOOLS
package javafile;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
Page 1 of 3
Program - 11
label[i].setBounds(x, y, 120, 25);
add(label[i]);
y=y+60;
}
y=80;
for(int i=0;i<objtxt.length-1;i++)
{
objtxt[i]=new TextField();
objtxt[i].setBounds(165, y, 120, 25);
add(objtxt[i]);
y=y+60;
objtxt[i].setEditable(true);
}
Page 2 of 3
Program - 11
}
});
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
dispose();
}
});
}
}
OUTPUT
Page 3 of 3
Program - 12
PROGRAM TO EXPLAIN THE GUI PROGRAMMING USING
RADIO BUTTONS AND CHECK BOXES AWT TOOLS
package javafile;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
Page 1 of 4
Program - 12
label[i]=new Label(str[i]);
label[i].setBounds(x, y, 120, 25);
add(label[i]);
y=y+60;
}
y=80;
for(int i=0;i<objtxt.length;i++)
{
objtxt[i]=new TextField();
objtxt[i].setBounds(165, y, 180, 25);
add(objtxt[i]);
y=y+60;
objtxt[i].setEditable(true);
}
Page 2 of 4
Program - 12
add(checkbox1);
Checkbox checkbox2=new Checkbox("Basketball");
checkbox2.setBounds(260, 320, 80, 20);
add(checkbox2);
Checkbox checkbox3=new Checkbox("Swimming");
checkbox3.setBounds(165, 380, 80, 20);
add(checkbox3);
Checkbox checkbox4=new Checkbox("Cycling");
checkbox4.setBounds(260, 380, 80, 20);
add(checkbox4);
Button objbtn=new Button("Upload");
objbtn.setBounds(100,440,180,35);
add(objbtn);
setSize(400,600);
setLayout(null);
setVisible(true);
//event handling
objbtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
label[5] = new Label();
label[5].setBounds(130,500,115,20);
label[5].setText("Details Submitted!!!");
label[5].setBackground(Color.BLACK);
label[5].setForeground(Color.white);
add(label[5]);
}
});
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
Page 3 of 4
Program - 12
dispose();
}
});
}
}
OUTPUT
Page 4 of 4
Program - 13
PROGRAM TO EXPLAIN THE GUI PROGRAMMING USING
LISTS AND COMBO BOXES AWT TOOLS
package javafile;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.List;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
*
* @author Manan
*/
public class GuiProgramming3 {
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
MainFrame3 obj= new MainFrame3("Student Details");
}
}
class MainFrame3 extends Frame{
MainFrame3(String title){
super(title);
Label[] label = new Label[6];
TextField[] objtxt=new TextField[3];
Page 1 of 4
Program - 13
String[] str= {"Enter First Name : ","Enter Last Name : ","e-Mail id : "};
int x=40,y=80;
for (int i=0;i<3;i++)
{
label[i]=new Label(str[i]);
label[i].setBounds(x, y, 120, 25);
add(label[i]);
y=y+60;
}
y=80;
for(int i=0;i<objtxt.length;i++)
{
objtxt[i]=new TextField();
objtxt[i].setBounds(165, y, 180, 25);
add(objtxt[i]);
y=y+60;
objtxt[i].setEditable(true);
}
Page 2 of 4
Program - 13
label[4].setBounds(40, 320, 120, 25);
add(label[4]);
Checkbox checkbox1=new Checkbox("Volleyball");
checkbox1.setBounds(165, 320, 80, 25);
add(checkbox1);
Checkbox checkbox2=new Checkbox("Basketball");
checkbox2.setBounds(260, 320, 80, 25);
add(checkbox2);
Checkbox checkbox3=new Checkbox("Swimming");
checkbox3.setBounds(165, 380, 80, 25);
add(checkbox3);
Checkbox checkbox4=new Checkbox("Cycling");
checkbox4.setBounds(260, 380, 80, 25);
add(checkbox4);
label[5] = new Label("Select Class");
label[5].setBounds(40, 440, 120, 25);
add(label[5]);
List l1 = new List(4);
l1.add("9th");
l1.add("10th");
l1.add("11th");
l1.add("12th");
add(l1);
l1.setBounds(165,440,120,35);
Button objbtn=new Button("Upload");
objbtn.setBounds(100,500,180,35);
add(objbtn);
setSize(400,600);
setLayout(null);
setVisible(true);
//event handling
objbtn.addActionListener(new ActionListener(){
Page 3 of 4
Program - 13
@Override
public void actionPerformed(ActionEvent e) {
label[5] = new Label();
label[5].setBounds(130,560,115,25);
label[5].setText("Details Submitted!!!");
label[5].setBackground(Color.BLACK);
label[5].setForeground(Color.white);
add(label[5]);
}
});
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
dispose();
}
});
}
}
OUTPUT
Page 4 of 4
Program - 14
PROGRAM TO EXPLAIN THE GUI PROGRAMMING USING
PASSWORD FIELD AND TEXTAREA BOXES AWT TOOLS
package javafile;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import static java.awt.Font.BOLD;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JPasswordField;
import java.awt.TextArea;
public class GuiProgramming4 {
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
MainFrame4 obj= new MainFrame4("Student Details");
}
}
class MainFrame4 extends Frame{
MainFrame4(String title){
super(title);
setLayout(null);
Label head = new Label("Login Form");
add(head);
head.setBounds(150, 45, 140, 35);
Font f = new Font("Courier", Font.PLAIN, 20);
Page 1 of 3
Program - 14
head.setFont(f);
Label objlbl=new Label("Enter name");
objlbl.setBounds(60, 100, 80,25);
add(objlbl);
setVisible(true);
setSize(400, 400);
//event handling
Page 2 of 3
Program - 14
objbtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
txt.setRows(7);
txt.setText("Your Login Details are : \n\n");
txt.setText(txt.getText()+ "Name : " + objtxt.getText());
txt.setText(txt.getText()+ "\nPassword : " +obJPasswordField1.getText());
}
});
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
dispose();
}
});
}
}
OUTPUT
Page 3 of 3
Program - 15
PROGRAM TO EXPLAIN FILE HANDLING IN JAVA
package javafile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
Page 1 of 4
Program - 15
}
}
catch (IOException e) {
System.out.println("Error : Can't create file!!!");
e.printStackTrace();
}
}
Page 2 of 4
Program - 15
}
objfis.close();
System.out.println("\n*****Data reading successful and File Closed*****");
}
catch (IOException e) {
e.printStackTrace();
}
}
Page 3 of 4
Program - 15
OUTPUT
Page 4 of 4
Program - 16
PROGRAM TO EXPLAIN EXCEPTION HANDLING IN JAVA
package javafile;
import java.io.*;
class Sample{
void myMethod() throws IOException{
System.out.println("In myMethod() before exception occurs");
throw new IOException("Device Not Working");
}
}
public class ExceptionHandling {
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
try{
Sample obj = new Sample();
obj.myMethod();
}
catch(IOException e){
System.out.println("Exception Handled");
}
finally{
System.out.println("Finally Block Executed");
}
System.out.println("Last Statement");
}
}
Page 1 of 2
Program - 16
OUTPUT
Page 2 of 2
Program - 17
PROGRAM TO EXPLAIN THE USE OF FINALIZE()
METHOD IN JAVA
package javafile;
class FinalizeDemo {
@Override
protected void finalize() throws Throwable {
try {
System.out.println("inside Finalizedemo's Class finalize()");
}
catch (Throwable e) {
throw e;
}
finally {
System.out.println("Calling finalize method" + " of the Object class");
super.finalize(); // Calling finalize() of Object class
}
}
}
public class FinalizeMethod {
public static void main(String[] args) throws Throwable {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
FinalizeDemo d = new FinalizeDemo();
d.finalize();
}
}
Page 1 of 2
Program - 17
OUTPUT
Page 2 of 2
Program - 18
PROGRAM TO EXPLAIN MULTI THREADING IN JAVA
package javafile;
Page 1 of 2
Program - 18
public static void main(String[] args) {
System.out.println("\t\tProgram by:-\n\t\tManan Pasricha\n\t\tMCA 1st Year\n");
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
t1.start();
t2.start();
}
}
OUTPUT
Page 2 of 2