0% found this document useful (0 votes)
3 views5 pages

Java Programs With Explanations and MiniProject (1)

The document provides a collection of Java programs ranging from beginner to advanced levels, each accompanied by explanations and code examples. It covers fundamental concepts such as user input, object-oriented programming, exception handling, multithreading, and lambda expressions. Additionally, it includes a mini project for a simple inventory system that integrates various Java concepts.

Uploaded by

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

Java Programs With Explanations and MiniProject (1)

The document provides a collection of Java programs ranging from beginner to advanced levels, each accompanied by explanations and code examples. It covers fundamental concepts such as user input, object-oriented programming, exception handling, multithreading, and lambda expressions. Additionally, it includes a mini project for a simple inventory system that integrates various Java concepts.

Uploaded by

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

Java Programs from Basic to Advanced with Explanations

Beginner Programs

- Hello World

Explanation:

This is the simplest Java program. It defines a class called HelloWorld and the main method, which is the

entry point. It prints a message to the console.

Code:

public class HelloWorld {

public static void main(String[] args) {

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

- User Input

Explanation:

This program demonstrates how to take user input using the Scanner class. It reads an integer and prints it.

Code:

import java.util.Scanner;

public class UserInput {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();

System.out.println("You entered: " + num);

Intermediate Programs
Java Programs from Basic to Advanced with Explanations

- Class and Object

Explanation:

Shows the basics of Object-Oriented Programming. A class defines properties and objects are created using

'new'.

Code:

class Car {

String color = "red";

public static void main(String[] args) {

Car myCar = new Car();

System.out.println(myCar.color);

- Try-Catch

Explanation:

This program catches an arithmetic exception (division by zero). It's used to handle errors at runtime.

Code:

public class TryCatchExample {

public static void main(String[] args) {

try {

int divide = 10 / 0;

} catch (ArithmeticException e) {

System.out.println("Error: " + e.getMessage());

Advanced Programs
Java Programs from Basic to Advanced with Explanations

- Thread Example

Explanation:

Demonstrates multithreading. The Thread class is extended, and the thread is started using start().

Code:

class MyThread extends Thread {

public void run() {

System.out.println("Thread is running...");

public static void main(String[] args) {

MyThread t1 = new MyThread();

t1.start();

- Lambda Expression

Explanation:

This uses Java 8 lambda expressions to iterate and print list elements in a functional style.

Code:

import java.util.*;

public class LambdaDemo {

public static void main(String[] args) {

List<String> list = Arrays.asList("a", "b", "c");

list.forEach(s -> System.out.println(s));

Mini Project
Java Programs from Basic to Advanced with Explanations

- Simple Inventory System

Explanation:

This is a basic inventory system using a HashMap. It allows adding items and viewing current stock. It's an

example of integrating multiple Java concepts: loops, conditionals, collections, and user input.

Code:

import java.util.HashMap;

import java.util.Scanner;

public class InventorySystem {

public static void main(String[] args) {

HashMap<String, Integer> inventory = new HashMap<>();

Scanner scanner = new Scanner(System.in);

while (true) {

System.out.println("1. Add item\n2. View inventory\n3. Exit");

int choice = scanner.nextInt();

scanner.nextLine(); // Consume newline

if (choice == 1) {

System.out.print("Enter item name: ");

String item = scanner.nextLine();

System.out.print("Enter quantity: ");

int qty = scanner.nextInt();

inventory.put(item, inventory.getOrDefault(item, 0) + qty);

} else if (choice == 2) {

System.out.println("Current Inventory:");

for (String key : inventory.keySet()) {

System.out.println(key + ": " + inventory.get(key));

} else {

break;
Java Programs from Basic to Advanced with Explanations

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy