The assignment states that there's another class that will call this class. It is similar to betting - you select 12 characters and put them in the array and the program will output a random set of characters. Then it will calculate how many of those matched.
The teacher told me that there should be a parameter of char[]
type in the print()
method and two in the checked method, but I don't understand the need for them. I could make something similar without his weird choices, but I'm stuck with that. Therefore, I am wondering, if anyone understands the reason for that. It is a method for user input already and it is a method for the computer generated randoms, so I don't see why I have to put a parameter on the other methods. Can't see how that is a logical choice.
Code to solve the assignment:
import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;
public class Tipping
{
char[] ukensRekke;
public Tipping()
{
ukensRekke = new char[12];
}
public void WeekResult(){
String HBU = "HBU";
for(int i = 0; i < ukensRekke.length; i++){
ukensRekke[i] = HBU.charAt((int)(Math.random()*3));
}
}
public char[] getWeekResult(){
return ukensRekke;
}
public void print(char[] tastet){
tastet = new char[12];
for(int i = 0; i < tastet.length; i++){
System.out.print(tastet[i]);
}
}
public char[] register(){
Scanner scan = new Scanner(System.in);
char[] vinnerTall = new char[12];
for(int i = 0; i < vinnerTall.length; i++){
System.out.println("Skriv inn H,B eller U for kamp" + (i+1) + "; ");
vinnerTall[i] =scan.next().charAt(0);
System.out.println(vinnerTall[i]);
}
return vinnerTall;
}
public int check(char[] original, char[] tastet){
original = ukensRekke;
tastet = register();
return tastet.length;
}
}
UPDATE: Hi, so I was somewhat able to solve the problem, but it's still one finishing touch. Here's the part of the code I have problems with, hope someone can help me out.
System.out.println("Klassen Tipping instansieres...");
System.out.println();
System.out.println("Ukens rekke genereres...");
System.out.println();
System.out.println("Brukers rekke registreres.");
tp.register();
System.out.println();
System.out.println("Ukens rekke hentes...");
System.out.println();
System.out.println("Ukens rekke;");
tp.print(tp.getWeekResult());
System.out.println("Brukers rekke;");
tp.print(tp.register());
System.out.println();
System.out.println("Bruker hadde" + tp.check(tp.getWeekResult(),tp.register()) + "riktige tippetanger");
this is the class that I use to make a kinda like sheet, it prints out everything on the screen and takes the user input, but the last two method calls doesn't work, the program just skips it and starts all over again.
and this is the code I use that gets called from:
public void WeekResult(){
String HBU = "HBU";
for(int i = 0; i < ukensRekke.length; i++){
ukensRekke[i] = HBU.charAt((int)(Math.random()*3));
}
}
public char[] getWeekResult(){
return ukensRekke;
}
public void print(char[] tastet){
System.out.print(taste);
}
public char[] register(){
Scanner scan = new Scanner(System.in);
char[] vinnerTall = new char[12];
for(int i = 0; i < vinnerTall.length; i++){
System.out.println("Skriv inn H,B eller U for kamp" + "; ");
vinnerTall[i] = scan.next().charAt(0);
}
return vinnerTall;
}