0

I'm trying to create a program for a competition using java where every participant gets 3 tries and the final result is the addition of all of those tries. In every try the participants may hit target 1-10 where target 1=100 points and target 10=10 points with the default value of 0.

The program will request for the target hit at the main class and insert that into the competition class where switch cases will happen to determine the points they get. The point i'm confused about it, how do i do switch cases for all 3 tries? I've tried making each of them individually within a method and then calling them from another method to get the total but it either displays 0 or the total of the target instead.

Here is what i've tried so far:

class Arrow {
    private int test1;
    private int test2;
    private int test3;
    private int end1;
    public int nl1, nl2, nl3;

    //constructor
    public Arrow() {
    }

    public Arrow(int test1, int test2, int test3) {
        this.test1 = test1;
        this.test2 = test2;
        this.test3 = test3;
        this.end1 = setEnd1();
    }

    //setter
    public void setScore(int test1, int test2, int test3) {
        this.test1 = test1;
        this.test2 = test2;
        this.test3 = test3;
        this.end1 = setEnd1();
    }

    public void setC1(int test1) {
        this.test1 = test1;
    }

    public void setC2(int test2) {
        this.test2 = test2;
    }

    public void setC3(int test3) {
        this.test3 = test3;
    }
    
    public int setScore(int i, int test1, int test2, int test3) {
        nl1 = switch (test1) {
            case 1 -> 100;
            case 2 -> 90;
            case 3 -> 80;
            case 4 -> 70;
            case 5 -> 60;
            case 6 -> 50;
            case 7 -> 40;
            case 8 -> 30;
            case 9 -> 20;
            case 10 -> 10;
            default -> 0;
        };
        nl2 = switch (test2) {
            case 1 -> 100;
            case 2 -> 90;
            case 3 -> 80;
            case 4 -> 70;
            case 5 -> 60;
            case 6 -> 50;
            case 7 -> 40;
            case 8 -> 30;
            case 9 -> 20;
            case 10 -> 10;
            default -> 0;
        };
        nl3 = switch (test3) {
            case 1 -> 100;
            case 2 -> 90;
            case 3 -> 80;
            case 4 -> 70;
            case 5 -> 60;
            case 6 -> 50;
            case 7 -> 40;
            case 8 -> 30;
            case 9 -> 20;
            case 10 -> 10;
            default -> 0;
        };
        return 0;
    }
    
    private int setEnd1() {
        return (nl1+nl2+nl3);
    }

    //getter
    public int getC1() {
        return test1;
    }

    public int getC2() {
        return test2;
    }

    public int getC3() {
        return test3;
    }

    //hitung nilai akhir
    public int getEnd1() {
        return end1;
    }
}

class Arrow1 extends Arrow{
    private Use ps;
    private Arrow[] score;

    public Arrow1() {

    }

    public Panah getScore(int i) {
        Arrow nl = new Arrow();
        nl.setScore(getC1(), getC2(), getC3());
        return nl;
    }
}

I tried changing the return (nl1+nl2+nl3); to return (getC1() + getC2() + getC3()) which resulted in the total amount of tries being displayed instead (for example if test1=1, test2=2, test3=3, displayed will be 6). From that i believe the main class is already fine as it has inserted the amount of tries and displayed the result correctly, it's just the switch cases that needs fixing. Can someone explain to me what i did wrong there? Or is this question still too vague?

static void Score1() {
        System.out.println("Scores");
        System.out.println("======================================================");
        Scanner objN = new Scanner(System.in);
        for (int i = 0; i < b; i++) {
            for (int j = 0; j < a; j++) {
                Use ps = lhs[j].getPs();
                String name = ps.getName();
                String nr = ps.getNr();
                System.out.println("Name: " + name + "   Number: " + nr);
                System.out.print("Input for try 1                : ");
                int test1= objN.nextInt();
                System.out.print("Input for try 2                : ");
                int test2= objN.nextInt();
                System.out.print("Input for try 3                : ");
                int test3= objN.nextInt();
                lhs[j].setScore(test1, test2, test3);
                System.out.println("======================================================");
            }
        }
    }
Vincent
  • 9
  • 2
  • `getScore` doesn't call your `setScore` method with the switches. – tgdavies Oct 16 '21 at 11:02
  • so should i change `setScore` to something else so that `getScore` can call it? – Vincent Oct 16 '21 at 11:06
  • You have two `setScore` methods. The one with the case statements takes four int parameters and returns an int. The other takes three in parameters. In `getScore` you are calling the latter `setScore`. – tgdavies Oct 17 '21 at 00:57

1 Answers1

2

There's many things wrong and I'm not entirely sure what kind of misunderstanding caused them, so I'll just list them as I see them:

  1. your setScore and setEnd1 methods don't actually set anything. They should be called something like calculateScore or calculateEnd.
  2. your setScore method takes test1, test2 and test3 as parameters even though those are already fields. Usually you want to do one or the other, doing both is confusing.
  3. your setScore method is defined to return int but doesn't ever return anything other than 0
  4. you call the setEnd1 method from your constructor, which adds nl1, nl2 and nl3, but those haven't been set at this point
  5. Your Arrow1 class extends Arrow which seems wrong: there is no obvious reason to duplicate all those fields and it's probably a mistake.
  6. You call getC1(), getC2() and getC3() in Arrow1.getScore() but the variables behind those methods have never been initialized (unless that happens in the code that we don't see).
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Thank you very much for your reply. I'm just a newbie so this is very helpful to me. Once again, thanks! – Vincent Oct 16 '21 at 11:33
  • May i ask, what should i do about point 3 and 6? what should i change it to? – Vincent Oct 16 '21 at 11:40
  • @Vincent: regarding #3 if there's no useful return value, just make the method return `void` (i.e. nothing). for #6 I don't know where you *should* be getting those values from, right now you attempt to get them "from thin air", which doesn't work. Should they user input? Constant values? I think we're missing the code that calls into the code you've shown. – Joachim Sauer Oct 16 '21 at 11:47
  • I've included the part from main class that should have filled `getC1` and the others in this post. I assumed that since Arrow class had `getC1` that returned `test1`, then that part from the main class should've filled `getC1` as well. And then Arrow1 would've received it as well since it was extended from Arrow. – Vincent Oct 16 '21 at 11:57
  • My apologies if my assumptions and codes irritate you, as i've made the entire program by copy pasting parts from another program and editing it. So i don't have a good understanding of this program or java myself... – Vincent Oct 16 '21 at 12:18
  • It's not that it irritates me, I just don't have unlimited time for 1:1 coaching like this that's unlikely to leave artifacts that can help others. It might sound painful, but I suggest you go back through your coursework and actually implement the earlier tasks you've been given, because this copy-and-paste approach might help you get the code done, but it will seriously mess up your understanding. And in software development your understanding is your strongest tool. If that's weak, then you'll never finish the course let alone get anywhere. – Joachim Sauer Oct 17 '21 at 10:05