HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

"Alien" distinguisher

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
distinguisheralienstackoverflow

Problem

I am practicing some simple Java coding problems in an attempt to learn while doing them. I would like to know if my code is redundant/is there an easier way to accomplish the same thing?

Question: Which Alien?


"A person who witnessed the appearance of the alien has come forward
to describe the alien's appearance."


The program will determine which alien has arrived. The three alien
species that it could be is:



-
TroyMartian, has at least 3 antenna and at most 4 eyes

-
VladSaturnian, has at most 6 antenna and at least 2 eyes

-
GraemeMercurian, has at most 2 antenna and at most 3 eyes



Sample session (with output shown in text, user input in italics)



How many antennas?


2


How many eyes?


3


VladSaturnian


GraemeMercurian



If the description does not match any of the aliens, there is no
output.

My code:

```
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Alien {

public static int antenna;
public static int eye;

public static void main(String args[]) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(
System.in))) {

System.out.println("How many antennas?");

antenna = Integer.parseInt(in.readLine());

System.out.println("How many eyes?");

eye = Integer.parseInt(in.readLine());

if(troy(antenna, eye)) {
System.out.println("TroyMartian");
}

if(vlad(antenna, eye)) {
System.out.println("VladSaturnian");
}

if(graeme(antenna, eye)) {
System.out.println("GraemeMercurian");
}

return;

}

catch (IOException e) {
System.err.println("Error");
}

}

public static boolean troy(int antenna, int eye) {
if ((antenna >= 3) && (eye = 2)) {
return true;
} else {
return false;
}

}

public static boolean graeme(int

Solution

You're on the right track. Good job using the try-with-resources block for the BufferedReader. You could use a Scanner instead for convenience. (You could even use Scanner.nextInt(), but it would work slightly differently: the newline in the input would be optional.)

The variables antenna and eye can be local to main(), and therefore should not be static members of the class.

In Java, a common naming convention for methods that return a boolean is isSomething() or hasSomething(). In this case, though, maybeSomething() seems more appropriate.

It is rarely necessary to write return true; or return false; explicitly. Usually, you would be better off returning a boolean expression.

import java.util.Scanner;

public class Alien {
    public static void main(String args[]) {
        try (Scanner in = new Scanner(System.in)) {
            System.out.println("How many antennas?");
            int antenna = Integer.parseInt(in.nextLine());

            System.out.println("How many eyes?");
            int eye = Integer.parseInt(in.nextLine());

            if (maybeTroy(antenna, eye)) {
                System.out.println("TroyMartian");
            }

            if (maybeVlad(antenna, eye)) {
                System.out.println("VladSaturnian");
            }

            if (maybeGraeme(antenna, eye)) {
                System.out.println("GraemeMercurian");
            }
        }
    }

    public static boolean maybeTroy(int antenna, int eye) {
        return ((antenna >= 3) && (eye = 2));
    }

    public static boolean maybeGraeme(int antenna, int eye) {
        return ((antenna <= 2) && (eye <= 3));
    }
}

Code Snippets

import java.util.Scanner;

public class Alien {
    public static void main(String args[]) {
        try (Scanner in = new Scanner(System.in)) {
            System.out.println("How many antennas?");
            int antenna = Integer.parseInt(in.nextLine());

            System.out.println("How many eyes?");
            int eye = Integer.parseInt(in.nextLine());

            if (maybeTroy(antenna, eye)) {
                System.out.println("TroyMartian");
            }

            if (maybeVlad(antenna, eye)) {
                System.out.println("VladSaturnian");
            }

            if (maybeGraeme(antenna, eye)) {
                System.out.println("GraemeMercurian");
            }
        }
    }

    public static boolean maybeTroy(int antenna, int eye) {
        return ((antenna >= 3) && (eye <= 4));
    }

    public static boolean maybeVlad(int antenna, int eye) {
        return ((antenna <= 6) && (eye >= 2));
    }

    public static boolean maybeGraeme(int antenna, int eye) {
        return ((antenna <= 2) && (eye <= 3));
    }
}

Context

StackExchange Code Review Q#79156, answer score: 6

Revisions (0)

No revisions yet.