[go: up one dir, main page]

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

Oops

The document contains multiple Java programs demonstrating various programming concepts including operators, salary calculations, menu-driven programs, patterns, digit counting, Fibonacci series, parameterized constructors, inheritance, bank transactions, exception handling, password validation, linked lists, and file copying. Each program is designed to illustrate specific functionalities and programming techniques. The examples cover a range of topics suitable for beginners to intermediate Java programmers.

Uploaded by

nishuuu8901
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)
2 views20 pages

Oops

The document contains multiple Java programs demonstrating various programming concepts including operators, salary calculations, menu-driven programs, patterns, digit counting, Fibonacci series, parameterized constructors, inheritance, bank transactions, exception handling, password validation, linked lists, and file copying. Each program is designed to illustrate specific functionalities and programming techniques. The examples cover a range of topics suitable for beginners to intermediate Java programmers.

Uploaded by

nishuuu8901
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/ 20

1.

Operators

public class BasicOperations {

public static void main(String[] args) {

int a = 10, b = 5;

System.out.println("Arithmetic Operations:");

System.out.println("a + b = " + (a + b));

System.out.println("a - b = " + (a - b));

System.out.println("a * b = " + (a * b));

System.out.println("a / b = " + (a / b));

System.out.println("a % b = " + (a % b));

System.out.println("\nRelational Operations:");

System.out.println("a == b: " + (a == b));

System.out.println("a != b: " + (a != b));

System.out.println("a > b: " + (a > b));

System.out.println("a < b: " + (a < b));

System.out.println("a >= b: " + (a >= b));

System.out.println("a <= b: " + (a <= b));

boolean x = true, y = false;

System.out.println("\nLogical Operations:");

System.out.println("x && y: " + (x && y));

System.out.println("x || y: " + (x || y));

System.out.println("!x: " + (!x));

System.out.println("\nBitwise Operations:");

System.out.println("a & b: " + (a & b));

System.out.println("a | b: " + (a | b));

System.out.println("a ^ b: " + (a ^ b));

System.out.println("~a: " + (~a));


System.out.println("\nUnary Operations:");

int c = 10;

System.out.println("Initial c = " + c);

System.out.println("++c = " + (++c));

System.out.println("--c = " + (--c));

System.out.println("c++ = " + (c++));

System.out.println("c-- = " + (c--));

System.out.println("Final c = " + c);

System.out.println("\nTernary Operation:");

int max = (a > b) ? a : b;

System.out.println("Max of a and b is: " + max);

System.out.println("\nShift Operations:");

System.out.println("a << 1: " + (a << 1));

System.out.println("a >> 1: " + (a >> 1));

System.out.println("a >>> 1: " + (a >>> 1));

2a.Salary

import java.util.Scanner;

public class GrossSalaryCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter Basic Salary (bs): ");

double bs = scanner.nextDouble();
double hra = 0, da = 0, gs;

if (bs <= 10000) {

hra = 0.20 * bs;

da = 0.80 * bs;

} else if (bs <= 20000) {

hra = 0.25 * bs;

da = 0.90 * bs;

} else if (bs <= 30000) {

hra = 0.30 * bs;

da = 0.95 * bs;

} else {

System.out.println("This program supports basic salary up to ₹30000 only.");

scanner.close();

return;

gs = bs + hra + da;

System.out.println("Gross Salary: " + gs);

scanner.close();

2b.menudriven

import java.util.Scanner;

public class MenuDrivenProgram {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


int choice, num;

do {

System.out.println("\n----- MENU -----");

System.out.println("1. Factorial");

System.out.println("2. Prime or Not");

System.out.println("3. Even or Odd");

System.out.println("4. Exit");

System.out.print("Enter your choice (1-4): ");

choice = sc.nextInt();

switch (choice) {

case 1:

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

num = sc.nextInt();

long fact = 1;

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

fact *= i;

System.out.println("Factorial of " + num + " is " + fact);

break;

case 2:

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

num = sc.nextInt();

boolean isPrime = true;

if (num <= 1) {

isPrime = false;

} else {

for (int i = 2; i <= Math.sqrt(num); i++) {

if (num % i == 0) {
isPrime = false;

break;

if (isPrime)

System.out.println(num + " is a Prime Number.");

else

System.out.println(num + " is Not a Prime Number.");

break;

case 3:

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

num = sc.nextInt();

if (num % 2 == 0)

System.out.println(num + " is Even.");

else

System.out.println(num + " is Odd.");

break;

case 4:

System.out.println("Exiting the program. Goodbye!");

break;

default:

System.out.println("Invalid choice! Please enter 1 to 4.");

} while (choice != 4);

sc.close();

}
}

2c.pattern

public class PyramidNumberPattern {

public static void main(String[] args) {

int rows = 5;

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

for (int space = 1; space <= rows - i; space++) {

System.out.print(" ");

for (int j = i; j >= 1; j--) {

System.out.print(j);

for (int j = 2; j <= i; j++) {

System.out.print(j);

System.out.println();

3a.no. of 2s

import java.util.Scanner;

public class DigitCounter {

public static int countTwos(int num) {

int count = 0;
while (num > 0) {

if (num % 10 == 2) {

count++;

num /= 10;

return count;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a non-negative integer: ");

int number = scanner.nextInt();

if (number < 0) {

System.out.println("Invalid input. Please enter a non-negative integer.");

} else {

System.out.println("The number is: " + number);

int twosCount = countTwos(number);

System.out.println("Number of 2s: " + twosCount);

scanner.close();

3b.fibonacci

public class FibonacciSeries {

public static void main(String[] args) {

int n = 10;
int a = 0, b = 1;

System.out.println("First " + n + " numbers of the Fibonacci series:");

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

System.out.print(a + " ");

int next = a + b;

a = b;

b = next;

4a.parameter books

class Book {

String title;

String author;

double price;

Book() {

title = "Unknown Title";

author = "Unknown Author";

price = 0.0;

System.out.println("Default Constructor:");

printDetails();

Book(String title, String author) {

this.title = title;

this.author = author;
this.price = 0.0; // Default price

System.out.println("\nConstructor with 2 parameters (title, author):");

printDetails();

Book(String title, String author, double price) {

this.title = title;

this.author = author;

this.price = price;

System.out.println("\nConstructor with 3 parameters (title, author, price):");

printDetails();

void printDetails() {

System.out.println("Title: " + title);

System.out.println("Author: " + author);

System.out.println("Price: ₹" + price);

public class BookTest {

public static void main(String[] args) {

Book b1 = new Book(); // default

Book b2 = new Book("Wings of Fire", "Dr. A.P.J. Abdul Kalam"); // 2 parameters

Book b3 = new Book("Clean Code", "Robert C. Martin", 599.99); // 3 parameters

4b.parameter accounts

class Accounts {

String accNo;

double balance;
Accounts(String accNo, double balance) {

boolean isValid = true;

if (accNo == null || accNo.trim().isEmpty()) {

System.out.println("Error: Account number must not be null or empty.");

isValid = false;

if (balance < 0) {

System.out.println("Error: Balance cannot be negative.");

isValid = false;

if (isValid) {

this.accNo = accNo;

this.balance = balance;

System.out.println("\nAccount created successfully:");

printDetails();

} else {

System.out.println("Account creation failed due to invalid data.");

void printDetails() {

System.out.println("Account Number: " + accNo);

System.out.println("Balance: ₹" + balance);

public class AccountTest {


public static void main(String[] args) {

Accounts acc1 = new Accounts("A123456", 5000.00);

Accounts acc2 = new Accounts("", 2000.00);

Accounts acc3 = new Accounts("B7891011", -100.00);

Accounts acc4 = new Accounts(null, -50.00);

5a.sport extend

class Sports {

void play() {

System.out.println("Playing some sport...");

class Football extends Sports {

void play() {

System.out.println("Playing Football on the field.");

class Basketball extends Sports {

void play() {

System.out.println("Dribbling and shooting in Basketball.");

class Rugby extends Sports {

void play() {

System.out.println("Tackling hard in Rugby.");

}
}

public class SportsTest {

public static void main(String[] args) {

Sports s1 = new Football();

Sports s2 = new Basketball();

Sports s3 = new Rugby();

s1.play();

s2.play();

s3.play();

5b.book details

class Publisher {

String name;

Publisher(String name) {

this.name = name;

void display() {

System.out.println("Publisher: " + name);

class Book extends Publisher {

String title;

double price;
Book(String name, String title, double price) {

super(name);

this.title = title;

this.price = price;

void display() {

System.out.println("Book Title: " + title);

System.out.println("Price: ₹" + price);

System.out.println("Publisher: " + name);

public class BookTestjava {

public static void main(String[] args) {

Book b = new Book("Penguin Books", "The Alchemist", 399.00);

b.display();

6.calculate interest

interface Transaction {

void deposit(double amount);

void withdraw(double amount);

class SavingsAccount implements Transaction {

double balance;

SavingsAccount(double balance) {

this.balance = balance;
}

public void deposit(double amount) {

balance += amount;

System.out.println("Deposited ₹" + amount + " in Savings Account.");

public void withdraw(double amount) {

if (amount <= balance) {

balance -= amount;

System.out.println("Withdrew ₹" + amount + " from Savings Account.");

} else {

System.out.println("Insufficient balance in Savings Account.");

void displayBalance() {

System.out.println("Savings Account Balance: ₹" + balance);

class CurrentAccount implements Transaction {

double balance;

CurrentAccount(double balance) {

this.balance = balance;

public void deposit(double amount) {

balance += amount;

System.out.println("Deposited ₹" + amount + " in Current Account.");


}

public void withdraw(double amount) {

if ((balance - amount) >= 2000) {

balance -= amount;

System.out.println("Withdrew ₹" + amount + " from Current Account.");

} else {

System.out.println("Minimum balance of ₹2000 must be maintained in Current


Account.");

void displayBalance() {

System.out.println("Current Account Balance: ₹" + balance);

class RateOfInterest {

void calculateInterest(double balance, double rate, int years) {

double interest = (balance * rate * years) / 100;

System.out.println("Interest for ₹" + balance + " at " + rate + "% for " + years + " years is ₹" +
interest);

public class BankTest {

public static void main(String[] args) {

SavingsAccount sa = new SavingsAccount(5000);

sa.deposit(1000);

sa.withdraw(2000);

sa.displayBalance();
CurrentAccount ca = new CurrentAccount(6000);

ca.deposit(1500);

ca.withdraw(5000);

ca.displayBalance();

RateOfInterest roi = new RateOfInterest();

roi.calculateInterest(sa.balance, 4.0, 2);

roi.calculateInterest(ca.balance, 3.5, 2);

7.zipcode

import java.util.*;

class CityNotFoundException extends Exception {

CityNotFoundException(String message) {

super(message);

class City {

HashMap<Integer, String> cityMap;

City() {

cityMap = new HashMap<>();

cityMap.put(110001, "Delhi");

cityMap.put(400001, "Mumbai");

cityMap.put(560001, "Bangalore");

cityMap.put(700001, "Kolkata");

cityMap.put(600001, "Chennai");

}
void findCityByZipCode(int zip) throws CityNotFoundException {

if (cityMap.containsKey(zip)) {

System.out.println("City for ZIP " + zip + ": " + cityMap.get(zip));

} else {

throw new CityNotFoundException("City not found for ZIP: " + zip);

public class CityTest {

public static void main(String[] args) {

City city = new City();

try {

city.findCityByZipCode(560001);

city.findCityByZipCode(123456);

} catch (CityNotFoundException e) {

System.out.println(e.getMessage());

8.password

import java.util.Scanner;

public class PasswordValidator {

static boolean isValidPassword(String password) {

if (password.length() < 8)

return false;

int digitCount = 0;
for (char ch : password.toCharArray()) {

if (Character.isDigit(ch))

digitCount++;

if (digitCount < 2 || digitCount > 4)

return false;

return true;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

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

String password = sc.nextLine();

if (isValidPassword(password)) {

System.out.println("Password valid: " + password);

} else {

System.out.println("Password invalid.");

9.linkedlist

import java.util.LinkedList;

public class LinkedListDemo {

public static void main(String[] args) {

LinkedList<String> cities = new LinkedList<>();


cities.add("Delhi");

cities.add("Mumbai");

cities.add("Chennai");

cities.add("Kolkata");

System.out.println("Original List: " + cities);

String firstCity = cities.get(0);

System.out.println("First City: " + firstCity);

cities.set(1, "Bangalore");

System.out.println("After Update: " + cities);

cities.remove("Chennai");

System.out.println("After Removal: " + cities);

10.copy content

import java.io.*;

public class FileCopy {

public static void main(String[] args) {

try {

FileInputStream in = new FileInputStream("source.txt");

FileOutputStream out = new FileOutputStream("destination.txt");

int byteData;

while ((byteData = in.read()) != -1) {

out.write(byteData);

}
in.close();

out.close();

System.out.println("File copied successfully.");

} catch (IOException e) {

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

You might also like