Tutorial 9
This commit is contained in:
parent
d50223dc4d
commit
f07a1f702d
|
@ -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>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -0,0 +1,6 @@
|
|||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Runnable r = () -> System.out.println("Hello, World");
|
||||
r.run();
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue