简体   繁体   中英

Creating a Creature App with Java, Object oriented and Constructors.

newbie here at Java. I'm working on a project to where I collect info from a user for two types of Creatures. The menu would look like: 1. Land based 2. Water based 3. Display Animal 4. Quit.

I'm wanting to collect the user information from the user and then display it back to them. I think I'm stuck on building the object constructors from the different object.

Here's what I have so far:

SuperClass:

package com.animal;

public class Creature {
private String size;
private String weight;

public Creature (String size, String weight){
    this.size = size;
    this.weight = weight;
}

public String getSize() {return size;}

public void setSize(String size) {this.size = size;}

public String getWeight() {return weight;   }

public void setWeight(String weight) {this.weight = weight; }

void displayCr(){
    System.out.println("***Creatures***");
    System.out.println("Size: " + size);
    System.out.println("Weight: " + weight);

This is my Land Subclass:

package com.animal;

public class Land extends Creature {
private String landAnimal;

public String getLandAnimal() {return landAnimal;}
public void setLandAnimal(String landAnimal) {this.landAnimal = landAnimal;}

public Land(String size, String weight, String landAnimal) {
    super(size, weight);
    this.landAnimal = landAnimal;

This is my Water subclass:

package com.animal;

public class Water extends Creature {
private String fish;

public String getFish() {return fish;}
public void setFish(String fish) {this.fish = fish;}

public Water(String size, String weight, String fish) {
    super(size, weight);
    this.fish = fish;
}

Then this is my Main called Kingdom:

package com.animal;

import java.util.ArrayList;
import java.util.Scanner;


public class Kingdom {

public static void main(String[] args) {
    ArrayList<Creature> user = new ArrayList<Creature>();
    Scanner input = new Scanner(System.in); 

    int selection = 0; 
    while(selection != 4){
        System.out.println("****Main Menu****");
        System.out.println("Enter 1 for Land Animal");
        System.out.println("Enter 2 for Water Animal");
        System.out.println("Enter 3 to Display Animal");
        System.out.println("Enter 4 to quit");

        selection = input.nextInt();
        if(selection==1 || selection==2){

            Creature userInfo = null;

            System.out.println("Enter Size ");
            String size = input.next();
            System.out.println("Enter Weight: ");
            String weight = input.next();
            }
        if(selection == 1){
            System.out.println("Enter Land animal type: ");
            String landAnimal = input.next();
            //userInfo = new Land(size, weight, landAnimal);
            //user.add(userInfo);
        }
        else if(selection == 2){
            System.out.println("Enter Water animal type: ");
            String fish = input.next();
            //userInfo = new Water(size, weight, fish);

        }
        //creature.add(userInfo);
        //System.out.println(user.displayCr());

    }

I feel like I'm on the right path, but the last steps just aren't clicking for me and I've been grinding on this, reading, videos and nothing is clicking.

Also, I apologize if I've made the newbie mistakes in this post. I will accept all criticism, suggestions and help as a positive lesson. Thanks.

Overall your code is fairly right.

Except: Creature userInfo = null; you are defining it inside the first IF, its scope will be limited to that IF. As soon as you leave that IF it ceases to exist, hence you can't use it in the following IFs.

Consider the following scope change:

if(selection==1 || selection==2){ // main IF
    Creature userInfo = null;
    ...
    if(selection == 1){
        ...
    }
    else {
        ...
    }
    System.out.println(userInfo.displayCr());
} // end of main IF

First of all expand the scope of size,weight and userInfo means keep it inside main but outside the conditions as it is being used everywhere. Something like this.

    int selection = 0;
    String size = null;
    String weight = null;
    Creature userInfo = null;

Instead of this creature.add(userInfo); should be

user.add(userInfo);

and call display like this

userInfo.displayCr(); because the return type of the method is void so cannot be used inside sysout and it should be inside that else if(){}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM