Metody w języku Java cd…

Kontynuując ostatni wpis, w Intellij mieliśmy następujący zapis kodu:

public class Main {

    public static void main(String[] args) {
        boolean gameOver = true;
        int score = 800;
        int levelCompleted = 5;
        int bonus = 100;

        calculateScore(true, 800,5,100);

        score = 10000;
        levelCompleted = 8;
        bonus = 200;
        if (gameOver == true) {
            int finalScore = score + (levelCompleted * bonus);
            System.out.println("Your final score was " + finalScore);
        }
    }
}
    public static void calculateScore(boolean gameOver, int score, int levelCompleted, int bonus) {

        if (gameOver == true) {
            int finalScore = score + (levelCompleted * bonus);
            System.out.println("Your final score was " + finalScore);
        }
    }
}

Zapis  if (gameOver ==true) można uprościć:  if (gameOver).

Możemy jeszcze bardziej uprościć powyższy kod o zaznaczone fragmenty kodu:

java3

java1java2

Pierwszy zaznaczony fragment kodu można zmienić zapisując:

calculateScore(true, 10000, 8, 200);

te cztery parametry chcemy wykonać w instrukcji:

java2

Możemy tą instrukcję teraz wykasować oraz można wykasować również:

java3

i pozostaje nam:

public class Main {

    public static void main(String[] args) {
        boolean gameOver = true;
        int score = 800;
        int levelCompleted = 5;
        int bonus = 100;

        calculateScore(true, 800,5,100);

        calculateScore(true, 10000, 8, 200);

    }

    public static void calculateScore(boolean gameOver, int score, int levelCompleted, int bonus) {

        if (gameOver) {
            int finalScore = score + (levelCompleted * bonus);
            finalScore+=1000;
            System.out.println("Your final score was " + finalScore);
        }
    }
}

Po uruchomieniu powyższego kodu otrzymujemy w oknie konsoli:

Your final score was 2300
Your final score was 12600

jeśli zmienimy wers:  finalScore+=1000; na  finalScore+=2000;  obie wartości zostają zaktualizowane, gdyż wykorzystują one tą samą metodę i uzyskujemy w oknie konsoli:

Your final score was 3300
Your final score was 13600.

Ostatecznie możemy usunąć:

java1

upraszczając nasz kod do:

public class Main {

    public static void main(String[] args) {
        calculateScore(true, 800,5,100);

        calculateScore(true, 10000, 8, 200);

    }

    public static void calculateScore(boolean gameOver, int score, int levelCompleted, int bonus) {

        if (gameOver) {
            int finalScore = score + (levelCompleted * bonus);
            finalScore+=2000;
            System.out.println("Your final score was " + finalScore);
        }

można również zostawić deklarowane wartości danych, ale wówczas w nawiasach przy metodach calculateScore należy umieścić nazwy zmiennych:

public class Main {

    public static void main(String[] args) {
        boolean gameOver = true;
        int score= 800;
        int levelCompleted = 5;
        int bonus = 100;

        calculateScore(gameOver, score,levelCompleted,bonus);
        score = 10000;
        levelCompleted=8;
        bonus=200;

        calculateScore(gameOver, score, levelCompleted,bonus);

    }

    public static void calculateScore(boolean gameOver, int score, int levelCompleted, int bonus) {

        if (gameOver) {
            int finalScore = score + (levelCompleted * bonus);
            finalScore+=2000;
            System.out.println("Your final score was " + finalScore);
        }
    }
}

 

możemy również zwrócić wynik naszych obliczeń do kodu wykonywanego wcześniej. Żeby to wykonać należy rozważyć keyword „void”, który oznacza, żeby nie odsyłać żadnej wartości. Dlatego jeśli tworzymy metodę i nie chcemy żeby nam zwracała jakąkolwiek wartość używamy wówczas „void”, natomiast jeśli chcemy coś zwrócić trzeba zmienić słowo „void” na typ danych, który chcemy zwrócić.

W naszym przypadku, odeślijmy wartość finalScore jako score, użyty w :

calculateScore(gameOver, score,levelCompleted,bonus);

w tym celu zmieniamy „void” na „int” oraz musimy dodać dodatkowy wers kodu, że chcemy zwrócić zmienną finalScore:

public class Main {

    public static void main(String[] args) {
        boolean gameOver = true;
        int score= 800;
        int levelCompleted = 5;
        int bonus = 100;

        calculateScore(gameOver, score,levelCompleted,bonus);
        score = 10000;
        levelCompleted=8;
        bonus=200;

        calculateScore(gameOver, score, levelCompleted,bonus);

    }

    public static int calculateScore(boolean gameOver, int score, int levelCompleted, int bonus) {

        if (gameOver) {
            int finalScore = score + (levelCompleted * bonus);
            finalScore+=2000;
            System.out.println("Your final score was " + finalScore);
            return finalScore;
        }
    }
}

Zadanie.

Jak zmienilibyśmy powyższy kod, żeby zwrócić „-1” jeśli gameOver byłoby równe „false”?

Są dwa sposoby, żeby to wykonać:

1.

public class Main {

    public static void main(String[] args) {
        boolean gameOver = true;
        int score= 800;
        int levelCompleted = 5;
        int bonus = 100;

        calculateScore(gameOver, score,levelCompleted,bonus);
        score = 10000;
        levelCompleted=8;
        bonus=200;

        calculateScore(gameOver, score, levelCompleted,bonus);

    }

    public static int calculateScore(boolean gameOver, int score, int levelCompleted, int bonus) {

        if (gameOver) {
            int finalScore = score + (levelCompleted * bonus);
            finalScore+=2000;
            System.out.println("Your final score was " + finalScore);
            return finalScore;
        } else{
            return -1;
            
        }
    }
}

2.

public class Main {

    public static void main(String[] args) {
        boolean gameOver = true;
        int score= 800;
        int levelCompleted = 5;
        int bonus = 100;

        calculateScore(gameOver, score,levelCompleted,bonus);
        score = 10000;
        levelCompleted=8;
        bonus=200;

        calculateScore(gameOver, score, levelCompleted,bonus);

    }

    public static int calculateScore(boolean gameOver, int score, int levelCompleted, int bonus) {

        if (gameOver) {
            int finalScore = score + (levelCompleted * bonus);
            finalScore+=2000;
            System.out.println("Your final score was " + finalScore);
            return finalScore;
        }
        return -1;
    }
}