Question

Intro to java project template with instructions below * IntegerSet.java */ /** * * @author StudentName...

Intro to java project

template with instructions below


* IntegerSet.java
*/

/**
*
* @author StudentName
*/
public class IntegerSet {
  
/**
* Creates a new instance of IntegerSet   
*/
// TODO: implement the constructor
  
/**
* Return a new IntegerSet containing the union of the two IntegerSet objects
* passed as arguments
*/
// TODO: implement the union method
  
/**
* Return a new IntegerSet containing the intersection of the two IntegerSet objects
* passed as arguments
*/
// TODO: implement the intersection method
  
/**
* Inserts an element into the IntegerSet by setting the corresponding
* value within the set array to true. Returns false if the value was out
* of range and true otherwise.
*/
// TODO: implement the insertElement method
  
/**
* Deletes an element from the IntegerSet by setting the corresponding
* value within the set array to false. Returns false if the value was out
* of range and true otherwise.
*/
// TODO: implement the deleteElement method
  
/**
* @Override the toString method in the Object class
* Displays the integers contained by the IntegerSet separated by spaces.
* An empty set should be displayed as:
* { --- }
* An integer set containing 5 and 10 should be displayed as:
* { 5 10 }
*/
// TODO: implement the toString method   
  
/**
* Returns true iff the current IntegerSet contains the same integers as
* the IntegerSet supplied as an argument
*/
   // TODO: implement the isEqualTo method   
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

IntegerSet.java

public class IntegerSet {
private static final int MAX_SIZE = 101;
private int[] set;
  
public IntegerSet()
{
this.set = new int[MAX_SIZE];
}
  
public IntegerSet(IntegerSet copy)
{
this.set = new int[MAX_SIZE];
for(int i = 0; i < set.length; i++)
this.set[i] = copy.set[i];
}
  
public IntegerSet union(IntegerSet other)
{
IntegerSet result = new IntegerSet(this);
for(int i = 0; i < set.length; i++)
{
if(other.isIntegerSet(i))
result.insert(i);
}
return result;
}
  
public IntegerSet intersection(IntegerSet other)
{
IntegerSet result = new IntegerSet(this);
for(int i = 0; i < set.length; i++)
{
if(!other.isIntegerSet(i))
result.delete(i);
}
return result;
}
  
public boolean insert(int num)
{
if(num >= 0 && num < set.length)
{
set[num] = 1;
return true;
}
return false;
}
  
public boolean delete(int num)
{
if(num >= 0 && num < set.length)
{
set[num] = 0;
return true;
}
return false;
}
  
public boolean isIntegerSet(int num)
{
return(set[num] == 1);
}
  
public boolean isEmpty()
{
for(int i = 0; i < set.length; i++)
{
if(isIntegerSet(i))
return false;
}
return true;
}
  
@Override
public String toString()
{
String out = "Set: [";
if(isEmpty())
out += "---";
for(int i = 0; i < set.length; i++)
{
if(isIntegerSet(i))
out += i + " ";
}
out = out.trim() + "]";
return out;
}
  
public boolean isEqualTo(IntegerSet other)
{
for(int i = 0; i < set.length; i++)
{
if (other.isIntegerSet(i) != isIntegerSet(i))
return false;
}
return true;
}
}

IntegerSetTest.java (Test class)

import java.util.Random;

public class IntegerSetTest {
  
public static void main(String[] args) {
IntegerSet set1 = new IntegerSet();
IntegerSet set2 = new IntegerSet();
  
// insert 10 random numbers between 10 and 99 to each of the 2 sets
Random rand = new Random();
for(int i = 0; i < 10; i++)
{
int pick = 10 + rand.nextInt(99 - 10);
while(pick < 10 || pick > 99)
pick = 10 + rand.nextInt(99 - 10);
set1.insert(pick);
}
  
for(int i = 0; i < 10; i++)
{
int pick = 10 + rand.nextInt(99 - 10);
while(pick < 10 || pick > 99)
pick = 10 + rand.nextInt(99 - 10);
set2.insert(pick);
}
  
System.out.println("SET 1 --> " + set1);
System.out.println("SET 2 --> " + set2);
  
IntegerSet union = set1.union(set2);
IntegerSet intersection = set1.intersection(set2);
  
System.out.println("SET 1 UNION SET 2 --> " + union);
System.out.println("SET 1 INTERSECTION SET 2 --> " + intersection);
}
}

*************************************************************** SCREENSHOT ************************************************************

run: SET 1 --> Set: [38 42 67 71 74 75 77 83 88] SET 2 --> Set: [14 22 31 56 58 67 76 77 88 93] SET 1 UNION SET 2 --> Set: [1

Add a comment
Know the answer?
Add Answer to:
Intro to java project template with instructions below * IntegerSet.java */ /** * * @author StudentName...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Java programming language, build a class called IntegerSet. Instructions - Create class IntegerSet An IntegerSet...

    Using Java programming language, build a class called IntegerSet. Instructions - Create class IntegerSet An IntegerSet object holds integers in the range 0-100 Represented by an array of booleans, such that array element a[i] is set to true if integer i is in the set, and false otherwise Create these constructors and methods for the class IntegerSet() public IntegerSet union(IntegerSet iSet) public IntegerSet intersection(IntegerSet iSet) public IntegerSet insertElement(int data) public IntegerSet deleteElement(int data) public boolean isEqualTo(IntegerSet iSet) public String toString()...

  • can i please get help on this? i don't know how to do it and I'm...

    can i please get help on this? i don't know how to do it and I'm so confused. This is in java. This is what I need to do. Thank you so much. In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node) , but...

  • I am having trouble with my C++ program.... Directions: In this lab assignment, you are to...

    I am having trouble with my C++ program.... Directions: In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted. The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the...

  • 3. (15 points) Fill in the method template below to make a working method. Make sure...

    3. (15 points) Fill in the method template below to make a working method. Make sure your method conforms to the instructions given in the comments for the method. public static boolean allIn ( Set < Integer > set , List < Integer> list ) { // This method takes a set and a list as inputs and returns true if every // element in the list is also an element of the set. Otherwise // it returns false. If...

  • Please use java code. Implement a test class named BatArray1Test that tests all the functionality of...

    Please use java code. Implement a test class named BatArray1Test that tests all the functionality of the following given 3 methods, including the invalid arguments: 1) commonEnd Given two arrays of ints, a and b, return true if they have the same first element or they have the same last element. This method should return false if either array is empty or null. 2) midThree Given an array of integers, return a new array of length 3 containing the elements...

  • This is a java file and I am very confused on how to do this project!...

    This is a java file and I am very confused on how to do this project! please help!! Specifications Overview: You will write a program this week that is composed of three classes: the first class defines Ellipsoid objects, the second class defines EllipsoidList objects, and the third, Ellipsoid ListApp, reads in a file name entered by the user then reads the list name and Ellipsoid data from the file, creates Ellipsoid objects and stores them in an ArrayList of...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • using CSCI300 java Given the following UML Class Diagram Club_Card # card_num : String # name...

    using CSCI300 java Given the following UML Class Diagram Club_Card # card_num : String # name : String # age: int # year: int + Club_Card (all parameters) + Setters & Getters + toString() : String + equals(x: Object): boolean [10 pts] Define the class Club_Card, which contains: 1. All arguments constructor which accepts all required arguments from the main and instantiate a Club_Card object. 2. Set & get method for each data attribute. 3. A toString() method which returns...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT