Create a collection class StringCollection for an ArrayList<String> with a metho
ID: 3821105 • Letter: C
Question
Create a collection class StringCollection for an ArrayList<String> with a method boolean that contains (string s). The contains method iterats over the collection and return true if it contains the string s, or false if it not. In addition, you should support Add and Remove, but you can delegate to ArrayList for the implementation.
Did you use inheritance or composition to create your collection class? Why?
Refactor your collection class to use the other type of collection class change from composition to inheritance depending on how the first one is made
Create a builder class to create a Car object. Cars can have GPS, TurbchargedEngine, Color, TransmissionType (automatic or manual), and an optional moonroof. You only need to show the definition of the Car class and the CarBuilder class.
For example, if your car defined a field of type GPS, you don’t need to show the GPS class.
Create a program that continuously reads input from the user. The program will read the user input and get a certain type of Animal based on that input. The program should support Dogs, Cats, Bears, Horses, and Cows.
The Animal can then “talk”. The talking involves printing out to the console, but can say anything.
The animals (Dogs, Cats, Bears, etc.) and their interface will be in one package – “Zoo” – and should not be visible outside of the package. Only the Animal interface they all inherit from should be visible from outside the Zoo package.
The main method will be in a separate package. Call this one “main”.
Explanation / Answer
// stringCollection class
import java.util.*;
public class StringCollection {
List<String> chars = new ArrayList<String>();
public void addString(String string) {
boolean temp=true;
for(String a: chars){
if (a.equals(string)){
System.out.println("Already in the ArrayList");
temp= false;
}
}
if (temp){
chars.add(string);
System.out.println("Done adding");
}
}
public void removeString(String string) {
boolean temp=true;
for(String a: chars){
if (a.equals(string)){
chars.remove(string);
System.out.println("Done Removing");
temp= false;
}
}
if (temp){
System.out.println(string+" is not existed");
}
}
public boolean contains(String string) {
return chars.contains(string);
}
}
// End of String Collection Class
// Test Class
public class Test {
public static void main(String[] args) {
StringCollection sc= new StringCollection();
sc.addString("This is Test String 1");
sc.addString("This is Test String 2");
sc.addString("This is Test String 3");
sc.removeString("This is Test String 2");
sc.removeString("This is Test String 6");
System.out.println(sc.contains("This is Test String 1"));
}
}
//End of test Class
Sample output:
Done adding
Done adding
Done adding
Done Removing
This is Test String 6 is not existed
true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.