1.
Hello World Program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!"); // Output Hello World
}
}
2. Addition of Two Numbers
import java.util.Scanner;
public class AddTwoNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();
int sum = num1 + num2;
System.out.println("The sum is: " + sum);
}
}
3.Demonstrates the use of a class
// Defining a Car class
public class Car {
// Attributes (or instance variables)
String model;
String color;
int year;
// Constructor to initialize the object
public Car(String model, String color, int year) {
this.model = model;
this.color = color;
this.year = year;
}
// Method to display car details
public void displayDetails() {
System.out.println("Car Model: " + model);
System.out.println("Car Color: " + color);
System.out.println("Car Year: " + year);
}
// Method to simulate driving
public void drive() {
System.out.println("The " + color + " " + model + " is driving.");
}
// Main method to run the program
public static void main(String[] args) {
// Creating an object of the Car class
Car myCar = new Car("Toyota Corolla", "Red", 2020);
// Calling methods on the object
myCar.displayDetails();
myCar.drive();
}
}
Output:
Car Model: Toyota Corolla
Car Color: Red
Car Year: 2020
The Red Toyota Corolla is driving.
4. Basic Blockchain Class
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;
public Block(String data, String previousHash) {
this.data = data;
this.previousHash = previousHash;
this.timeStamp = new Date().getTime();
this.hash = calculateHash();
}
public String calculateHash() {
String input = previousHash + Long.toString(timeStamp) + data;
return Integer.toString(input.hashCode());
}
}
public class SimpleBlockchain {
public static ArrayList<Block> blockchain = new ArrayList<>();
public static void main(String[] args) {
blockchain.add(new Block("First block", "0"));
blockchain.add(new Block("Second block", blockchain.get(blockchain.size() -
1).hash));
blockchain.add(new Block("Third block", blockchain.get(blockchain.size() -
1).hash));
for (Block block : blockchain) {
System.out.println("Hash: " + block.hash);
System.out.println("Previous Hash: " + block.previousHash);
System.out.println();
}
}
}
Key Components:
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:
Hash: 123456789 (calculated hash for the first block)
Previous Hash: 0
Hash: 987654321 (calculated hash for the second block)
Previous Hash: 123456789
Hash: 543216789 (calculated hash for the third block)
Previous Hash: 987654321