From 11fd702f142e0dae87576fb12badc25f2808c97a Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Mon, 10 Aug 2026 16:34:38 +0300 Subject: [PATCH 1/4] added exercises in 6.3 --- source/ch6_definingclasses.ptx | 74 +++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index f2c8bf7..e7a99da 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -628,8 +628,80 @@ public class Fraction { - + + +

+ Rearrange the blocks to create a Fraction class that uses constructor overloading and chaining to perform subtraction. The subtract method should accept another Fraction object, and an overloaded constructor should easily convert an int into a Fraction so both types can be subtracted seamlessly. + + + + + public class Fraction { + private int num, den; + public Fraction(int num, int den) { + this.num = num; + this.den = den; + } + + + public class Fraction { + private int num, den; + public void Fraction(int num, int den) { + this.num = num; + this.den = den; + } + + + + + + public Fraction(int wholeNumber) { + this(wholeNumber, 1); + } + + + public Fraction(int wholeNumber) { + Fraction(wholeNumber, 1); + } + + + + + + public Fraction subtract(Fraction other) { + int newNum = this.num * other.den - other.num * this.den; + int newDen = this.den * other.den; + return new Fraction(newNum, newDen); + } + + + public Fraction subtract(Fraction other) { + int newNum = this.num * other.den - other.num * this.den; + int newDen = this.den * other.den; + return new Fraction(); + } + + + + + + public Fraction subtract(int number) { + return this.subtract(new Fraction(number)); + } + + + public Fraction subtract(int number) { + return this.subtract(Fraction(number)); + } + + + + + } + + +

From 2b73d6b70cf368fd3249fd3f95cb460ff6cf315a Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Mon, 10 Aug 2026 18:04:31 +0300 Subject: [PATCH 2/4] fixed placement --- source/ch6_definingclasses.ptx | 142 ++++++++++++++++----------------- 1 file changed, 70 insertions(+), 72 deletions(-) diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index e7a99da..941da7e 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -423,6 +423,76 @@ public Fraction(Integer num, Integer den) { + + +

+ Rearrange the blocks to create a Cat class with instance variables name and age. Implement a default constructor that initializes name to "Unknown" and age to 0. Finally, instantiate a Cat object in main and print its values. +

+
+ + + + public class Cat { + private String name; + private int age; + + + public class Cat { + private String name = "Unknown"; + private int age = 0; + + + + + + public Cat() { + this.name = "Unknown"; + this.age = 0; + } + + + public void Cat() { + this.name = "Unknown"; + this.age = 0; + } + + + + + + public static void main(String[] args) { + + + public void main(String[] args) { + + + + + + Cat myCat = new Cat(); + + + Cat myCat = Cat(); + + + + + + System.out.println("Name: " + myCat.name); + System.out.println("Age: " + myCat.age); + } + } + + + System.out.println("Name: " + Cat.name); + System.out.println("Age: " + Cat.age); + } + } + + + +
+
@@ -629,79 +699,7 @@ public class Fraction { - - -

- Rearrange the blocks to create a Fraction class that uses constructor overloading and chaining to perform subtraction. The subtract method should accept another Fraction object, and an overloaded constructor should easily convert an int into a Fraction so both types can be subtracted seamlessly. - - - - - public class Fraction { - private int num, den; - public Fraction(int num, int den) { - this.num = num; - this.den = den; - } - - - public class Fraction { - private int num, den; - public void Fraction(int num, int den) { - this.num = num; - this.den = den; - } - - - - - - public Fraction(int wholeNumber) { - this(wholeNumber, 1); - } - - - public Fraction(int wholeNumber) { - Fraction(wholeNumber, 1); - } - - - - - public Fraction subtract(Fraction other) { - int newNum = this.num * other.den - other.num * this.den; - int newDen = this.den * other.den; - return new Fraction(newNum, newDen); - } - - - public Fraction subtract(Fraction other) { - int newNum = this.num * other.den - other.num * this.den; - int newDen = this.den * other.den; - return new Fraction(); - } - - - - - - public Fraction subtract(int number) { - return this.subtract(new Fraction(number)); - } - - - public Fraction subtract(int number) { - return this.subtract(Fraction(number)); - } - - - - - } - - -

From 5f5742ad7bd8dba4d774348545d91f3f68f708d5 Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Mon, 10 Aug 2026 21:21:04 +0300 Subject: [PATCH 3/4] added a smaller question --- source/ch6_definingclasses.ptx | 58 ++++++++++++++++------------------ 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index 941da7e..916820a 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -423,75 +423,73 @@ public Fraction(Integer num, Integer den) { - + +

- Rearrange the blocks to create a Cat class with instance variables name and age. Implement a default constructor that initializes name to "Unknown" and age to 0. Finally, instantiate a Cat object in main and print its values. + Rearrange the blocks to construct a fully encapsulated BankAccount class. The class should feature: +

    +
  • A private double balance instance variable.
  • +
  • A public getter method getBalance() that returns the balance.
  • +
  • A public setter method setBalance(double balance) that updates the balance only if the value is non-negative ($ \ge 0 $).
  • +

- public class Cat { - private String name; - private int age; + public class BankAccount { + private double balance; - public class Cat { - private String name = "Unknown"; - private int age = 0; + public class BankAccount { + public double balance; - public Cat() { - this.name = "Unknown"; - this.age = 0; + public double getBalance() { + return balance; } - public void Cat() { - this.name = "Unknown"; - this.age = 0; + public void getBalance() { + return balance; } - public static void main(String[] args) { + public void setBalance(double balance) { - public void main(String[] args) { + public double setBalance(double balance) { - Cat myCat = new Cat(); + if (balance >= 0.0) { + this.balance = balance; + } + } - Cat myCat = Cat(); + if (balance >= 0.0) { + balance = balance; + } + } - - System.out.println("Name: " + myCat.name); - System.out.println("Age: " + myCat.age); - } - } - - - System.out.println("Name: " + Cat.name); - System.out.println("Age: " + Cat.age); - } - } - + }
+
From 732fbdfb230100adda7d2a56831dc781b90580fd Mon Sep 17 00:00:00 2001 From: Habiba Sorour Date: Mon, 10 Aug 2026 21:26:47 +0300 Subject: [PATCH 4/4] fixed math formatting --- source/ch6_definingclasses.ptx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index 916820a..f1dd27e 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -431,7 +431,7 @@ public Fraction(Integer num, Integer den) {
  • A private double balance instance variable.
  • A public getter method getBalance() that returns the balance.
  • -
  • A public setter method setBalance(double balance) that updates the balance only if the value is non-negative ($ \ge 0 $).
  • +
  • A public setter method setBalance(double balance) that updates the balance only if the value is non-negative ($ \ge 0 $).