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

Himanshu JAVA FILE'

The document outlines a Java Programming Lab course at Maharaja Agrasen Institute of Technology, detailing various experiments and coding assignments. It includes an introduction to Java, its history, principles, features, and types of applications, along with specific coding exercises for students. Each experiment aims to teach fundamental programming concepts through practical coding tasks.

Uploaded by

Hardik Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Himanshu JAVA FILE'

The document outlines a Java Programming Lab course at Maharaja Agrasen Institute of Technology, detailing various experiments and coding assignments. It includes an introduction to Java, its history, principles, features, and types of applications, along with specific coding exercises for students. Each experiment aims to teach fundamental programming concepts through practical coding tasks.

Uploaded by

Hardik Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 38

JAVA PROGRAMMING LAB

(ETCS-357)

MAHARAJA AGRASEN INSTITUTE OF TECHNOLOGY


GGSIPU, DELHI: PSP Area, Plot No. 1, Sector-22, Rohini, New
Delhi, Delhi 110086

STUDENT NAME: - FACULTY NAME: -


HIMANSHU GOYAL Dr SHIKHA GUPTA
35414803120
INDEX
S.no Experiment Name Date of Date of Remarks Mark
performance checking (10)
1. Introduction to Java
2. WAP to find factorial of
a number in java.
3. WAP to print Fibonacci
series till nth term.
4. WAP to calculate sum of
number until negative
number is enter.
5. WAP to find the average
marks of n students,
calculate their percentage
and assign them grades.
6. WAP that takes three
inputs from the user: one
operator and 2 numbers.
Based on the operator
provided by the user, it
performs the calculation
on the numbers. Then the
result is displayed on the
screen.
7. WAP to calculate area of
circle using the radius.
8. WAP to display personal
data of user using
scanner class.
9. WAP to display odd
number up to 20.
10. WAP to calculate sum,
difference, product,
quotient of two number
enter by the user.
11. WAP to implement stack
and queue concept in
java.
12. WAP to implement
single inheritance
concept in java.
13. WAP to implement
multiple inheritance
concept in java.
14. WAP to implement
multilevel hierarchical
inheritance concept in
java.
15. WAP to show
multithreaded producer
and consumer
application.
16. Create a customized
exception and make use
of all the 5 exception
keywords.
Experiment -1
AIM: - Introduction to Java.
THEORY: -
 Java - The new programming language developed by Sun Microsystems
in 1991.
 Originally called Oak by James Gosling, one of the inventors of the Java
Language.
 Java Authors: James, Arthur Van, and others
 Java is really “C++ -- ++ “
 Originally created for consumer electronics (TV, VCR, Washing
Machine, Mobile Phone).
 Java - CPU Independent language
 Internet and Web were just emerging, so Sun turned it into a language of
Internet Programming.
 It allows you to publish a webpage with Java code in it.
History of Java
The history of Java is very interesting. Java was originally designed for
interactive television, but it was too advanced technology for the digital cable
television industry at the time. The history of Java starts with the Green Team.
Java team members (also known as Green Team), initiated this project to
develop a language for digital devices such as set-top boxes, televisions, etc.
However, it was best suited for internet programming. Later, Java technology
was incorporated by Netscape.

The principles for creating Java programming were "Simple, Robust, Portable,
Platform-independent, Secured, High Performance, Multithreaded, Architecture
Neutral, Object-Oriented, Interpreted, and Dynamic". Java
was developed by James Gosling, who is known as the father of Java, in 1995.
James Gosling and his team members started the project in the early '90s.

James Gosling - founder of java


Currently, Java is used in internet programming, mobile devices, games, e-
business solutions, etc. Following are given significant points that describe the
history of Java.

1) James Gosling
, Mike Sheridan, and Patrick Naughton initiated the Java language project in
June 1991. The small team of sun engineers called Green Team. Play Vide ox.
2) Initially it was designed for small, embedded systems in electronic appliances
like set-top boxes.
3) Firstly, it was called "Green talk" by James Gosling, and the file extension
was.gt.
4) After that, it was called Oak and was developed as a part of the Green
project.
Why Java was named as "Oak"?
Java History from Oak to Java
5) Why Oak? Oak is a symbol of strength and chosen as a national tree of many
countries like the U.S.A., France, Germany, Romania, etc.
6) In 1995, Oak was renamed as "Java" because it was already a trademark by
Oak Technologies.
Why Java Programming named "Java"?
7) Why had they chosen the name Java for Java language? The team gathered to
choose a new name. The suggested words were "dynamic", "revolutionary",
"Silk", "jolt", "DNA", etc. They wanted something that reflected the essence of
the technology: revolutionary, dynamic, lively, cool, unique, and easy to spell,
and fun to say.
According to James Gosling, "Java was one of the top choices along with Silk".
Since Java was so unique, most of the team members preferred Java than other
names.
8) Java is an island in Indonesia where the first coffee was produced (called
Java coffee). It is a kind of espresso bean. Java name was chosen by James
Gosling while having a cup of coffee nearby his office.
9) Notice that Java is just a name, not an acronym.
10) Initially developed by James Gosling at Sun Microsystems
(Which is now a subsidiary of Oracle Corporation) and released in 1995.
11) In 1995, Time magazine called Java one of the Ten Best Products of 1995.

12) JDK 1.0 was released on January 23, 1996. After the first release of Java,
there have been many additional features added to the language. Now Java is
being used in Windows applications, Web applications, enterprise applications,
mobile applications, cards, etc. Each new version adds new features in Java.
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic

Features of Java
The primary objective of Java programming

language creation was to make it portable, simple and secure programming language. Apart from
this, there are also some excellent features which play an important role in the popularity of this
language. The features of Java are also known as Java buzzwords.

A list of the most important features of the Java language is given below.
Types of Java Applications
There are mainly 4 types of applications that can be created using Java
programming:
1) Standalone Application: -
Standalone applications are also known as desktop applications or window-
based applications. These are traditional software that we need to install on
every machine. Examples of standalone application are Media player, antivirus,
etc. AWT and Swing are used in Java for creating standalone applications.
2) Web Application: -
An application that runs on the server side and creates a dynamic page is called
a web application. Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc.
technologies are used for creating web applications in Java.
3) Enterprise Application: -
An application that is distributed in nature, such as banking applications, etc. is
called an enterprise application. It has advantages like high-level security, load
balancing, and clustering. In Java, EJB is used for creating enterprise
applications.
4) Mobile Application: -
An application which is created for mobile devices is called a mobile
application. Currently, Android and Java ME are used for creating mobile
applications.
Java Platforms / Editions
There are 4 platforms or editions of Java:
1) Java SE (Java Standard Edition): -
It is a Java programming platform. It includes Java programming APIs such as
java.lang, java.io, java.net, java.util, java.sql, java.math etc. It includes core
topics like OOPs, String, Regex, Exception, Inner classes, Multithreading, I/O
Stream, Networking, AWT, Swing, Reflection, Collection, etc.
2) Java EE (Java Enterprise Edition): -
It is an enterprise platform that is mainly used to develop web and enterprise
applications. It is built on top of the Java SE platform. It includes topics like
Servlet, JSP, Web Services, EJB, JPA, etc.
3) Java ME (Java Micro Edition): -
It is a micro platform that is dedicated to mobile applications.
4) JavaFX: -
It is used to develop rich internet applications. It uses a lightweight user
interface API.
Experiment -2
AIM: - WAP to find factorial of a number in java.
CODE: -
import java.util.Scanner;
public class factorial {
public static void main(String[] args) {

int fact = 1, i = 1;
Scanner sc = new Scanner (System.in);
System.out.println("Enter a number whose factorial is to be found: ");
int num = sc.nextInt();
while (i <= num) {
fact = fact * i;
i++;
}
System.out.println("\nFactorial of " + num + " is: " +fact);
}

}
OUTPUT: -
Experiment -3
AIM: - WAP to print Fibonacci series till nth term.
CODE: -
import java.util.Scanner;
public class NthFibonacci {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
int n=s.nextInt();

int f1 = 0, f2 = 1, f;
System.out.print(f1+" "+f2);

for(int i = 2; i<n; i++){


f = f1 + f2;
System.out.print(" "+f);
f1 = f2;
f2 = f;
}

}
OUTPUT: -
Experiment – 4
AIM; - WAP to calculate sum of number until negative number is enter.
CODE: -
import java.util.Scanner;
public class SumofNumberNegative {

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
System.out.println("Please enter a number.");
int input = s.nextInt();
int sum = 0;
while(input >= 0)
{
sum += input;
System.out.println("Please enter a number.");
input=s.nextInt();
}
System.out.println("Sum of the numbers are = " + sum);
}
}
OUTPUT: -
Experiment – 5
AIM; - WAP to find the average marks of n students, calculate their
percentage and assign them grades.
CODE: -

OUTPUT: -
Experiment – 6
AIM; - WAP that takes three inputs from the user: one operator and 2
numbers. Based on the operator provided by the user, it performs the calculation
on the numbers. Then the result is displayed on the screen.
CODE: -
import java.util.Scanner;
public class Operator {

public static void main(String[] args) {


char operator;
Double number1, number2, result;
Scanner input = new Scanner(System.in);

System.out.println("Choose an operator: +, -, *, or /");


operator = input.next().charAt(0);

System.out.println("Enter first number");


number1 = input.nextDouble();

System.out.println("Enter second number");


number2 = input.nextDouble();

switch (operator) {

// performs addition between numbers


case '+':
result = number1 + number2;
System.out.println(number1 + " + " + number2 + " = " + result);
break;

// performs subtraction between numbers


case '-':
result = number1 - number2;
System.out.println(number1 + " - " + number2 + " = " + result);
break;

// performs multiplication between numbers


case '*':
result = number1 * number2;
System.out.println(number1 + " * " + number2 + " = " + result);
break;

// performs division between numbers


case '/':
result = number1 / number2;
System.out.println(number1 + " / " + number2 + " = " + result);
break;

default:
System.out.println("Invalid operator!");
break;
}

}
OUTPUT: -
Experiment – 7
AIM; - WAP to calculate area of circle using the radius.
CODE: -
import java.util.Scanner;
public class CircleRadius {

public static void main(String[] args) {


Scanner s= new Scanner(System.in);

System.out.println("Enter the radius:");


double r= s.nextDouble();
double area=(22*r*r)/7 ;
System.out.println("Area of Circle is: " + area);

}
OUTPUT: -
Experiment – 8
AIM; - WAP to display personal data of user using scanner class.
CODE: -
import java.util.Scanner;
public class scannerwithUSer {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input.nextLine();
System.out.println("My name is " + name+".");
}
}
OUTPUT: -
Experiment – 9
AIM; - WAP to display odd number up to 20.
CODE: -
import java.util.Scanner;
public class OddNumber {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int number = s.nextInt();
System.out.print("List of odd numbers from 1 to " + number + ": ");
for (int i = 1; i <= number; i++) {
if (i % 2 != 0) {
System.out.print(i + " ");
}
}
}
}
OUTPUT: -
Experiment – 10
AIM; - WAP to calculate sum, difference, product, quotient of two number
enter by the user.
CODE: -
import java.util.Scanner;
public class TwoNumberUser {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a= s.nextInt() , b=s.nextInt();
int sum = a+b;
int diff = a-b;
int product = a*b;
double quot = a/b;
System.out.println("Sum: " +sum);
System.out.println("Difference: " +diff);
System.out.println("Product: " +product);
System.out.println("Quotient: " +quot);
}
}
OUTPUT: -
Experiment – 11
AIM; - WAP to implement stack and queue concept in java.
1. Implement Stack
CODE: -
public class Implementstack {
private int arr[];
private int top;
private int capacity;
Implementstack(int size) {
// initialize the array
// initialize the stack variables
arr = new int[size];
capacity = size;
top = -1;
}
public void push(int x) {
if (isFull()) {
System.out.println("Stack OverFlow");

// terminates the program


System.exit(1);
}
// insert element on top of stack
System.out.println("Push " + x);
arr[++top] = x;
}
public int pop() {
if (isEmpty()) {
System.out.println("STACK EMPTY");
// terminates the program
System.exit(1);
}

// pop element from top of stack


return arr[top--];
}

// return size of the stack


public int getSize() {
return top + 1;
}
public Boolean isEmpty() {
return top == -1;
}
public Boolean isFull() {
return top == capacity - 1;
}

// display elements of stack


public void printStack() {
for (int i = 0; i <= top; i++) {
System.out.print(arr[i] + ", ");
}
}
public static void main(String[] args) {
Implementstack stack = new Implementstack(5);

stack.push(1);
stack.push(2);
stack.push(3);

System.out.print("Stack: ");
stack.printStack();

stack.pop();
System.out.println("\nAfter popping out");
stack.printStack();

}
}
OUTPUT: -
2. Implement Queue
CODE: -
public class ImplementQueue {
int SIZE = 5;
int items[] = new int[SIZE];
int front, rear;

ImplementQueue() {
front = -1;
rear = -1;
}

// check if the queue is full


boolean isFull() {
if (front == 0 && rear == SIZE - 1) {
return true;
}
return false;
}

// check if the queue is empty


boolean isEmpty() {
if (front == -1)
return true;
else
return false;
}
// insert elements to the queue
void enQueue(int element) {

// if queue is full
if (isFull()) {
System.out.println("Queue is full");
} else {
if (front == -1) {
// mark front denote first element of queue
front = 0;
}

rear++;
// insert element at the rear
items[rear] = element;
System.out.println("Insert " + element);
}
}

// delete element from the queue


int deQueue() {
int element;

// if queue is empty
if (isEmpty()) {
System.out.println("Queue is empty");
return (-1);
} else {
// remove element from the front of queue
element = items[front];

// if the queue has only one element


if (front >= rear) {
front = -1;
rear = -1;
} else {
// mark next element as the front
front++;
}
System.out.println(element + " Deleted");
return (element);
}
}

// display element of the queue


void display() {
int i;
if (isEmpty()) {
System.out.println("Empty Queue");
} else {
// display the front of the queue
System.out.println("\nFront index-> " + front);

// display element of the queue


System.out.println("Items -> ");
for (i = front; i <= rear; i++)
System.out.print(items[i] + " ");

// display the rear of the queue


System.out.println("\nRear index-> " + rear);
}
}

public static void main(String[] args) {

// create an object of Queue class


ImplementQueue q = new ImplementQueue();

// try to delete element from the queue


// currently queue is empty
// so deletion is not possible
q.deQueue();

// insert elements to the queue


for (int i = 1; i < 6; i++) {
q.enQueue(i);
}

// 6th element can't be added to queue because queue is full


q.enQueue(6);
q.display();
q.deQueue();
q.display();
}
}

OUTPUT: -

Experiment – 12
AIM; - WAP to implement single inheritance concept in java.
CODE: -
public class Shape {
int length;
int breadth;
}
import java.util.Scanner;
public class Rectangle extends Shape {
int area;
public void calcualteArea() {
area = length * breadth;
System.out.println(area);
}
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
Rectangle r = new Rectangle();
r.length = s.nextInt();
r.breadth= s.nextInt();
r.calcualteArea();
}
}

OUTPUT: -
Experiment – 13
AIM; - WAP to implement multiple inheritance concept in java.
CODE: -
interface Event {
public void start();
}
interface Sports {
public void play();
}
interface Hockey extends Sports, Event {
public void show();
}
public class Tester {
public static void main(String[] args){
Hockey hockey = new Hockey() {
public void start() {
System.out.println("Start Event");
}
public void play() {
System.out.println("Play Sports.");
}
public void show() {
System.out.println("Show Hockey.");
}
};
hockey.start();
hockey.play();
hockey.show();
}
}
OUTPUT: -
Experiment – 14
AIM; - WAP to implement multilevel hierarchical inheritance concept in java.
CODE: -
public class A {
void funcA() {
System.out.println("This is class A");
}
}
public class B extends A {
void funcB() {
System.out.println("This is class B");
}
}
public class C extends B {
void funcC() {
System.out.println("This is class C");
}
}
public class Demo {
public static void main(String args[]) {
C obj = new C();
obj.funcA();
obj.funcB();
obj.funcC();
}
}
OUTPUT: -
Experiment – 15
AIM; - WAP to show multithreaded producer and consumer application.
CODE: -
public class Multithreading {
public static void main(String[] args) throws InterruptedException {
final PC pc = new PC();
// Create producer thread
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
pc.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// Create consumer thread
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
pc.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// Start both threads
t1.start();
t2.start();

// t1 finishes before t2
t1.join();
t2.join();
}

// This class has a list, producer (adds items to list


// and consumer (removes items).
public static class PC {

// Create a list shared by producer and consumer


// Size of list is 2.
LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;

// Function called by producer thread


public void produce() throws InterruptedException {
int value = 0;
while (true) {
synchronized (this) {
// producer thread waits while list
// is full
while (list.size() == capacity)
wait();
System.out.println("Producer produced-" + value);
// to insert the jobs in the list
list.add(value++);

// notifies the consumer thread that


// now it can start consuming
notify();
// makes the working of program easier
// to understand
Thread.sleep(1000);
}
}
}

// Function called by consumer thread


public void consume() throws InterruptedException {
while (true) {
synchronized (this) {
// consumer thread waits while list
// is empty
while (list.size() == 0)
wait();

// to retrieve the first job in the list


int val = list.removeFirst();
System.out.println("Consumer consumed-" + val);
// Wake up producer thread
notify();

// and sleep
Thread.sleep(1000);
}
}
}
}
}
OUTPUT: -
Experiment – 16
AIM; - Create a customized exception and make use of all the 5 exception
keywords.
CODE: -
public class CustomizedException {
public static void main(String[] args) {
try {
String welcomeMessage = welcomeMessage("SJ");
System.out.println("The returned welcome message : " + welcomeMessage);
} catch (NullPointerException npex) {
System.out.println("Exception handled : " + npex.toString());
} finally {
System.out.println("Rest of the clean-up code here");
}
}
// division method returning quotient
public static String welcomeMessage(String name)throws NullPointerException
{
if (name == null) {
throw new NullPointerException("Invoke method with VALID name");
}
String welcomeMsg = "Welcome " + name;
return welcomeMsg;
}
}
OUTPUT: -

You might also like