Skip to content

Commit 1788408

Browse files
committed
Tutorial 04 & 05 Done ✅
Tutorial 04 Ex01 Ex02 Tutorial 05 Ex01 Q01 Ex01 Q02 Ex01 Q03
1 parent 21c4a2c commit 1788408

File tree

13 files changed

+435
-0
lines changed

13 files changed

+435
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Implement a class called Book with properties bookID, title, publisher which uses
2+
// the interfaces IDisplay and IInput
3+
4+
import java.util.Scanner;
5+
6+
public class Book implements IDisplay, IInput {
7+
Scanner input = new Scanner(System.in);
8+
private int bookID;
9+
private String title;
10+
private String publisher;
11+
12+
// default constructor
13+
public Book() {
14+
this.bookID = 0;
15+
this.title = "";
16+
this.publisher = "";
17+
}
18+
19+
public Book(int bookID, String title, String publisher) {
20+
this.bookID = bookID;
21+
this.title = title;
22+
this.publisher = publisher;
23+
}
24+
25+
public int getBookID() {
26+
return bookID;
27+
}
28+
29+
public void setBookID(int bookID) {
30+
this.bookID = bookID;
31+
}
32+
33+
public String getTitle() {
34+
return title;
35+
}
36+
37+
public void setTitle(String title) {
38+
this.title = title;
39+
}
40+
41+
42+
public String getPublisher() {
43+
return publisher;
44+
}
45+
46+
47+
public void setPublisher(String publisher) {
48+
this.publisher = publisher;
49+
}
50+
51+
@Override
52+
public void print() { // Print in one line
53+
System.out.println("Book ID: " + bookID + " Title: " + title + " Publisher: " + publisher);
54+
}
55+
56+
@Override
57+
public void printDetails() { // Print in multiple Lines
58+
System.out.println("Book ID: " + bookID);
59+
System.out.println("Title: " + title);
60+
System.out.println("Publisher: " + publisher);
61+
}
62+
63+
@Override
64+
public void input() {
65+
66+
System.out.println("Enter Book ID: ");
67+
bookID = input.nextInt();
68+
69+
System.out.println("Enter Title: ");
70+
title = input.next();
71+
72+
System.out.println("Enter Publisher: ");
73+
publisher = input.next();
74+
75+
}
76+
}
77+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
interface IDisplay {
2+
void print(); // Print in one line
3+
4+
void printDetails(); // Print in multiple Lines
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface IInput {
2+
void input();
3+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Create objects of the Book and Student in the main method
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
Book book1 = new Book(1, "Java Programming", "John Wiley & Sons");
6+
Book book2 = new Book(2, "C Programming", "Pearson");
7+
Book book3 = new Book(3, "C++ Programming", "Pearson");
8+
9+
Student student1 = new Student(1, "John");
10+
Student student2 = new Student(2, "Mary");
11+
Student student3 = new Student(3, "Peter");
12+
13+
// Create a variable of the IDisplay and IInput interfaces and call the
14+
// printDetails() and input() methods respectively of Book and Student objects
15+
16+
// For Book
17+
IDisplay displayBook = book1;
18+
displayBook.printDetails();
19+
IInput inputBook = book1;
20+
inputBook.input();
21+
22+
// For Student
23+
IDisplay displayStudent = student1;
24+
displayStudent.printDetails();
25+
IInput inputStudent = student1;
26+
inputStudent.input();
27+
28+
}
29+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Implement a class called Student with properties studentID, and name which uses the
2+
// interfaces IDisplay and IInput
3+
4+
import java.util.Scanner;
5+
6+
public class Student implements IDisplay, IInput {
7+
Scanner input = new Scanner(System.in);
8+
private int studentID;
9+
private String name;
10+
11+
// default constructor
12+
public Student() {
13+
this.studentID = 0;
14+
this.name = "";
15+
}
16+
17+
public Student(int studentID, String name) {
18+
this.studentID = studentID;
19+
this.name = name;
20+
}
21+
22+
public int getStudentID() {
23+
return studentID;
24+
}
25+
26+
public void setStudentID(int studentID) {
27+
this.studentID = studentID;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
@Override
39+
public void print() {
40+
System.out.println("Student ID: " + studentID + " Name: " + name);
41+
}
42+
43+
@Override
44+
public void printDetails() { // Print in multiple Lines
45+
System.out.println("Student ID: " + studentID);
46+
System.out.println("Name: " + name);
47+
}
48+
49+
@Override
50+
public void input() {
51+
52+
System.out.print("Enter Student ID: ");
53+
studentID = input.nextInt();
54+
55+
System.out.print("Enter Student Name: ");
56+
name = input.next();
57+
58+
59+
}
60+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Implement an abstract class called Account. Have the following properties accountNo,
2+
// name, balance.
3+
4+
public abstract class Account {
5+
protected int accountNo;
6+
protected String name;
7+
protected double balance;
8+
9+
// default constructor
10+
public Account() {
11+
this.accountNo = 0;
12+
this.name = "";
13+
this.balance = 0.0;
14+
}
15+
16+
public Account(int accountNo, String name, double balance) {
17+
this.accountNo = accountNo;
18+
this.name = name;
19+
this.balance = balance;
20+
}
21+
22+
public int getAccountNo() {
23+
return accountNo;
24+
}
25+
26+
public void setAccountNo(int accountNo) {
27+
this.accountNo = accountNo;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public void setName(String name) {
35+
this.name = name;
36+
}
37+
38+
public double getBalance() {
39+
return balance;
40+
}
41+
42+
public void setBalance(double balance) {
43+
this.balance = balance;
44+
}
45+
46+
47+
// Implement a Deposit() method to deposit money. The amount deposited should
48+
// update the balance.
49+
public void deposit(double amount) {
50+
this.balance += amount;
51+
}
52+
53+
// Have an abstract method called calculateInterest() which returns a double value
54+
public abstract double calculateInterest();
55+
56+
// Implement a method to display() the account details.
57+
public void display() {
58+
System.out.println("Account No: " + this.accountNo);
59+
System.out.println("Name: " + this.name);
60+
System.out.println("Balance: " + this.balance);
61+
}
62+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Implement a new class called FixedDepositAccount which extends the Account class
2+
// It should have a new property called interestRate and Interest.
3+
// Write a setter and getter for the interestRate.
4+
// Implement the calculateInterest() method assuming that the Balance has been
5+
// held for the entire year. interest = balance * interestRate/100;
6+
7+
public class FixedDepositAccount extends Account {
8+
protected double interestRate;
9+
protected double interest;
10+
11+
// default constructor
12+
public FixedDepositAccount() {
13+
super();
14+
interestRate = 0.0;
15+
interest = 0.0;
16+
}
17+
18+
// parameterized constructor
19+
public FixedDepositAccount(int accountNumber, String accountHolderName, double balance, double interestRate) {
20+
super(accountNumber, accountHolderName, balance);
21+
this.interestRate = interestRate;
22+
}
23+
24+
public void setInterestRate(double interestRate) {
25+
this.interestRate = interestRate;
26+
}
27+
28+
public double getInterestRate() {
29+
return interestRate;
30+
}
31+
32+
public double calculateInterest() {
33+
interest = super.getBalance() * interestRate / 100;
34+
return interest;
35+
}
36+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Main {
2+
public static void main(String[] args) {
3+
// Create objects from the FixedDeposit and SavingAccount. Call the Deposit() and withdraw() methods (Only SavingsAccount have withdrawals)
4+
5+
FixedDepositAccount fixedDepositAccount = new FixedDepositAccount(1, "John", 1000, 0.05);
6+
fixedDepositAccount.deposit(500);
7+
fixedDepositAccount.display();
8+
double interest = fixedDepositAccount.calculateInterest();
9+
System.out.println("Interest: " + interest);
10+
11+
SavingAccount savingAccount = new SavingAccount(2, "Jane", 1000, 0.05);
12+
savingAccount.deposit(500);
13+
savingAccount.withdraw(100);
14+
savingAccount.display();
15+
interest = savingAccount.calculateInterest();
16+
System.out.println("Interest: " + interest);
17+
}
18+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Implement a new class called SavingAccount which inherits the
2+
// FixedDepositAccount class
3+
// Implement a withdraw() method that allows you to withdraw money from the SavingsAccount.
4+
// Implement the calculateInterest() method assuming that the Balance has been held for the one Month. Interest = balance * interestRate/100/12;
5+
6+
class SavingAccount extends FixedDepositAccount {
7+
8+
// default constructor
9+
public SavingAccount() {
10+
super();
11+
}
12+
13+
// parameterized constructor
14+
public SavingAccount(int accountNumber, String accountHolderName, double balance, double interestRate) {
15+
super(accountNumber, accountHolderName, balance, interestRate);
16+
}
17+
18+
public void withdraw(double amount) {
19+
if (amount > super.getBalance()) {
20+
System.out.println("You can't withdraw more than your balance");
21+
} else {
22+
super.setBalance(super.getBalance() - amount);
23+
}
24+
}
25+
26+
public double calculateInterest() {
27+
double interest = super.getBalance() * interestRate / 100 / 12;
28+
return interest;
29+
}
30+
}
31+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Write a simple program that calculates the square root of a number (Math.sqrt()).
2+
// Identify the exception that can occur and write code to handle this using a try catch block.
3+
4+
5+
import java.util.InputMismatchException; // Import InputMismatchException
6+
import java.util.Scanner; // Import the Scanner class
7+
8+
public class Main { // Main class
9+
10+
public static void main(String[] args) { // main method
11+
12+
13+
Scanner input = new Scanner(System.in); // Create a scanner object
14+
15+
try {
16+
System.out.print("Enter a number: "); // Prompt the user to enter a number
17+
double num = input.nextDouble(); // Store the number entered by the user
18+
19+
double sqrt = Math.sqrt(num); // Calculate the square root of the number
20+
21+
if(Double.isNaN(sqrt)) { // If the number is not a number
22+
throw new ArithmeticException("Answer unreal"); // Throw an exception
23+
}
24+
25+
System.out.println("The square root of " + num + " is " + sqrt); // Print the answer
26+
}
27+
28+
catch (InputMismatchException e) { // If the user enters a non-numeric value
29+
System.out.println(e); // Print the exception
30+
System.out.println("Invalid input"); // Print an error message
31+
}
32+
33+
catch (ArithmeticException e) { // If the number is not a number
34+
System.out.println(e.getMessage()); // Print the exception
35+
}
36+
37+
input.close(); // Close the scanner object
38+
}
39+
}

0 commit comments

Comments
 (0)