Generate Random Letters in Java [closed] - java

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
hey guys this is my problem..
I want to generate random letters in an specific length of a word, but the Starting Letter should corresponds to value of the variable I declare.
Example:
A3 should generate AER
A5 should generate AJIEH
B2 should generate BJ

Working with the variable names will be tedious (although it is possible I suppose through reflection). You could, however, try something like this:
public static String genString(char first, int len) {
String s = "";
for (int i = 1 ; i < len ; i++)
s += (char)(Math.random() * ('Z' - 'A' + 1) + 'A');
return first + s;
}
For example:
System.out.println(genString('A', 4));
Output (one of many possible):
AVGH

Related

all possible subsets in java in different sequences [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a set =(1, 2, 3), and I need to get all possible subsets of set with different sequences (with repeating elements).The out put look:
1
2
3
1,2
1,3
2,1
2,3
3,1
3,2
1,2,3
1,3,2
2,1,3
2,3,1
3,1,2
3,2,1
please can someone help me with that? thxx
If you want subsets, then Google Guava has a method for you:
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Sets.html#powerSet(java.util.Set)
But in your example you have some duplicate sets (remember, sets are unordered). So you might want to get all the possible permutations of each subset as well:
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Collections2.html#permutations(java.util.Collection)
The problem can be solved by finding all combinations using bitwise operations.
The idea is: Generate all the subsets of a given array (set), this set is known as a power set .For each of the subset(Combination), find its permutation too.
Refer the following tutorials, to learn, how can you find all combinations using Bit Wise Operations. http://www.codechef.com/wiki/tutorial-bitwise-operations
void PERMUTE()
{
/*PERMUTATION FUNCTION THAT PERMUTES THE SUBSET ARRAY*/
}
public static void main(String[] args)
{
// TODO code application logic here
int Set[]={1,2,3};
int n=Set.length;
for (int i = 0; i <= (1 << n); ++i)
{
System.out.print("[");
int subsetSz=0;
int A[]=new int[100];
for (int j = 0; j < n; ++j)
{
if ((i & 1 << j)!=0)
{
System.out.print(Set[j]+",");
A[subsetSz++]=Set[j];
}
}
System.out.println("]");
/*Permute the subset*/
if(subsetSz>1)
{
PERMUTE(A);
}
}
}

java.lang.StringIndexOutOfBoundsException [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Code :
for(int i = 0; i < rate.length; i++){
if(i == 0 | i == 4 | i == 8 | i == 12 | i == 18 | i == 22 | i == 26 |
i == 30 | i == 34){
System.out.println(rate[i]);
String parsedRate = rate[i].substring(0, 5);
System.out.println(parsedRate);
double rt = Double.parseDouble(parsedRate);
if(rt > max){
max = rt;
}
}
}
Output:
10.00%
10.00
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.substring(String.java:1907)
at parserTax.main(parserTax.java:48)
I am getting the desired 10.00... but then it fails. Is this a bug? I know it has to do something with that special character '%'... I am trying to parse a file for analysis can anyone help me?
Most probably the error is in this line.-
String parsedRate = rate[i].substring(0, 5);
You're trying to get a subtring from rate[i] from position 0 to 5. Have you checked that all rate strings have at least 6 characters? Keeping in mind the output, it seems the exception is thrown the second time the if condition is true (i == 4), so chances are that rate[4] contains an empty string.

Java array initialization [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What happens when we print reference variable of array initialization?
int[] it=new int[10];
sop(it);
What is the result?
int[] it = new int[10];
System.out.println(it);
it is an object, hence you are calling println(Object) of PrintStream (System.out), which calls toString() on the passed object internally. The arrays' toString() is similar to Object's toString():
getClass().getName() + "#" + Integer.toHexString(hashCode());
So the output would be something like:
[I#756a7c99
where [ represnts the depth of the array, and I refers to int. 756a7c99 is the value returned from hashCode() as a hex number.
Read Class.getName() JavaDoc.
To print an array, use Arrays.toString(), something like:
int[] it = new int[10];
System.out.println(Arrays.toString(it));
OUTPUT:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Assuming that sop is System.out.println, it will show you a String resulting returned by the toString method. In this case it will be the name of the class + "#" + the hexa of the hashcode.
Something like [I#30c221
It's the memory address of your new array
int[] it=new int[10];
System.out.println(it);

Calculating the first and last k digits in n^n [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I need to print the first k digits and the last k digits in
n^n (n to the power of n, where n is an integer)
For example :
Input Output
n k First k digits Last k digits
4 2 --> 25 56
9 3 --> 387 489
I sense that it requires some clever mathematics, however I am unable to think of anything as such. Please suggest a way to approach the problem.
The last k digits are easy, you just need to calculate it modulo 10^k. To do this, after every multiplication, just apply the modulo, ie. intermediate_result %= 10^k.
Of course, you will need to calculate 10^k using some other method, because ^ does not mean power of in C or Java.
To find the first k digits, see first n digits of an exponentiation.
Thanks everyone for their help. My final code is
#include <stdio.h>
#include <math.h>
long int lastKdigits(long long n,int k)
{
long long i,res=1,div=pow(10,k);
for(i=1;i<=n;i++)
{
res=(res*n)%div;
}
return res;
}
long int firstKdigits(long long n,int k)
{
long double x, y;
x = n*log10(n);
y = floor(pow(10,x-floor(x) +k-1));
return ((int)y);
}
int main()
{
long long n;
int k;
scanf("%lld %d",&n,&k);
printf("%ld\t",firstKdigits(n,k));
printf("%ld\n",lastKdigits(n,k));
}
return 0;
}
For last k digits it's pretty easy you just need to calculate n^n (mod 10^k) but I don't know any solution for the other k diggits!

recursion parameter issue [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
public void mystery1(int n) {
if (n <= 1) {
System.out.print(n);
} else {
mystery1(n / 2);
System.out.print(", " + n);
}
}
What gives this code for odd numbers. Becuase when we divide it it will not be an integer.
There is not mystery, because result of the integer division in Java is integer.
In Java or most other programming languages, when you divide an integer by an integer, the result will be an integer. If a decimal number occurs, say for example:
5/2=2.5
then, the number before the decimal point will be treated as the integer and 2 will be chosen.
In case you want to explicitly convert the integer into float or double, you can use any of the following conversions:
(float) 3/2;
(double) n/2;
The above explicitly converts it to a decimal.
n / 2, this is an integer division, where the fraction part will be ignored.
System.out.println(3/2); // prints 1
System.out.println(3.0/2); // prints 1.5
System.out.println(3/2.0); // prints 1.5
System.out.println(3.0/2.0); // prints 1.5
Param will rounded to int, for example if param will be 5, the next call the function will be with param 2

Categories