Java File Lab
Java File Lab
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.
Page 1
Rakesh
Mca-3rd year
RollNo-19001601039
VALUES.
FLOAT NUMBERS.
Page 2
Rakesh
Mca-3rd year
RollNo-19001601039
IN YOUR PROGRAM.
Page 3
Rakesh
Mca-3rd year
RollNo-19001601039
ANOTHER CLASS
Page 4
Rakesh
Mca-3rd year
RollNo-19001601039
PACKAGE.
GIVEN NUMBER.
Page 5
Rakesh
Mca-3rd year
RollNo-19001601039
Page 6
Rakesh
Mca-3rd year
RollNo-19001601039
Output :
Page 7
Rakesh
Mca-3rd year
RollNo-19001601039
Output:
Page 8
Rakesh
Mca-3rd year
RollNo-19001601039
Output:
Page 9
Rakesh
Mca-3rd year
RollNo-19001601039
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
Page 11
Rakesh
Mca-3rd year
RollNo-19001601039
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
Output:
Page 14
Rakesh
Mca-3rd year
RollNo-19001601039
Output:
Page 15
Rakesh
Mca-3rd year
RollNo-19001601039
Output:
Page 16
Rakesh
Mca-3rd year
RollNo-19001601039
Output:
Page 17
Rakesh
Mca-3rd year
RollNo-19001601039
Output:
Page 18
Rakesh
Mca-3rd year
RollNo-19001601039
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
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
Page 23
Rakesh
Mca-3rd year
RollNo-19001601039
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
class MethodOverloading
{
Page 25
Rakesh
Mca-3rd year
RollNo-19001601039
Output:
Page 26
Rakesh
Mca-3rd year
RollNo-19001601039
Page 27
Rakesh
Mca-3rd year
RollNo-19001601039
MathOperation.add(2f,4f);
}}
Output:
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
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:
class Student
{
int rollNo;
String name;
static String college;
Student(int r,String n)
{
rollNo=r;
name=n;
}
Page 30
Rakesh
Mca-3rd year
RollNo-19001601039
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
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
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
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
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";
}
class TestStudent
{
Page 37
Rakesh
Mca-3rd year
RollNo-19001601039
}}
Output:
Page 38
Rakesh
Mca-3rd year
RollNo-19001601039
package Bank.Details;
import java.util.*;
Page 39
Rakesh
Mca-3rd year
RollNo-19001601039
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
Page 41
Rakesh
Mca-3rd year
RollNo-19001601039
"+balance);
}
else
System.out.println("Yout total amount is: "+balance);
}
}
s.calAmount();
CurrentAccount obj = new
CurrentAccount(1000051360125369L,0,"Stephen","Sandiago");
obj.show();
}
}
Output:
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
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
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
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
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
import java.applet.Applet;
import java.awt.Graphics;
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
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
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