0% found this document useful (0 votes)
36 views42 pages

I It Java Coding Only

this is a study material

Uploaded by

Kalaiyarasi.p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views42 pages

I It Java Coding Only

this is a study material

Uploaded by

Kalaiyarasi.p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

1.

BASIC PROGRAMS

SOURCE CODE:

public class Hello {

public static void main(String[] args) {

System.out.println("Hello, World!");

}
OUTPUT:

C:\Users\kavya>cd\

C:\>cd jp

C:\jp>set path="C:\Program Files\Java\jdk1.7.0_51\bin";

C:\jp>javac Hello.java

C:\jp>java Hello

Hello, World!
2.ARRAYS

SOURCE CODE:

class Main {

public static void main(String[] args) {

int[] age = {12, 4, 5, 2, 5};

System.out.println("Accessing Elements of Array:");

System.out.println("First Element: " + age[0]);

System.out.println("Second Element: " + age[1]);

System.out.println("Third Element: " + age[2]);

System.out.println("Fourth Element: " + age[3]);

System.out.println("Fifth Element: " + age[4]);

}
OUTPUT:

C:\Users\kavya>cd\

C:\>cd jp

C:\jp>set path="C:\Program Files\Java\jdk1.7.0_51\bin";

C:\jp>javac Main.java

C:\jp>java Main

Accessing Elements of Array:

First Element: 12

Second Element: 4

Third Element: 5

Fourth Element: 2

Fifth Element: 5
3.STRINGS

SOURCE CODE:

class Main1 {
public static void main(String[] args) {
String first = "Java";
String second = "Python";
String third = "JavaScript";
System.out.println(first); // print Java
System.out.println(second); // print Python
System.out.println(third); // print JavaScript
}
}
OUTPUT :

C:\Users\kavya>cd\

C:\>cd jp

C:\jp>set path="C:\Program Files\Java\jdk1.7.0_51\bin";

C:\jp>javac Main1.java

C:\jp>java Main1

Java

Python

JavaScript
4. ARRAY LIST, HASHSET AND VECTOR COLLECTION CLASSES

import java.io.*;
import java.util.*;
class GFG
{
public static void main (String[] args)
{
ArrayList<String> al = new ArrayList<String>();
al.add("Practice.GeeksforGeeks.org");
al.add("www.GeeksforGeeks.org");
al.add("code.GeeksforGeeks.org");
al.add("contribute.GeeksforGeeks.org");
System.out.println("ArrayList elements are:");
Iterator it = al.iterator();
while (it.hasNext())
System.out.println(it.next());
Vector<String> v = new Vector<String>();
v.addElement("Practice");
v.addElement("quiz");
v.addElement("code");
System.out.println("\nVector elements are:");
Enumeration e = v.elements();
while (e.hasMoreElements())
System.out.println(e.nextElement());

}
}
OUTPUT :

C:\Users\kavya>cd\

C:\>cd jp

C:\jp>set path="C:\Program Files\Java\jdk1.7.0_51\bin";

C:\jp>javac GFG.java

C:\jp>java GFG

ArrayList elements are:

Practice.GeeksforGeeks.org

www.GeeksforGeeks.org

code.GeeksforGeeks.org

contribute.GeeksforGeeks.org

Vector elements are:

Practice

quiz

code
5.CLASSES AND OBJECTS

SOURCE CODE:

public class Computer


{
String name;
String config;
int cost;
String os;
public Computer(String name, String config,
int cost, String os)
{
this.name = name;
this.config = config;
this.cost = cost;
this.os = os;
}
public String getName()
{
return name;
}

public String getConfig()


{
return config;
}
public int getCost()
{
return cost;
}
public String getOs()
{
return os;
}

public static void main(String[] args)


{

Computer c1 = new Computer("Apple","i5", 50000, "IOS");

System.out.println("The company name is "+ c1.getName());


System.out.println("The configuration is "+ c1.getConfig());
System.out.println("Its Cost is "+ c1.getCost());
System.out.println("Its operating System "+ c1.getOs());

}
}
OUTPUT :

C:\Users\kavya>cd\

C:\>cd jp

C:\jp>set path="C:\Program Files\Java\jdk1.7.0_51\bin";

C:\jp>javac Computer.java

C:\jp>java Computer

The company name is Apple

The configuration is i5

Its Cost is 50000

Its operating System IOS


6. INTERFACE

SOURCE CODE:

interface printable{

void print();

class A6 implements printable{

public void print(){System.out.println("Hello");}

public static void main(String args[]){

A6 obj = new A6();

obj.print();

}
OUTPUT :

C:\Users\kavya>cd\

C:\>cd jp

C:\jp>set path="C:\Program Files\Java\jdk1.7.0_51\bin";

C:\jp>javac A6.java

C:\jp>java A6

Hello
7.INHERITANCE

SOURCE CODE:

class Employee{

float salary=40000;

class Programmer extends Employee{

int bonus=10000;

public static void main(String args[]){

Programmer p=new Programmer();

System.out.println("Programmer salary is:"+p.salary);

System.out.println("Bonus of Programmer is:"+p.bonus);

}
OUTPUT :

C:\Users\kavya>cd\

C:\>cd jp

C:\jp>set path="C:\Program Files\Java\jdk1.7.0_51\bin";

C:\jp>javac Programmer.java

C:\jp>java Programmer

Programmer salary is:40000.0

Bonus of Programmer is:10000


8.PACKAGES

SOURCE CODE:

//save by Add.java

package mypack;

public class Add

public void addition()

int a = 100;

int b = 200;

System.out.println("The Sum is:" +(a+b));

//save by Myclass.java

import mypack.Add;

public class Myclass

public static void main(String args[])

Add a=new Add();

a.addition();

}
OUTPUT :

C:\Users\kavya>cd\

C:\>cd jp

C:\jp>set path="C:\Program Files\Java\jdk1.7.0_51\bin";

C:\jp>javac -d . Add.java

C:\jp>javac Myclass.java

C:\jp>java Myclass

The Sum is:300


9. EXCEPTION HANDLING

SOURCE CODE:

class Main3 {

public static void main(String[] args) {

try {

int divideByZero = 5 / 0;

System.out.println("Rest of code in try block");

catch (ArithmeticException e) {

System.out.println("ArithmeticException => " + e.getMessage());

}
OUTPUT :

C:\Users\kavya>cd\

C:\>cd jp

C:\jp>set path="C:\Program Files\Java\jdk1.7.0_51\bin";

C:\jp>javac Main3.java

C:\jp>java Main3

ArithmeticException => / by zero


10. THREADS

SOURCE CODE:

class threads {

static String reverseString(String s) {

StringBuilder res = new StringBuilder();

for (int i = s.length() - 1; i >= 0; i--) {

res.append(s.charAt(i));

return res.toString();

public static void main(String[] args) {

String s = "abdcfe";

String res = reverseString(s);

System.out.print(res);

}
OUTPUT :

C:\Users\kavya>cd\

C:\>cd jp

C:\jp>set path="C:\Program Files\Java\jdk1.7.0_51\bin";

C:\jp>javac threads.java

C:\jp>java threads

efcdba
11.LINKED LIST

SOURCE CODE:

import java.util.LinkedList;

public class AddElements {

// Main driver method


public static void main(String[] args) {
// Creating a LinkedList
LinkedList<String> l = new LinkedList<String>();

// Adding elements to the LinkedList using add() method


l.add("One");
l.add("Two");
l.add("Three");
l.add("Four");
l.add("Five");

// Printing the LinkedList


System.out.println(l);
}
}
OUTPUT :

C:\Users\kavya>cd\

C:\>cd jp

C:\jp>set path="C:\Program Files\Java\jdk1.7.0_51\bin";

C:\jp>javac AddElements.java

C:\jp>java AddElements

[One, Two, Three, Four, Five]


12.STACK

SOURCE CODE:

public class StackExample {


private int maxSize;
private int[] stackArray;
private int top;
public StackExample(int size) {
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}
// Method to push an element onto the stack
public void push(int value) {
if (top == maxSize - 1) {
System.out.println("Stack overflow");
return;
}
stackArray[++top] = value;
System.out.println(value + " pushed into the stack");
}
public int pop() {
if (top == -1) {
System.out.println("Stack underflow");
return -1;
}
int poppedElement = stackArray[top--];
System.out.println(poppedElement + " popped from the stack");
return poppedElement;
}

// Method to peek the top element of the stack


public int top() {
if (top == -1) {
System.out.println("Stack is empty");
return -1;
}
return stackArray[top];
}

// Method to check if the stack is empty


public boolean isEmpty() {
return (top == -1);
}

public static void main(String[] args) {


StackExample stack = new StackExample(5);
// Creating a stack of size 5
// Pushing elements onto the stack
stack.push(10);
stack.push(20);
// Peeking the top element
System.out.println("Top element of the stack: " + stack.top());

// Popping elements from the stack


stack.pop();
stack.pop();
stack.pop(); // Trying to pop from an empty stack

// Checking if the stack is empty


System.out.println("Is stack empty? " + stack.isEmpty());
}
}
OUTPUT :

C:\Users\kavya>cd\

C:\>cd jp

C:\jp>set path="C:\Program Files\Java\jdk1.7.0_51\bin";

C:\jp>javac StackExample.java

C:\jp>java StackExample

10 pushed into the stack

20 pushed into the stack

Top element of the stack: 20

20 popped from the stack

10 popped from the stack

Stack underflow

Is stack empty? True


13.QUEUE

SOURCE CODE:

import java.util.Queue;

import java.util.LinkedList;

public class Demo {

public static void main(String[] args) {

System.out.println("The required packages have been imported");

Queue<Integer> input_queue = new LinkedList<>();

input_queue.offer(150);

input_queue.offer(300);

input_queue.offer(450);

input_queue.offer(600);

System.out.println("The queue is defined as: " + input_queue);

int removedNumber = input_queue.poll();

System.out.println("After removing an element, the elements of the queue are:

" +input_queue);

}
OUTPUT :

C:\Users\kavya>cd\

C:\>cd jp

C:\jp>set path="C:\Program Files\Java\jdk1.7.0_51\bin";

C:\jp>javac Demo.java

C:\jp>java Demo

The required packages have been imported

The queue is defined as: [150, 300, 450, 600]

After removing an element, the elements of the queue are: [300, 450, 600]
14. SORTING

SOURCE CODE:

class sorting {
public static void main(String[] args)
{
int arr[] = { 4, 3, 2, 1 };
for (int i = 0; i< arr.length; i++) {

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

// Checking elements
int temp = 0;
if (arr[j] < arr[i]) {

// Swapping
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

// Printing sorted array elements


System.out.print(arr[i] + " ");
}
}
}
OUTPUT :

C:\Users\kavya>cd\

C:\>cd jp

C:\jp>set path="C:\Program Files\Java\jdk1.7.0_51\bin";

C:\jp>javac sorting.java

C:\jp>java sorting

1234
15.BINARY TREE REPRESENTATION

class Node {
int key;
Node left, right;

public Node(int item) {


key = item;
left = right = null;
}
}

// BinaryTree Class
public class BinaryTree {
Node root;

public BinaryTree() {
root = null;
}

// Method to insert a new node with given key


public void insert(int key) {
root = insertRec(root, key);
}
// A recursive function to insert a new key in BST
private Node insertRec(Node root, int key) {
// If the tree is empty, return a new node
if (root == null) {
root = new Node(key);
return root;
}
// Otherwise, recur down the tree
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);
// return the (unchanged) node pointer
return root;
}
// Method to print the tree inorder
public void inorder() {
inorderRec(root);
}
// A utility function to do inorder traversal of BST
private void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.key + " ");
inorderRec(root.right);
}
}

// Method to search for a key in the tree


public boolean search(int key) {
return searchRec(root, key);
}

// A utility function to search for a key in BST


private boolean searchRec(Node root, int key) {
if (root == null)
return false;

if (root.key == key)
return true;

if (key < root.key)


return searchRec(root.left, key);
else
return searchRec(root.right, key);
}
// Method to find the minimum value in the tree
public int findMin() {
return findMinRec(root);
}

// A utility function to find the minimum value in BST


private int findMinRec(Node root) {
if (root == null)
throw new IllegalStateException("Tree is empty");

if (root.left == null)
return root.key;

return findMinRec(root.left);
}

// Method to find the maximum value in the tree


public int findMax() {
return findMaxRec(root);
}

// A utility function to find the maximum value in BST


private int findMaxRec(Node root) {
if (root == null)
throw new IllegalStateException("Tree is empty");

if (root.right == null)
return root.key;

return findMaxRec(root.right);
}

public static void main(String[] args) {


BinaryTree tree = new BinaryTree();
// Insert some nodes
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);

// Print inorder traversal of the tree


System.out.println("Inorder traversal:");
tree.inorder();
// Output: 20 30 40 50 60 70 80
// Search for a key
int searchKey = 40;
if (tree.search(searchKey))
System.out.println("\nKey " + searchKey + " found in the tree.");
else
System.out.println("\nKey " + searchKey + " not found in the tree.");

// Find minimum and maximum values


System.out.println("Minimum value in the tree: " + tree.findMin());
System.out.println("Maximum value in the tree: " + tree.findMax());
}
}
OUTPUT :

C:\Users\kavya>cd\

C:\>cd jp

C:\jp>set path="C:\Program Files\Java\jdk1.7.0_51\bin";

C:\jp>javac .java

C:\jp>java

Inorder traversal:
20 30 40 50 60 70 80
Key 40 found in the tree.
Minimum value in the tree: 20
Maximum value in the tree: 80
16.DATABASE USING JDBC

import java.io.*;
import java.sql.*;

class GFG {
public static void main(String[] args) throws Exception
{
String url
= "jdbc:mysql://localhost:3306/table_name"; // table details
String username = "rootgfg"; // MySQL credentials
String password = "gfg123";
String query
= "select *from students"; // query to be run
Class.forName(
"com.mysql.cj.jdbc.Driver"); // Driver name
Connection con = DriverManager.getConnection(
url, username, password);
System.out.println(
"Connection Established successfully");
Statement st = con.createStatement();
ResultSet rs
= st.executeQuery(query); // Execute query
rs.next();
String name
= rs.getString("name"); // Retrieve name from db

System.out.println(name); // Print result on console


st.close(); // close statement
con.close(); // close connection
System.out.println("Connection Closed....");
}
}
OUTPUT :

Closing the connections

con.close();
17. WEB APPLICATION USING SERVLET

<IDOCTYPE html>
<html>
<head>

<meta http-equiv"Content-Type" content="text/html; charset-UTF-8">


<title>JSP Page</title>
/head>
<body>
<h1>Hello World!</h1>
</body
</html>

<!DOCTYPE html>
<html>
<head>
Smeta http-equiv="Content-Tyne" content="text/html; charset=UTF-8">
title>Welcome to MyFirstServlet example page</title>
</head>
<body>
<h1>A Welcome Web Application</h1>
<form method="POST" action="WelcomeServlet">
<label for="name" title="Enter the name">Name: </label>
<input type="text" id="txtName" name="txtName"/><br><br>|
input type="submit" value="Submit"/>
</form>
</body>
</html>
OUTPUT:

You might also like