Basic Java Program
Basic Java Program
class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, World!"); // Output Hello World
}
}
Output:
Hello, World!
import java.util.Scanner;
Output:
}
}
Output:
Steps:
1. Create a Package: Define the package name at the beginning of the Java file using the
package keyword.
2. Create Classes Inside the Package: Define classes within the package.
3. Use the Package: Import the package into another class or program and use the classes inside
the package.
// File: MyClass.java
package myPackage;
After creating the Java file, save it and compile it using the following commands in the terminal:
>javac -d . MyClass.java
Now, we will create another Java file that will use the myPackage package.
// File: Main.java
import myPackage.MyClass;
>javac Main.java
>java Main
Output:
A Singly Linked List is a data structure that consists of nodes, where each node contains data and a
reference (or pointer) to the next node in the sequence. The last node points to null, indicating the
end of the list.
1. Node Class
class ListNode {
int data; // Data stored in the node
ListNode next; // Reference to the next node
This class will contain methods to manage the singly linked list, such as adding nodes,
removing nodes, and printing the list.
// Search for the key to be deleted, and keep track of the previous node
ListNode current = head;
ListNode prev = null;
while (current != null && current.data != key) {
prev = current;
current = current.next;
}
// Adding nodes
list.addNode(10);
list.addNode(20);
list.addNode(30);
list.addNode(40);
// Deleting a node
list.deleteNode(20);
System.out.print("After deletion of 20: ");
list.printList(); // Output: 10 -> 30 -> 40 -> null
}
}
Explanation:
1. ListNode Class:
● This class defines the structure of a node in the singly linked list. Each node contains two fields:
data and next, where data holds the value of the node, and next points to the next node in
the list.
2. SinglyLinkedList Class:
● head: The head of the linked list. Initially, it's set to null to indicate that the list is empty.
● addNode(int data): This method adds a new node at the end of the list. If the list is empty, it
sets the new node as the head. Otherwise, it traverses to the last node and links the new node
to the end of the list.
● printList(): This method prints all the nodes in the list, starting from the head, and
continues until it reaches the end (null).
● deleteNode(int key): This method deletes the node that contains the specified key (data
value). If the node to be deleted is the head, it updates the head. Otherwise, it searches for the
node and unlinks it from the list.
Example:
1. Adding Nodes:
○ We add nodes with values 10, 20, 30, and 40. After adding these nodes, the list looks
like this:
10 -> 20 -> 30 -> 40 -> null
Deleting a Node:
This program demonstrates the structure of a simple blockchain with blocks linked together.
import java.util.ArrayList;
import java.util.Date;
class Block {
public String hash;
public String previousHash;
private String data;
private long timeStamp;
1. Block Class:
○ Attributes:
■ hash: The current block's hash (unique identifier).
■ previousHash: The hash of the previous block in the chain.
■ data: The information stored in the block (in this case, it's a string, but in
real blockchains, it could be transactions).
■ timeStamp: The time at which the block was created.
○ Constructor:
■ Initializes the data, previousHash, and timeStamp when a block is
created.
■ Calls the calculateHash() method to generate a unique hash for the
block.
○ calculateHash():
■ Combines previousHash, timeStamp, and data to generate the block's
hash. It uses hashCode() to compute a hash, though in actual
blockchains, a more complex and secure hashing algorithm like SHA-256
would be used.
2. SimpleBlockchain Class:
○ ArrayList<Block> blockchain:
■ A list to store all the blocks in the chain.
○ Main Method:
■ Creates three blocks.
■ Adds each block to the blockchain, where each block's previousHash
refers to the hash of the last block.
■ Prints out the hash and previousHash of each block in the chain.
Output Example: