0% found this document useful (0 votes)
83 views

Java File Lab

This document contains 35 programming problems and their solutions in Java. It begins with identifying information for a student named Rakesh in the 3rd year of an MCA program with a roll number. The bulk of the document consists of 35 programming problems numbered 1 through 35, each with a brief description of the problem and the code for the solution. At the end, each code sample provides the expected output.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Java File Lab

This document contains 35 programming problems and their solutions in Java. It begins with identifying information for a student named Rakesh in the 3rd year of an MCA program with a roll number. The bulk of the document consists of 35 programming problems numbered 1 through 35, each with a brief description of the problem and the code for the solution. At the end, each code sample provides the expected output.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 59

Rakesh

Mca-3rd year
RollNo-19001601039

SNO. PROGRAMS
1 WAP TO PRINT TODAY IS TUESDAY.
2 WAP TO PRINT SUM OF ALL DIGIT OF A NO 345.
3 WAP TO PRINT RIGHT ANGLE TRIANGLE OF 9 LINES.
4 WAP TO FIND SQUARE ROOT OF A NUMBER.
5 WAP TO PRINT SMALLEST AMONG THREE NUMBERS.
6 WAP TO PRINT ENTERED CHARACTER IS ALPHABET ,DIGIT, OR
SPECIAL CHARACTER.

7 WAP TO FIND SUM AND COUNT OF ALL THE INTEGERS BETWEEN 10


TO 100 WHICH IS DIVISIBLE BY 7.

8 WAP TO SOLVE EQUATIONS USING TERNARY OPERATOR.


9 WAP TO FIND SALARY USING TERNARY OPERATOR.
10 WAP TO PRINT ENTERED NUMBER IS PALINDROME OR NOT.
11 WAP TO PRINT TO FIND ALL PRIME NUMBER BETWEEN 1 TO 30.
12 WAP TO PRINT ALL COMBINATION OF 1,2,3.
13 WAP TO PRINT STAR PATTERN IN DIAMOND SHAPE.
14 WAP TO FIND THE REAL SOLUTIONS OF A QUADRATIC EQUATION.

Page 1
Rakesh
Mca-3rd year
RollNo-19001601039

15 WAP TO CALCULATE ARE OF A ROOM. MAKE A CLASS ROOM IN TWO


DATA MEMBERS LENGTH AND BREATH AND ONE MEMBER FUNCTION

CAL_AREA AND CREATE OBJECT OF ROOM CLASS IN ANOTHER CLASS.

16 WAP TO FIND FACTORIAL OF A NUMBER.


17 WAP TO DEMONSTRATE METHOD OVERLOADING. CREATE A METHOD
LARGER WHICH COMPARES TWO INTEGERS AS WELL AS FLOAT

VALUES.

18 CREATE A CLASS MATHOPERATION AND MAKE TWO METHODS ADD


AND MUL TO PERFORM ADDITION AND MULTIPLICATION OF TWO

FLOAT NUMBERS.

19 DESIGN A CLASS TO REPRESENT BANK ACCOUNT INCLUDES


FOLLOWING MEMBERS NAME OF THE DEPOSITER, ACCOUNT NUMBER,

TYPE OF ACCOUNT, BALANCE AMOUNT IN THE ACCOUNT. METHODS

TO DEPOSIT AN AMOUNT, WITHDRAWAL AMOUNT AFTER CHECKING

THE BALANCE IN THE ACCOUNT, DISPLAY NAME AND BALANCE. ALSO

DEFINE A CONSTRUCTOR FOR INITIALIZING THE VALUES.

20 MAKE A CLASS STUDENT AND DEFINE DATA MEMBERS AS NAME, ROLL

Page 2
Rakesh
Mca-3rd year
RollNo-19001601039

NO AND STATIC VENUE AND MAKE A STATIC METHOD CHANGE TO

CHANGE THE VALUE OF VENUE.

21 WRITE A PROGRAM TO DEMONSTRATE USE OF THIS KEYWORD.


22 WRITE A PROGRAM USING THIS TO SHOW CONSTRUCTOR CHAINING.
23 WRITE A PROGRAM TO MERGE TO INT ARRAYS HAVING NUMBER IN
ASCENDING ORDER.

24 CREATE A CLASS BANK WITH DATA_MEMBER NAME OF THE ACCOUNT


HOLDER, ACCOUNT NUMBER, TYPE OF ACCOUNT AND BALANCE IN

THE ACCOUNT AND MAKE MEMBER_FUNCTIONS AS DEPOSIT AMOUNT,

WITHDRAWAL AMOUNT AFTER CHECKING THE BALANCE, DISPLAY

NAME OF ACCOUNT HOLDER AND BALANCE. ALSO USE CONSTRUCTOR

IN YOUR PROGRAM.

25 CREATE A CLASS STUDENT WITH DATA_MEMBERS NAME OF STUDENT,


ROLLNO OF A STUDENT AND A STATIC DATA_MEMBER VENUE. MAKE A

STATIC METHOD CHANGE TO CHANGE THE VALUE OF VENUE. ALSO

USE CONSTRUCTOR FOR INITIALIZING THE VALUES.

26 CREATE AN ABSTRACT CLASS WITH THE FOLLOWING DETAIL -

Page 3
Rakesh
Mca-3rd year
RollNo-19001601039

DATA_MEMBERS BALANCE, ACCOUNT NUMBER, ACCOUNT HOLDER


NAME, ADDRESS AND ABSTRACT METHOD WITHDRAWAL, DEPOSIT

AND DISPLAY TO SHOW THE ACCOUNT BALANCE AND ACCOUNT

HOLDER NAME. CREATE A SUBCLASS SAVING_ACCOUNT OF THIS

CLASS WHICH HAS FOLLOWING DETAILS - DATA_MEMBERS RATE OF

INTEREST AND METHOD CALCULATE AMOUNT AND DISPLAY RATE OF

INTEREST WITH NEW BALANCE AND FULL ACCOUNT HOLDER DETAILS.

CREATE ANOTHER SUBCLASS OF THIS CLASS CURRENT_ACCOUNT


HAVING DATA_MEMBERS OVER DRAFT LIMIT AND METHOD TO SHOW

OVER DRAFT LIMIT ALONG WITH FULL ACCOUNT HOLDER DETAILS.

CREATE OBJECT OF SUBCLASSES WITH APPROPRIATE METHOD, MAKE


USE OF CONSTRUCTOR .

27 CREATE A CLASS STUDENT HAVING MEMBER_FUNCTIONS GET_ROLLNO


AND PUT_ROLLNO AND CREATE TEST CLASS WHICH EXTENDS

STUDENT CLASS HAVING METHODS GETMARKS AND PUTMARKS.


CREATE AN INTERFACE SPORT HAVING A FLOAT DATA_MEMBER OF
VALUE 6 AND AN ABSTRACT METHOD PUTTWEIGHT. CREATE

ANOTHER CLASS
Page 4
Rakesh
Mca-3rd year
RollNo-19001601039

RESULT WHICH EXTENDS TEST CLASS AND IMPLEMENT SPORT

INTERFACE AND DEFINE A FUNCTION DISPLAY IN RESULT CLASS

WHICH DISPLAY TOTAL MARKS OBTAINED BY STUDENT. CREATE MAIN

CLASS HYBRID IN WHICH THE OBJECT OF RESULT CLASS IS CREATED,

PASS VALUES IN FUNCTIONS AND DISPLAY THE RESULT OF STUDENT.

28 WRITE A PROGRAM IN JAVA TO IMPLEMENT THE CONCEPT OF

PACKAGE.

29 WRITE A PROGRAM IN WHICH WE CREATE A PACKAGE


PACKAGEEXAMPLE IN WHICH WE MAKE A CLASS IMPORTPACKAGE.

CREATE ANOTHER CLASS USEPACKAGE AND USE IMPORTPACKAGE


CLASS IN THE USEPACKAGE CLASS TO FIND THE FACTORIAL OF A

GIVEN NUMBER.

30 WRITE A PROGRAM TO IMPLEMENT SIMPLE EXAMPLE OF JAVA MULTI-


CATCH BLOCK.

31 WRITE A PROGRAM TO CREATE A CLASS MYEXCEPTION IN WHICH


TWO INT DATA MEMBERS X=5 , Y=1000 AND A FLOAT Z=X/Y.

Page 5
Rakesh
Mca-3rd year
RollNo-19001601039

IF THE VALUE OF Z IS LESS THAN .01 THEN THROW NEW


MYEXCEPTION AS NUMBER IS TOO SMALL.

32 WRITE A PROGRAM TO CREATE MULTIPLE THREADS IN JAVA.


33 WRITE A PROGRAM TO PRINT HELLOWORLD IN APPLET.
34 WRITE A PROGRAM TO CREATE A SMILING FACE IN APPLET.
35 WRITE A PROGRAM TO DISPLAY THE URL OF THE DIRECTORY
CONTAINING THE .CLASS FILE AND .HTML FILE. USE GETCODEBASE ()

AND GETDOCUMENTBASE () METHOD.

Page 6
Rakesh
Mca-3rd year
RollNo-19001601039

Program 1: WAP to print today is tuesday.

public class Day


{
public static void main(String[] args)
{
System.out.println("today is tuesday");
}
}

Output :

Page 7
Rakesh
Mca-3rd year
RollNo-19001601039

Program 2: WAP to print sum of all the digit of no 345.

public class SumOfDigit


{
public static void main(String[] args)
{
int num=345,temp=num;
int sum=0,result=0;
while(num>0)
{
sum=num%10;
result=sum+result;
num=num/10;
}
System.out.println("the sum of the digit of "+temp+" is "+result);
}}

Output:

Page 8
Rakesh
Mca-3rd year
RollNo-19001601039

Program 3: WAP to print right angle triangle of 9 lines.

public class RightAngle


{
public static void main(String[]args)
{
int n=9;
System.out.println();
for (int i=1;i<=n;i++)
{
for (int j=1;j<=i;j++)
{
System.out.printf("*");
}
System.out.println();
}
}}

Output:

Page 9
Rakesh
Mca-3rd year
RollNo-19001601039

Program 4 : WAP to find square root of a number.

import java.lang.Math;
public class SquareRoot
{
public static void main(String[] args)
{
int num=16;
System.out.println("the square root of "+num+" is "+Math.sqrt(num));
}
}

Output:

Page 10
Rakesh
Mca-3rd year
RollNo-19001601039

Program 5: WAP to print smallest among three numbers.

public class Smallest


{
public static void main (String[] args)
{
int a=20,b=15,c=65;
if(a<b && a<c)
{
System.out.println(" "+a+" is the smallest");
}
else if(b<c)
{
System.out.println(" "+b+" is the smallest");
}
else
{
System.out.println(" "+c+" is the smallest");
}
}}
Output:

Page 11
Rakesh
Mca-3rd year
RollNo-19001601039

Program 6: WAP to print entered character is alphabet ,digit, or


special character.
class Alphabet
{
public static void main(String[] args)
{
char ch='A';
if((ch>='a'&&ch<='z') || (ch>='A'&&ch<='Z'))
{
System.out.println(ch+" character is alphabet");
}
else if(ch>='0'&&ch<='9')
{
System.out.println(ch+" character is digit");
}
else
{
System.out.println(ch+" character is special character");
}}}

Output:

Page 12
Rakesh
Mca-3rd year
RollNo-19001601039

Program 7: WAP to find sum and count of all the integers between
10 to 100 which is divisible by 7.
class Div_By_7
{
public static void main (String [] args)
{
int num, sum=0, flag=0;
for (num=10; num<100; num++)
{
if(num%7==0)
{
sum=sum+num;
flag++;
}
}
System.out.println("Sum :"+sum);
System.out.println("count:"+flag);
}
}

Output:

Page 13
Rakesh
Mca-3rd year
RollNo-19001601039

Program 8: WAP to solve equations using ternary operator.

public class Equation


{
public static void main(String[] args)
{
double x=1.5d , y=0 , result;
result=(x<=2) ? (1.5*x+3):(2*x+5);
System.out.println("\n"+result);
}
}

Output:

Page 14
Rakesh
Mca-3rd year
RollNo-19001601039

Program 9: WAP to find salary using ternary operator.


class Testing
{
public static void main(String[] args)
{
double x=42,result;
result=(x<40) ? (4*x+100) : (x==40)? 300 : (4.5*x+150);
System.out.println(result);
}
}

Output:

Page 15
Rakesh
Mca-3rd year
RollNo-19001601039

Program 10: WAP to print entered number is palindrome or not.


public class Palindrome
{
public static void main (String [] args)
{
int num=Integer.parseInt(args [0]);
int temp=num;
int result=0;
while(num!=0)
{
int rem=num%10;
result=result*10+rem;
num=num/10;
}
if(result==temp)
System.out.println(" "+temp+" is palindrome number");
else
System.out.println(" "+temp+" is not palindrome number");
}}

Output:

Page 16
Rakesh
Mca-3rd year
RollNo-19001601039

Program 11: WAP to print to find all prime number between 1 to


30.
class Prime
{
public static void main (String [] args)
{
int n1, n2, x, i;
n1=Integer.parseInt(args [0]);
n2=Integer.parseInt(args[1]);
for(x=n1+1;x<=n2-1;x++)
{
for(i=2;i<x;i++)
if(x%i==0)
break;
if(i==x)
System.out.printf(" "+x);
}}}

Output:

Page 17
Rakesh
Mca-3rd year
RollNo-19001601039

Program 12: WAP to print all combination of 1,2,3.


class Combination
{
public static void main(String[] args)
{
int i,j,k;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
for(k=1;k<=3;k++)
System.out.printf(" "+("{ "+( i+" "+j+" "+k+" }")));
System.out.println();
}}
}}

Output:

Page 18
Rakesh
Mca-3rd year
RollNo-19001601039

Program 13: WAP to print star pattern in diamond shape.

class Diamond
{
public static void main(String[] args)
{
int n=Integer.parseInt(args[0]);
int space=n-1;
for(int j=1;j<=n;j++)
{
for(int i=1;i<=space;i++)
{
System.out.print(" ");
}
space--;
for(int i=1;i<=2*j-1;i++)
{
System.out.print("*");
}
System.out.println();
}
space=1;
for(int j=1;j<=n-1;j++)
{
for (int i=1;i<=space;i++)
{
System.out.print(" ");
}
space++;
for (int i=1; i<=2*(n-j)-1; i++)

Page 19
Rakesh
Mca-3rd year
RollNo-19001601039

{
System.out.print("*");
}
System.out.println(" ");
}
}
}

Output:

Page 20
Rakesh
Mca-3rd year
RollNo-19001601039

Program 14: WAP to find the real solutions of a quadratic equation.

import java.lang.Math;
class Quadratic
{
public static void main(String[] args)
{
double r1,r2;
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int c=Integer.parseInt(args[2]);
int d=(b*b-4*a*c);

if(d>0)
{
System.out.println("roots are real");
r1=(-b+Math.sqrt(d))/(2*a);
r2=(-b-Math.sqrt(d))/(2*a);
System.out.println(r1);
System.out.println(r2);
}
else if(d==0)
{
System.out.println("roots are equal");
r1=-b/(2*a);
r2=-b/(2*a);
}
else
{
System.out.println("ROOTS are imaginary");

Page 21
Rakesh
Mca-3rd year
RollNo-19001601039

r1=(-b+Math.sqrt(d))/(2*a);
r2=(-b-Math.sqrt(d))/(2*a);
}
}
}

Output:

Page 22
Rakesh
Mca-3rd year
RollNo-19001601039

Program 15: WAP to calculate are of a room. Make a class room in


two data members length and breath and one member function
cal_area and create object of room class in another class.
class Room
{
int length,breath;
int cal_Area(int length,int breath)
{
this.length=length;
this.breath=breath;
int area=length*breath;
return area;
}}
class Room_Area
{
public static void main(String[] args)
{
Room obj=new Room();
System.out.println("the Area of room is "+obj.cal_Area(6,7));
}}
Output:

Page 23
Rakesh
Mca-3rd year
RollNo-19001601039

Program 16: WAP to find factorial of a number.

class Factorial
{
public int fact(int n)
{
if(n<=0)
{
return 1;
}
else
{
return (n*fact(n-1));
}}}
class FindFactorial
{
public static void main(String[] args)
{
Factorial obj=new Factorial();
int i=Integer.parseInt(args[0]);
System.out.println("factorial is "+obj.fact(i));
}}
Output:

Page 24
Rakesh
Mca-3rd year
RollNo-19001601039

Program 17: WAP to demonstrate method overloading. Create a


method larger which compares two integers as well as float values.

public class Dems


{
public void f1(int a , int b )
{
if(a>b)
System.out.println(""+a+" is greater");
else
System.out.println(""+b+" is greater");
}
public void f1(float a , float b)
{
if(a>b)
System.out.println(""+a+" is greater");
else
System.out.println(""+b+" is greater");
}
}

class MethodOverloading
{
Page 25
Rakesh
Mca-3rd year
RollNo-19001601039

public static void main(String[] args)


{
Dems dems=new Dems();
dems.f1(22,23);
dems.f1(12.03f,12.003f);
}
}

Output:

Page 26
Rakesh
Mca-3rd year
RollNo-19001601039

Program 18: Create a class Mathoperation and make two methods


add and mul to perform addition and multiplication of two float
numbers.
class MathOperation
{
public static void mul(float a,float b)
{
float c=a*b;
System.out.println(" "+""+a+" * "+b+"="+c);
}
public static void add(float a,float b)
{
float c=a+b;
System.out.println(" "+""+a+" + "+b+"="+c);
}}
class Math
{
public static void main(String[] args)
{
MathOperation.mul(2f,4f);

Page 27
Rakesh
Mca-3rd year
RollNo-19001601039

MathOperation.add(2f,4f);
}}
Output:

Program 19: Design a class to implement bank account mechanism.

class BankAccount
{
String name;
int accountNo;
String accType;
int balance;
BankAccount(String n,int a,String acc,int bal)
{
name=n;
accountNo=a;
accType=acc;
balance=bal;
}
public void deposit(int r)
{
int ruppe=r;
balance=balance+ruppe;
}
Page 28
Rakesh
Mca-3rd year
RollNo-19001601039

public void withdrawal(int p)


{
int atm=p;
if(balance>=atm)
{
balance=balance-atm;
System.out.println("Remaining Balance="+balance);
}
else
{
System.out.println("Insufficient Balance");
}}

public void display()


{
System.out.println("the name of the account holder= "+name);
System.out.println("Total balance in account= "+balance);
}}

class TestBank
{
public static void main(String[] args)
{
BankAccount obj=new
BankAccount("Rakesh",1400160,"Savings",20000);

obj.deposit(19000);
obj.withdrawal(38000);
obj.display();

}}

Page 29
Rakesh
Mca-3rd year
RollNo-19001601039

Output:

Program 20: Make a class student and define data members as


name, roll no and static venue and make a static method change to
change the value of venue.

class Student
{
int rollNo;
String name;
static String college;

Student(int r,String n)
{
rollNo=r;
name=n;
}

public static void change()


{
college="JC-BOSE";
}

Page 30
Rakesh
Mca-3rd year
RollNo-19001601039

public void display()


{
System.out.println("rollNo= "+rollNo);
System.out.println("Name= "+name);
System.out.println("College= "+college);
}}

class TestStudent
{
public static void main(String[] args)
{
Student s1=new Student(111,"Raju");
Student.change();
s1.display();
Student s2=new Student(111,"Y-only-Y");
s2.display();
}
}

Output:

Page 31
Rakesh
Mca-3rd year
RollNo-19001601039

Program 21: Write a program to demonstrate use of this keyword.

class Box
{
int length,breath;
Box(int length,int breath)
{
this.length=length;
this.breath=breath;
}
public void area()
{

Page 32
Rakesh
Mca-3rd year
RollNo-19001601039

System.out.println("the area of box is "+length*breath);


}}
class MainBox
{
public static void main(String[] args)
{
Box box=new Box(10,20);
box.area();
}}
Output:

Program 22: Write a program using this to show constructor


chaining.

class Child
{
Child()
{
this(10);
System.out.println("Child class default constructor");
}

Page 33
Rakesh
Mca-3rd year
RollNo-19001601039

Child(int y)
{
this(10,20);
System.out.println("Child class of single parametrized");
}

Child(int x,int y)
{
super();
System.out.println("Child class of double parametrized");
}
}

class ConstructorChaining
{
public static void main(String[] args)
{
Child obj=new Child();
}
}

Output:

Page 34
Rakesh
Mca-3rd year
RollNo-19001601039

Page 35
Rakesh
Mca-3rd year
RollNo-19001601039

Program 23: Write a program to merge to int arrays having number


in ascending order.

import java.util.Arrays;
class ArrayTest
{
public static void main(String[] args)
{
int a[]={1,2,5,6};
int b[]={7,9,10,12};
int c[]=new int[8];
System.arraycopy(a,0,c,0,4);
System.arraycopy(b,0,c,4,4);
System.out.print("the array after merging ={");
for(int i=0;i<c.length;i++)
{
System.out.print(" "+c[i]);
}
System.out.print("}");
}}
Output:

Page 36
Rakesh
Mca-3rd year
RollNo-19001601039

Program 24: Create a class Student with data_members name of


student, rollNo of a student and a static data_member venue. Make
a static method change to change the value of venue. Also use
constructor for initializing the values.

class Student
{
int rollNo;
String name;
static String college;
Student(int r,String n)
{
rollNo=r;
name=n;
}
public static void change()
{
college="JC-BOSE";
}

public void display()


{
System.out.println("rollNo= "+rollNo);
System.out.println("Name= "+name);
System.out.println("College= "+college);
}}

class TestStudent
{

Page 37
Rakesh
Mca-3rd year
RollNo-19001601039

public static void main(String[] args)


{
Student s1=new Student(111,"Raju");
Student.change();
s1.display();

Student s2=new Student(111,"Y-only-Y");


s2.display();

}}

Output:

Page 38
Rakesh
Mca-3rd year
RollNo-19001601039

Program 26: create a class to represent bank Details.

package Bank.Details;
import java.util.*;

abstract class Account


{
long account_no;
static double balance;
String accountHolderName;
String address;
Account(long account_no,double balance, String accountHolderName,
String address)
{
this.account_no=account_no;
this.balance=balance;
this.accountHolderName=accountHolderName;
this.address=address;
}
abstract void deposit();
abstract void withdrawn();

class Saving extends Account


{
static double amount;
final static double Roi=9.0;

public Saving(long account_no, double balance, String

Page 39
Rakesh
Mca-3rd year
RollNo-19001601039

accountHolderName, String address)


{
super(account_no, balance, accountHolderName, address);
}
void deposit()
{
Scanner in = new Scanner(System.in);
System.out.println("How many amount you want to deposit?");
amount = in.nextDouble();
if(balance==0)
{
balance+=amount;
}
System.out.println("your " +balance+ "is deposited");
}

void withdrawn()
{
int pincode=3133;
Scanner pc = new Scanner(System.in);
System.out.println("How many amount you want to withdrawn");
amount = pc.nextDouble();
Scanner in = new Scanner(System.in);
System.out.println("Enter the Pincode");
pincode = in.nextInt();
if(pincode==3133 && (balance!=0))
{
balance=balance-amount;
System.out.println("Succesfully Withdrawn");
System.out.println("Yout total amount is: "+balance);
}
Page 40
Rakesh
Mca-3rd year
RollNo-19001601039

else
{
System.out.println("Pincode is incorrect");
}
}

void calAmount()
{
double n;
if(balance!=0)
{
n=balance*Roi*0.01;
balance=balance+n;
System.out.println("The amount is after add interest: "+balance);
}
else
System.out.println("You have no balance in your account");
}

void display()
{
System.out.println("The account no: "+account_no+"\nThe

account balance: "+balance+"\nAccount holder name:

"+accountHolderName+"\nAccount holder Address: "+address);


}
}

class CurrentAccount extends Saving


{

Page 41
Rakesh
Mca-3rd year
RollNo-19001601039

CurrentAccount(double balance, long account_no, String

accountHolderName, String address)


{
super(account_no, balance, accountHolderName, address);
}

static void show()


{
double limit=125000.64;
if(limit!=amount)
{
Scanner am = new Scanner(System.in);
System.out.println("Enter the amount");
amount=am.nextDouble();
balance=balance+Roi;
System.out.println("Your account is overdraft and balance is:

"+balance);
}
else
System.out.println("Yout total amount is: "+balance);
}
}

public class BankOp


{
public static void main(String srgs[])
{
Saving s = new Saving(1500510,0,"RAKESH","FARIDABAD");
s.deposit();
s.display();
s.withdrawn();
Page 42
Rakesh
Mca-3rd year
RollNo-19001601039

s.calAmount();
CurrentAccount obj = new
CurrentAccount(1000051360125369L,0,"Stephen","Sandiago");
obj.show();
}
}

Output:

Program 28: Write a program in java to implement the concept of


package.

package com.rp.java.sum;
class Sum
Page 43
Rakesh
Mca-3rd year
RollNo-19001601039

{
public static void main(String[] args)
{
int a=10,b=20;
int c=a+b;
System.out.println("the sum of a and b is "+c);
}
}

Output:

Page 44
Rakesh
Mca-3rd year
RollNo-19001601039

Page 45
Rakesh
Mca-3rd year
RollNo-19001601039

Program 29: Write a program in which we create a package


packageExample in which we make a class importPackage. Create
another class usePackage and use importPackage class in the
usePackage class to find the factorial of a given number.

package packageExample;
class ImportPackage
{
public int fact(int n)
{
if(n<=0)
{
return 1;
}
else
{
return (n*fact(n-1));
}
}
}

import packageExample.ImportPackage;
class UsePackage
{
public static void main(String[] args)
{
ImportPackage obj=new ImportPackage();
int i=Integer.parseInt(args[0]);
System.out.println("factorial is "+obj.fact(i));
}

Page 46
Rakesh
Mca-3rd year
RollNo-19001601039

}
Output:

Page 47
Rakesh
Mca-3rd year
RollNo-19001601039

Program 30: Write a program to implement simple example of java


multi-catch block.

import java.util.Scanner;
public class TestEx
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
try
{
int n = Integer.parseInt(sc.nextLine());
if (99%n == 0)
System.out.println(n + " is a factor of 99");
}
catch (ArithmeticException ex)
{
System.out.println("Arithmetic " + ex);
}
catch (NumberFormatException ex)
{
System.out.println("Number Format Exception \n" + ex);
}
}
}

Page 48
Rakesh
Mca-3rd year
RollNo-19001601039

Output:

Page 49
Rakesh
Mca-3rd year
RollNo-19001601039

Program 31: Write a program to create a class myException in


which two int data members x=5 , y=1000 and a float z=x/y. If the
value of z is less than .01 then throw new myException as number is
too small.

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

class TestException
{
public static void main(String[] args)
{
int x=5, y=1000;
try
{
float z=x/y;
if(z<0.01)
throw new MyException("number is two small");
}
catch(MyException e)
{
System.out.println(" "+e.getMessage());
}
}

Page 50
Rakesh
Mca-3rd year
RollNo-19001601039

Output:

Page 51
Rakesh
Mca-3rd year
RollNo-19001601039

Program 32: Write a program to create multiple Threads in java.

class P extends Thread


{
public void run()
{
int i;
for(i=0;i<5;i++)
{
System.out.println("THREAD P = "+i);
}}}

class Q extends Thread


{
public void run()
{
int i;
for(i=0;i<5;i++)
{
System.out.println("THREAD Q = "+i);
}}}

class R extends Thread


{
public void run()
{
int i;
for(i=0;i<5;i++)
{

Page 52
Rakesh
Mca-3rd year
RollNo-19001601039

System.out.println("THREAD R = "+i);
}}}

class TestThread
{
public static void main(String[] args)
{
P t1=new P();
Q t2=new Q();
R t3=new R();
t1.start();
t2.start();
t3.start();
}}

Output:

Page 53
Rakesh
Mca-3rd year
RollNo-19001601039

Program 33: Write a program to print helloworld in applet.

import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorld extends Applet


{
public void paint(Graphics g)
{
g.drawString("Hello World", 20, 20);
}
}

HTML code:
<html>
<head>
<title>applet</title>
</head>
<body>
<h1>hello</h1>
<applet code="HelloWorld" width=200 height=60>
</applet>
</body>
</html>

Page 54
Rakesh
Mca-3rd year
RollNo-19001601039

Output:

Page 55
Rakesh
Mca-3rd year
RollNo-19001601039

Program 34: Write a program to create a smiling face in Applet.

import java.applet.*;
import java.awt.*;
public class Smiley extends Applet
{
public void paint(Graphics g)
{
g.drawOval(80, 70, 150, 150);
g.setColor(Color.BLACK);
g.fillOval(120, 120, 15, 15);
g.fillOval(170, 120, 15, 15);
g.drawArc(130, 180, 50, 20, 180, 180);
}
}

HTML code:

<html>
<title>applet</title>
<body>
<applet code="Smiley.class" width=400 height=400>
</applet>
</body>
</html>

Page 56
Rakesh
Mca-3rd year
RollNo-19001601039

Output:

Page 57
Rakesh
Mca-3rd year
RollNo-19001601039

Program 35 : Write a program to display the URL of the directory


containing the .class file and .html file. Use getCodeBase () and
getDocumentBase () method.

import java.applet.*;
import java.awt.*;
public class Link extends Applet
{
public void paint(Graphics g)
{
g.drawString("a= "+getCodeBase(),20,20);
g.drawString("a= "+getDocumentBase(),20,40);
}
}

HTML code:

<html>
<title>applet</title>
<body>
<applet code="Link.class" width=400 height=400>
</applet>
</body>
</html>

Page 58
Rakesh
Mca-3rd year
RollNo-19001601039

Output:

Page 59

You might also like