Merge remote-tracking branch 'origin/main'

# Conflicts:
#	T3Q1-4/src/VendingMachine.java
This commit is contained in:
Marcus Vinicius de Carvalho 2025-03-27 13:59:03 +08:00
commit 42c22c106b
74 changed files with 668 additions and 3 deletions

View File

@ -18,8 +18,10 @@ public class VendingMachine {
public void start() {
// This is an infinite loop. This is quite common in applications that must run 24/7
do {
double amountInserted = 0.0;
selectDrink();
checkChange(insertCoins());
insertCoins();
checkChange(amountInserted);
printReceipt();
} while (true);
}
@ -28,7 +30,7 @@ public class VendingMachine {
drinkSelection = presentDrinkMenu();
}
private double insertCoins() {
private void insertCoins() {
double amount = 0.0;
Scanner sc = new Scanner(System.in);
@ -39,7 +41,6 @@ public class VendingMachine {
amount += processCoinAmount(coin);
System.out.printf("Coins inserted: %.2f \n", amount);
} while (amount < availableDrinks[drinkSelection].getPrice());
return amount;
}
private void checkChange(double amount)

11
T5Q1/T5Q1.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

5
T5Q1/src/ClassA.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassA {
public void print(int x, String y) {
System.out.println(x + " " + y);
}
}

5
T5Q1/src/ClassB.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassB extends ClassA {
public void print(int x) {
System.out.println(x);
}
}

5
T5Q1/src/ClassC.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassC extends ClassA {
public void print(String x, String y) {
System.out.println(x + " " + y);
}
}

5
T5Q1/src/ClassD.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassD extends ClassC {
public void print(String a, String b) {
System.out.println(a + " " + b);
}
}

5
T5Q1/src/ClassE.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassE extends ClassC {
public void print(String x) {
System.out.println(x);
}
}

5
T5Q1/src/ClassF.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassF extends ClassE {
public void print(int x) {
System.out.println(x);
}
}

5
T5Q1/src/ClassG.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassG extends ClassF {
public void print(String x) {
System.out.println(x);
}
}

11
T5Q1/src/Main.java Normal file
View File

@ -0,0 +1,11 @@
public class Main {
public static void main(String[] args) {
ClassF z = new ClassF();
z.print(9);
z.print(2, "Cx2002");
z.print("Object");
z.print("OODP", "Java");
// z.print("OODP", 2002); // Compilation error
}
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,5 @@
public class ClassA {
public void print(int x, String y) {
System.out.println(x + " " + y);
}
}

View File

@ -0,0 +1,5 @@
public class ClassB extends ClassA {
public void print(int x) {
System.out.println(x);
}
}

View File

@ -0,0 +1,3 @@
public abstract class ClassC extends ClassA {
public abstract void print(String x, String y);
}

View File

@ -0,0 +1,5 @@
public class ClassD extends ClassC {
public void print(String a, String b) {
System.out.println(a + " " + b);
}
}

View File

@ -0,0 +1,8 @@
public class ClassE extends ClassC {
public void print(String x) {
System.out.println(x);
}
public void print(String x, String y) {
System.out.println(x + " " + y);
}
}

View File

@ -0,0 +1,5 @@
public class ClassF extends ClassE {
public void print(int x) {
System.out.println(x);
}
}

View File

@ -0,0 +1,5 @@
public class ClassG extends ClassF {
public void print(String x) {
System.out.println(x);
}
}

View File

@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
ClassF z = new ClassF();
}
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,5 @@
public class ClassA {
public void print(int x, String y) {
System.out.println(x + " " + y);
}
}

View File

@ -0,0 +1,5 @@
public class ClassB extends ClassA {
public void print(int x) {
System.out.println(x);
}
}

View File

@ -0,0 +1,3 @@
public abstract class ClassC extends ClassA {
public abstract void print(String x, String y);
}

View File

@ -0,0 +1,5 @@
public class ClassD extends ClassC {
public void print(String a, String b) {
System.out.println(a + " " + b);
}
}

View File

@ -0,0 +1,6 @@
public abstract class ClassE extends ClassC {
public void print(String x) {
System.out.println(x);
}
public abstract void print(String x, String y);
}

View File

@ -0,0 +1,8 @@
public class ClassF extends ClassE {
public void print(int x) {
System.out.println(x);
}
public void print(String x, String y) {
System.out.println(x + " " + y);
}
}

View File

@ -0,0 +1,8 @@
public class ClassG extends ClassF {
public void print(String x) {
System.out.println(x);
}
public void print(String x, String y) {
System.out.println(x + " " + y);
}
}

View File

@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
}
}

11
T5Q2-A/T5Q2-A.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

5
T5Q2-A/src/ClassA.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassA {
public void print(int x, String y) {
System.out.println(x + " " + y);
}
}

5
T5Q2-A/src/ClassB.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassB extends ClassA {
public void print(int x) {
System.out.println(x);
}
}

3
T5Q2-A/src/ClassC.java Normal file
View File

@ -0,0 +1,3 @@
public abstract class ClassC extends ClassA {
public abstract void print(String x, String y);
}

5
T5Q2-A/src/ClassD.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassD extends ClassC {
public void print(String a, String b) {
System.out.println(a + " " + b);
}
}

5
T5Q2-A/src/ClassE.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassE extends ClassC {
public void print(String x) {
System.out.println(x);
}
}

5
T5Q2-A/src/ClassF.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassF extends ClassE {
public void print(int x) {
System.out.println(x);
}
}

5
T5Q2-A/src/ClassG.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassG extends ClassF {
public void print(String x) {
System.out.println(x);
}
}

5
T5Q2-A/src/Main.java Normal file
View File

@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
ClassF z = new ClassF();
}
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,5 @@
public class ClassA {
public void print(int x, String y) {
System.out.println(x + " " + y);
}
}

View File

@ -0,0 +1,5 @@
public class ClassB extends ClassA {
public void print(int x) {
System.out.println(x);
}
}

View File

@ -0,0 +1,3 @@
public abstract class ClassC extends ClassA {
public abstract void print(String x, String y);
}

View File

@ -0,0 +1,5 @@
public class ClassD extends ClassC {
public void print(String a, String b) {
System.out.println(a + " " + b);
}
}

View File

@ -0,0 +1,8 @@
public class ClassE extends ClassC {
public void print(String x) {
System.out.println(x);
}
public void print(String x, String y) {
System.out.println(x + " " + y);
}
}

View File

@ -0,0 +1,5 @@
public class ClassF extends ClassE {
public void print(int x) {
System.out.println(x);
}
}

View File

@ -0,0 +1,5 @@
public class ClassG extends ClassF {
public void print(String x) {
System.out.println(x);
}
}

View File

@ -0,0 +1,15 @@
public class Main {
public static void main(String[] args) {
// I - Works
// ClassC c = new ClassD();
// c.print("hello", "there");
// II - Does not compile because class C is abstract
// ClassA a = new ClassC();
// a.print(1, "there");
// III - Doesn't work - Class A does not have print(string, string) method
// ClassA a = new ClassF();
// a.print("hello", "there");
}
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,5 @@
public class ClassA {
public void print(int x, String y) {
System.out.println(x + " " + y);
}
}

View File

@ -0,0 +1,5 @@
public class ClassB extends ClassA {
public void print(int x) {
System.out.println(x);
}
}

View File

@ -0,0 +1,3 @@
public abstract class ClassC extends ClassA {
public abstract void print(String x, String y);
}

View File

@ -0,0 +1,5 @@
public class ClassD extends ClassC {
public void print(String a, String b) {
System.out.println(a + " " + b);
}
}

View File

@ -0,0 +1,6 @@
public abstract class ClassE extends ClassC {
public void print(String x) {
System.out.println(x);
}
public abstract void print(String x, String y);
}

View File

@ -0,0 +1,8 @@
public class ClassF extends ClassE {
public void print(int x) {
System.out.println(x);
}
public void print(String x, String y) {
System.out.println(x + " " + y);
}
}

View File

@ -0,0 +1,8 @@
public class ClassG extends ClassF {
public void print(String x) {
System.out.println(x);
}
public void print(String x, String y) {
System.out.println(x + " " + y);
}
}

View File

@ -0,0 +1,15 @@
public class Main {
public static void main(String[] args) {
// I - Works
ClassC c = new ClassD();
c.print("hello", "there");
// II - Does not compile because class C is abstract
// ClassA a = new ClassC();
// a.print(1, "there")
// III - Works
ClassF a = new ClassF();
a.print("hello", "there");
}
}

11
T5Q2-C/T5Q2-C.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

5
T5Q2-C/src/ClassA.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassA {
public void print(int x, String y) {
System.out.println(x + " " + y);
}
}

5
T5Q2-C/src/ClassB.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassB extends ClassA {
public void print(int x) {
System.out.println(x);
}
}

5
T5Q2-C/src/ClassC.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassC extends ClassA {
public void print(String x, String y) {
System.out.println(x + " " + y);
}
}

5
T5Q2-C/src/ClassD.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassD extends ClassC {
public void print(String a, String b) {
System.out.println(a + " " + b);
}
}

5
T5Q2-C/src/ClassE.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassE extends ClassC {
public void print(String x) {
System.out.println(x);
}
}

5
T5Q2-C/src/ClassF.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassF extends ClassE {
public void print(int x) {
System.out.println(x);
}
}

5
T5Q2-C/src/ClassG.java Normal file
View File

@ -0,0 +1,5 @@
public class ClassG extends ClassF {
public void print(String x) {
System.out.println(x);
}
}

32
T5Q2-C/src/Main.java Normal file
View File

@ -0,0 +1,32 @@
public class Main {
public static void main(String[] args) {
// I - Doesn't work because downcast needs to be explicitly declared
// ClassC c = new ClassD();
// ClassE e = c;
// II - Can´t upcast because they are from different family branches
// ClassB b = new ClassE();
// b.print("hello");
// III
// ClassA a = new ClassF(); // Ok
// a.print(12, "there"); // Ok
// a.print(88); // Compilation error
// IV -
// ClassA a = new ClassC(); // Ok
// ClassG g = (ClassG)a; // Runtime error
// g.print("hello");
// V - We lost track of the print(string, string) in C become it become a A first
// ClassA a = new ClassC(); // ok
// ClassG g = (ClassG)a; // Runtime error
// g.print("hello", "there");
// VI
// ClassA a = new ClassF(); // OK
// ClassC f = (ClassC)a; // Ok
// f.print(88, "there"); // Ok
}
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,9 @@
public class ClassA {
public void method1() {
System.out.println("ClassA.method1");
}
public void method2() {
System.out.println("ClassA.method2");
}
}

View File

@ -0,0 +1,9 @@
public class ClassB extends ClassA {
public void method2() {
System.out.println("ClassB.method2");
}
public void method3() {
System.out.println("ClassB.method3");
}
}

View File

@ -0,0 +1,9 @@
public class ClassC extends ClassB {
public void method3() {
System.out.println("ClassC.method3");
}
public void method4() {
System.out.println("ClassC.method4");
}
}

25
T5Q2-Marcus/src/Main.java Normal file
View File

@ -0,0 +1,25 @@
public class Main {
public static void main(String[] args) {
// I
// ClassA a = new ClassB();
// ClassB b = (ClassB) a;
// b.method2(); // ClassB.method2
// b.method1(); // ClassA.method1
// II
// ClassA c = new ClassB();
// ClassC d = (ClassC) c; // Runtime error here
// d.method3(); // Never executed
// III
// ClassA e = new ClassC();
// ClassB f = (ClassB) e;
// f.method3(); // ClassC.method3
// f.method2(); // ClassB.method2
// f.method1(); // ClassA.method1
// IV
// ClassC g = (ClassC) new ClassA(); // Runtime error
// g.method1(); // never executed
}
}

11
T5Q3/T5Q3.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

73
T5Q3/src/Polygon.java Normal file
View File

@ -0,0 +1,73 @@
//public abstract class Polygon {
public class Polygon {
public enum KindofPolygon {
POLY_PLAIN,
POLY_RECT,
POLY_TRIANG
};
protected String name;
protected float width;
protected float height;
protected KindofPolygon polytype;
public Polygon(String theName, float theWidth, float theHeight) {
name = theName;
width = theWidth;
height = theHeight;
polytype = KindofPolygon.POLY_PLAIN;
}
public KindofPolygon getPolytype() {
return polytype;
}
public void setPolytype(KindofPolygon value) {
polytype = value;
}
public String getName() {
return name;
}
public float calArea() {
return 0;
}
public void printWidthHeight( ) {
System.out.println("Width = " + width + " Height = " + height);
}
// public abstract float calArea();
}

10
T5Q3/src/Rectangle.java Normal file
View File

@ -0,0 +1,10 @@
public class Rectangle extends Polygon {
public Rectangle(String theName, float theWidth, float theHeight) {
super(theName, theWidth, theHeight) ;
this.polytype = KindofPolygon.POLY_RECT;
}
public float calArea() {
return width * height;
}
}

77
T5Q3/src/TestPolygon.java Normal file
View File

@ -0,0 +1,77 @@
public class TestPolygon {
public static void printArea(Rectangle rect) {
float area = rect.calArea();
System.out.println("The area of the " + rect.getPolytype() + " is " + area);
}
public static void printArea(Triangle tri) {
float area = tri.calArea();
System.out.println("The area of the " + tri.getPolytype() + " is " + area);
}
public static void main(String[] args ) {
Rectangle rect = new Rectangle("Rectangle", 3.0f, 4.0f);
printArea(rect); // static binding
rect.printWidthHeight();
Triangle triangle= new Triangle("Triangle", 3.0f, 4.0f);
printArea(triangle); // static binding
triangle.printWidthHeight();
}
public static void printArea(Polygon poly) {
float area = poly.calArea( );
System.out.println("The area of the " + poly.getPolytype() + " is " + area);
}
public static void main2(String[] args ) {
Rectangle rect = new Rectangle("Rectangle", 3.0f, 4.0f);
printArea(rect); // dynamic binding
rect.printWidthHeight();
Triangle triangle= new Triangle("Triangle", 3.0f, 4.0f);
printArea(triangle); // dynamic binding
triangle.printWidthHeight();
}
}

10
T5Q3/src/Triangle.java Normal file
View File

@ -0,0 +1,10 @@
public class Triangle extends Polygon {
public Triangle (String theName, float theWidth, float theHeight) {
super(theName, theWidth, theHeight) ;
this.polytype = KindofPolygon.POLY_TRIANG;
}
public float calArea() {
return 0.5f * width * height;
}
}