WEEK 6 Ctood PDF
WEEK 6 Ctood PDF
WEEK 6 Ctood PDF
class Arithmetic {
public int add(int a, int b) {
return a + b;
}
}
pg no106 Q2
class Animal {
void walk() {
System.out.println("I am walking");
}
}
void sing() {
System.out.println("I am singing");
}
}
pg 107 Q3
class Singleton:
__instance = None
instance_variable = "Hello, I am a singleton!"
def __init__(self):
if Singleton.__instance is not None:
raise Exception("Only one instance of Singleton class is
allowed")
else:
Singleton.__instance = self
@staticmethod
def get_single_instance():
if Singleton.__instance is None:
Singleton()
return Singleton.__instance
post lab
Q1 pg108
import java.util.Scanner;
public Triangle() {
this(1.0, 1.0, 1.0);
}
Q2 pg109
public GeometricShape() {
this.borderColor = "black";
this.filled = false;
}
public Rectangle() {
this.length = 1.0;
this.width = 1.0;
}
public Cuboid() {
this.height = 1.0;
}
public Person() {
this("", "", "", "");
}
@Override
public String toString() {
return "Person: " + name;
}
}
public Student() {
this("", "", "", "", "");
}
@Override
public String toString() {
return "Student: " + getName();
}
}
public Employee() {
this("", "", "", "", "", 0.0, new Date());
}
@Override
public String toString() {
return "Employee: " + getName();
}
}
public Faculty() {
this("", "", "", "", "", 0.0, new Date(), "", "");
}
@Override
public String toString() {
return "Faculty: " + getName();
}
}
public Staff() {
this("", "", "", "", "", 0.0, new Date(), "");
}
@Override
public String toString() {
return "Staff: " + getName();
Q3 pg112
import java.util.Date;
@Override
public String toString() {
return "Account #" + accountNumber + " Balance: $" + balance;
}
}
@Override
public void withdraw(double amount) {
if (amount > (balance + overdraftLimit)) {
System.out.println("Insufficient funds.");
} else {
balance -= amount;
}
}
@Override
public String toString() {
return "Checking Account #" + getAccountNumber() + " Balance: $"
+ getBalance();
}
}
public class SavingsAccount extends Account {
public SavingsAccount(int accountNumber, double balance, double
annualInterestRate) {
super(accountNumber, balance, annualInterestRate);
}
@Override
public void withdraw(double amount) {
if (amount > balance) {
System.out.println("Insufficient funds.");
} else {
balance -= amount;
}
}
@Override
public String toString() {
return "Savings Account #" + getAccountNumber() + " Balance: $" +
getBalance();
}
}
Q4 pg113
import java.util.ArrayList;
import java.util.Scanner;
Q5 pg114
class Member {
String name;
int age;
String phoneNumber;
String address;
double salary;
System.out.println("Employee Details:");
System.out.println("Name: " + employee.name);
System.out.println("Age: " + employee.age);
System.out.println("Phone Number: " + employee.phoneNumber);
System.out.println("Address: " + employee.address);
employee.printSalary();
System.out.println("Specialization: " + employee.specialization);
System.out.println();
System.out.println("Manager Details:");
System.out.println("Name: " + manager.name);
System.out.println("Age: " + manager.age);
System.out.println("Phone Number: " + manager.phoneNumber);
System.out.println("Address: " + manager.address);
manager.printSalary();
System.out.println("Department: " + manager.department);
}
}
Q6 pg 115
import java.util.Scanner;
interface AdvancedArithmetic {
int divisor_sum(int n);
}