Given that s refers to a set, write a statement that adds the int value 42 to th
ID: 3933897 • Letter: G
Question
Given that s refers to a set, write a statement that adds the int value 42 to the set. Write a statement that associates s with a set that contains the following elements: 23, 42, -11, 89. Given that s refers to a set, write a statement that adds the int value 42 to the set. Given that s refers to a set, write a statement that removes the int value 5 from the set. Given that s refers to a set, write a statement that attempts to remove the int value 11 from the set, but will do nothing if 11 is not in the set. Given that v has been defined, and that s that refers to a set, write a statement that adds the value referred to by v to set s. Given that v has been defined, and that s that refers to a set, write a statement that removes the value associated with v from the set. Given a set, weights, and an integer desired_weight, remove the element of the set that is closest to desired_weight (the closest element can be less than, equal to OR GREATER THAN desired_weight), and associate it with the variable actual_weight. For example, if weights is (12, 19, 6, 14, 22, 7) and desired_weight is 18, then the resulting set would be (12, 6, 14, 22, 7) and actual_weight would be 19. If there is a tie, the element LESS THAN desired_weight is to be chosen. Thus if the set is (2, 4, 6, 8, 10) and desired_welght is 7, the value chosen would be 6, not 8. Assume there is at least one value in the set. 9. Given that s has been defined, and that the_set that refers to a set, write an expression that whose value is True if and only if the value to which s refers is in the_set.Explanation / Answer
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class SetOperations {
public static void main(String[] args) {
/****** 9.2.1 **************/
//by 1. its seems set is the type of int
Set <Integer> s = new HashSet<Integer>();
// adding 42 to s
s.add(42);
System.out.println("1. list is : ");
print(s);
/****** 9.2.1 end **************/
/****** 9.2.2 start **************/
// we need to create another set for this
Set <Integer> newSet = new HashSet<Integer>();
newSet.add(23);
newSet.add(42);
newSet.add(-11);
newSet.add(89);
s = newSet;
System.out.println("2. List is : ");
print(s);
/****** 9.2.2 end **************/
/****** 9.2.3 start **************/
s.add(42);
System.out.println("3. List is : ");
print(s);
/****** 9.2.3 end **************/
/****** 9.2.4 start **************/
// lets first add 5 to the list
s.add(5);
System.out.println("4. Before removal 5, list is : ");
print(s);
s.remove(5);
System.out.println("4. after removal of 5 from the list : ");
print(s);
/****** 9.2.4 end **************/
/****** 9.2.5 start **************/
if(s.contains(11)){
s.remove(11);
System.out.println("5. After removal of 11 list is : ");
print(s);
}
/****** 9.2.5 end **************/
/****** 9.2.6 start **************/
// here the type of v is not clear so I am taking it as list
ArrayList<Integer> v = new ArrayList<Integer>();
v.add(1);
v.add(2);
// now we have to add this element to set s
for(Integer i : v){
s.add(i);
}
System.out.println("6 . After adding v's data to set : " );
print(s);
/****** 9.2.6 end **************/
/****** 9.2.7 start **************/
for(Integer i : v){
s.remove(i);
}
System.out.println("6 . After removing v's data from set : " );
print(s);
/****** 9.2.7 end **************/
/******** 9.2.8********************/
Set <Integer> weight = new HashSet<Integer>();
//here i am assigning s value to weight
weight = s;
Integer desired_weight = 18;
Integer diff = desired_weight;
Integer toRemove = null ;
System.out.println("8. before removal of desired weight : ");
print(weight);
for(Integer check : weight){
if((diff > Math.abs (diff - check) )||( toRemove==null)){
diff = Math.abs (diff - check);
toRemove =check;
}
}
weight.remove(toRemove);
System.out.println("8. After removal of desired weight : ");
print(weight);
/***********9.2.8 close **************/
/***********9.2.9 start **************/
// to check the value of one set is present in other . we need to iterate it for that i am using for each loop
//Actually this part, I am not getting correctly. the question seems not clear.
// so simply I am checking if the value is present in the other set or not if it is then I am printing true
Set <Integer> the_set = new HashSet<Integer>();
// for simplicity i am not adding value to this set simply assigning previous value of weigth to this
the_set = weight;
for(Integer set1 : s){
for(Integer s2 : the_set){
if(set1 == s2 ){
System.out.println(set1+" : true");
}
}
}
/***********9.2.9 close **************/
}
public static void print(Set<Integer>set){
for(Integer i : set){
System.out.println(i+" ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.