63 140
Assignments Done
99,1%
Successfully Done
In July 2018

Answer to Question #218 in Java | JSP | JSF for Linda

Question #218
How to create a Java program that gives off 10 alphabets (a-z) randomly? There must be at least 2 vowels too.
Expert's answer
It will make a permutation of those ones which were taken from 2 combinations. First combination consists of randomly taken 2 vowels and the other one consists of 8 randomly taken consonants.
For example:
import java.util.ArrayList;
import java.util.HashSet;


public class Test {

/**
* @param args
*/
public static void main(String[] args) {

ArrayList<Integer> vowels = new ArrayList<Integer>();

vowels .add(65);
vowels .add(69);
vowels .add(73);
vowels .add(79);
vowels .add(85);
//y and w are vowels
vowels .add(87);
vowels .add(89);
int counter = 0;
ArrayList<Integer> letters = new ArrayList<Integer>();
for(int i=0;i<10;i++){
int help = (int) (Math.random()*26 + 65);
letters.add(new Integer(help));
// System.out.print((char)(letters.get(i).intValue()));
if (vowels .contains(new Integer(help))) counter++;
}
// System.out.println(counter);
while (counter < 2){
int rand = (int) (Math.random()*10);
Integer a = letters.get(rand);
if (vowels.contains(a) == false){
int rand2 = (int) (Math.random()*6);
letters.set(rand, vowels.get(rand2));
counter++;
}
}
// System.out.println();
for(int i=0;i<10;i++){
System.out.print((char)(letters.get(i).intValue()));
}

}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions