In: Computer Science
Question 1
A substring of String s is a sequence of k >= 0 characters in s, in the order in which they occur in s.
The letters in a substring may be either contiguous (next to each other) or non-contiguous in the original String s.
For instance, these are the substrings of String s="abc".
String s All substrings of s
======== ==================================
"abc" "" "a" "b" "ab" "c" "ac" "bc" "abc"
Write a function allSubstrings that returns an ArrayList containing all the substrings (contiguous or non-contiguous) of a String parameter s, and test it on the String "abcde".
// Return all substrings of the String s
public static ArrayList allSubstrings(String s) { ... }
Question 2
Implement a function that reads numbers in from a file with one number per line and outputs all the possible sums that can be formed by subsets of the numbers. For instance, if the numbers in the file are 1 2 4, then the output would be 0, 1, 2, 4, 3, 5, 6, 7. Note that 0 is in the output because it uses none of the numbers, while 7 is the sum of all of the numbers.
// Return all sums that can be formed from subsets of elements in arr
public static ArrayList allSums( ArrayList arr ) { ... }
q1.java
package a1;
/**
* Name:
* Student ID:
* Description of solution:
*/
import static org.junit.Assert.*;
public class Q3 {
// Return the number of ways to choose a subset of k distinct elements from a
set of n elements
public static int C( int n, int k ) {
return 0;
}
public static void main(String[] args) {
System.out.println("Testing...");
assertEquals(C(14,3), 364);
assertEquals(C(14,11), 364);
assertEquals(C(18,8), 43758);
System.out.println("Success!");
}
}
q2.java
package a1;
/**
* Name:
* Student ID:
* Description of solution:
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import static org.junit.Assert.*;
public class Q2 {
// Return all sums that can be formed from subsets of elements in arr
public static ArrayList allSums( ArrayList arr ) {
return null;
}
public static void main(String[] args) {
//https://www.baeldung.com/java-file-to-arraylist
ArrayList result = new ArrayList();//=
Files.readAllLines(Paths.get("nums.txt"));
try {
BufferedReader br = new BufferedReader(new
FileReader("nums.txt"));
while (br.ready()) {
result.add(Integer.getInteger(br.readLine()));
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
ArrayList sums = allSums(result);
System.out.println("Testing...");
assertEquals(sums.size(), 8);
assertEquals(sums.contains(0), true);
assertEquals(sums.contains(1), true);
assertEquals(sums.contains(2), true);
assertEquals(sums.contains(4), true);
assertEquals(sums.contains(3), true);
assertEquals(sums.contains(5), true);
assertEquals(sums.contains(6), true);
assertEquals(sums.contains(7), true);
System.out.println("Success!");
}
}
q3.java
package a1;
/**
* Name:
* Student ID:
* Description of solution:
*/
import java.util.ArrayList;
import static org.junit.Assert.*;
public class Q1 {
// Return all substrings of the String s
public static ArrayList allSubstrings(String s){
return null;
}
public static void main(String[] args) {
ArrayList s = allSubstrings("abcde");
System.out.println("Testing...");
assertEquals(s.size(), 32);
assertEquals(s.contains(""), true);
assertEquals(s.contains("abcd"), true);
assertEquals(s.contains("abce"), true);
assertEquals(s.contains("abcde"), true);
System.out.println("Success!");
}
}
nums.txt
1
2
4
package a1; /** * Name: * Student ID: * Description of solution: */ import java.util.ArrayList; import static org.junit.Assert.*; public class Q1 { // Return all substrings of the String s public static ArrayList<String> allSubstrings(String s) { ArrayList<String> result = new ArrayList<>(); if(s.isEmpty()) { result.add(""); return result; } ArrayList<String> subResult = allSubstrings(s.substring(0, s.length() - 1)); char c = s.charAt(s.length() - 1); result.addAll(subResult); for(String x: subResult) { result.add(x + c); } return result; } public static void main(String[] args) { ArrayList s = allSubstrings("abcde"); System.out.println("Testing..."); assertEquals(s.size(), 32); assertEquals(s.contains(""), true); assertEquals(s.contains("abcd"), true); assertEquals(s.contains("abce"), true); assertEquals(s.contains("abcde"), true); System.out.println("Success!"); } } Answering your first question, Since both are different question and require significant efforts on both.. Please ask them separately. ************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.