Generating sum of minterms in java - java

How can I generate the sum of minterms (boolean algebra) in java? We can generate sum of minterms throw ANDing with (X+X'). The following example explains the algorithm for a function with three variables A,B and C:
F(A,B,C)= A + B´*C
= A*(B+B´) + B´*C
= A*B + A*B´ + B´*C
= A*B*(C+C´) + A*B´*(C+C´) + B´*C*(A+A´)
= A*B*C+A*B*C´+A*B´*C+A*B´*C´+B´*C*A+B´*C*A´
= A*B*C+A*B*C´+A*B´*C+A*B´*C´+A*B´*C+A´*B´*C
The method in java looks like this:
String generateSumOfMinterms(String termsOfTheFunction, String variables){}
// Examples for functions with 2 variables A,B
generateSumOfMinterms("A", "A,B"){
//The result should looks like this
return "A*B+A*B'";
}
generateSumOfMinterms("A+B'", "A,B"){
//The result should looks like this (repeated terms are ok for example A*B')
return "A*B+A*B'+A'*B'+A*B'";
}
// Example for a function with 3 variables A,B,C
generateSumOfMinterms("A", "A,B,C"){
//The result should looks like this
return "A*B*C+A*B*C'+A*B'*C+A*B'*C'";
}
I have tried the following:
public List<Minterm> completeMinterm(Minterm minterm, String variables){
List<Minterm> minterms=new ArrayList<Minterm>();
minterms.add(minterm);
Minterm m1=new Minterm();
Minterm m2=new Minterm();
for (int k = 0; k < minterms.size(); k++) {
//A AB--> AB+AB'
for (int i = 0; i < variables.length(); i++) {
boolean varInMinterm=false;
for (int j = 0; j < minterms.get(k).atoms.size(); j++) {
if(minterms.get(k).atoms.get(j).variable==variables.charAt(i)){
varInMinterm=true;
break;
}
}
if(!varInMinterm){
varInMinterm=false;
m1= minterms.get(k);
m1.addAtom(new Atom(variables.charAt(i),false));
m2 = minterms.get(k);
m2.addAtom(new Atom(variables.charAt(i),true));
minterms.remove(k);
minterms.add(m1);
minterms.add(m2);
k=0;
}
}
}
I used eclipse debugger to find errors, I don't understand, why the atom added to m2 is added to m1 too in the same time, when this line is run:
m2.addAtom(new Atom(variables.charAt(i),true));

Here is an outline of a possible approach: First, you should create a more convenient representation of the expression - for example, the expression could be a list of instances of a Minterm class, and Minterm could contain a list of instances of an Atom class, each of which could contain a char that tells which variable it is and a boolean that tells whether the variable is negated or not. The first thing you should do is to loop through termsOfTheFunction and create such objects that represent the expression. Then, you can loop through the minterms, and every time you see a minterm that is missing one variable, you can remove it from the list and add two new minterms with the missing variable. Finally, you can loop through the finished minterms and "print" them to a result String.
Class declarations per request and for clarity (using public fields for brevity):
public class Atom {
public final char variable;
public final bool negated;
public Atom(char variable, bool negated) {
this.variable = variable;
this.negated = negated;
}
}
public class Minterm {
public final List<Atom> atoms = new ArrayList<Atom>();
}
In generateSumOfMinterms():
List<Minterm> expression = new ArrayList<Minterm>();
Minterm currentMinterm = new Minterm();
expression.add(currentMinterm);
Then, loop through the characters of termsOfTheFunction. Each time you see a letter, look at the next character to see if it is a ´, and add an Atom with that letter and with the correct negation. Each time you see a +, create a new Minterm and add it to expression, and keep going. Afterwards, you can start analyzing the minterms and expanding them.
Edit in response to your code: Looks like you're well on your way! The reason both atoms get added to the same minterm is that both m1 and m2 refer to the k'th minterm since you say m1 = minterms.get(k); and m2 = minterms.get(k);. get() does not copy or remove an element from a list; the element will still be inside the list. So for m2, you need to create a new minterm that has all of the atoms from the old one, plus the new atom.

Related

How to generate 1000 unique email-ids using java

My requirement is to generate 1000 unique email-ids in Java. I have already generated random Text and using for loop I'm limiting the number of email-ids to be generated. Problem is when I execute 10 email-ids are generated but all are same.
Below is the code and output:
public static void main() {
first fr = new first();
String n = fr.genText()+"#mail.com";
for (int i = 0; i<=9; i++) {
System.out.println(n);
}
}
public String genText() {
String randomText = "abcdefghijklmnopqrstuvwxyz";
int length = 4;
String temp = RandomStringUtils.random(length, randomText);
return temp;
}
and output is:
myqo#mail.com
myqo#mail.com
...
myqo#mail.com
When I execute the same above program I get another set of mail-ids. Example: instead of 'myqo' it will be 'bfta'. But my requirement is to generate different unique ids.
For Example:
myqo#mail.com
bfta#mail.com
kjuy#mail.com
Put your String initialization in the for statement:
for (int i = 0; i<=9; i++) {
String n = fr.genText()+"#mail.com";
System.out.println(n);
}
I would like to rewrite your method a little bit:
public String generateEmail(String domain, int length) {
return RandomStringUtils.random(length, "abcdefghijklmnopqrstuvwxyz") + "#" + domain;
}
And it would be possible to call like:
generateEmail("gmail.com", 4);
As I understood, you want to generate unique 1000 emails, then you would be able to do this in a convenient way by Stream API:
Stream.generate(() -> generateEmail("gmail.com", 4))
.limit(1000)
.collect(Collectors.toSet())
But the problem still exists. I purposely collected a Stream<String> to a Set<String> (which removes duplicates) to find out its size(). As you may see, the size is not always equals 1000
999
1000
997
that means your algorithm returns duplicated values even for such small range.
Therefore, you'd better research already written email generators for Java or improve your own (for example, by adding numbers, some special characters that, in turn, will generate a plenty of exceptions).
If you are planning to use MockNeat, the feature for implementing email strings is already implemented.
Example 1:
String corpEmail = mock.emails().domain("startup.io").val();
// Possible Output: tiptoplunge#startup.io
Example 2:
String domsEmail = mock.emails().domains("abc.com", "corp.org").val();
// Possible Output: funjulius#corp.org
Note: mock is the default "mocking" object.
To guarantee uniqueness you could use a counter as part of the email address:
myqo0000#mail.com
bfta0001#mail.com
kjuy0002#mail.com
If you want to stick to letters only then convert the counter to base 26 representation using 'a' to 'z' as the digits.

issues with clone and for loop

private void buildDeck(ArrayList<Card> Monsters, ArrayList<Card> Spells) {
int monstersQouta = 15;
int spellsQouta = 5;
Random r = new Random();
for (; monstersQouta > 0; monstersQouta--) {
int randomIndex = r.nextInt(monsters.size());
MonsterCard monster = (MonsterCard) monsters.get(randomIndex);
MonsterCard clone = new MonsterCard(monster.getName(),
monster.getDescription(), monster.getLevel(),
monster.getAttackPoints(), monster.getDefensePoints());
clone.setMode(monster.getMode());
clone.setHidden(monster.isHidden());
clone.setLocation(Location.DECK);
deck.add(clone);
}
I need to know why we used here clone()
and how this for loop in this code works
As Andy Turner said for your first question :
clone() is not being used. It just so happens that the variable is called clone, but that has no semantic importance to the code.
Concerning the for statement he's composed of 3 parts ,each separated by one ; , and none of them is obligated to be there :
The first part is where you can, or not, declare your incremental variable : int i = 0;
The second part is where you must put an evaluation resulting on a boolean value : kappa.lengh >= i;
The thrid part is where you will modify the variable(s) values : i--;
Since none of these parts are obligated, you could write a correct for loop like this one : for(;;).
Here's a link to the documentation : For statement.

Why this for statement shows Dead code in Java?

In this class, I defined a constructor that initializes an array and fill it with Point2D.Double. I want to define a toString method that outputs the Point2D.Double in the array. So inside the toString method, I make a for loop that returns every Point2D.Double in the array. The problem is, I don't know why Eclipse tells me that the update in the for statement is dead code.
import java.awt.geom.Point2D;
public class SimplePolygon {
public int n; // number of vertices of the polygon
public Point2D.Double[] vertices; // vertices[0..n-1] around the polygon
// boundary
public SimplePolygon(int size) {
n = size;
vertices = new Point2D.Double[n]; // creates array with n size. Elements are doubles.
for(int i = 0; i < n; i++)
{
Point2D.Double point = new Point2D.Double(Math.random() * 6, Math.random() * 6);
vertices[i] = point;
}
}
public String toString() {
for(int i = 0 ; i < n ; i++)
{
return "" + vertices[i];
}
return "";
}
I too was puzzled by this. (And the other answers!) So I cut-and-pasted it into Eclipse to see what it actually says.
And what Eclipse is actually says is that i++ is unreachable in this line.
for(int i = 0 ; i < n ; i++)
And in fact, that is correct! If you ever enter the loop body, the body will unconditionally return. Hence the i++ can never be executed.
Note also that this is a warning not an error. This code is not invalid according to the JLS rules about unreachability.
You are right to be puzzled by the other explanations. The final return statement is reachable. Consider the case where the class is instantiated with a negative value for n (or size). In that case, the for loop body will never be executed, and control will go to the final return.
However, their suggestions as to how to fix the problem are correct. You should not have a return in the loop body.
The problem is because of the return statement in the for loop. Remember, whenever you use return, you immediately end the method and stop running any code. That means that your toString method will loop exactly only once, returning only vertices[0]. The second return below the loop never has a chance to execute, so is considered dead code.
This is actually incorrect! See Stephan's answer for a better/accurate explanation of what's going on.
Regardless, you still need to fix your code. Instead of returning something inside the loop, you probably want to combine the values and return them all at once at the very end. An easy way to do this might be:
public String toString() {
String output = "";
for(int i = 0 ; i < n ; i++)
{
output += vertices[i] + " ";
}
return output;
}
Now, instead of returning immediately, we're accumulating values in the loop and returning at the very end.
(Note that the code here isn't very efficient -- you'd probably want to use something like String.join or StringBuilder instead, but if you're a beginner, this works for now)

permutation with local variables bugs

I'm testing the permutation codes from Stanford c++ lecture. Why do I introduce a bug if i use my local variables? I tried debugging it but I still don't understand it.
public class JavaApplication9 {
public static void permutate(String so_far, String rest ){
if (rest.equals("")){
System.out.println(so_far);
} else {
for (int i = 0; i < rest.length(); i++){
so_far = so_far + rest.charAt(i);
rest = rest.substring(0,i) + rest.substring(i+1);
permutate(so_far, rest);
}
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
permutate("", "ab");
}
}
Output should be: ab, ba.
Currently getting just: ab
By overwriting the values of rest and so_far inside the for loop, you lose the original version. Here's what the calls to permutate looklike if we unroll the loop.
so_far = "a"
rest = "b"
next time through loop:
so_far = "ab"
rest = ""
Because the values of so_far and rest changed inside the for loop, when i=1, the original values passed in to permutate can't be used only the new modified versions are.
However, if you use different variable names inside your for loop the unrolled loop looks like this:
tmp_so_far = "a"
tmp_rest = "b"
tmp_so_far = "b"
tmp_rest = "a"
Next time through loop:
tmp_so_far = "ab"
tmp_rest = ""
tmp_so_far = "ba"
tmp_rest = ""
Because when you update rest to the new rest, you overwrite the old string. You're modifying it in the loop so when you do rest.length() it's the new length as you've lost the old string.
Example:
when the passed in rest == "ab", in the first iteration rest.length() == 2. but in the loop you updated rest to "b". so when you got back from permute() your rest is now "b" and the length is 1, so when you increment i, it becomes 1 and you fall out of the loop.
Changing to newRest solves the problem as you still have the old rest passed into the method.

How to output binary search answer?

I get the error message cannot find symbol, symbol: method books(int[], int) when I try to compile the following code.
For further explanation about what I want the code to do, see below the code.
public class books {
public void main(String[] args) {
int searchValue = 0, index;
int refNum[] = new int[4]; // the array
refNum[0] = 4; //numbers to refer to (aka to find)
refNum[1] = 6;
refNum[2] = 10;
refNum[3] = 12;
refNum[4] = 14;
int input = Integer.parseInt(enterValue.getText()); //takes user's input
for (int x = 0; x < refNum.length; x++) {
refNum[x] = input; //Tells refNum value to be
}
searchValue = input;
index = books(refNum, searchValue); //"books" is underlined
if (index != -1) {
binarySearchField.setText("We found: " + index);
} else {
binarySearchField.setText("Sorry! Not Found!");
}
public static Boolean binarySearch(String [] refNum, int left, int right, String search){
//Boolean code for later
}
This program uses binary search to find values stored in array after user inputs number, if they match then the item is successfully found. User inputs desired number in 'enterNumber' which is a TextField. Now in my code )which I'm 78% sure will work if it wasn't for this one little thing) there is an all important that is underlined which shouldn't be, (I've commented beside the line to show)
Now I had thought I was suppose to put the class name there, but apparently since it is underlined that is not the case. Any ideas on what I should be putting there in it's place?
And I apologize for the question may be a bit misleading on what I'm really asking, I just wasn't sure how to word the question.
The line
index = books(refNum, searchValue);
seems to be underlined because you have no method called books that takes an int[] and an int as arguments in your books class definition.
Now I had thought I was suppose to put the class name there Why do you assume you have to put the class name there? Figure out what you are trying to do with this code and then you will understand what goes in that line (at least in pseudocode).
Also it seems like you have a method declared directly inside another method. That is not legal in java. If this is not the case, please show us correct code.
books is your class's name..that might be the reason you are getting this error. You can't call constructor like a method. Change class's name to Books or something else..or change method's name

Categories

Resources