
Concept explainers
JAVA
Using the code provided below called intSet create an iterator called sortedElements and the inner class. This will return an interator that will produce each element of the inset in ascending order.
- Specify and implement an iterator method called sortedElements for IntSet. The iterator method sortedElements returns an iterator that will produce each element of the intset in ascending order. The intset cannot be mutated when the iterator is in use.
Be sure to specify the iterator method before implementation. Do not forget to give the rep invariant and abstraction function for your implementation.
Note: IntSet.java has been provided. You can directly add in the code the specification, the implementation of the iterator method sortedElements, and the inner iterator class.
There is a segment of simple testing code for sortedElements in main() close to the end in the provided IntSet.java. The program will not compile until you have properly implemented sortedElements
//provided code and tester class
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* OVERVIEW: IntSets are unbounded, mutable sets of integers. A typical IntSet
* is {x1,...,xn}.
*/
public class IntSet {
/**
* EFFECTS: Constructor. Initializes this to be empty.
*/
public IntSet() {
els = new ArrayList<>();
}
/**
* MODIFIES: this
*
* EFFECTS: Adds x to the elements of this.
*/
public void insert(int x) {
if (getIndex(x) < 0) {
els.add(x);
}
}
/**
* MODIFIES: this
*
* EFFECTS: Removes x from this.
*/
public void remove(int x) {
int i = getIndex(x);
if (i < 0) {
return;
}
els.set(i, els.get(els.size() - 1));
els.remove(els.size() - 1);
}
/**
* EFFECTS: Returns true if x is in this; else returns false.
*/
public boolean isIn(int x) {
return getIndex(x) >= 0;
}
/**
* EFFECTS: Returns the cardinality of this.
*/
public int size() {
return els.size();
}
/**
* EFFECTS: If this is empty throws EmptyException; else returns an
* arbitrary element of this.
*/
public int choose() throws EmptyException {
if (els.isEmpty()) {
throw new EmptyException("IntSet.choose");
}
return els.get(0);
}
/**
* EFFECTS: Returns an iterator that produces all the elements of this (as
* Integers), each exactly once, in arbitrary order.
*
* REQUIRES: this must not be modified while the iterator is in use.
*/
public Iterator elements() {
return els.iterator();
}
/**
* EFFECTS: Returns an iterator that produces all the elements of this (as
* Integers), each exactly once, in arbitrary order.
*
* REQUIRES: this must not be modified while the iterator is in use.
*/
public Iterator elements2() {
return new IntSetGen(this);
}
// inner class
private static class IntSetGen implements Iterator {
IntSetGen(IntSet set) {
// REQUIRES: set != null
s = set;
n = 0;
}
@Override
public boolean hasNext() {
return n < s.els.size();
}
@Override
public Integer next() throws NoSuchElementException {
if (hasNext()) {
n++;
return s.els.get(n - 1);
} else {
throw new NoSuchElementException("IntSet.elements");
}
}
// unsupported
@Override
public void remove() {
throw new UnsupportedOperationException("IntSet.elements");
}
// The abstraction function for IntSet1Gen is:
// AF(c) = [c.s.els[n], c.s.els[n + 1], ...]
// The rep invariant for IntSet1Gen is
// I(c) = c.s != null && (0 <= c.n <= c.s.size)
private IntSet s; // the IntSet object being iterated
private int n; // the index of the next element in els to return
} // end IntSetGen
@Override
public IntSet clone() {
return new IntSet(els);
}
public boolean equals(IntSet s) {
if (s == null || els.size() != s.els.size()) {
return false;
}
for (var val : els) {
if (s.els.indexOf(val) == -1) {
return false;
}
}
return true;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof IntSet)) {
return false;
}
return equals((IntSet) o);
}
@Override
public String toString() {
if (els.isEmpty()) {
return "IntSet: { }";
}
String s = "IntSet: {" + els.get(0);
for (int i = 1; i < els.size(); i++) {
s = s + ", " + els.get(i);
}
return s + "}";
}
public boolean repOk() {
if (els == null) {
return false;
}
for (int i = 0; i < els.size(); i++) {
int x = els.get(i);
for (int j = i + 1; j < els.size(); j++) {
if (x == els.get(j)) {
return false;
}
}
}
return true;
}
/**
* REQUIRES: v is not null.
*
* EFFECTS: Private Constructor. Initializes this to have the same elements
* as those in v.
*/
private IntSet(ArrayList v) {
els = new ArrayList<>();
for (var val : v) {
els.add(val);
}
}
/**
* EFFECTS: If x is in this returns the index where x appears; else returns
* -1.
*/
private int getIndex(Integer x) {
for (int i = 0; i < els.size(); i++) {
if (x.equals(els.get(i))) {
return i;
}
}
return -1;
}
// A typical IntSet is {x1,...,xn}.
// The abstraction function is
// AF(c) = { c.els[i] | 0 <= i < c.els.size }
// The rep invariant is
// I(c) = c.els != null &&
// for all int i, j, 0 < i, j < c.els.size &&
// i != j => c.els[i] != c.els[j]
private ArrayList els; // the rep

Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 2 images

- 1. Divisors: In a class Divisors.java, read in a maximum integer n and use nested loops to print a list of divisors for each of 1 through n as shown in the following transcript. Sample transcript (input underlined): Largest integer? 100 1: 1 2: 1 2 3: 1 3 4: 1 2 4 95: 1 5 19 95 96: 1 2 3 4 6 8 12 16 24 32 48 96 97: 1 97 98: 1 2 7 14 49 98 99: 1 3 9 11 33 99 100: 1 2 4 5 10 20 25 50 100arrow_forwardCONSTRUCTOR// IntSet()// Pre: (none)// Post: The invoking IntSet is initialized to an empty// IntSet (i.e., one containing no relevant elements).// CONSTANT MEMBER FUNCTIONS (ACCESSORS)// int size() const// Pre: (none)// Post: Number of elements in the invoking IntSet is returned.// bool isEmpty() const// Pre: (none)// Post: True is returned if the invoking IntSet has no relevant// relevant elements, otherwise false is returned.// bool contains(int anInt) const// Pre: (none)// Post: true is returned if the invoking IntSet has anInt as an// element, otherwise false is returned.// bool isSubsetOf(const IntSet& otherIntSet) const// Pre: (none)// Post: True is returned if all elements of the invoking IntSet// are also elements of otherIntSet, otherwise false is// returned.// By definition, true is returned if the invoking IntSet// is empty (i.e., an empty IntSet…arrow_forward//asking this question again as the answer I was provided did not appear to follow the instructions of the question below Write a Java program that implements both Linear Search and Binary Search. The program willtake a collection of objects (generic type data) as input and print the number of comparisons neededto find a target element within that collection.You will create the following two Java classes: 1. SearchCombo.java : Code for both linearSearch and binarySearch will be in this class. Youmay take help from the textbook Chapter 9, Section 9.1. However, note that the design require-ments are different from the textbook code.•Both search methods must use the Comparable<T> interface and the compareTo() method.•Your program must be able to handle different data types, i.e., use generics.•For binarySearch, if you decide to use a midpoint computation formula that is different fromthe textbook, explain that formula briefly as a comment within your code. 2.…arrow_forward
- Create another class TestCarwhich creates array of n number of cars, uses appropriate set methods to set the values of parametersof every car. Display the details of each car. (If max speed is between 120 and 150 then category is“High speed” otherwise “Normal”) (JAVA)arrow_forwardHelparrow_forwardData structure & Algrothium java program Create the three following classes: 1. class containing two strings attributes first name and last name.2. class containing the name object and an ArrayList of string to store the list gifts. This class would extend the attached NicePersonInterface.java3. class containing one ArrayList of Names to store the naughty names. Another ArrayList of NicePerson to store the nice names and gifts. Add atleast 4 names in each list and display all information.arrow_forward
- True/False 6. Polymorphism literally means "many changes."arrow_forwardMatrix Multiplication Write a method called multiply in the Matrix class that takes an instance of Matrix as a parameter and returns the result of matrix multiplication. Complete the definition of the class Matrix and method definition multiply shown here and in the starter code. public class Matrix { public Matrix multiply(Matrix other) { // fill in code here } } For purposes of this exercise, you should assume (without checking) that the matrices to be multiplied have the correct shapes, ie, that this.numCols() is equivalent to other.numRows(). To multiply matrices, the number of columns in the first matrix must equal the number of rows in the second matrix. In general, if we multiply an m x n matrix A (ie, having m rows and n columns) by a n x p matrix B (ie, having n rows and p columns), the product will have size m x p (ie, m rows and p columns). Here is a simple illustration of the computation for two 2 x 2 matrices: Examples Example 1 If A and B were both instances of the…arrow_forwardJAVA Using the code provided below called intSet create an iterator called sortedElements and the inner class. This will return an interator that will produce each element of the inset in ascending order. Specify and implement an iterator method called sortedElements for IntSet. The iterator method sortedElements returns an iterator that will produce each element of the intset in ascending order. The intset cannot be mutated when the iterator is in use. Be sure to specify the iterator method before implementation. Do not forget to give the rep invariant and abstraction function for your implementation. Note: IntSet.java has been provided. You can directly add in the code the specification, the implementation of the iterator method sortedElements, and the inner iterator class. There is a segment of simple testing code for sortedElements in main() close to the end in the provided IntSet.java. The program will not compile until you have properly implemented sortedElements…arrow_forward
- Can you please help me with this, its in java. Thank you. Write classes in an inheritance hierarchy Implement a polymorphic method Create an ArrayList object Use ArrayList methods For this, please design and write a Java program to keep track of various menu items. Your program will store, display and modify salads, sandwiches and frozen yogurts. Present the user with the following menu options: Actions: 1) Add a salad2) Add a sandwich3) Add a frozen yogurt4) Display an item5) Display all items6) Add a topping to an item9) QuitIf the user does not enter one of these options, then display:Sorry, <NUMBER> is not a valid option.Where <NUMBER> is the user's input. All menu items have the following: Name (String) Price (double) Topping (StringBuilder or StringBuffer of comma separated values) In addition to everything that a menu item has, a main dish (salad or sandwich) also has: Side (String) A salad also has: Dressing (String) A sandwich also has: Bread type…arrow_forwardDesign a struct with following members: p_num_lots, int* type pa_lots, int [] type (dynamically allocated) Implement following methods: parameterized constructor which takes an int for num of lots copy constructor copy assignment operator destructorarrow_forwardTalking: Heba Abde QUESTION 1: Implement class myTime, this class should: 1) Encapsulate information about the hours (integer), minutes (integer), seconds (integer), AM PM (String) 2) Forbid invalid times, where the following criteria should be considered: - hours are in the range [1 – 12], default is 12. - minutes are in the range [0 – 59], default is 0. - seconds are in the range [0-59], default is 0. - AM PM can only be either “AM" or "PM", default is "AM" If any forbidden value entered, you should set the value to default value. 3) Two different constructors (default and parameterized) 4) A print function that will print the time as (hours : minutes : seconds PM/AM) Example: 02:45:30 PM 6) implement function ticktack, which adds one to the seconds, notice that if seconds is 59 and you add one, it should become 0 and the minutes should be incremented by one, also, if after incrementing minutes by one it becomes 60, it should be set to 0 and hours must be incremented by 1, if after…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





