Ssd1 exercise5 answer

Source: Internet
Author: User
Tags java decompiler

It seems that when I was a freshman, I used DJ Java decompiler to decompile the. Class file that icarnegie gave and copied some code. The code I have given now does not seem to be compiled. Please be careful.
Import java. util .*;
/*
* Created on Jan 6, 2004
*
*/

/**
* Basic catfish-simulates a catfish-can swim and consume
* Energy in the process.
*
* @ Author icarnegie SRT
*
*/
Public class catfish extends livingbeing {

/**
* The catfish is born "alive ".
* Then it dies, becoming a corpse.
*/
Private string alive = "alive ";

/**
* The catfish is born "alive ".
* Then it dies, becoming a "dead" corpse.
*/
Private string dead = "dead ";

/**
* Energy needed to swim in a block of time.
*/
Private int energy_to_swim = 2;



/**
* Row-wise location of the catfish
*/
Private int row;

/**
* Column-wise location of the catfish
*/
Private int column;

/**
* Image of the catfish-is really a filename
*/
Private string imagefilename;


/**
* Is the catfish dead or alive?
*/
Private string deadoralive;

/**
* Age expressed as blocks of time lived
*/
Private int age;
Private int energy;

/**
* The simulation to which this catfish belongs.
* This is needed so the catfish can send a message
* To simulation.
*/
Private simulation;

/**
* Constructor. initialize a catfish to start life at a specified
* Location with a specified energy. If location is out of bounds,
* Locate the catfish at the nearest edge.
*
* @ Param initialrow-the row at which the catfish is located
* @ Param initialcolumn-the column at which the catfish is located
* @ Param initialsimulation-the simulation that the catfish belongs
*/
Public Catfish (
Int initialrow,
Int initialcolumn,
Simulation initialsimulation ){

Simulation = initialsimulation;

Deadoralive = alive;

Age = 0;
Energy = 10;

Imagefilename = "/Catfish-right.gif ";

Row = initialrow;
Column = initialcolumn;
}

/**
* Get the row at which the catfish is located
*
* @ Return-the row of the catfish's location.
*/
Public int getrow (){
Return row;
}

/**
* Get the column at which the catfish is located
*
* @ Return-the column of the catfish's location.
*/
Public int getcolumn (){
Return column;
}

/**
* Get filename of catfish Image
*
* @ Return filename of catfish Image
*/
Public String getimage (){
Return imagefilename;
}

/**
* Get the catfish's age
*
* @ Return the age of the catfish expressed in blocks of time
*/
Public int getage (){

Return age; // Student: Please complete this method.
}

/**
* Get the energy of the catfish
*
* @ Return current energy level of the catfish
*/
Public int getenergy (){

Return energy; // Student: Please complete this method.
}

/**
* Die: The Catfish dies.
*/
Public void die (){

// Student: Please complete this method.

Deadoralive = dead; // set the deadoralive attribute to indicate that the catfish is dead.
}

/**
* Is the catfish dead?
*
* @ Return <code> true </code> if dead. <code> false </code>, otherwise.
*/
Public Boolean isdead (){

If (deadoralive = dead)
Return true;
Else
Return false;
}

/**
* Swim to a new location if possible.
* Consumes some energy.
*/
Private void swimifpossible (){

// Student: Please complete this method.
Energy = energy-ENERGY_TO_SWIM; // consume energy_to_swim units of energy to swim.

If (Energy <= 0) // check if there is any energy left after consumption.
{
Return;
} Else
{// Swim at random in one of four ctions.
Row = simulation. getrand (). nextint (10); // assign a random row location for the catfish as follows.
// (1) Send the "getrand" message to the "simulation" object to get a random number generator.
// The "simulation" object is initialized in the constructor above.
// (2) Send the "nextint" message to the random number generator obtained above.
// The "nextint" behavior returns an integer between 0 (aggressive) and the integer specified as a parameter (exclusive ).
// Thus, specifiying a value of 10 to the "nextint" behavior will return an integer between 0 and 9.
Column = simulation. getrand (). nextint (10); // Similarly, assign a random column location for the catfish
Return;
}

}

/**
* Catfish lives its life. dies if it has no energy left.
*/
Public void livealittle (){

// Student: Please complete this method.

If (Energy <= 0)
{
Die ();
Return;
Die (); // if there is no energy left, send a "die" message to this catfish
Return;
} Else
{
Age = age + 1; // The Age Of the catfish by 1
Swimifpossible (); // try to swim by sending a "swimifpossible" Message
Return;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.