Sei sulla pagina 1di 13

____________________________

Programming with Core Java


PRACTICAL FILE
____________________________

SUBMITTED BY: SUBMITTED TO:


Lalit Kumar Sachin Bhardwaj
Assistant Professor
16001406
Department of CSE
B.Tech, CSE
AP Goyal Shimla University
6th Sem

1
INDEX
Sr.No Practical Name Page No. Date Remarks
1 Installation of JDK.
3-4 18-02-2019

2 Write a Simple program to print Hello


world. 5 25-02-2019
3 Write a Program to generate random
numbers. 6 11-03-2019

4 Write a program to generate the factorial


of a number. 7 18-03-2019

5 Write a program for the Fibonacci series. 08-04-2019


8

6 Write a program that states the largest


number among a given input of user 9 15-04-2019
defined numbers.
7 Write a java program which explains the
concept of single inheritance. 10 22-04-2019

8 Write a java program which shows the


priority in threads. 11 22-04-2019

9 Write a program to print a pattern (LOOP) 06-05-2019


***** 12
****
***
**
*
10 Write a program to find out whether the 06-05-2019
given year is leap year or not. 13

2
PRACTICAL 1

AIM: How to Install JDK

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

PATH = <JDK installation directory>\


PATH = <JDK installation directory>\bin
PATH = <JDK installation directory>\lib

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

Aim: Write a simple program to print hello world.


Program:
class hello

public static void main(String args[])

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{

public static void main(String args[]){

int i,fact=1;

6
int number=5;//It is the number to calculate factorial

for(i=1;i<=number;i++){

fact=fact*i; }

System.out.println("Factorial of "+number+" is: "+fact); } }

Output:

Practical-05

Aim: Write a program for the Fibonacci series.

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

of user defined numbers.

Program:

public class Greatest {


public static void main(String[] args) {
double n1 = -4.5, n2 = 3.9, n3 = 2.5;

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

Aim: Write a java program which explains the concept of single


inheritance.

Program:
class SingleInheritance{

static int num1=10;

static int num2=5;

9
}

class MainInheritance extends SingleInheritance{

public static void main(String[] args){

int num3=2;

int result=num1+num2+num3;

System.out.println("Result of child class is "+result);

}}

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 {

public static void main(String[] args) {

int rows = 5;

for(int i = rows; i >= 1; --i) {

for(int j = 1; j <= i; ++j) {

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

Potrebbero piacerti anche