Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Question1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.ArrayList;
import java.util.List;

public class Question1 {

/*
* A function that takes an integer n and returns a list of all prime between 0 and n.
* The function takes one parameter n, which represents the end of the range.
*/
public List<Integer> primeNumbers(int n) {
List<Integer> primeNumbersList = new ArrayList<Integer>();

for (int i = 0; i <= n; i++) {
boolean isPrime = true;

// If the number n is less than 2, then it is not a prime number.
if (i < 2) isPrime = false;

for (int j = 2; j <= i / 2; j++) {
if (i % j == 0 && i != j) isPrime = false;
}

if (isPrime) primeNumbersList.add(i);
}

return primeNumbersList;
}
}

22 changes: 22 additions & 0 deletions Question2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.ArrayList;
import java.util.List;

public class Question2 {

public List<Integer> factorial(int n) {
List<Integer> factorialList = new ArrayList<Integer>();

int fact = 1;
for (int i = 0; i <= n; i++) {
if (i == 0) {
fact = 1;
factorialList.add(fact);
} else {
fact = fact * i;
factorialList.add(fact);
}
}

return factorialList;
}
}
16 changes: 16 additions & 0 deletions Question3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Question3 {
/*
* Match a case of type Int, String and float
*/
public boolean equals(Object o) {
if (o instanceof Integer) {
return true;
} else if (o instanceof String) {
return true;
} else if (o instanceof Float) {
return true;
}

return false;
}
}
12 changes: 12 additions & 0 deletions Question4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Question4 {
public String containsNumber(String s) {
String result = null;

if (s.matches(".*\\d+.*")) {
result = s.replaceAll("\\D+", "");
}

return "Some(" + result + ")";
}
}

26 changes: 26 additions & 0 deletions Question5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Person {

private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = Math.abs(age);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}