Search code examples
javavariablesbooleansuperclass

Assigning Variable Types to numbers java


I'm trying to figure out how to assign multiple variables to int type but I'm not sure how to assign it to them. Also, I'm having difficulty understanding how you can use a boolean with this restriction. I was thinking about putting them in an array but I'm not sure if there is an easier approach to the problem. Any help is much appreciated!

Instructions for Attraction.java

Write a Java program Attraction.java that has the following characteristics.

extends Place New attributes: type (int) price (double) New Methods: public Attraction(String name, String desc, double latitude, double longitude, double price, int type) where for type values:

0 is an amusement park 1 is an aquarium 2 is a zoo public double getPrice() -- returns current price

public int getType() -- returns type

public boolean hasAnimals() -- returns true if type contains "zoo" or "aquarium"


Solution

  • Your hasAnimals method needs to return a boolean value i.e. true or false based on the value of the type variable.

    You're .. kind of on the right track, but you're not honoring the requirements of the method. It shouldn't print anything, and it shouldn't return type because type is an int and not the boolean that is required.

    public boolean hasAnimals() {
        if(type == 1) {
            return true; // aquarium has animals
        } else if (type == 2) {
            return true; // zoo has animals
        } else {
            return false;
        }
    }
    

    Think carefully about what a method is called, and what it should do. This method is just a way to answer a yes/no question.