0% found this document useful (0 votes)
2 views16 pages

Java Concepts Codes

The document outlines practical Java programming exercises focusing on various concepts such as exception handling, inheritance, method overloading, encapsulation, abstraction, interfaces, multithreading, and stream operations. Each program demonstrates specific Java features, including custom exceptions, validation, and file management. The exercises are designed to enhance understanding of Java programming principles and practices.

Uploaded by

gajulajhansi01
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)
2 views16 pages

Java Concepts Codes

The document outlines practical Java programming exercises focusing on various concepts such as exception handling, inheritance, method overloading, encapsulation, abstraction, interfaces, multithreading, and stream operations. Each program demonstrates specific Java features, including custom exceptions, validation, and file management. The exercises are designed to enhance understanding of Java programming principles and practices.

Uploaded by

gajulajhansi01
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

Practical Java Programs (NEW)

Program 1

Bank Account System

Create a class where withdrawing more than the available balance throws an exception.

Concepts:

 Exception Handling
 throw

try-catch

ArithmeticException works, but it's not the best choice.

Why?

Because this isn't an arithmetic error like 10 / 0.

It's a business rule violation (insufficient balance).

In real applications, developers often use:

 IllegalArgumentException

 a custom exception like InsufficientBalanceException

However, if your topic is just learning throw, using ArithmeticException


is perfectly acceptable.

class Bank{

public static void main(String[] args){

int balance=100;

int withdrawal=120;

if(balance<withdrawal){

throw new ArithmeticException("insufficient balance");

[Link]("withdrawal successfull");
}

Or

Custom exceptions

class Bank {

public static void main(String[] args) throws


InsufficientBalanceException {

int balance = 100;

int withdrawal = 120;

if (withdrawal > balance) {

throw new InsufficientBalanceException("Insufficient Balance");

[Link]("Withdrawal Successful");

Program 2

Student Marks Validator

If marks are less than 0 or greater than 100, throw an exception.

Concepts:

 Custom validation
 Exception Handling

class Student{

public static void main(String[] args) {


int marks=-1;

if (marks < 0 || marks > 100){

throw new IllegalArgumentException("marks must be between 0-100");

[Link]("marks:"+marks);

This is a rule you'll use everywhere:

 Valid range: marks >= 0 && marks <= 100


 Invalid range: marks < 0 || marks > 100

⭐ Interview tip: Whenever you see a range question, first ask yourself:

"Am I checking the valid range or the invalid range?"

Program 3

Animal → Dog

Practice:

 Inheritance

 Method Overriding (basic)

class Animal{

void eat(){

[Link]("all animals eat");

class Dog extends Animal{


void bark(){

[Link]("all dogs barks");

void eat(){

[Link]("overriding");

class Main{

public static void main(String[] args){

Dog d=new Dog();

[Link]();

[Link]();

Program 4

Calculator

Implement:

add(int a, int b)

add(int a, int b, int c)

Practice:

 Method Overloading

class Parent{

int add(int a,int b){

return a+b;

}
}

class Child extends Parent{

int add(int a,int b, int c){

return a+b+c;

class Calc{

public static void main(String[] args){

Child c=new Child();

[Link]([Link](10,20));

[Link]([Link](10,20,30));

Program 6

Student Management

Private variables

Getter

Setter

Encapsulation

requirements

A student has:

 Student ID
 Name
 Age
 Course
class Student{

private int studentId;

private String name;

private int age;

private String course;

public int getStudentId(){

return studentId;

public void setStudentId(int studentId){

[Link]=studentId;

public String getName(){

return name;

public void setName(String name){

[Link]=name;

public int getAge(){

return age;

public void setAge(int age){

[Link]=age;

public String getCourse(){

return course;

public void setCourse(String course){

[Link]=course;
}

class Main{

public static void main(String[] args){

Student s=new Student();

[Link](12);

[Link]([Link]());

[Link]("jhansi");

[Link]([Link]());

[Link](21);

[Link]([Link]());

[Link]("java");

[Link]([Link]());

Program-7

Employee Salary System

 Abstract class Employee


 FullTimeEmployee
 PartTimeEmployee
 Implement calculateSalary()

Concept:

 Abstraction

abstract class Employee{

private int id;


private String name;

abstract int calculateSalary();

public int getId(){

return id;}

public void setId(int id){

[Link]=id;

public String getName(){

return name;

public void setName(String name){

[Link]=name;

}}

class FullTimeEmployee extends Employee{

@Override

int calculateSalary() {

return 50000;

class PartTimeEmployee extends Employee{

@Override

int calculateSalary() {

return 10000;

class Main{

public static void main(String[] args){


FullTimeEmployee f = new FullTimeEmployee();

PartTimeEmployee p=new PartTimeEmployee();

[Link](1);

[Link]("lee");

[Link](2);

[Link]("jhansi");

[Link]( "Employee Id :" + [Link]());

[Link]( "Employee Name :"+ [Link]());

[Link]( "Employee Id :"+ [Link]());

[Link]( "Employee Name:" + [Link]());

[Link]("Employee salary : " + [Link]());

[Link]("Employee salary :" + [Link]());

Program-8

Create:

 Notification interface
 EmailNotification
 SMSNotification
 PushNotification

Use:

 Interface
 Runtime Polymorphism

interface Notification {

default void sound(){

[Link]("many types of notification sounds");


}

class EmailNotification implements Notification{

@Override

public void sound(){

[Link]("notification turned on");

class SMSNotification implements Notification{

@Override

public void sound(){

[Link]("notification is in mute");

class PushNotification implements Notification{

@Override

public void sound(){

[Link]("notification turned off");

class Main{

public static void main(String[] args){

Notification pn=new PushNotification(); //run time polymorphism

[Link]();

Program-9

Student Result System


Create:

 Accept student marks.


 Throw a custom exception for invalid marks.
 Use try-catch-finally.
 Use a Supplier to generate a default grade/message when needed.

import [Link];

import [Link];

class InvalidMarksException extends Exception {

InvalidMarksException(String msg) {

super(msg);

class Result {

void result() {

Scanner sc = new Scanner([Link]);

Supplier<String> defaultMessage = () -> "Fail";

[Link]("Enter Marks: ");

int marks = [Link]();

try {

if (marks < 0 || marks > 100) {

throw new InvalidMarksException("Invalid Marks");

if (marks >= 35) {

[Link]("Result : Pass");

[Link]("Marks : " + marks);

} else {

[Link]("Result : " + [Link]());

} catch (InvalidMarksException e) {
[Link]([Link]());

} finally {

[Link]("Thank you");

[Link]();

public class Main {

public static void main(String[] args) {

Result r = new Result();

[Link]();

Program-10

Student File Manager

Features:

 Write student details to a file


 Read them back
 Append new records
 Handle exceptions properly

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

class Student{
public static void main(String[] args){

try{

FileWriter fw=new FileWriter("[Link]",true);

Scanner sc = new Scanner([Link]);

[Link]("Enter Name: ");

String name = [Link]();

[Link]("Enter Age: ");

int age = [Link]();

[Link](name + "," + age + "\n");

[Link]();

[Link]();}

catch(IOException e){

[Link]("I/O Error");

try{

BufferedReader br=new BufferedReader(new FileReader("[Link]"));

String line;

while((line=[Link]())!=null){

[Link](line);

[Link]();

}catch (IOException e){

[Link]("I/O Error");

}
}

Program-11

Thread Counter

Create:

 One thread prints 1–10


 Another thread prints A–J
 Observe concurrent execution

Concepts:

 Multithreading
 Runnable

class MyThread implements Runnable{

public void run(){

for(int i=1;i<=10;i++){

[Link](i);

class Alpha implements Runnable{

public void run(){

for (char ch = 'A'; ch<='J';ch++) {

[Link](ch);

class Main{

public static void main(String[] args){


// MyThread t1=new MyThread();

// Thread tr1=new Thread(t1);

Thread tr1 = new Thread(new MyThread());

Thread tr2 = new Thread(new Alpha());

// Alpha t2=new Alpha();

// Thread tr2=new Thread(t2);

[Link]();

[Link]();

Program-12

Employee Stream Manager

 Create a list of employees.


 Filter employees with salary > X.
 Convert employee names to uppercase.
 Print using forEach().

import [Link];
import [Link];
class Emp{
String name;
int salary;
Emp(String name,int salary){
[Link]=name;
[Link]=salary;
}}
class Main{

public static void main(String[] args){


List<Emp> emp1=new ArrayList<>();
[Link](new Emp("jk",90));
[Link](new Emp("k",70));
[Link](new Emp("kj",80));
[Link](new Emp("jj",30));
[Link]("salary>60" );
[Link]()
.filter(emp->[Link]>30)
.forEach(emp->
[Link]([Link]+" " +[Link]));
[Link]("\nNames in Uppercase");
[Link]()
.map(emp -> [Link]())
.forEach([Link]::println);
}
}

You might also like