Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5992883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:35:46+00:00 2026-05-22T23:35:46+00:00

I’m working on an assignment and it’s going fairly well, but I am confused

  • 0

I’m working on an assignment and it’s going fairly well, but I am confused about one thing so far. The point is to learn about inheritance and reading through existing code. Without adding another method, I need to make the getMove() method return the same random number three times in a row, and then pick a new random number and have it return the new number three times. etc. etc.

There are several other classes, including a class that maintains a count separate from the one I established here. If it would be useful to see any of those classes let me know and I’ll post them, but I think they are fairly irrelevant to the question.

Edit: Clarification

I need to make the getMove() method return one int per call. The 1st 3 calls should return the same randomInt. After that a new randomInt should be chosen, and that should be returned for the next three calls. This should repeat as long as it’s called.

The final solution:

public class Crab extends SeaCreature {
    private static final char CRAB = 'C';
    private int direction = rand.nextInt(4);
    private int count;
    /**
     * Construct a SeaCreature object with the given character representation
     * @param c  the character for this SeaCreature
     */
    public Crab(){
        super(CRAB);

    }

    /** Answers back the next move for this SeaCreature.
     * @return 0, 1, 2, or 3
     */
    public int getMove() {
        if (count < 3) {
            count ++;
            return direction;            
        }
        count = 1;
        direction = rand.nextInt(4);
        return direction;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:35:47+00:00Added an answer on May 22, 2026 at 11:35 pm

    Not that it’s a “problem”, but your fields should be instance fields (ie not static).

    However, to isolate the problem from your classes, here’s code that works. Note that you can get the same move on subsequent calls to rand().

    private static int direction = rand();
    private static int count;
    
    public static int getMove()
    {
        if (count < 3)
        {
            count++;
            return direction;
        }
    
        count = 0;
        direction = rand();
        return direction;
    }
    
    private static int rand()
    {
        return (int) (Math.random() * 4); // 0, 1, 2 or 3
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.