0% found this document useful (0 votes)
63 views

WEEK 6 Ctood PDF

The document contains code snippets from different pages related to Java classes and inheritance. Page 104 defines an Arithmetic class with an add method and an Adder class that inherits from Arithmetic. Page 106 defines an Animal class with a walk method and a Bird class that inherits from Animal and defines fly and sing methods. Page 107 defines a Singleton class that ensures only one instance can be created. The remaining pages contain code for classes related to geometric shapes, people (with Student and Employee subclasses), bank accounts (with CheckingAccount subclass), and methods to get/set attributes and override toString.

Uploaded by

DHARAM TEJ D
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)
63 views

WEEK 6 Ctood PDF

The document contains code snippets from different pages related to Java classes and inheritance. Page 104 defines an Arithmetic class with an add method and an Adder class that inherits from Arithmetic. Page 106 defines an Animal class with a walk method and a Bird class that inherits from Animal and defines fly and sing methods. Page 107 defines a Singleton class that ensures only one instance can be created. The remaining pages contain code for classes related to geometric shapes, people (with Student and Employee subclasses), bank accounts (with CheckingAccount subclass), and methods to get/set attributes and override toString.

Uploaded by

DHARAM TEJ D
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/ 11

pg no 104 Q1

class Arithmetic {
public int add(int a, int b) {
return a + b;
}
}

class Adder extends Arithmetic {


// Adder inherits the add method from Arithmetic
}

pg no106 Q2
class Animal {
void walk() {
System.out.println("I am walking");
}
}

class Bird extends Animal {


void fly() {
System.out.println("I am flying");
}

void sing() {
System.out.println("I am singing");
}
}

public class Solution {


public static void main(String[] args) {
Bird bird = new Bird();
bird.walk();
bird.fly();
bird.sing();
}
}
output =I am walking
I am flying
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 class Triangle extends GeometricShape {


private double side1;
private double side2;
private double side3;

public Triangle() {
this(1.0, 1.0, 1.0);
}

public Triangle(double side1, double side2, double side3) {


this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}

public double getSide1() {


return side1;
}

public double getSide2() {


return side2;
}

public double getSide3() {


return side3;
}

public double getArea() {


double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}

public double getPerimeter() {


return side1 + side2 + side3;
}

public String toString() {


return "Triangle: side1 = " + side1 + " side2 = " + side2 + "
side3 = " + side3;
}

public static void main(String[] args) {


Scanner input = new Scanner(System.in);

System.out.print("Enter the length of side 1: ");


double side1 = input.nextDouble();

System.out.print("Enter the length of side 2: ");


double side2 = input.nextDouble();
System.out.print("Enter the length of side 3: ");
double side3 = input.nextDouble();

System.out.print("Enter the color: ");


String color = input.next();

System.out.print("Is the triangle filled (true or false)? ");


boolean filled = input.nextBoolean();

Triangle triangle = new Triangle(side1, side2, side3);


triangle.setColor(color);
triangle.setFilled(filled);

System.out.println("Area: " + triangle.getArea());


System.out.println("Perimeter: " + triangle.getPerimeter());
System.out.println("Color: " + triangle.getColor());
System.out.println("Filled: " + triangle.isFilled());
}
}

Q2 pg109

public class GeometricShape {


private String borderColor;
private boolean filled;

public GeometricShape() {
this.borderColor = "black";
this.filled = false;
}

public GeometricShape(String borderColor, boolean filled) {


this.borderColor = borderColor;
this.filled = filled;
}

public String getBorderColor() {


return borderColor;
}

public void setBorderColor(String borderColor) {


this.borderColor = borderColor;
}

public boolean isFilled() {


return filled;
}

public void setFilled(boolean filled) {


this.filled = filled;
}

public String toString() {


return "Border Color: " + borderColor + ", Filled: " + filled;
}
}

public class Rectangle extends GeometricShape {


private double length;
private double width;

public Rectangle() {
this.length = 1.0;
this.width = 1.0;
}

public Rectangle(double length, double width) {


this.length = length;
this.width = width;
}

public double getLength() {


return length;
}

public void setLength(double length) {


this.length = length;
}

public double getWidth() {


return width;
}

public void setWidth(double width) {


this.width = width;
}

public String toString() {


return super.toString() + ", Length: " + length + ", Width: " +
width;
}
}

public class Cuboid extends Rectangle {


private double height;

public Cuboid() {
this.height = 1.0;
}

public Cuboid(double length, double width, double height) {


super(length, width);
this.height = height;
}

public double getHeight() {


return height;
}

public void setHeight(double height) {


this.height = height;
}

public String toString() {


return super.toString() + ", Height: " + height;
}
}
Q2 pg111
import java.util.Date;

public class Person {


private String name;
private String address;
private String phone;
private String email;

public Person() {
this("", "", "", "");
}

public Person(String name, String address, String phone, String


email) {
this.name = name;
this.address = address;
this.phone = phone;
this.email = email;
}

public String getName() {


return name;
}

public String getAddress() {


return address;
}

public String getPhone() {


return phone;
}

public String getEmail() {


return email;
}

@Override
public String toString() {
return "Person: " + name;
}
}

public class Student extends Person {


public static final String FRESHMAN = "Freshman";
public static final String SOPHOMORE = "Sophomore";
public static final String JUNIOR = "Junior";
public static final String SENIOR = "Senior";
private String status;

public Student() {
this("", "", "", "", "");
}

public Student(String name, String address, String phone, String


email, String status) {
super(name, address, phone, email);
this.status = status;
}
public String getStatus() {
return status;
}

@Override
public String toString() {
return "Student: " + getName();
}
}

public class Employee extends Person {


private String office;
private double salary;
private Date dateHired;

public Employee() {
this("", "", "", "", "", 0.0, new Date());
}

public Employee(String name, String address, String phone, String


email, String office, double salary, Date dateHired) {
super(name, address, phone, email);
this.office = office;
this.salary = salary;
this.dateHired = dateHired;
}

public String getOffice() {


return office;
}

public double getSalary() {


return salary;
}

public Date getDateHired() {


return dateHired;
}

@Override
public String toString() {
return "Employee: " + getName();
}
}

public class Faculty extends Employee {


private String officeHours;
private String rank;

public Faculty() {
this("", "", "", "", "", 0.0, new Date(), "", "");
}

public Faculty(String name, String address, String phone, String


email, String office, double salary, Date dateHired, String officeHours,
String rank) {
super(name, address, phone, email, office, salary, dateHired);
this.officeHours = officeHours;
this.rank = rank;
}
public String getOfficeHours() {
return officeHours;
}

public String getRank() {


return rank;
}

@Override
public String toString() {
return "Faculty: " + getName();
}
}

public class Staff extends Employee {


private String title;

public Staff() {
this("", "", "", "", "", 0.0, new Date(), "");
}

public Staff(String name, String address, String phone, String email,


String office, double salary, Date dateHired, String title) {
super(name, address, phone, email, office, salary, dateHired);
this.title = title;
}

public String getTitle() {


return title;
}

@Override
public String toString() {
return "Staff: " + getName();

Q3 pg112

import java.util.Date;

public class Account {


private int accountNumber;
private double balance;
private double annualInterestRate;
private Date dateCreated;

public Account(int accountNumber, double balance, double


annualInterestRate) {
this.accountNumber = accountNumber;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
this.dateCreated = new Date();
}

public int getAccountNumber() {


return accountNumber;
}

public double getBalance() {


return balance;
}

public double getAnnualInterestRate() {


return annualInterestRate;
}

public Date getDateCreated() {


return dateCreated;
}

public double getMonthlyInterest() {


return (annualInterestRate / 12) * balance;
}

public void withdraw(double amount) {


balance -= amount;
}

public void deposit(double amount) {


balance += amount;
}

@Override
public String toString() {
return "Account #" + accountNumber + " Balance: $" + balance;
}
}

public class CheckingAccount extends Account {


private double overdraftLimit;

public CheckingAccount(int accountNumber, double balance, double


annualInterestRate, double overdraftLimit) {
super(accountNumber, balance, annualInterestRate);
this.overdraftLimit = overdraftLimit;
}

public double getOverdraftLimit() {


return overdraftLimit;
}

@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;

public class ArrayListExample {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Read the number of lines


int n = sc.nextInt();

// Create an ArrayList of ArrayLists to store the lines


ArrayList<ArrayList<Integer>> lines = new ArrayList<>();

// Read the lines and add them to the ArrayList


for (int i = 0; i < n; i++) {
int m = sc.nextInt();
ArrayList<Integer> line = new ArrayList<>();
for (int j = 0; j < m; j++) {
int num = sc.nextInt();
line.add(num);
}
lines.add(line);
}

// Read the number of queries


int q = sc.nextInt();

// Process the queries


for (int i = 0; i < q; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
if (y > lines.get(x - 1).size()) {
System.out.println("ERROR!");
} else {
System.out.println(lines.get(x - 1).get(y - 1));
}
}
}
}

Q5 pg114
class Member {
String name;
int age;
String phoneNumber;
String address;
double salary;

public void printSalary() {


System.out.println("Salary: " + salary);
}
}

class Employee extends Member {


String specialization;
}

class Manager extends Member {


String department;
}

public class Main {


public static void main(String[] args) {
Employee employee = new Employee();
employee.name = "John Doe";
employee.age = 30;
employee.phoneNumber = "123-456-7890";
employee.address = "123 Main St, Anytown USA";
employee.salary = 50000.0;
employee.specialization = "Software Engineer";

Manager manager = new Manager();


manager.name = "Jane Smith";
manager.age = 40;
manager.phoneNumber = "555-555-1212";
manager.address = "456 Elm St, Anytown USA";
manager.salary = 80000.0;
manager.department = "Engineering";

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);
}

class MyCalculator implements AdvancedArithmetic {


public int divisor_sum(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
sum += i;
}
}
return sum;
}
}

public class Main {


public static void main(String[] args) {
MyCalculator my_calculator = new MyCalculator();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(my_calculator.divisor_sum(n));
sc.close();
}
}

You might also like