There are 4-5 pointers for improving this code for clean code, resusability, DRY, SOLID etc. Your junior comes to you with this code, what will you ask them to improve?
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
public class Program {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> evenNumbers = GetEvenNumbers(numbers);
System.out.println("Even Numbers: " + String.join(", ", evenNumbers.stream().map(String::valueOf).collect(Collectors.toList())));
}
public static List<Integer> GetEvenNumbers(List<Integer> numbers) {
List<Integer> evenNumbers = new ArrayList<>();
for (int number : numbers) {
if (number % 2 == 0) {
evenNumbers.add(number);
}
}
return evenNumbers;
}
public static int SumEvenNumbers(List<Integer> numbers) {
int sum = 0;
for (int i = 0; i < numbers.size(); i++) {
if (numbers.get(i) % 2 == 0) {
sum += numbers.get(i);
}
}
return sum;
}
}
Write a Java program that accepts a list of integers and returns the even numbers from the list. Also, return the sum of the even numbers.
import java.util.*;
import java.util.stream.Collectors;
public class EvenNumbers {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> evenNumbers = getEvenNumbers(numbers);
int sumEvenNumbers = sumEvenNumbers(numbers);
System.out.println("Even Numbers: " + evenNumbers);
System.out.println("Sum of Even Numbers: " + sumEvenNumbers);
}
private static List<Integer> getEvenNumbers(List<Integer> numbers) {
return numbers.stream().filter(n -> n % 2 == 0).collect(Collectors.toList());
}
private static int sumEvenNumbers(List<Integer> numbers) {
return numbers.stream().filter(n -> n % 2 == 0).mapToInt(Integer::intValue).sum();
}
}
public class Container
{
public int Value;
}
public class Program
{
static void Main()
{
int x = 10;
Container container = new Container();
container.Value = x;
x = 20;
container.Value = 30;
Console.WriteLine("x: " + x);
Console.WriteLine("container.Value: " + container.Value);
}
}
public class Program {
public static void main(String[] args) {
int x = 123;
Object obj = x; // Boxing
int y = (int)obj; // Unboxing
x = 456;
System.out.println("x: " + x);
System.out.println("obj: " + obj);
System.out.println("y: " + y);
}
}
class Person {
public String name;
}
public class Program {
static void modifyValue(int value) {
value = 100;
}
static void modifyReference(Person person) {
person.name = "Charlie";
}
public static void main(String[] args) {
int x = 50;
Person person = new Person();
person.name = "Alice";
modifyValue(x);
modifyReference(person);
System.out.println("x: " + x);
System.out.println("person.name: " + person.name);
}
}
class Person {
public String name;
}
public class Program {
public static void main(String[] args) {
Person person1 = new Person();
person1.name = "Alice";
Person person2 = person1;
person2.name = "Bob";
System.out.println("person1.name: " + person1.name);
System.out.println("person2.name: " + person2.name);
}
}
public class Program {
public static void main(String[] args) {
int a = 5;
int b = a;
b = 10;
System.out.println("a: " + a);
System.out.println("b: " + b);
}
}
public class Program
{
public static async Task Main()
{
Task<int> task = GetNumberAsync();
Console.WriteLine("Waiting for the task to complete...");
int result = await task;
Console.WriteLine($"Result: {result}");
}
public static async Task<int> GetNumberAsync()
{
await Task.Delay(2000); // Simulate a delay
return 42;
}
}
public class Program
{
public static async Task Main()
{
Task<int> task = GetNumberAsync();
Console.WriteLine("Waiting for the task to complete...");
int result = await task;
Console.WriteLine($"Result: {result}");
}
public static async Task<int> GetNumberAsync()
{
Console.WriteLine("called");
await Task.Delay(2000); // Simulate a delay
return 42;
}
}
write the javascript function to be added under script tag. The function should be triggered on click of "Update Heading" button and should update the h1 tag's html from "Hello, World" to "How are you?"
<!DOCTYPE html>
<html>
<head>
<title>InnerHTML Example</title>
</head>
<body>
<h1 id="myHeading">Hello, World!</h1>
<button onclick="updateHeading()">Update Heading</button>
<script>
//your code goes here
</script>
</body>
</html>
public class Factorial {
public static int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
public static void main(String[] args) {
int num = 5;
System.out.println("Factorial of " + num + " is " + factorial(num)); // Outputs 120
}
}
public class PalindromeCheck {
public static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j) {
if (str.charAt(i) != str.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
public static void main(String[] args) {
String str = "madam";
if (isPalindrome(str)) {
System.out.println(str + " is a palindrome");
} else {
System.out.println(str + " is not a palindrome");
}
}
}
public class PrimeNumber {
public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
public static void main(String[] args) {
int num = 29;
if (isPrime(num)) {
System.out.println(num + " is a prime number");
} else {
System.out.println(num + " is not a prime number");
}
}
}