Return randomized String to other class in project
Hi, I want to return a randomized string from Class2 to Class1 (the classes are in same "Project". How do I do to make the returned String relevant in Class1?
CLASS 1
Code Java:
public class p12NameGen
{
public static void main(String[] args) {
//I want the randomized String from class 2 to be here
}
}//class end
CLASS2
Code Java:
import java.util.Random;
public class p12NamnLista
{
Random slumpNamn = new Random();
String[] p12namn ={"Coolboy_97"};
public String getp12Namn()
{
int namn = slumpNamn.nextInt(p12namn.length);
return p12namn[namn];
}
}// class end
Excuse the sweinglish in the code =:)
Re: Return randomized String to other class in project
See Classes and Objects for a thorough description on how to use classes and objects. This should hopefully answer your question, if not let us know if there is still confusion
Re: Return randomized String to other class in project
Ok yes thanks a lot :), it helped me understand a bit about classes. Then I looked on the code of a almost similar program we wrote in school and did it like this.
CLASS 1
Code Java:
public class p12NameGen
{
public static void main(String[] args)
{
p12NamnLista randomp12Namn = new p12NamnLista();
System.out.println(randomp12Namn.toString());
}
}//class end
CLASS 2
Code Java:
public class p12NamnLista
{
String[] p12namn ={"Coolboy_97", "Niklas_Paladin"};
int namnet = (int) (p12namn.length*Math.random());
private String namn = p12namn[namnet];
public String getNamn() {
return namn;
}
public void setNamn(String namn) {
this.namn = namn;
}
@Override
public String toString()
{
String slumpatNamn = this.namn;
return slumpatNamn;
}
}// class end
Works fine <:-P