Question

In: Computer Science

Using Java, design a program to let user enter two lists of numbers within the range...

Using Java, design a program to let user enter two lists of numbers within the range [0, 9] from keyboard, you could either use flag to denote end of the list or ask user to enter the list size first and then enter the list numbers. Each of these two lists represents a set (keep in mind that duplicate elements are not allowed in the same set but are allowed between sets), so we have two sets A and B. Make sure each type of calculation is in a separate method.

Suppose the universal set is U = {0,1,2,···,9}. Write a program to implement the following functions:

(1) Print out A∪B.

(2) Print out A∩B.

(3) Print out B'

(4) Print out (A∩B)×A.

Solutions

Expert Solution

java code:

package setdriver;

import java.util.Scanner;

public class SetDriver {

public static void main(String[] args) {

int[] A=new int[10];

int[] B=new int[10];

int[] AUB=new int[10];

int[] AnB=new int[10];

int[] BC=new int[10];

int[] AM=new int[10];

int n=0,m=0;

Scanner sc=new Scanner(System.in);

A=getElements(sc);

n=getLength(A);

//print A set

System.out.print("\nSet A: ");

printElements(A,n);

System.out.println("\n================================================");

//Set B

B=getElements(sc);

m=getLength(B);

//print B set

System.out.print("\nSet B: ");

printElements(B,m);

System.out.println("\n================================================");

//call union function

System.out.print("Union of set A and B: ");

union(A,n,B,m);

//call intersection function

System.out.print("\nIntersection of set A and B: ");

intersetion(A,n,B,m);

//call complement function

System.out.print("\nSet B's complement: ");

complement(B,m);

//call production function

System.out.print("\nProduction of (intersection of set A and B) and A: ");

production(A,n,B,m);

}

//get elements from user

public static int[] getElements(Scanner sc)

{

int n=0;

int[] A=new int[10];

System.out.println("Please enter unique element and enter -99 to end the entering:");

while(n<10)

{

A[n]=sc.nextInt();

n++;

if(A[n-1]==-99){

return A;

}

for(int i=0;i<n-1;i++){

if(A[i]==A[n-1]){

System.out.println("Element already exists, please reenter");

n--;

}

}

}

return A;

}

//returnt the set length

private static int getLength(int[] A) {

for(int i=0;i<A.length;i++)

{

if(A[i]==-99)

return i;

}

return 10;

}

//print the set

public static void printElements(int[] A,int n)

{

System.out.print("{");

for(int i=0;i<n;i++)

System.out.print(A[i]+" ");

System.out.print("}");

}

//union function

public static void union(int[] A,int n, int[] B,int m)

{

int i=0,j=0,k=0;

int[] U=new int[50];

for(i=0;i<n;i++) //add A set

{

U[k]=A[i];

k++;

}

//add B set elements

for(j=0;j<m;j++)

{

U[k]=B[j];

k++;

for(int x=0;x<k-1;x++)

{

if(U[x]==B[j]) //ignore the same elements

k--;

}

}

printElements(U,k); //call print function

}

//instersection of A and B

public static void intersetion(int A[],int n, int B[],int m)

{

int[] N=new int[n];

int k=0;

for(int i=0;i<n;i++){

for(int j=0;j<m;j++){

if(A[i]==B[j]){

N[k]=A[i];

k++;

}

}

}

printElements(N,k); //call print function

}

//complementof B set

private static void complement(int[] B, int m) {

int[] C=new int[11];

int k=0;

for(int i=0;i<10;i++)

{

C[k]=i;

k++;

for(int j=0;j<m;j++)

{

if(C[k-1]==B[j])

k--;

}

}

printElements(C,k); //call print function

}

//production

private static void production(int[] A, int n, int[] B, int m) {

int[] N=new int[n];

int k=0;

//computethe intersection

for(int i=0;i<n;i++){

for(int j=0;j<m;j++){

if(A[i]==B[j]){

N[k]=A[i];

k++;

}

}

}

//compute and print the production

System.out.print("{");

for(int i=0;i<k;i++){

for(int j=0;j<n;j++)

{

System.out.print("("+N[i]+","+A[j]+")");

}

if(i<k-1)

System.out.print(",");

}

System.out.print("}");

}

}

output:

plzz rate me my answer...


Related Solutions

Write a program that prompts the user to enter a number within the range of 1...
Write a program that prompts the user to enter a number within the range of 1 to 10 inclusive • The program then generates a random number using the random class: o If the users guess is out of range use a validation loop (DO) to repeat the prompt for a valid number o If the users guess matches the random number tell them they win! o If the users guess does not match the random number tell them they...
Write a program that prompts the user to enter a number within the range of 1...
Write a program that prompts the user to enter a number within the range of 1 to 10 inclusive • The program then generates a random number using the random class: o If the users guess is out of range use a validation loop (DO) to repeat the prompt for a valid number o If the users guess matches the random number tell them they win! o If the users guess does not match the random number tell them they...
IN JAVA Write a complete program that asks the user to enter two real numbers from...
IN JAVA Write a complete program that asks the user to enter two real numbers from the console. If both numbers are positive print the product, if both numbers are negative print the quotient, otherwise print INVALID INPUT. Use a nested if; output should be to a dialog and console; use printf to format the console output (for real numbers specify the width and number of digits after the decimal). The output must be labeled. Follow Java conventions, indent your...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute the sum of the numbers between these two numbers (including the numbers entered). If the user Enters 2 and 6. The program will calculate the sum of the numbers: 2+3+4+5+6 and will display the result. Enter First number> 2 Enter second number> 6 The sum is 20
Design a modular program which asks the user to enter a list of numbers. The numbers...
Design a modular program which asks the user to enter a list of numbers. The numbers must be stored in an array. The program then finds the index of the first occurrence of the smallest element in the array and the last occurrence of the largest element in the array. The program displays the position and value of each of these items.
(JAVA) Implementing a Program Design a program that prompts the user for twenty numbers. If the...
(JAVA) Implementing a Program Design a program that prompts the user for twenty numbers. If the number is positive, then add the number to the current sum. If the number is negative, then subtract the sum by one. Implement just the main method. Assume that all libraries are imported, and class has been declared.
Design the logic for a program that allows a user to enter 20 numbers, then displays...
Design the logic for a program that allows a user to enter 20 numbers, then displays them in the reverse order of entry. Design the logic for a program that allows a user to enter 20 numbers, then displays each number and its difference from the numeric average of the numbers entered. The program is C++. I need a Pseudocode
Design a complete program that asks the user to enter a series of 20 numbers. The...
Design a complete program that asks the user to enter a series of 20 numbers. The program should store the numbers in an array and then display each of the following data: I. The lowest number in the array II. The highest number in the array III. The total of the numbers in the array IV. The average of the numbers in the array *PYTHON NOT PSUEDOCODE AND FLOW CHART!!!!*
​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
using MIPs assembly, ask the user to enter two numbers. Then multiply the two numbers using...
using MIPs assembly, ask the user to enter two numbers. Then multiply the two numbers using bit shifting (not 'mul'). This should work multiplying 2 positive numbers, 2 negative numbers, and a positive/negative number. Then output the result and ask user if he/she has more calculations to complete.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT