Question

u are to write a program in Java that takes two sets of up to 5...

u are to write a program in Java that takes two sets of up to 5 symbols(letters, numbers or any characters) and prints the cartesian product of the two sets. The program should allow the user to enter the characters for each set and the output should, consist of the name of each set followed by the characters and on the following line print the cartesian product.

Example:

{1,3,*} X {3,?,D} = {(1,3),(1,?),(1,D),(3,3),(3,?),(3,D,(*,3),(*,?),(*,D)}

or

{2,3,5,7} X (5,6} = {(2,5),(2,6),(3,5),(3,6),(5,5),(5,6),(7,5),(7,6)}

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

import java.io.*;
import java.util.*;

public class Main {

  
   public static void main (String[] args) {
      
   Scanner s = new Scanner(System.in);
System.out.println("Enter upto 5 Characters only for first array");
char[] array_1 = s.next().toCharArray();
  
System.out.println("Enter upto 5 Characters only for second array");
char[] array_2 = s.next().toCharArray();
      
       int n1 = array_1.length;
       int n2 = array_2.length;
      
       System.out.print("array_1 = ");
       System.out.println( Arrays.toString( array_1 ) );
  
   System.out.print("array_2 = ");
       System.out.println( Arrays.toString( array_2 ) );
      
       System.out.print("array_1 X array_2 = ");
      
       System.out.print("{ ");
      
       for (int i = 0; i < n1; i++)
       for (int j = 0; j < n2; j++)
           System.out.print("{"+ array_1[i]+", "
                           + array_2[j]+"} ");
      
       System.out.print("}");
  
   }
}

Note: While taking input do not put space while typing characters. Just type the number, letters or any other symbol like i have typed in the screenshot. Also, do let me know in the comments if there is any problem while executing the code. I will rectify it with the best of my capability.

Please upvote if you find this answer satisfactory.

Add a comment
Know the answer?
Add Answer to:
u are to write a program in Java that takes two sets of up to 5...
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
  • An experiment consists of rolling two fair dice and adding the dots on the two sides...

    An experiment consists of rolling two fair dice and adding the dots on the two sides facing up. Using the sample space provided below and assuming each simple event is as likely as any other, find the probability that the sum of the dots is not 2 or 8. 1 5 (1,5) First Die (1,1) 2 (2.1) (3,1) 4 (4.1) (5,1) 6 (61) 2 (1,2) (2.2) (3,2) (4,2) (5,2) (6,2) لا لا ، الا ان Second Die 3 4 (1,3)...

  • write a java program that will perform the following. Given two finite sets, list all elements...

    write a java program that will perform the following. Given two finite sets, list all elements in the Cartesian product of these two sets. Given a finite set, list all elements of its power set.

  • Program has to be in C# language Write a computer program that takes two sets (let...

    Program has to be in C# language Write a computer program that takes two sets (let the user determine the cardinality of each set and enter them manually) and calculates the following operations: Union Intersection Difference (set1 - set2) Cartesian product (set2 X set1) Check whether set2 is a subset of set1 or set1 is a subset of set2 Find the powerset of set1 and print out its elements and cardinality

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

  • Thank you 24. A better model for two indistinguishable fair dice? You may be uncomfortable with...

    Thank you 24. A better model for two indistinguishable fair dice? You may be uncomfortable with the sample space given in class for the possible outcomes of the roll of a pair of fair dice because when dealing with two dice you cannot, or at least you do not, distinguish between them. Thus for example, the two outcomes (1,3) and (3,1) should perhaps be considered as the same outcome. This argument leads to the following 21-member sample space: (23>)A56.6 (2,2)...

  • Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...

    Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

  • Program Description: A Java program is to be created to produce Morse code. The Morse code...

    Program Description: A Java program is to be created to produce Morse code. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (such as period, comma, colon, and semicolon). In sound-oriented systems, the dot represents a short sound and the dash represents a long sound. Separation between words is indicated by a space, or, quite simply, the absence of a dot or dash. In a sound-oriented...

  • Write the Java code for the class WordCruncher. Include the following members:

    **IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite the Java code for the class WordCruncher. Include the following members:A default constructor that sets the instance variable 'word' to the string "default".A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make...

  • **This is for the C Language. Trigonometric Calculator Specification You are required to write a program...

    **This is for the C Language. Trigonometric Calculator Specification You are required to write a program that calculates and displays values of the trigonometric functions sine, cosine, and tangent for user-supplied angular values. The basic program must comply with the following specification. 1. When the program is run, it is to display a short welcome message. TRIG: the trigonometric calculator 2. The program shall display a message prompting the user to enter input from the keyboard. Please input request (h-help,...

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
ADVERTISEMENT