Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I HAVE THE FOLLOWING ASSIGNMENT WHICH REQUIRES ME TO ADD WHAT IS ASKED BELOW TO A CODE THAT I ALREADY HAVE......PLEAE HELP AND MAKE COMMENTS

I HAVE THE FOLLOWING ASSIGNMENT WHICH REQUIRES ME TO ADD WHAT IS ASKED BELOW TO A CODE THAT I ALREADY HAVE......PLEAE HELP AND MAKE COMMENTS AS ADDING THE CONSTRUCTORS AND METHOD. THIS IS FOR DISCRETE..... JAVA PLZ

REQUIREMENT:

Extend the Sets class from lab 1.

Remember that has sets for positive integers that you will need the following functions:

addElement , will take a positive integer and add it to the set

Note: you need to change it so that it will not add a duplicate element

getElement, will take a position number and return the element at the position (return -1 if bad position)

getSize, return the size of the set

isSubset, takes a sets object (call it b) and see if the current set (call it a) is a subset of b. If so, return a Boolean true, else return a Boolean false

(You can keep other functions in if need be).

Add the functions of:

unionOps, that takes a Sets object and does a union with the current set

intersection, that takes a Sets object and does an intersection with the current set

printSet, will print the Sets object contents

It is recommended to add a constructor that will take an array and add its elements to the current set.

Define a set a with certain values, like 1, 2, 3, 4 and set b with certain values like 3, 4, 5, 6

Have main create three objects: setA with set a, setB with set b, and setC with set a.

Print the setA, setB, setC

Do an union of setA with setB (setA will be changed) then print setA

Do an intersection of setC (setC will be changed) with setB then print setC...

---------------------------------------------------------------------COPY THIS CODE AND ADD ON TO IT WHAT IS LISTED ABOVE----------------------------------------------------------

//importation of packages import java.util.HashSet; import java.util.Iterator; import java.util.Set; //class public class Sets { Set intSet = new HashSet(); //Method for adding element(addElement) void addElement(Integer x) { intSet.add(x); } //getting element with the integer index as premeter int getElement(int index) { Iterator it = intSet.iterator(); int i = 0; Integer currentInt = null; //While loop while(it.hasNext() && i < index) { currentInt = it.next(); i++; } return currentInt; } //get size int getSize() { int size =intSet.size(); return size; } boolean isSubset(Sets s) { //if and else for containing the integers if (this.intSet.containsAll(s.intSet)) return true; else return false; } //if its subset boolean isProper(Sets s){ if (!isSubset(s)) return false; if(this.getSize()>s.getSize()) return true; return false; } //this will print the sets a and b as ordered pairs public void printOrderedPairs(Sets a,Sets b) { int size1=a.intSet.size(); int size2=b.intSet.size(); System.out.println("Ordered Pairs "); for(int i=1;i<=(size1*size2);i++) { for(int j=1;j<=size2;j++) System.out.println("["+a.getElement(i)+","+b.getElement(j)+"]"); } System.out.println(" "); } }//close class

--------------------------MAIN-----------------------

//importation of packages import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class Main { //Main (running intSet) public static void main(String args[]) { Sets setA = new Sets(); Sets setB = new Sets(); setA.addElement(3); setA.addElement(5); setA.addElement(8); setB.addElement(3); setB.addElement(4); Sets obj=new Sets(); obj.printOrderedPairs(setA,setB); if(setA.isSubset(setB)) { System.out.println("Set B is subset Of Set A "); } else { System.out.println("Set B is not subset Of A "); }//close else }//close main }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 4.5 Databases

Authors: Sandeep Chanda, Damien Foggon

3rd Edition

1430243805, 978-1430243809

More Books

Students also viewed these Databases questions

Question

Solve the equation. |3x - 1| = 2

Answered: 1 week ago

Question

107 MA ammeter 56 resistor ? V voltmeter

Answered: 1 week ago

Question

Generally If Drug A is an inducer of Drug B , Drug B levels will

Answered: 1 week ago