4.We converted an IntArrayBag to a generic ArrayBag<E>. During the conversion, d
ID: 3880824 • Letter: 4
Question
4.We converted an IntArrayBag to a generic ArrayBag<E>. During the conversion, does every occurrence of int get changed to E? Explain.
5.The original countOccurrences method tested for the occurrence of a target by using the boolean expression target == data[index]. What different boolean expression is used in the countOccurrences method of the bag of objects?
6.Suppose x and y are non-null references to two nodes. The data in each node is a non-null Object. Write two boolean expressions: (1) an expression that is true if x and y refer to exactly the same node, and (2) an expression that is true if the data from the x node is equal to the data from the y node. Use the equals method where appropriate.
7.Using the generic ArrayBag class, write one to three lines of Java to perform the following:
a)Create a ArrayBag of Location
b)Add a Location to the ArrayBag
c)Remove the location created in (b) from the ArrayBag
Explanation / Answer
Q 4. During the conversion, does every occurence of int get changed to E?
A. I am not going into details of generics but when you try to add an int, it will be converted into Integer (not E). Your primitive int will be converted to Integer object because generics don't support primitive types. The main agenda of using generics is that you can create a class which can accomodate any type of Object without having to explicitly declare or change anything. I guess this is the reason why you have switched from IntArrayBag (specific to primitive int) to ArrayBag<E> (generic in nature to accomodate any object).
Q 5. What different boolean expression is used in the countOccurrences method of the bag of objects?
A. I would suggest you to make use of equals method. I have implemented the same in below class. Please have a look.
Q 6. An expression that is true if x and y refer to exactly the same node
A. if(x == Y) { both are referring to the same node } else { both are not referring to the same node }
Q 6. An expression that is true if the data from the x node is equal to the data from the y node
A. if(x.equals(y)) { both are having the same data } else { both are not having the same data }
BELOW IS THE JAVA CODE FOR Q. 7
Since I don't know what all attributes Location class contains. I am creating a new Location class which contains city, state and country as attributes. If you are unable to follow then do leave your queries in the comment's section.
Location.java
public class Location {
private String city;
private String state;
private String country;
public Location(String city, String state, String country) {
this.city = city;
this.state = state;
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
// Overriding equals method because countOccurence method will be making use of it
// If you don't override equals method then countOccurence will not work as expected
@Override
public boolean equals(Object e) {
boolean status = false;
if(e instanceof Location) {
Location tempLocation = (Location) e;
if(tempLocation.getCity().equals(this.city)) {
if(tempLocation.getState().equals(this.state)) {
if(tempLocation.getCountry().equals(this.country)) {
status = true;
}
}
}
}
return status;
}
// Overriding toString method because it will print the data on the console in a human readable form
// This method is not mandatory to override. But if we don't override this method then default implementation of toString method will be used
// This method will prove useful.
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("City: "+this.getCity()+"; ");
sb.append("State: "+this.getState()+"; ");
sb.append("Country: "+this.getCountry());
return sb.toString();
}
}
----------------------------------
ArrayBag.java
public class ArrayBag<E> {
private int size = 10;
private int dataSize = 0;
private Object[] data = new Object[size];
public void add(E e) {
if(dataSize == size) {
addSize();
}
data[dataSize] = e;
dataSize += 1;
}
public boolean remove(E e) {
boolean status = false;
for(int i=0; i<dataSize; i++) {
if(data[i] == e) {
data[i] = null;
dataSize -= 1;
status = true;
}
}
return status;
}
public int countOccurences(E e) {
int count = 0;
for(int i=0; i<dataSize; i++) {
if(data[i].equals(e)) {
count += 1;
}
}
return count;
}
public void printBag() {
for(int i=0; i<dataSize; i++) {
System.out.println(data[i]);
}
}
private void addSize() {
Object[] tempData = new Object[size*2];
System.arraycopy(data, 0, tempData, 0, size);
data = tempData;
size *= 2;
}
}
---------------------------------
ArrayBagTest.java
public class ArrayBagTest {
public static void main(String[] args) {
ArrayBag<Location> locationBag = new ArrayBag<Location>();
Location location1 = new Location("San Francisco", "California", "US");
Location location2 = new Location("San Francisco", "California", "US");
Location location3 = new Location("Las Vegas", "Nevada", "US");
Location location4 = new Location("Las Vegas", "Nevada", "US");
Location location5 = new Location("Barcelona", "Catalonia", "Spain");
locationBag.add(location1);
locationBag.add(location2);
locationBag.add(location3);
locationBag.add(location4);
locationBag.add(location5);
System.out.println("Count of San Francisco : "+locationBag.countOccurences(location1));
System.out.println("Count of Las Vegas : "+locationBag.countOccurences(location4));
System.out.println("List of locations in the bag before removing");
locationBag.printBag();
System.out.println("Removing Barcelona from the list : "+locationBag.remove(location5));
System.out.println("List of locations in the bag after removing");
locationBag.printBag();
}
}
Q 7a - Create a ArrayBag of Location
ArrayBag<Location> locationBag = new ArrayBag<Location>();
Q 7b - Add a Location to the ArrayBag
Location location5 = new Location("Barcelona", "Catalonia", "Spain");
locationBag.add(location5);
Q 7c - Remove the location created in (b) from the ArrayBag
// System.out.println("Removing Barcelona from the list : "+locationBag.remove(location5));
locationBag.remove(location5); // This particular line will remove the location from the bad which was added in the above step
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.