Programming  >  SOPHIA Milestone  >  Virginia Tech - CS 1114 Shark.java (All)

Virginia Tech - CS 1114 Shark.java

Document Content and Description Below

import sofia.micro.*; import java.util.List; import java.util.ArrayList; //------------------------------------------------------------------------- /** * A shark that chases and eats sardines. ... * * @author Emily Ashburn (emily2000) * @version (2019.03.26) */ public class Shark extends Actor { //~ Fields ................................................................ private List<Actor> stomach; //~ Constructor ........................................................... // ---------------------------------------------------------- /** * Creates a new Shark object. */ public Shark() { super(); stomach = new ArrayList<Actor>(); } //~ Methods ............................................................... // ---------------------------------------------------------- /** * Calculate the distance to another actor. * * @param actor The other actor. * @return The distance from this shark to the other actor. */ public double distanceTo(Actor actor) { int xDistance = actor.getGridX() - this.getGridX(); int yDistance = actor.getGridY() - this.getGridY(); return (Math.sqrt((xDistance * xDistance) + (yDistance * yDistance))); } // ---------------------------------------------------------- /** * Find the sardine nearest to this shark. * * @return The sardine that is closest, if there is one, or * null if there are no more sardines in the sea. */ public Sardine nearestSardine() { Sardine nearest = null; double distance = 600; for (Sardine sardine: this.getWorld().getObjects(Sardine.class)) { if (this.distanceTo(sardine) < distance) { distance = this.distanceTo(sardine); nearest = sardine; } } return nearest; } https://www.coursehero.com/file/54213464/Sharkjava/ This study resource was shared via CourseHero.com// ---------------------------------------------------------- /** * Eat a sardine by removing it from the world and adding it * to this shark's stomach contents. * * @param sardine The sardine to eat. */ public void eat(Sardine sardine) { sardine = this.getOneIntersectingObject(Sardine.class); stomach.add(sardine); sardine.remove(); } // ---------------------------------------------------------- /** * Sharks move towards the nearest sardine, eating it if they * reach it. */ public void act() { if (this.nearestSardine() != null) { this.turnTowards(nearestSardine()); this.move(1); } if (this.getOneIntersectingObject(Sardine.class) != null) { this.eat(nearestSardine()); } } /** * stomach contents * @return List this is a getStomachContents() */ public List<Actor> getStomachContents() { return (stomach); [Show More]

Last updated: 3 years ago

Preview 1 out of 2 pages

Buy Now

Instant download

We Accept:

Payment methods accepted on Scholarfriends (We Accept)
Preview image of Virginia Tech - CS 1114 Shark.java document

Buy this document to get the full access instantly

Instant Download Access after purchase

Buy Now

Instant download

We Accept:

Payment methods accepted on Scholarfriends (We Accept)

Reviews( 0 )

$5.00

Buy Now

We Accept:

Payment methods accepted on Scholarfriends (We Accept)

Instant download

Can't find what you want? Try our AI powered Search

155
0

Document information


Connected school, study & course


About the document


Uploaded On

Mar 20, 2021

Number of pages

2

Written in

All

Seller


Profile illustration for QuizMaster
QuizMaster

Member since 6 years

1194 Documents Sold

Reviews Received
185
56
29
11
17
Additional information

This document has been written for:

Uploaded

Mar 20, 2021

Downloads

 0

Views

 155

Document Keyword Tags


$5.00
What is Scholarfriends

Scholarfriends.com Online Platform by Browsegrades Inc. 651N South Broad St, Middletown DE. United States.

We are here to help

We're available through e-mail, Twitter, Facebook, and live chat.
 FAQ
 Questions? Leave a message!

Follow us on
 Twitter

Copyright © Scholarfriends · High quality services·