Program Modeling Flea Experiment in the small Java world

Source: Internet
Author: User

Package Com.chigoe;
House class
Class House {
private int m;//number of rows saved
private int n;//Save column Count
Private int[][] A;

Public house () {//No parameter construction method
m = 10;
n = 10;
for (int i = 0; i < m; i++)
for (int j = 0; J < N; j + +) {
A[I][J] = 0;
}
}
public house (int m,int n) {//With parameter construction method
This.m=n;this.n=n;
A=new Int[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; J < N; j + +) {
A[I][J] = 0;
}
}
public int Getm () {
return m;
}
public int Getn () {
return n;
}
Public int[][] Geta () {
return A;
}
public int getelment (int i,int j) {
return A[I][J];
}
public void setelment (int i,int J,int v) {
A[i][j]=v;
}
Determine if the flea has gone through so the tiles
public Boolean Checkzero () {
for (int i = 0; i < m; i++)
for (int j = 0; J < N; j + +) {
if (a[i][j]==0) {
return true;
}
}
return false;
}
Iterating through an array
public void display () {
for (int i = 0; i < m; i++) {
for (int j = 0; J < N; j + +) {
System.out.print ("+a[i][j]+");
}
System.out.println ();//Meet a line of tiles to wrap
}
}

}

Flea class
public class Tiaozao {

private static final int up=0;
private static final int down=1;
private static final int right=2;
private static final int left=3;
private int x, y;
private int totals;//Save total number of times
Private house ahouse;//that the member variables of this class are themselves, that is, an object of their own.
Public Tiaozao (House h) {
Ahouse=h;
totals=0;
x= (int) (Math.random () *ahouse.getm ());//should be the initial location of randomly generated fleas
y= (int) (Math.random () *ahouse.getn ());

}
public int gettotals () {
return totals;
}
public boolean walk (int direction) {
System.out.println ("x=" +x+ ", y=" +y+ "direction=" +direction ");
switch (direction) {
Case Up:if (y==0) return false;
else{
Ahouse.setelment (x, Y, ahouse.getelment (x, y) +1);
Y=y-1;
return true;
}
Case Down:if (Y==AHOUSE.GETN ()-1) return false;
else{
Ahouse.setelment (x, Y, ahouse.getelment (x, y) +1);
y=y+1;
return true;
}
Case Left:if (x==0) return false;
else{
Ahouse.setelment (x, Y, ahouse.getelment (x, y) +1);
X=x-1;
return true;
}
Case Right:if (X==ahouse.getm ()-1) return false;
else{
Ahouse.setelment (x, Y, ahouse.getelment (x, y) +1);
x=x+1;
return true;
}

Default
SYSTEM.OUT.PRINTLN ("illegal movement");
return true;
}
}
public void Move () {
int nexdirection;
Boolean success;
do{
nexdirection= (int) (Math.random ());
Success=walk (nexdirection);
if (success) {
totals++;
}
}while (Ahouse.checkzero ());
}
public static void Main (string[] args) {

House Ahouse=new House (BIS);
Tiaozao atiaozao=new Tiaozao (ahouse);//object as a parameter, reference data passing
Atiaozao.move ();
Ahouse.display ();
System.out.println ("totals =" +atiaozao.gettotals ());

}

}

Program Modeling Flea Experiment in the small Java world

Related Article

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.