4 Replies - 241 Views - Last Post: 22 May 2019 - 06:25 AM Rate Topic: -----

#1 moonwolf332   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-May 19

Homework Help: Magic Eight Ball

Posted 21 May 2019 - 07:10 PM

In my computer science class we were given the assignment of basically creating a Magic Eight Ball using constructors to create an array where the user inputs the amount of values in the array, a method where the user inputs the values themselves in the array using a loop, along with another method that for returning a response. After writing the code, I tested it and found that instead of outputting the value of array like it was supposed to it, it instead outputs null. I'm still a beginner at coding so I have no clue how to fix it so any help would be much appreciated.

Main class:
 
import static java.lang.System.*;
import java.util.Scanner;
import java.util.Arrays;

public class MagicEightBallTest 

{
  public static void main (String[] args)
  
  {
    
    Scanner input = new Scanner(System.in);
    
    String answer = "a";
    String question = " ";
    int currentResponse = 0;
    
    out.println("How many respsonses would you like there to be?");
    int numResponses = input.nextInt();
    input.nextLine();
    
    MagicEightBall ball = new MagicEightBall(numResponses);
    
    ball.answers();
    
    ball.questions();
    
    out.println("Thank you for using the Magic Eight Ball");



Other class:
import static java.lang.System.*;
import java.util.Scanner;
import java.util.Arrays; 

public class MagicEightBall

{
  
  MagicEightBall(int num)
    {
      numResponses = num;
    }
    
    public int currentResponse;
    
    private String question = " ";
    private int numResponses;
    private String answer = " ";
    
    Scanner input = new Scanner(System.in);
    
    
    public void shake(int numResponses)
    {
      currentResponse = (int)(Math.random()*numResponses);
      out.println(currentResponse);
    }
    
    public void answers()
    {
      String[] responses = new String[numResponses];
      
      for (int i = 0; i < numResponses; i++)
      {
        out.println("Enter an answer: ");
        answer = input.nextLine();
        responses[i] = answer;
      }
    }
    
//So this is the method that's giving me the problem I think:
    public void questions()
    {
      MagicEightBall ball = new MagicEightBall(numResponses);
      String[] responses = new String[numResponses];
      
      while (!question.equalsIgnoreCase("stop"))
      {
        out.println("What is your question?");
        question = input.nextLine();
        
        ball.shake(numResponses);
        
        out.println(responses[currentResponse]);
      }
    }
  
}



The output that I'm aiming for is something like this:
How many responses would you like there to be?
 4 //user input
Enter an answer: 
 a
Enter an answer: 
 s
Enter an answer: 
 d
Enter an answer: 
 f
What is your question?
 asdfv
1 //The array index number 
s //The array value for the array index above
What is your question?
 aefvg
0
a
What is your question?
 aegrv
0
a



Instead, I'm getting this:
How many responses would you like there to be?
 4 //user input
Enter an answer: 
 a
Enter an answer: 
 s
Enter an answer: 
 d
Enter an answer: 
 f
What is your question?
 asdfv
1 //The array index number 
null //The array value for the array index above
What is your question?
 aefvg
0
null
What is your question?
 aegrv
0
null



Is This A Good Question/Topic? 0
  • +

Replies To: Homework Help: Magic Eight Ball

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3638
  • View blog
  • Posts: 16,706
  • Joined: 20-September 08

Re: Homework Help: Magic Eight Ball

Posted 22 May 2019 - 01:32 AM

Quote

MagicEightBall ball = new MagicEightBall(numResponses);


Get rid of that in questions(). It creates a new instance, which you don't want

btw method names should be verbs, not nouns. So getAnswers() etc.

This post has been edited by g00se: 22 May 2019 - 01:33 AM

Was This Post Helpful? 2
  • +
  • -

#3 moonwolf332   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-May 19

Re: Homework Help: Magic Eight Ball

Posted 22 May 2019 - 06:00 AM

But then it gives me this error because I don't have it instanced there

Quote

/MagicEightBall.java:50: error: cannot find symbol
ball.shake(numResponses);
^
symbol: variable ball
location: class MagicEightBall
1 error

Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3638
  • View blog
  • Posts: 16,706
  • Joined: 20-September 08

Re: Homework Help: Magic Eight Ball

Posted 22 May 2019 - 06:05 AM

Well, obviously you need to get rid of that too (ball) ;)

This post has been edited by g00se: 22 May 2019 - 06:06 AM
Reason for edit:: Clarification

Was This Post Helpful? 2
  • +
  • -

#5 moonwolf332   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-May 19

Re: Homework Help: Magic Eight Ball

Posted 22 May 2019 - 06:25 AM

Okay so I got rid of that too and changed the code to try and get the array to each method by passing it as a parameter and it worked so thank you so much!!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1