This is a short set of problems using interfaces. You will implement two of the
ID: 3710332 • Letter: T
Question
This is a short set of problems using interfaces. You will implement two of the methods of a class called StringList that uses interfaces for performing various kinds of processing on text, as well as some sample classes that implement those interfaces. In all, you'll implement eight additional classes:
public StringList map(Transformation t) This method is already implemented. Returns a new StringList obtained by invoking the given Transformation's apply method to each string in this StringList. The apply method just takes a string and returns another string. public StringList filter(Selector selector) Returns a new StringList containing only the elements of this StringList for which the Selector's select method returns true. The select method just takes a string and returns true or false. public ArrayList mapToList(Converter converter) Converts each string in this StringList into an object of some type T, and returns them in an ArrayList. The Converter has one method, convert(), that takes a string and returns an object of the specified type T. public T reduce(Combiner combiner, T initialValue) This method is already implemented. Combines all strings in this StringList into a single value of some type T. A Combiner has one method, combine, that takes a given object of type T plus a string, and returns a value of type T.
package mini2;
import api.Combiner;
/**
* Combiner that appends the first letter of a string onto
* the accumulator. If the string is empty, returns the
* accumulator.
*/
public class FirstLetterCombiner implements Combiner<String>
{
// TODO
}
package mini2;
import api.Combiner;
/**
* Adds the length of the given string to the accumulator
* and returns the result.
*/
public class LengthCombiner implements Combiner<Integer>
{
// TODO
}
package mini2;
import api.Transformation;
/**
* Transformation whose <code>apply</code> method prepends a line number to each string.
* For a newly created LineNumberer, the line numbers start at 1 and increase on
* each successive call. The line number is left-justified in a field 5 spaces wide.
*/
public class LineNumberer implements Transformation
{
// TODO
}
package mini2;
import api.Selector;
/**
* Selector that returns false if the current string is
* within a javadoc comment, true otherwise. Using a NonJavadocSelector
* in the filter method of a StringList has the effect of removing all
* javadoc comments. Note that we are assuming that javadoc comments
* always start and end on different lines, and that no executable
* code ever appears on the same line as a javadoc comment.
*/
public class NonJavadocSelector implements Selector
{
// TODO
}
package mini2;
import api.Selector;
/**
* Selector whose <code>select</code> method returns false for strings whose first non-whitespace
* text is "//", and true for all other strings.
*/
public class NonLineCommentSelector implements Selector
{
// TODO
}
package mini2;
import java.awt.Point;
import java.util.Scanner;
import api.Converter;
import plotter.Polyline;
/**
* Converts a string into a <code>Polyline</code> object. The given
* string must conform to the format specified for one valid line of the file
* as described in Lab 8, checkpoint 2. See
* <pre>
* http://web.cs.iastate.edu/~cs227/labs/lab8/page12.html
* </pre>
*/
public class PolylineConverter implements Converter<Polyline>
{
// TODO
}
package mini2;
import api.Combiner;
/**
* Combiner whose <code>combine</code> method, given an Integer n and a string,
* returns n if the string is a blank line or a line containing
* a single curly brace; otherwise returns n + 1. (A curly brace on a line whose
* only other text is an end-of-line comment is treated as just a curly brace.)
* Using a SlocCounter in the reduce method has the general effect of counting
* the number of "source lines of code" that are actual program statements,
* assuming that line comments and javadoc comments have already been
* filtered out.
*/
public class SlocCounter implements Combiner<Integer>
{
// TODO
}
package mini2;
import api.Selector;
/**
* Selector that returns false for a strings that are
* empty, that are all whitespace, or whose first non-whitespace
* character is the '#' character.
*/
public class ValidLineSelector implements Selector
{
// TODO
}
Explanation / Answer
Below is your code. Please add import statements if missing ...
package mini2;
/**
* Combiner that appends the first letter of a string onto
* the accumulator. If the string is empty, returns the
* accumulator.
*/
public class FirstLetterCombiner implements Combiner<String> {
public String combine(String accumulator, String val){
if(val.length() == 0){
return accumulator;
}
return accumulator + val.charAt(0);
}
}
package mini2;
/**
* Adds the length of the given string to the accumulator
* and returns the result.
*/
public class LengthCombiner implements Combiner<Integer> {
public Integer combine(Integer accumulator,String val) {
return accumulator + val.length();
}
}
package mini2;
/**
* Transformation whose <code>apply</code> method prepends a line number to each string.
* For a newly created LineNumberer, the line numbers start at 1 and increase on
* each successive call. The line number is left-justified in a field 5 spaces wide.
*/
public class LineNumberer implements Transformation {
public String apply(String victim) {
String res = "";
String[] lines = victim.split(" ");
for (int i=0; i<lines.length; i++) {
res += (i+1) + ":" + lines[i];
}
return res;
}
}
package mini2;
/**
* Selector that returns false if the current string is
* within a javadoc comment, true otherwise. Using a NonJavadocSelector
* in the filter method of a StringList has the effect of removing all
* javadoc comments. Note that we are assuming that javadoc comments
* always start and end on different lines, and that no executable
* code ever appears on the same line as a javadoc comment.
*/
public class NonJavadocSelector implements Selector {
//Assuming a string in this case is a line
public boolean select(String line) {
if (line.length()<2) {
return true;
}
if(line.charAt(0) == '*' && line.charAt(1) == ' '){
return false;
}
if (line.equals("/**") || line.equals("/** ")) {
return false;
}
if (line.equals("*/") || line.equals("*/ ")) {
return false;
}
return true;
}
}
package mini2;
/**
* Selector whose <code>select</code> method returns false for strings whose first non-whitespace
* text is "//", and true for all other strings.
*/
public class NonLineCommentSelector implements Selector {
public boolean select(String line) {
line = line.trim();
return line.charAt(0) != '/' || line.charAt(1) != '/';
}
}
package mini2;
/**
* Converts a string into a <code>Polyline</code> object. The given
* string must conform to the format specified for one valid line of the file
* as described in Lab 8, checkpoint 2. See
* <pre>
* http://web.cs.iastate.edu/~cs227/labs/lab8/page12.html
* </pre>
*/
public class PolylineConverter implements Converter<Polyline> {
public Polyline convert(String val) {
Polyline res;
// TODO
// http://web.cs.iastate.edu/~cs227/labs/lab8/page12.html contains irrelevant info
return res;
}
}
package mini2;
/**
* Combiner whose <code>combine</code> method, given an Integer n and a string,
* returns n if the string is a blank line or a line containing
* a single curly brace; otherwise returns n + 1. (A curly brace on a line whose
* only other text is an end-of-line comment is treated as just a curly brace.)
* Using a SlocCounter in the reduce method has the general effect of counting
* the number of "source lines of code" that are actual program statements,
* assuming that line comments and javadoc comments have already been
* filtered out.
*/
public class SlocCounter implements Combiner<Integer>{
private boolean isCurly(String val) {
val = val.trim();
boolean c = val.charAt(0)=='{' || val.charAt(0)=='}';
if(c){
return !(val.charAt(1) == '/' && (val.charAt(2) == '/' || val.charAt(2) == '*'));
}
return true;
}
public Integer combine(Integer n, String val) {
if(val.trim().length()==0 || isCurly(val)){
return n;
}
return n+1;
}
}
package mini2;
/**
* Selector that returns false for a strings that are
* empty, that are all whitespace, or whose first non-whitespace
* character is the '#' character.
*/
public class ValidLineSelector implements Selector {
public boolean select(String val) {
val = val.trim();
if(val.length() == 0 || val.charAt(0) == '#'){
return false;
}
return true;
}
}
package api;
public interface Combiner<T> {
public T combine(T accumulator, String val);
}
package api;
public interface Converter<T> {
public T convert(String val);
}
package api;
public interface Selector {
public boolean select(String val);
}
package api;
public interface Transformation {
public String apply(String victim);
}
package mini2;
public class StringList{
private ArrayList<String> list;
public StringList(int size) {
list = new ArrayList<String>(size);
}
public StringList filter(Selector sel){
StringList res = new StringList(list.size());
for (String s : list) {
if(sel.select(s)){
res.list.add(s);
}
}
return res;
}
public <T> ArrayList<T> mapToList(Converter<T> converter) {
ArrayList<T> res = new ArrayList<T>(list.size());
for (String s : list) {
res.add(converter.convert(s));
}
return res;
}
public <T> T reduce(Combiner<T> combiner, T initialValue) {
T acc = initialValue;
for (String s : list) {
acc = combiner.combine(acc,s);
}
return acc;
}
//rest said to be already implemented
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.