b.wood
b.wood

Reputation: 11

Creating a new object based on user selection

I am trying to return the created dog in step 6.

For step 6, returning the created dog would it just be

return selected_dog; 

or something else? because selected_dog returns an error

here is the DogTest class

import java.util.Scanner;

public class DogsTest
{
  private static Scanner input = new Scanner(System.in);

  public static void main(String[] args)
  {
    final String YES = "y";

    String answer;
    Dog my_dog;                          // Step 1

    do
    {
      // ------------------------------------------------------------
      // The compiler cannot know at compile time what type my_dog is
      // so it is determined at runtime every time the loop iterates
      // ------------------------------------------------------------
      my_dog = getDog();
      System.out.println(my_dog.getName() + " says " + my_dog.speak());

      System.out.print("Try again? ");
      answer = input.next();
    } while (answer.equalsIgnoreCase(YES));
  }

  public static Dog getDog()                           // Step 2
  {
    int choice;
    Dog selected_dog;                               // Step 3
    String name,
           color;

    do
    {
      // ----------------------------------
      // A null reference indicates that an
      // invalid menu choice was entered
      // ----------------------------------
      selected_dog = null;
      System.out.print("Choose a Breed (1. Labrador  2. Yorkshire): ");
      choice = input.nextInt();

      switch (choice)
      {
        case 1:  System.out.print("Enter dog's name: ");
                 name = input.next();
                 System.out.print("Enter dog's color: ");
                 color = input.next();
                 selected_dog =   Labrador;  // Step 4
                 break;
        case 2:  System.out.print("Enter dog's name: ");
                 name = input.next();
                 selected_dog = ______;  // Step 5
                 break;
        default: System.out.println("Invalid choice");
                 break;
      }
    } while (selected_dog == null);
    return __________________;                                       // Step 6
  }
}

The Dog class

public class Dog
{
  protected String name;

  public Dog(String name)
  {
    this.name = name;
  }

  public String getName()
  {
    return name;
  }

  public String speak()
  {
    return "Woof";
  }
}

The Labrador Class

public class Labrador extends Dog
{
  private String color;
  private static int breed_weight = 75;

  public Labrador(String name, String color)
  {
    this.color = color;
  }

  // =========================================
  // Big bark -- overrides speak method in Dog
  // =========================================
  public String speak()
  {
    return "WOOF";
  }

  public static int avgBreedWeight()
  {
    return breed_weight;
  }
}

Upvotes: 0

Views: 433

Answers (2)

Manuel Meireles
Manuel Meireles

Reputation: 31

On the first lines of code you've shown, you try to create three objects by using the "default constructor" of those classes. It would work, but you miss the parenthesis. It should go like this:

selected_dog = new  Labrador();

But, in your dog and labrador class, you have other constructors beside the deafult one. So, if you want to create a new labrador with a specific name and color, you should write like this:

selected_dog = new Labrador("name", "color");

One last thing. In the constructor of labrador, you are passing the name as parameter but not using it. You should add to the first line of that constructor the call to the super class constructor, like this:

public Labrador(String name, String color)
  {
    super(name);
    this.color = color;
  }

I hope I was clear enough :) Happy learning!

RE-EDITED:

Take a look at this code and try to understand it:

DogsTest class:

public class DogsTest {

    private static final Scanner INPUT = new Scanner(System.in);

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        final String YES = "y";
        final String NO = "n";

        String answer;
        Dog my_dog;                          // Step 1

        do {
            // ------------------------------------------------------------
            // The compiler cannot know at compile time what type my_dog is
            // so it is determined at runtime every time the loop iterates
            // ------------------------------------------------------------
            my_dog = getDog();
            System.out.println(my_dog.getName() + " says " + my_dog.speak());

            do {
            System.out.print("Try again? ");
            answer = INPUT.next();
            } while ( !( answer.equalsIgnoreCase(YES) || answer.equalsIgnoreCase(NO) ) );
        } while (answer.equalsIgnoreCase(YES));
    }

    public static Dog getDog() {
        int choice;
        String name;
        String color;
        boolean loop = true;
        do {
            System.out.print("Choose a Breed (1. Labrador  2. Yorkshire) 3. Cancel: ");
            choice = INPUT.nextInt();
            switch (choice) {
                case 1:
                    System.out.print("Enter dog's name: ");
                    name = INPUT.next();
                    System.out.print("Enter dog's color: ");
                    color = INPUT.next();
                    return new Labrador(name, color);
                case 2:
                    System.out.print("Enter dog's name: ");
                    name = INPUT.next();
                    System.out.print("Enter dog's color: ");
                    color = INPUT.next();
                    return new Yorkshire(name, color);
                case 3:
                    loop = false;
                    break;
                default:
                    System.out.println("Invalid choice");
                    break;
            }
        } while (loop);
        return null;
    }

}

Dog class:

public class Dog {

// ====================================================================================================

// DATA STRUCTURE

// Instance data
    private String name;

// Class data
    private static final String DEFAULT_NAME = "No name";

// ====================================================================================================

// CONTRUCTORS

// Complete
    public Dog(String name) {
        this.name = name;
    }

// Default
    public Dog() {
        this.name = DEFAULT_NAME;
    }

// Copy
    public Dog(Dog dogToCopy) {
        this.name = dogToCopy.name;
    }

// ====================================================================================================

// GET
    public String getName() {
        return name;
    }

// ====================================================================================================

// SET
    public void setName(String name) {
        this.name = name;
    }

// ====================================================================================================

// TO STRING
    @Override
    public String toString() {
        return "The dog's name is "+name+". ";
    }

// ====================================================================================================

// FUNCTIONALITIES
    public String speak() {
        return "woof";
    }

// ====================================================================================================

}

Labrador class:

public class Labrador extends Dog{

// ====================================================================================================

// DATA STRUCTURE

// Instance data
    private String color;

// Class data
    private static int breedWeight = 75;
    private static final String DEFAULT_COLOR = "No color";


// ====================================================================================================

// CONTRUCTORS

// Complete
    public Labrador(String name, String color) {
        super(name);
        this.color = color;
    }

// Incomplete
    public Labrador(String name) {
        super(name);
    }

// Default
    public Labrador() {
        super();
        this.color = DEFAULT_COLOR;
    }

// Copy
    public Labrador(Labrador labradorToCopy) {
        super(labradorToCopy.getName());
        this.color = labradorToCopy.color;
    }

// ====================================================================================================

// GET
    public String getColor() {
        return color;
    }

// ====================================================================================================

// SET
    public void setColor(String color) {
        this.color = color;
    }

    public static void setBreedWeight(int newBreedWeight) {
        Labrador.breedWeight = newBreedWeight;
    }

// ====================================================================================================

// TO STRING
    @Override
    public String toString() {
        return super.toString()+"The dog's color is "+color+". ";
    }

// ====================================================================================================

// FUNCTIONALITIES
    @Override
    public String speak() {
        return "WOOF";
    }

    public static int avgBreedWeight() {
        return breedWeight;
    }

// ====================================================================================================

}

Yorkshire class:

public class Yorkshire extends Dog {

// ====================================================================================================

// DATA STRUCTURE

// Instance data
    private String color;

// Class data
    private static int breedWeight = 75;
    private static final String DEFAULT_COLOR = "No color";


// ====================================================================================================

// CONTRUCTORS

// Complete
    public Yorkshire(String name, String color) {
        super(name);
        this.color = color;
    }

// Incomplete
    public Yorkshire(String name) {
        super(name);
    }

// Default
    public Yorkshire() {
        super();
        this.color = DEFAULT_COLOR;
    }

// Copy
    public Yorkshire(Yorkshire yorkshireToCopy) {
        super(yorkshireToCopy.getName());
        this.color = yorkshireToCopy.color;
    }

// ====================================================================================================

// GET
    public String getColor() {
        return color;
    }

// ====================================================================================================

// SET
    public void setColor(String color) {
        this.color = color;
    }

    public static void setBreedWeight(int newBreedWeight) {
        Yorkshire.breedWeight = newBreedWeight;
    }

// ====================================================================================================

// TO STRING
    @Override
    public String toString() {
        return super.toString()+"The dog's color is "+color+". ";
    }

// ====================================================================================================

// FUNCTIONALITIES
    @Override
    public String speak() {
        return "WOOF";
    }

    public static int avgBreedWeight() {
        return breedWeight;
    }

// ====================================================================================================

}

Can you understand what have I done in it? Watch out that the way it is, the app can crash if you don't write an int on dog type choice. There are ways to prevent that ;)

Upvotes: 1

lightup
lightup

Reputation: 674

Seems you forgot to type it correctly

selected_dog = new  Labrador("Bingo", "White");  

Doe that solve your problem?

Upvotes: 0

Related Questions