tasks.supelpawel
supelpawel
java
2 years ago
3.5 kB
3
Indexable
Never
// 1st task public class Main01 { public static void createPicture(int n) { if (n > 0) { int i; int j; int min; for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { min = i < j ? i : j; System.out.print(n - min + 1); } for (j = n - 1; j >= 1; j--) { min = i < j ? i : j; System.out.print(n - min + 1); } System.out.println(); } for (i = n - 1; i >= 1; i--) { for (j = 1; j <= n; j++) { min = i < j ? i : j; System.out.print(n - min + 1); } for (j = n - 1; j >= 1; j--) { min = i < j ? i : j; System.out.print(n - min + 1); } System.out.println(); } } else { throw new IllegalArgumentException("n must be positive"); } } public static void main(String[] args) { createPicture(4); } } // 2nd task import java.util.ArrayList; public class Main02 { public static ArrayList<Integer> findNPerfectNumbers(int n) { int sum; ArrayList<Integer> resultList = new ArrayList<>(); for (int i = 6; resultList.size() < n; i++) { ArrayList<Integer> factorList = new ArrayList<>(); for (int j = 1; j <= i / 2; j++) { if (i % j == 0) { factorList.add(j); } } sum = 0; for (Integer f : factorList) { sum += f; } if (sum == i) { resultList.add(i); } } return resultList; } public static void main(String[] args) { System.out.println(findNPerfectNumbers(4)); } } // 3rd task - Dto Class (Lombok dependency) import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; @Data @ToString @NoArgsConstructor @AllArgsConstructor public class PostDto { private long userId; private long id; private String title; private String body; } // 3rd task (Jackson dependency) import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.util.List; public class Main03 { private static final String POSTS_API_URL = "https://jsonplaceholder.typicode.com/posts"; public static void getPosts() throws IOException, InterruptedException { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .GET() .header("accept", "application/json") .uri(URI.create(POSTS_API_URL)) .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); ObjectMapper mapper = new ObjectMapper(); List<PostDto> posts = mapper.readValue(response.body(), new TypeReference<>() { }); posts.forEach(System.out::println); } public static void main(String[] args) throws IOException, InterruptedException { getPosts(); } }