Java Practical File
Java Practical File
1
INDEX
Sr.No Practical Name Page No. Date Remarks
1 Installation of JDK.
3-4 18-02-2019
2
PRACTICAL 1
STEP 1
1st go to oracle java official site.
https://www.oracle.com/technetwork/java/javase/downloads/index.html
STEP 2
Click on Download JDK, accept license & select the version for your operating system. Then
hit install and continue.
3
STEP 3
Select This PC or Go to File manager & hit right click for This PC or Select This PC and
right click for find the properties. Then select Advanced System Settings.
STEP 4
Select Environment Variable and add New Variable as follows, find the jdk installed folder
url, bin folder and lib folder, copy that and paste it on as Variable Value as follows with
Now We successfully installed JDK on Our system, for check it by CMD, just type javac and hit
enter, It will shows the java details, for check version type java –version.
4
Practical-02
System.out.println("HELLO WORLD");
Output:
Practical-03
Aim: Write a Program to generate random numbers.
Program:
5
import java.util.Random;
public class RandomNumbers
{
public static void main (String args[])
{
Random obj=new Random();
for(int i=0;i<10;i++)
{
int randomNumber=obj.nextInt(100);
System.out.println("randomNumber:"+randomNumber);
}}}
Output:
Practical-04
Aim: Write a program to generate the factorial of a number.
Program:
class Factorial{
int i,fact=1;
6
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i; }
Output:
Practical-05
Program:
import java.util.Scanner;
public class Fibonacci
{
public static void main(String[] args) {
int n, a = 0, b = 0, c = 1;
Scanner s = new Scanner(System.in);
7
System.out.print("Enter value of n:");
n = s.nextInt();
System.out.print("Fibonacci Series:");
for(int i = 1; i <= n; i++) {
a = b;
b = c;
c = a + b;
System.out.print(a+" ");}
}}
Output:
Practical-06
Aim: Write a program that states the largest number among a given input
Program:
8
if( n1 >= n2 && n1 >= n3)
System.out.println(n1 + " is the largest number.");
else if (n2 >= n1 && n2 >= n3)
System.out.println(n2 + " is the largest number.");
else
System.out.println(n3 + " is the largest number.");
}}
Output:
Practical-07
Program:
class SingleInheritance{
9
}
int num3=2;
int result=num1+num2+num3;
}}
Output:
Practical-08
Aim: Write a java program which shows the priority in threads
Program:
public class ThreadPriority extends Thread
{
public void run()
{
System.out.println("run() method");
String threadName = Thread.currentThread().getName();
Integer threadPrio = Thread.currentThread().getPriority();
System.out.println(threadName + " has priority " + threadPrio);
10
}
public static void main(String[] args) throws InterruptedException
{
ThreadPriority t1 = new ThreadPriority();
ThreadPriority t2 = new ThreadPriority();
ThreadPriority t3 = new ThreadPriority();
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t3.setPriority(Thread.NORM_PRIORITY);
t1.start();
t2.start();
t3.start();
}}
OUTPUT:
Practical-09
Aim: Write a program to print a following pattern (using nested loop)
*****
****
***
11
**
*
Program:
public class Pattern {
int rows = 5;
System.out.print("* ");
System.out.println();
}}}
Outout:
Practical-10
Aim: Write a program to find out whether the given year is leap
year or not.
Program:
12
public class LeapYear {
public static void main(String[] args) {
int year = 1900;
boolean leap = false;
if(year % 4 == 0) {
if( year % 100 == 0) {
if ( year % 400 == 0) // year is divisible by 400, hence the year is a leap year
leap = true; else
leap = false; else
leap = true;} else
leap = false; if(leap)
System.out.println(year + " is a leap year."); else
System.out.println(year + " is not a leap year."); } }
Output:
13