-2

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;
    }
  • Your current print function doesn't make any sense, I'd be lying if I said I know what it will print, but I guarantee I try won't be what you expect. – Geoff Nov 11 '16 at 16:52
  • sorry, that was the messed up code, it wasn't originally that way, but that was the result of hours of scratching my head trying to figure out why there should be a char[] in the parameter, my original code worked well, but it doesn't seem like that's how my teacher want it to be – Anh Vũ Mai Nov 11 '16 at 16:58
  • You are tending to shift from your original question. We can only answer one question on each post. – progyammer Nov 12 '16 at 04:00

1 Answers1

0

Your teacher asks you to use a char[] array over a String object simply to use less memory.

Read the answer(s) to this question: How much memory does a string use in Java 8?

One of the answer shows that a String object takes up a minimum of 80 (-8) bytes in memory. In your case, the char array ukensRekke needs only 12 * 2 = 24 bytes. So less memory is used. This is a good advice. You should always keep these efficiency factors in mind to be able to make efficient programs.

Community
  • 1
  • 1
progyammer
  • 1,498
  • 3
  • 17
  • 29