Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

4. (10 points) Left Initial Position Right Write a class Bug that models a bug moving along a horizontal line. The bug moves either to

image text in transcribed

4. (10 points) Left Initial Position Right Write a class Bug that models a bug moving along a horizontal line. The bug moves either to the right or left. Initially, the bug moves to the right, but it can turn to change its direction. In each move, its position changes by one unit in the current direction. Declare two instance variables: current Position and direction Provide a constructor public Bug (int initialPosition) and accessor/mutator methods public int getPosition() public void move() public void turn() Sample code for testing: Bug bugsy = new Bug (10); //starts at initial position 10 bugsy.move(); // Now the position is 11 bugsy.move(); // Now the position is 12 System.out.println (bugsy.getPosition()); //should print 12 bugsy.turn(); // Now the direction has changed bugsy.move(); // Now the position is 11 System.out.println (bugsy.getPosition() ); //should print 11

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands On Database

Authors: Steve Conger

1st Edition

013610827X, 978-0136108276

More Books

Students also viewed these Databases questions

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago