0% found this document useful (0 votes)
19 views10 pages

Ex No 06

Uploaded by

VK LAKSHMANDEV
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)
19 views10 pages

Ex No 06

Uploaded by

VK LAKSHMANDEV
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
You are on page 1/ 10

JAVA PROGRAMMING

EX NO: 06

1)PROGRAM:

interface Book {
String getTitle();
String getAuthor();
boolean isAvailable();
void borrow();
void returnBook();
}

class main implements Book {


public String title;
public String author;
public boolean available;
public main(String title, String author) {
this.title = title;
this.author = author;
this.available = true;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public boolean isAvailable() {
return available;
}
public void borrow() {
if (isAvailable()) {
available = false;
System.out.println("borrowed the book: " + getTitle());
} else {
System.out.println("book not available.");
}
}
public void returnBook() {
if (!isAvailable()) {
available = true;
System.out.println("returned the book: " + getTitle());
} else {
System.out.println("book not borrowed.");
}
}
public static void main(String[] args) {
main book1 = new main("Java", "Dev");
System.out.println("Title: " + book1.getTitle());
System.out.println("Author: " + book1.getAuthor());
if (book1.isAvailable()) {
System.out.println("book available.");
book1.borrow();
}
book1.borrow();
book1.returnBook();
book1.returnBook();
}
}

OUTPUT:

2)PROGRAM:

import java.util.Random;

import java.util.Scanner;

interface PasswordGenerator {

String generatePassword(int length);

boolean validatePassword(String password);

abstract class BasePasswordGenerator implements PasswordGenerator {

protected static final char[] CHAR_SET =


"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();

protected Random random = new Random();

protected char getRandomCharacter() {


return CHAR_SET[random.nextInt(CHAR_SET.length)];

class PalindromicPasswordGenerator extends BasePasswordGenerator {

@Override

public String generatePassword(int length) {

if (length < 1) {

throw new IllegalArgumentException("Length must be at least 1.");

char[] password = new char[length];

int halfLength = length / 2;

for (int i = 0; i < halfLength; i++) {

char randomChar = getRandomCharacter();

password[i] = randomChar;

password[length - i - 1] = randomChar;

if (length % 2 != 0) {

password[halfLength] = getRandomCharacter();

return new String(password);

@Override

public boolean validatePassword(String password) {

int length = password.length();

for (int i = 0; i < length / 2; i++) {


if (password.charAt(i) != password.charAt(length - i - 1)) {

return false;

return true;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

PalindromicPasswordGenerator generator = new PalindromicPasswordGenerator();

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

int length = scanner.nextInt();

String password = generator.generatePassword(length);

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

if (generator.validatePassword(password)) {

System.out.println("The password is palindromic.");

} else {

System.out.println("The password is not palindromic.");

scanner.close();

OUTPUT:
3)PROGRAM:

interface Product {

String getProductDetails();

double calculatePriceAfterDiscount();

boolean isAvailable();

abstract class AbstractProduct implements Product {

protected String name;

protected double price;

protected int stockQuantity;

public AbstractProduct(String name, double price, int stockQuantity) {

this.name = name;

this.price = price;

this.stockQuantity = stockQuantity;

public String getProductDetails() {

return "Name: " + name + ", Price: $" + price + ", Stock: " + stockQuantity;

public boolean isAvailable() {

return stockQuantity > 0;

protected abstract double applyDiscount(double discountPercentage);


}

final class Electronics extends AbstractProduct {

private final String warrantyPeriod;

private static final double MAX_DISCOUNT = 15.0;

public Electronics(String name, double price, int stockQuantity, String warrantyPeriod) {

super(name, price, stockQuantity);

if (stockQuantity <= 0) {

throw new IllegalArgumentException("Stock must be greater than 0 for Electronics.");

this.warrantyPeriod = warrantyPeriod;

protected double applyDiscount(double discountPercentage) {

return price * Math.min(discountPercentage, MAX_DISCOUNT) / 100;

public double calculatePriceAfterDiscount() {

return price - applyDiscount(MAX_DISCOUNT);

@Override

public String getProductDetails() {

return super.getProductDetails() + ", Warranty: " + warrantyPeriod;

final class Clothing extends AbstractProduct {

private final String size;

private static final double MAX_DISCOUNT = 20.0;


public Clothing(String name, double price, int stockQuantity, String size) {

super(name, price, stockQuantity);

this.size = size;

protected double applyDiscount(double discountPercentage) {

return price * Math.min(discountPercentage, MAX_DISCOUNT) / 100;

public double calculatePriceAfterDiscount() {

return price - applyDiscount(MAX_DISCOUNT);

public boolean isAvailable() {

return stockQuantity >= 5;

@Override

public String getProductDetails() {

return super.getProductDetails() + ", Size: " + size;

final class Grocery extends AbstractProduct {

private final String expirationDate;

private static final double MAX_DISCOUNT = 5.0;

public Grocery(String name, double price, int stockQuantity, String expirationDate) {

super(name, price, stockQuantity);

this.expirationDate = expirationDate;
}

protected double applyDiscount(double discountPercentage) {

return price * Math.min(discountPercentage, MAX_DISCOUNT) / 100;

public double calculatePriceAfterDiscount() {

return price - applyDiscount(MAX_DISCOUNT);

public boolean isAvailable() {

return stockQuantity >= 10;

@Override

public String getProductDetails() {

return super.getProductDetails() + ", Expiration Date: " + expirationDate;

final class ShoppingCart {

private static final int MAX_CAPACITY = 10;

private final Product[] cart;

private int count;

public ShoppingCart() {

cart = new Product[MAX_CAPACITY];

count = 0;

public boolean addProduct(Product product) {


if (count < MAX_CAPACITY && product.isAvailable()) {

cart[count++] = product;

System.out.println("Product added to cart: " + product.getProductDetails());

return true;

} else {

System.out.println("Unable to add product to cart: " + product.getProductDetails());

return false;

public void displayCart() {

System.out.println("Shopping Cart Contents:");

if (count == 0) {

System.out.println("Cart is empty.");

} else {

for (int i = 0; i < count; i++) {

System.out.println(cart[i].getProductDetails());

public double calculateTotalPrice() {

double total = 0;

for (int i = 0; i < count; i++) {

total += cart[i].calculatePriceAfterDiscount();

return total;

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

ShoppingCart cart = new ShoppingCart();

Product laptop = new Electronics("Laptop", 1200.00, 10, "2 Years");

Product shirt = new Clothing("Shirt", 50.00, 8, "L");

Product apple = new Grocery("Apple", 2.00, 20, "2024-12-01");

cart.addProduct(laptop);

cart.addProduct(shirt);

cart.addProduct(apple);

cart.displayCart();

System.out.printf("Total Price after Discounts: $%.2f%n", cart.calculateTotalPrice());

OUTPUT:

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