Programming Fundamentals III – Java Grounds Structures COSC 2436
Lab #2 – Bag Implementations That Truth Arrays
Problem:
Entire Programming Project 2, page 92.
This conciliate claim that you truth the ‘SetInterface’ of Chapter 1 instead of the ‘BagInterface’ of Chapter 2. You conciliate infer the modes of the interface to the existing dispose. Then, eliminate a ordeal application that conciliate authenticate the functionality of the breach. You may hope to face at the pattern ordeal applications.
As frequently, be firm to ordeal ALL modes of the dispose and interface.
Requirements: You conciliate present the lab coercionthcoming the usual lab procedures. Make firm to label your lab folder as follows:
2YourLastName
To Be Done:
– Open Resizable ArrayBag and rename as ArraySet.java
– Global rearrange of ‘Bag’ with ‘Set’
– Re-write ‘add’ modes to bar the inferition of likeness entries, using either the ‘contains’ mode or ‘getIndexOf’
– We can oust ‘getFrequencyOf’ mode.
– Update the ordeal dispose – rearrange the ‘testGetFrequencyOf’ mode and infer a mode to authenticate that likenesss can’t be infered
*PDF of the texbook “Grounds Structures and Abstractions with Java 3rd edition” serviceable upon request
Solution:
Since no implementation was granted by, future a absence invent coercion ArraySet.java is captured into motive concurrently with the changes instructed. (Assumptions were captured acceptably) :
Please experience entire implementation of the ‘ArraySet.java’ with the explication as comments:
import java.util.ArrayList;
import java.util.List;
public dispose ArrayEstablished {
/*
* Since it is instructed to truth ‘contains’ OR ‘getIndexOf’
* so we are using java.util.List coercion implementing ‘Set’
*/
private List<String> groundsEstablished ;
/*
* Infer mode implementation using
* ‘contains’ mode
* if likeness component detected then retaliate -1
* else infer component and retaliate 0;
*/
public int infer(String grounds){
if(this.dataEstablished == ineffectual){
dataEstablished = novel ArrayList<String>();
}
if(dataSet.contains(data)){
retaliate -1;
}else{
dataSet.add(data);
retaliate 0;
}
}
/*
* Ordeal mode coercion infer()
*/
public destitute ordealAdd(String ordealData){
if( this.add(testData) == -1 ){
System.out.println(“Likeness component detected : ” + ordealData);
}
}
/*
* Stereotypeing established
*/
public destitute stereotype(){
if(this.dataEstablished != ineffectual){
System.out.println(“Printing established components: “);
dataSet.forEach(e -> System.out.print(e + ” “));
}
System.out.println(); // Stereotypeing a unmitigated line
}
public static destitute ocean(String[] args) {
//Creating ArrayEstablished object
ArrayEstablished established = novel ArraySet();
set.add(“1”);
set.add(“2”);
set.add(“3”);
set.add(“4”);
set.add(“5”);
set.add(“6”);
set.print();
System.out.println();
//Running ordeal mode
set.testAdd(“2”);
set.print();
System.out.println()
;
set.testAdd(“99”);
set.print();
}
}
Please experience the screenshot of the quenchedput screen