Vous êtes sur la page 1sur 23

PRACTICAL-1

AIM: Introduction to Java.

History:
Java was started as a project called "Oak" by James Gosling in June 1991. Gosling's goals were
to implement a virtual machine and a language that had a familiar C-like notation but with
greater uniformity and simplicity than C/C++. The first public implementation was Java 1.0 in
1995. It made the promise of "Write Once, Run Anywhere", with free runtimes on popular
platforms. It was fairly secure and its security was configurable, allowing for network and file
access to be limited. The major web browsers soon incorporated it into their standard
configurations in a secure "applet" configuration popular quickly. New versions for large and
small platforms (J2EE and J2ME) soon were designed with the advent of "Java 2". Sun has not
announced any plans for a "Java 3".

Features:
Following are the notable features of Java:

Object Oriented: In Java, everything is an Object. Java can be easily extended since it is based
on the Object model.

Platform Independent: Unlike many other programming languages including C and C++, when
Java is compiled, it is not compiled into platform specific machine, rather into platform-
independent byte code. This byte code is distributed over the web and interpreted by the
Virtual Machine (JVM) on whichever platform it is being run on.

Simple: Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it
would be easy to master.

Secure: With Java's secure feature it enables to develop virus-free, tamper-free systems.
Authentication techniques are based on public-key encryption.

JVM:
A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs
as well as programs written in other languages that are also compiled to Java bytecode. The
JVM is detailed by a specification that formally describes what is required in a JVM

GUNEET KAUR
implementation. Having a specification ensures interoperability of Java programs across
different implementations so that program authors using the Java Development Kit (JDK) need
not worry about idiosyncrasies of the underlying hardware platform.

The JVM reference implementation is developed by the OpenJDK project as open source code
and includes a JIT compiler called HotSpot. The commercially supported Java releases available
from Oracle Corporation are based on the OpenJDK runtime. Eclipse OpenJ9 is another open
source JVM for Open JDK.

Difference between java and C++:

NetBeans:
NetBeans is an integrated development environment (IDE) for Java. NetBeans allows
applications to be developed from a set of modular software components called modules.
NetBeans runs on Windows, macOS, Linux and Solaris. In addition to Java development, it has
extensions for other languages like PHP, C, C++, HTML5,and JavaScript. Applications based on
NetBeans, including the NetBeans IDE, can be extended by third party developers.

GUNEET KAUR
Eclipse:
Eclipse is an integrated development environment (IDE) used in computer programming, and in
2014 was the most widely used Java IDE in one website's poll. It contains a base workspace and
an extensible plug-in system for customizing the environment. Eclipse is written mostly in Java
and its primary use is for developing Java applications, but it may also be used to develop
applications in other programming languages via plug-ins, including Ada, ABAP, C, C++, C#,
Clojure, COBOL, D, Erlang, Fortran, Groovy, Haskell, JavaScript, Perl, PHP, Python, Ruby etc.

JDK:
The Java Development Kit (JDK) is an implementation of either one of the Java Platform,
Standard Edition, Java Platform, Enterprise Edition, or Java Platform, Micro Edition platforms
released by Oracle Corporation in the form of a binary product aimed at Java developers on
Solaris, Linux, macOS or Windows. The JDK includes a private JVM and a few other resources to
finish the development of a Java Application. Since the introduction of the Java platform, it has
been by far the most widely used Software Development Kit (SDK).[citation needed] On 17
November 2006, Sun announced that they would release it under the GNU General Public
License (GPL), thus making it free software. This happened in large part on 8 May 2007, when
Sun contributed the source code to the OpenJDK.

GUNEET KAUR
GUNEET KAUR
……………….
PRACTICAL-2
AIM: To find factorial of a given number.

PROGRAM:
import java.util.Scanner;
public class Factorial {
public static void main(String... arg){
System.out.print("Enter n: ");
int n = new Scanner(System.in).nextInt(), fact=1;
for(int i=n; i>0; i--){
fact *=i;
}
System.out.println("Factorial of "+n+" is "+fact);
}
}

GUNEET KAUR
……………….
PRACTICAL-3
AIM: To print Prime numbers till nth term.

PROGRAM:
import java.io.*;
import java.util.*;
import java.util.Scanner;
class prime
{
static boolean isPrime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 ||
n % 3 == 0)
return false;
for (int i = 5;
i * i <= n; i = i + 6)
if (n % i == 0 ||
n % (i + 2) == 0)
return false;
return true;
}
static void printPrime(int n)
{
for (int i = 2; i <= n; i++)
{
if (isPrime(i))
System.out.print(i + " ");
}
}
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter the number");
n=sc.nextInt();
printPrime(n);
}
}

GUNEET KAUR
……………….
PRACTICAL-4
AIM: To find average marks of n students and calculate their percentages and assign them
grades.

PROGRAM:
import java.util.Scanner;
import java.io.*;
import java.util.*;
public class Grade {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n,m1,m2,m3,m4,m5;
int[] a=new int[100];
System.out.print("Enter the number of students\n");
n=sc.nextInt();
System.out.print("Student no\n");
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
System.out.print("\nEnter the marks of student");
System.out.print("\nEnter the marks of 1st subject");
m1=sc.nextInt();
System.out.print("\nEnter the marks of 2nd subject");
m2=sc.nextInt();
System.out.print("\nEnter the marks of 3rd subject");
m3=sc.nextInt();
System.out.print("\nEnter the marks of 4th subject");
m4=sc.nextInt();
System.out.print("\nEnter the marks of 5th subject");
m5=sc.nextInt();
float avg;
avg=(m1+m2+m3+m4+m5)/5;
System.out.format("\nAverage of first 5 marks %.2f\n",avg);
if(avg >= 95) {
System.out.format("You got A+ grade\n");
}
else if(avg>=90 && avg<95) {
System.out.format("You got A grade\n");
}

GUNEET KAUR
else if(avg>=85 && avg<90){

System.out.format("You got B+ grade\n");


}
else if(avg>=80 && avg<85){
System.out.format("You got B grade\n");
}
else if(avg>=75 && avg<80){
System.out.format("You got C+ grade\n");
}
else if(avg<75){
System.out.format("You got C grade\n");
}
}
}
}

GUNEET KAUR
……………….
PRACTICAL-5
AIM: To find biggest of three given numbers.

PROGRAM:
import java.util.Scanner;
class Largest
{
public static void main(String args[])
{
int x,y,z;
System.out.println("enter three integers");
Scanner in =new Scanner(System.in);
x=in.nextInt();
y=in.nextInt();
z=in.nextInt();
if(x>y && x>z)
System.out.println("first number is largest");
else if(y>x && y>z)
System.out.println("second number is largest");
else if(z>x && z>y)
System.out.println("third number is largest");
else
System.out.println("the numbers are not distinct");
}
}

GUNEET KAUR
……………….
PRACTICAL-6
AIM: To define a class, describe its constructor, overload the constructor and instantiate its
object.

PROGRAM:
public class A
{
int id, age; String name;
A()
{
id =0;
name = "NULL";
age=0;
}
A(int i,String n,int a)
{
id = i;
name = n;
age=a;
}
A(A a)
{
id=a.id;
name=a.name;
age=a.age;
}
void display(){
System.out.println(id+" "+name+" "+age);
}
public static void main(String args[]){
A s1 = new A();
A s2 = new A(222,"Aryan",25);
System.out.println("Default constructor:"); s1.display();
System.out.println(" Parameterised constructor:"); s2.display();
A s3=s2;
System.out.println(" Copy constructor:"); s3.display();
}
}

GUNEET KAUR
……………….
PRACTICAL-7
AIM: To create a Java program to implement stack and queue concept.

PROGRAM:
import java.util.Scanner;
import java.io.*;
import java.util.*;
class Stack {
static final int MAX = 100;
int top;
int a[] = new int[MAX]; // Maximum size of Stack
boolean isEmpty() {
return (top < 0);
}
Stack() {
top = -1;
}
boolean push(int x){
if (top >= (MAX - 1)) {
System.out.println("Stack Overflow");
return false;
}
else {
a[++top] = x;
System.out.println(x + " pushed into stack");
return true;
}
}
int pop() {
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top--];
return x;
}
}

GUNEET KAUR
int peek() {
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top];
return x;
}
}
}

class Queue {
int front, rear, size;
int capacity;
int array[];
public Queue(int capacity) {
this.capacity = capacity;
front = this.size = 0;
rear = capacity - 1;
array = new int[this.capacity];
}
boolean isFull(Queue queue) {
return (queue.size == queue.capacity);
}
boolean isEmpty(Queue queue) {
return (queue.size == 0);
}
void enqueue( int item) {
if (isFull(this))
return;
this.rear = (this.rear + 1)%this.capacity;
this.array[this.rear] = item;
this.size = this.size + 1;
System.out.println(item+ " enqueued to queue");
}
int dequeue() {
if (isEmpty(this)){
System.out.println("Queue is Empty");
return 0;
}
int item = this.array[this.front];

GUNEET KAUR
this.front = (this.front + 1)%this.capacity;
this.size = this.size - 1;

return item;
}
int front() {
if (isEmpty(this))
return Integer.MIN_VALUE;
return this.array[this.front];
}
int rear() {
if (isEmpty(this))
return Integer.MIN_VALUE;
return this.array[this.rear];
}
}
public class StackQueue {
public static void main(String[] args) {
int a,x,y,b,n,d=0;
Scanner sc = new Scanner(System.in);
System.out.println("Options:\n1.Stack\n2.Queue\n3.Exit");
x=sc.nextInt();
switch(x){
case 1:
Stack s = new Stack();
do{
System.out.println("Which operation you want to
perform\n1.Push\n2.Pop\n3.Exit");
a=sc.nextInt();
switch(a){
case 1:
System.out.println("Enter the integer you want to push into stack");
b=sc.nextInt();
s.push(b);
break;
case 2:
System.out.println(s.pop() + " Popped from stack");
break;
case 3:
break;
default:
break;
}
System.out.println("Do you do more operations(0/1)");

GUNEET KAUR
d=sc.nextInt();
}while(d==1);
break;

case 2:
Queue queue = new Queue(1000);
do{
System.out.println("Which operation you want to
perform\n1.Enqueue\n2.Dequeue\n3.Front item in queue\n4.Rear item in
queue\n5.Exit");
a=sc.nextInt();
switch(a){
case 1:
System.out.println("Enter the integer you want to add in queue");
b=sc.nextInt();
queue.enqueue(b);
break;
case 2:
System.out.println(queue.dequeue() + " dequeued from queue\n");
break;
case 3:
System.out.println("Front item is " + queue.front());
break;
case 4:
System.out.println("Rear item is " + queue.rear());
break;
case 5:
break;
default:
break;
}
System.out.println("Do you do more operations(0/1)");
d=sc.nextInt();
}while(d==1);
break;
case 3:
break;
default:
break;
}
}
}

GUNEET KAUR
……………….
PRACTICAL-8
AIM: To demonstrate use of a nested class.

PROGRAM:
class Outerclass
{
static int outer_x = 10;
int outer_y = 20;
private int outer_private = 30;
class InnerClass
{
void display()
{

System.out.println("outer_x = " + outer_x);


System.out.println("outer_y = " + outer_y);
System.out.println("outer_private = " + outer_private);
}
}
}

// Driver class
public class Innerclass
{
public static void main(String[] args)
{
Outerclass outerObject = new Outerclass();
Outerclass.InnerClass innerObject = outerObject.new InnerClass();
innerObject.display();

}
}

GUNEET KAUR
……………….
PRACTICAL-9
AIM: To print all real solutions to the quadratic equation ax2+bx2+c=0. Read in a, b, c and use
quadratic formula and display a message stating there are no real roots if discriminant is
negative.

PROGRAM:
import java.util.Scanner;
class Quadratic
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("Input a : ");
double a=input.nextDouble();
System.out.println("Input b : ");
double b=input.nextDouble();
System.out.println("Input c : ");
double c=input.nextDouble();
double result=b*b-4.0*a*c;
if(result>0.0)
{
double r1=(-b+Math.pow(result,0.5))/(2.0*a);
double r2=(-b+Math.pow(result,0.5))/(2.0*a);
System.out.println("The roots are "+r1 +"and"+ r2);
}
else if(result==0.0)
{
double r1=-b/(2.0*a);
System.out.println("The root is"+r1);
}
else
{
System.out.println("The equation has no real roots. ");
}
}
}

GUNEET KAUR
……………….
PRACTICAL-10
AIM: To write a Java program that uses both recursive and non-recursive functions to print the
nth value of Fibonacci series.

PROGRAM:
import java.util.*;
class fibo
{
static int n1=0,n2=1,n3=0;
static void fib(int n)
{
if (n > 0)
{
n3=n1+n2;
n1=n2;
n2=n3;
System.out.print(" " + n3);
fib(n-1);
}
}
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int x,y,n;
int[] a=new int[100];
System.out.println("Which method you want to use\n1.Non-Recursive\n2.Recursive\n3.Exit");
x=s.nextInt();
switch(x){
case 1:System.out.println("Enter the value of n:");
n=s.nextInt();
int t1=0,t2=1;
System.out.print("Upto " + n + ": ");
while (t1 < n)
{
System.out.print(t1 + " ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}
break;
case 2:System.out.println("Enter the value of n:");

GUNEET KAUR
n=s.nextInt();
System.out.print(n1+" "+n2);
fib(n-2);
break;
default:break;
}
}
}

GUNEET KAUR
GUNEET KAUR
GUNEET KAUR
GUNEET KAUR
GUNEET KAUR
GUNEET KAUR

Vous aimerez peut-être aussi