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

Comp 155 Assignment

Uploaded by

thetamish1111
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 views5 pages

Comp 155 Assignment

Uploaded by

thetamish1111
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/ 5

Q.

1
Code

interface Pet {

void displayDetails();

void feed(String food);


}

class Dog implements Pet {

String name;
String breed;
int age;

Dog(String name, String breed, int age) {


this.name = name;
this.breed = breed;
this.age = age;
}

public void displayDetails() {


System.out.println("Dog Name: " + name + ", Breed: " + breed + ",
Age: " + age);
}

public void feed(String food) {


System.out.println("Feeding " + name + " the dog some " + food);
}
}

class Cat implements Pet {

String name;
String color;
int age;

Cat(String name, String color, int age) {


this.name = name;
this.color = color;
this.age = age;
}

public void displayDetails() {


System.out.println("Cat Name: " + name + ", Color: " + color + ",
Age: " + age);
}
public void feed(String food) {
System.out.println("Feeding " + name + " the cat some " + food);
}
}

class Bird implements Pet {

String name;
String species;
int age;

Bird(String name, String species, int age) {


this.name = name;
this.species = species;
this.age = age;
}

public void displayDetails() {


System.out.println("Bird Name: " + name + ", Species: " + species +
", Age: " + age);
}

public void feed(String food) {


System.out.println("Feeding " + name + " the bird some " + food);
}
}

class PetCare {

void careForPet(Pet pet, String food) {


pet.displayDetails();
pet.feed(food);
}
}

public class Main {

public static void main(String[] args) {


Dog dog = new Dog("Timmy", "Golden Retriever", 3);
Cat cat = new Cat("Johnny", "Black", 2);
Bird bird = new Bird("Kuku", "Canary", 1);

PetCare petCare = new PetCare();


petCare.careForPet(dog, "Dog Food");
petCare.careForPet(cat, "Cat Food");
petCare.careForPet(bird, "Bird Seed");
}
}
output

Q.2

squid
creature 1
tentacles

BIG!
spout
creature 2

ocean-dwelling
creature 1 creature 2

ocean-dwelling
warm-blooded
creature 2
Q.3
1. They cannot access the name and age as they are marked as
private. But, the UndergraduateStudent class can call
the setAge method because it is public, allowing it to be accessed
by subclasses and other classes.
2.
public class UndergraduateStudent extends Student {
private int year;

public UndergraduateStudent(String name) {


super(name, 18);
this.year = 0;
}
}

3.
@Override
public void setAge(int age) {
super.setAge(age);
this.year++;
}

Q.4
Code
class ExceedsBalanceLimitException extends Exception {
public ExceedsBalanceLimitException(String message) {
super(message);
}
}

class AccountNotFoundException extends Exception {


public AccountNotFoundException(String message) {
super(message);
}
}

class InvalidDepositAmountException extends Exception {


public InvalidDepositAmountException(String message) {
super(message);
}
}

class Account {
private String accountID;
private double currentBalance;
private int transactionCount;

public Account(String accountID, double initialBalance) throws


AccountNotFoundException {
if (!validateAccountID(accountID)) {
throw new AccountNotFoundException("Account ID is invalid:
" + accountID);
}
this.accountID = accountID;
this.currentBalance = initialBalance;
this.transactionCount = 0;
}

private boolean validateAccountID(String accountID) {


return accountID != null && accountID.matches("\\d{8}");
}

public void addFunds(double amount) throws


InvalidDepositAmountException {
if (amount <= 0) {
throw new InvalidDepositAmountException("Deposit amount
must be positive: " + amount);
}
currentBalance += amount;
transactionCount++;
System.out.println("Added " + amount + ". Updated balance: "
+ currentBalance);
}
public void withdrawFunds(double amount) throws
ExceedsBalanceLimitException {
if (amount > currentBalance) {
throw new ExceedsBalanceLimitException("Withdrawal
denied. Current balance: " + currentBalance);
}
currentBalance -= amount;
transactionCount++;
System.out.println("Withdrew " + amount + ". Updated
balance: " + currentBalance);
}

public double getBalance() {


return currentBalance;
}

public int getTransactionCount() {


return transactionCount;
}
}

public class Main {


public static void main(String[] args) {
try {
Account account = new Account("87654321", 1200);
account.addFunds(300);
account.addFunds(-50);
account.withdrawFunds(100);
account.withdrawFunds(2000);
System.out.println("Final Balance: " + account.getBalance());
System.out.println("Total Transactions: " +
account.getTransactionCount());

} catch (AccountNotFoundException e) {
System.out.println(e.getMessage());
} catch (InvalidDepositAmountException e) {
System.out.println(e.getMessage());
} catch (ExceedsBalanceLimitException e) {
System.out.println(e.getMessage());
}
}
}
Output

You might also like