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 6611917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:02:00+00:00 2026-05-25T20:02:00+00:00

I have a Java assignment in which my professor requires me to use a

  • 0

I have a Java assignment in which my professor requires me to use a LeJOS NXT to make a robot that simulates a certain animal’s behaviors. I chose to develop a dragon. All the possible behaviors that I’ve come up so far is:

  • Turning around if it’s too close to an obstacle.
  • Going to sleep when battery is low.
  • Pushing an object if touches.
  • If it’s too bright, find a dark spot.
  • etc.

I’m now quite confused because I don’t know whether to develop it sequentially in one class or to split all the dragon’s behaviors into different classes. Please have a look at my explanation below.

Instead of writing everything inside one class like this:

Dragon.java

public class Dragon {
   LightSensor ls = new LightSensor
   public static main(String args[]) {
       while (!BUTTON.Escape.IsPressed()) {
           if (this.closeToObject()) {
               this.turnAround();
           }

           // more conditions
       }
   }
   private boolean closeToObject() {
       //TODO
       return false;
   }
   private void turnAround() {
       //TODO
   }

   //... more methods
}

However, I want to make it appears to be more object-oriented as the course is meant to help us gain more OOP skills. So what my second option is to create action classes that extends Dragon’s Behavior abstract class like this (roughly):

Dragon.java

public class Dragon {
   Detect detect = new Detect();  // carry all the detection methods: distance, sound, etc.
   TurnAround turnAround = new TurnAround();
   public static main(String args[]) {
       while (!BUTTON.Escape.IsPressed()) {
           if (detect.tooCloseToObject()) {
               turnAround.prepare(); // beep beep alert
               turnAround.setDerection(true); // e.g. turn right
               turnAround.turn();
           }
       }
   }
}

DragonBehaviors.java

abstract class DragonBehavior {
    abstract void prepare();
    public void setDirection(boolean direction) {
        //...
    }
}

TurnAround.java

public class TurnAround extends DragonBehaviors {
    String direction;
    public void TurnAround() {}
    public void prepare() {
        // sound alert
    }
    public void setDirection(boolean direction) {
        if (direction) this.direction = "Right";
        else this.direction = "Left";
    }
    public void turn() {
        // TODO
    }
}

The code above is roughly a draft, don’t focus on it. Eventually, I want to ask if my idea about the OO structure above is reasonable, otherwise it’s much easier to develop the whole thing in one class, but it has nothing to do with OOP. I also have several group members to make the code finished, so I think it could be better if we share the classes to develop in an OOP way.

Which way should I go in this circumstance?

I appreciate all the comments (:

  • 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-25T20:02:01+00:00Added an answer on May 25, 2026 at 8:02 pm

    Your choice of extracting different actions into classes with common super class is IMHO reasonable. However I would make Dragon class only aware of the DragonBehavior abstract class, not the subclasses. This way you can add and remove behaviours to the dragon without actually changing it.

    How? Look at Chain-of-responsibility pattern – each behaviour has its place in the chain. If behaviour decides to activate itself (i.e. perform something) it may or may not allow further behaviours to be triggered. Moreover, you can and remove behaviours (even at runtime!) and rearrange them to change the precedence (is pushing the obstacle more or less important than going to sleep?).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a short java assignment that I have been set. The
I have an assignment in which I need to create a function that tells
I have a function in java which is written with a recursive algorithm that
We've gotten a homework assignment in Java, which relates to inheritance. I don't have
I have to write a multiplayer pacman game in Java for a university assignment
I have Java Map (out of Strings and Ints) objects that I want to
This maybe a related question: Java assignment issues - Is this atomic? I have
I'm working on a Java assignment that has to be done using AWT. I
I am completely new to Java and have an assignment coming up; with the
I am doing a college assignment in Java that deals with currency. For that

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.