Tutorial 9

This commit is contained in:
Marcus Vinicius de Carvalho 2025-04-07 16:22:02 +08:00
parent d50223dc4d
commit f07a1f702d
24 changed files with 369 additions and 0 deletions

11
T9Q1-A/T9Q1-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>

18
T9Q1-A/src/Main.java Normal file
View File

@ -0,0 +1,18 @@
public class Main {
public static <T> void printArray(T[] array) {
for (T element : array) {
System.out.print(element + " ");
}
System.out.println();
}
public static void main(String[] args) {
Integer[] intArray = {1, 2, 3, 4, 5};
String[] strArray = {"Hello", "World"};
Double[] doubleArray = {1.0, 2.0, 3.0, 4.0, 5.0};
printArray(intArray);
printArray(strArray);
printArray(doubleArray);
}
}

11
T9Q1-B/T9Q1-B.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>

22
T9Q1-B/src/Main.java Normal file
View File

@ -0,0 +1,22 @@
public class Main {
public static <T> void printArray(T[] array) {
for (T element : array) {
System.out.print(element + " ");
}
System.out.println();
}
public static void main(String[] args) {
Integer[] intArray = {1, 2, 3, 4, 5};
String[] strArray = {"Hello", "World"};
Double[] doubleArray = {1.0, 2.0, 3.0, 4.0, 5.0};
int[] intpArray = {1, 2, 3, 4, 5};
double[] doublepArray = {1, 2, 3, 4, 5};
printArray(intArray);
printArray(strArray);
printArray(doubleArray);
printArray(intpArray);
printArray(doublepArray);
}
}

11
T9Q1-C/T9Q1-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>

36
T9Q1-C/src/Main.java Normal file
View File

@ -0,0 +1,36 @@
public class Main {
public static <T> void printArray(T[] array) {
for (T element : array) {
System.out.print(element + " ");
}
System.out.println();
}
public static void printArray(int[] array) {
for (int element : array) {
System.out.print(element + " ");
}
System.out.println();
}
public static void printArray(double[] array) {
for (double element : array) {
System.out.print(element + " ");
}
System.out.println();
}
public static void main(String[] args) {
Integer[] intArray = {1, 2, 3, 4, 5};
String[] strArray = {"Hello", "World"};
Double[] doubleArray = {1.0, 2.0, 3.0, 4.0, 5.0};
int[] intpArray = {1, 2, 3, 4, 5};
double[] doublepArray = {1, 2, 3, 4, 5};
printArray(intArray);
printArray(strArray);
printArray(doubleArray);
printArray(intpArray);
printArray(doublepArray);
}
}

11
T9Q2-A/T9Q2-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>

21
T9Q2-A/src/Main.java Normal file
View File

@ -0,0 +1,21 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(10, 9, 8, 7, 6, 5, 4, 3, 2, 1);
// Sort in descending order
Collections.sort(numbers, Collections.reverseOrder());
System.out.println("Sorted in descending order: " + numbers);
// Shuffle the list
Collections.shuffle(numbers);
System.out.println("Shuffled list: " + numbers);
// Find max and min
System.out.println("Max: " + Collections.max(numbers));
System.out.println("Min: " + Collections.min(numbers));
}
}

11
T9Q2-B/T9Q2-B.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>

23
T9Q2-B/src/Main.java Normal file
View File

@ -0,0 +1,23 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList();
// Sort in descending order
Collections.sort(numbers, Collections.reverseOrder());
System.out.println("Sorted in descending order: " + numbers);
// Shuffle the list
Collections.shuffle(numbers);
System.out.println("Shuffled list: " + numbers);
numbers = Arrays.asList(null);
// Find max and min
System.out.println("Max: " + Collections.max(numbers));
System.out.println("Min: " + Collections.min(numbers));
}
}

11
T9Q3-A/T9Q3-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>

16
T9Q3-A/src/Main.java Normal file
View File

@ -0,0 +1,16 @@
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> words = Arrays.asList("Apple", "Banana", "Kiwi", "Orange", "Pear");
List<String> result = words.stream()
.filter(s -> s.length() >= 5) // Filter strings with length >= 5
.map(String::toUpperCase) // Convert to uppercase
.collect(Collectors.toList()); // Collect into a list
System.out.println(result);
}
}

11
T9Q3-B/T9Q3-B.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>

22
T9Q3-B/src/Main.java Normal file
View File

@ -0,0 +1,22 @@
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> words = Arrays.asList("Apple", "Banana", "Kiwi", "Orange", "Pear");
List<String> result = words.stream()
.filter(s -> s.length() >= 5) // Filter strings with length >= 5
.map(String::toUpperCase) // Convert to uppercase
.collect(Collectors.toList()); // Collect into a list
System.out.println(result);
long count = words.stream()
.filter(s -> s.length() >= 5)
.count();
System.out.println("Count: " + count);
}
}

11
T9Q4/T9Q4.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>

6
T9Q4/src/Main.java Normal file
View File

@ -0,0 +1,6 @@
public class Main {
public static void main(String[] args) {
Runnable r = () -> System.out.println("Hello, World");
r.run();
}
}

11
T9Q5/T9Q5.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>

14
T9Q5/src/Main.java Normal file
View File

@ -0,0 +1,14 @@
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = 0;
for (int num : numbers) {
if (num % 2 == 0) {
sum += num;
}
}
System.out.println("Sum of even numbers: " + sum);
}
}

11
T9Q6-B/T9Q6-B.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>

28
T9Q6-B/src/Main.java Normal file
View File

@ -0,0 +1,28 @@
interface Vehicle {
default void start() {
System.out.println("Vehicle started.");
}
}
interface Car {
default void start() {
System.out.println("Car started.");
}
}
class Jeep implements Car, Vehicle {
@Override
public void start() {
Car.super.start(); // or Vehicle.super.start()
}
}
public class Main {
public static void main(String[] args) {
Vehicle jeep1 = new Jeep();
jeep1.start();
Car jeep2 = new Jeep();
jeep2.start();
}
}

11
T9Q6/T9Q6.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>

19
T9Q6/src/Main.java Normal file
View File

@ -0,0 +1,19 @@
interface Vehicle {
default void start() {
System.out.println("Vehicle started.");
}
}
class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car started.");
}
}
public class Main {
public static void main(String[] args) {
Vehicle car = new Car();
car.start();
}
}

11
T9Q7/T9Q7.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>

12
T9Q7/src/Main.java Normal file
View File

@ -0,0 +1,12 @@
public class Main {
public static void main(String[] args) {
int day = 3;
String dayType = switch(day) {
case 1, 2, 3, 4, 5 -> "Weekday";
case 6, 7 -> "Weekend";
default -> "Invalid day";
};
System.out.println(dayType);
}
}