Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners a program in java to generate the powerset of a given set
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Shilpa
a program in java to generate the powerset of a given set
1 Answer(s)      4 years and 9 months ago
Posted in : Java Beginners


Hello :)

am a fresher to java
i am in need of a java code which generates the powerset of a given set
i.e
suppose the set S be {a,b,c}
the power set would be
{}
{a}
{b}
{c}
{a,b}
{a,c}
{b,c}
{a,b,c}
and also the size of the set may vary

kindly do the needfull

thanks and regards
Shilpa.R

Advertisement
View Answers

March 5, 2010 at 11:31 AM


Hi Friend,

Try the following code:

import java.util.*;
public class PowerSet{
public static void main(String[] args) {
String st[] = {"a", "b", "c"};
LinkedHashSet hashSet = new LinkedHashSet();
int len = st.length;
int elements = (int) Math.pow(2,len);
for (int i = 0; i < elements; i++) {
String str = Integer.toBinaryString(i);
int value = str.length();
String pset = str;
for (int k = value; k < len; k++) {
pset = "0" + pset;
}
LinkedHashSet set = new LinkedHashSet();
for (int j = 0; j < pset.length(); j++) {
if (pset.charAt(j) == '1')
set.add(st[j]);
}
hashSet.add(set);
}
System.out.println(hashSet.toString());
}
}

Thanks


Related Tutorials/Questions & Answers:
a program in java to generate the powerset of a given set - Java Beginners
a program in java to generate the powerset of a given set  Hello :) am a fresher to java i am in need of a java code which generates the powerset of a given set i.e suppose the set S be {a,b,c} the power set would
Java generate the powerset of a given set
Java generate the powerset of a given set In this section, you will learn how to generate the powerset of a given set of values. As you have already know...]); }  and at last we get the powerset of the given set. Here is the code
Advertisements
Java program to generate the total count
Java program to generate the total count  I need a java program which would take a text file as an input and scans through it to generate the output. For Ex. : If the text file contains the following information: There is/are 3
program on factorial of a given number in java
program on factorial of a given number in java  please tell me, how to write program on factorial of a given number in java
program on factorial of a given number in java
program on factorial of a given number in java  please tell me, how to write program on factorial of a given number in java
Please provide the java code for the given program.
Please provide the java code for the given program.   We need an application for managing an educational institute. That application should provide...: StudentAddr: StudentDob: Email: Course ALL Java C C++ Oracle Fees: Duration: Faculty
write a java program for inserting a substring in to the given main string from a given position
write a java program for inserting a substring in to the given main string from a given position  write a java program for inserting a substring in to the given main string from a given position
Write a java program to display the season given the month using switch case
Write a java program to display the season given the month using switch case  Write a java program to display the season given the month using switch case
Write a program that converts any given year to Roman Numeral in Java
Write a program that converts any given year to Roman Numeral in Java  Write a program that converts any given year to Roman Numeral
program to create student report using java applet,read the input using text boxesand generate the grades..?
program to create student report using java applet,read the input using text boxesand generate the grades..?   sir plz give me java applet codes for above question
to generate barcharts - Java Beginners
) If you want to generate bar chart using JFreeChart in java, try the following...to generate barcharts  Please kindly suggest: to generate a bar chart which should take data from backend(JDBC),in which Y-axis should represent
Various Commands that are used in java are given below
Various Commands that are used in java are given below   1.javac c:\ javac classname.java A Java Compiler javac is a computer program or set of programs which translate java source code into java byte code
java code for given query
java code for given query  i am not able to display to display graph in all browsers it is only displaying in ie using java
Generate random numbers in Java
Generate random numbers in Java - How to use the java.util.Random class to generate desired random number in Java program? In this tutorial I will teach you how to write Java code using the java.util.Random class to generate single
how to generate code - Java Beginners
how to generate code   hi friends, this is poornima i want to generate code about online shopping by using servlets,jsp,html,jdbc and eclipse (IDE) could you please tell me about what are steps included. Thanks in advance
How to Generate Reports in Java - Java Beginners
How to Generate Reports in Java  How to Display and Generate Reports in Java? Give Me Full Sample Code  Hi Friend, What do you want to display on reports.Please elaborate it. Thanks
i need a program that will generate daily base log file dynamically - Log4J
i need a program that will generate daily base log file dynamically  i need a program that will generate daily base log file dynamically For example: if suppose today date is 2 sep 2009, when i execute the program it ll
Day for the given Date in Java
Day for the given Date in Java  How can i get the day for the user input data ?   Hello Friend, You can use the following code: import java.util.*; import java.text.*; class CheckDay{ public static void main
Set Different Color to different row in Java Program
Java Set Different Color to different row In this section, you will learn how to set different color to table row using java swing.For this purpose, we have.... This class provides the method setBackground() which allow us to set different
Java converts any given year to Roman Numeral
Java converts any given year to Roman Numeral  Write a program that converts any given year to Roman Numeral
generate java code and html code - Java Beginners
generate java code and html code  create a web application for museum....a)the first page wil be a login page for administrators.. passwords for all acounts should be stored in the database and checked on login... on success ogin
Program to check whether two given words pronouncing same or not?
Program to check whether two given words pronouncing same or not?  Program to check whether two given words pronouncing same or not? example: raju and rajoo pronouncing same please help me out
Write a program to calculate factorial of any given number
program to calculate factorial of any given number. First of all define a class... in java. Here is the code of program: import ... Factorial Examples - Java Factorial Example to calculate factorial of any
Prime number program in java
Prime number program in java In this example you will learn how to write a program to generate and check  prime number  in java. As we know prime...,stc; String c; do { System.out.println("Press 1 for generate prime
Java: Some problems in given condition
Java: Some problems in given condition   SIR, I want to get the values from the table( database), if any one of the column of the table value is 0. Then one alert should b printed in application using Java. If(att.getdata1()==0
Java code for set...!!!
Java code for set...!!!  Create 2 classes in same package Product.java productId, name, price ProductImpl.java create a set in this and try... not allow the duplicate products to be added into the set(equal productId). Hint
how to generate random questions - Java Beginners
how to generate random questions  i am designing a test engine, i... friend, Plz specify the technology with full details like Java,JSP,Servlet,ASP and .net etc... See the below code in Java to help in solving the problem
How to generate random number in java
How to generate random number in java In this section you will learn how to generate random number in java. Java API  provide a random class...() method is most convenient way to generate random number in java which return
Result=Set - Java Beginners
result set, first move the pointer from first record to last record and get
java program
java program  Write a java program to do matrix addition operation On two given matrices
java program
java program  write a java program to display array list and calculate the average of given array
Advertisements
 

 

 

DMCA.com