Sweetcharge
Sweetcharge

Reputation: 9

How to get my program to print out?

Having trouble trying to get my program to work for my AP Computer Science class. Comments have been made inside the code to show my problem. Thanks guys/gals.

Java Class

import java.util.*;
public class Allosaur extends Dinosaur
{
private boolean hungry;
private String response, answer;
private String Allosaur;

 // Prompt asks for 3 constructors: A Default constructor, a constructor with just a name, and a constructor with a name and hunger "response"

public Allosaur()
{
}

public Allosaur(String name)
{
 Allosaur=name;   
}

public Allosaur(String name, boolean hungry)
{
    Allosaur=name;
    this.hungry=hungry;
}

// Used this method to "find out" whether the dinosaur is hungry or not
public boolean getHunger()

{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Are you hungry? ");
    response = keyboard.next();
    if(response.equals("Yes"))
    hungry = true;
    else if(!response.equals("Yes"))
    hungry= false;

    return hungry;

}

// Asks us to print out "RRRRRRR" if the dinosaur is NOT hungry and "HUNGRRRRRRRY" if the dinosaur IS hungry   

public String roar()
{
    if(hungry == true)
    answer = "HUNGRRRRRRRY";
    else if(hungry == false)
    answer = "RRRRRRR";

    return answer;
}

//When I use the toString() method in my driver class, none of these pop up, why?

public String toString()
{
    String Dino = super.toString();
    Dino = "The Dinosaur's name is: " + Allosaur;
    Dino += "Is the Dinosaur hungry? :" + getHunger() + "\n" + roar();
    return Dino;

} 

}

And here is my Driver Class:

public class DinosaurMain extends Allosaur
{
public static void main(String[] args) 
{
       Allosaur Dino = new Allosaur("Jacob");
       Dino.toString();

}

}

I'm very confused as to why nothing will show up when I run the program.

Upvotes: 0

Views: 432

Answers (1)

chsbellboy
chsbellboy

Reputation: 490

Nothing shows up after the String input is requested because you're simply doing the call in DinosaurMain as Dino.toString(). This just makes the String representation for your object. It doesn't print that String representation out. If you were to change it to System.out.println(Dino.toString()); you would see the result.

Upvotes: 3

Related Questions