السلام عليكــم ورحمـة الله وبركاتــة
اتمنى مساعدة من يملك القدره على المساعده ....انا طالبة وادرس برمجه والحين تواجهني مشكلة حاولت التوصل لحلها ولكن لم اتمكن من ايجاد الحل ...
فأتمنى ان يساعدني من يملك القدره على ذلك ... شاكرا جميع الاعضاء سلفا...
سأرفق جميع ما تحتاجوه لفهم المسأله
هذا هو السؤال او الاسايمنت:::
Description:
In this assignment you are required to write a java program that takes as an input a twodimensional
array whose elements represent cells of certain colors. A cell could be one of two
different colors only: either green or white. The color “green” will be used to indicate an
abnormal cell, whereas “white” will indicate a normal cell. There should be more normal cells
than abnormal ones. The task of the program is to find the number of abnormal cells that are in
the same continuous area as a given cell (i.e cell whose coordinates will be specified). Your
program should implement a recursive method to achieve this task.
Sample output:
The following is a sample output of what you are expected to achieve. Your program must
behave exactly the same.
(Run 1)
(Run 2)
As you can see from the above two runs, the user will be asked to specify the size of the twodimensional
array. So if the user inputs the value (5), then the array should be of the size [5][5].
The array will then be displayed showing the color of each cell (“g” for green and “w” for
white). The code for drawing the array is provided in one of the given files. However, before
you can display the array, of course you have to initialize its elements. The elements of the
array are instances of the class “Cell” which is included as one of the three files. This class must
have one data field only:
private String color;
It must also implement the interface “Interface1” which provides all the constants needed and
the signatures of the methods that must be implemented by the “Cell” class.
Instructions:
1- You are not allowed to change anything in the interface “Interface1” such
as changing the name of a constant or adding more methods.
2- You are not allowed to create more files than what you are already
provided. So submit only three files.
وهذا هو ما قمت بعمله للان
اولا :Intrface class
/**
* Interface1 defines some constants and declares
* some methods that must be implemented by the
* class "Cell". The purpose of each method is
* described below. Note the statement that says
* public class Cell implements Interface1
*/
public interface Interface1 {
final String normal="white";
final String abnormal="green";
final String temp="red";
/**
* Method initializeColor will be used to initialize the color
* of each cell in the two-dimensional array.
* The cell can be one of two colors: "green" or "white". Whether
* a cell will be "white" or "green" should be decided randomly.
* Since the assignment specifies that there should be more normal
* (white) cells than abnormal (green) cells, you should devise an
* algorithm that assigns:
* white: 70% of the time
* green: 30% of the time
* (hint:you can use Math.random() for this purpose)
*/
public void initializeColor();
/**
* The method setColor is a typical setter method that
* sets the color of a given cell to a certain color.
* This method is needed especially to re-color a cell
* to a temporary color (i.e "red")
*/
public void setColor(String color);
/**
* The method getColor will return the color of the
* specified cell. This method is called to identify
* the color of the cell.
*/
public String getColor();
}
ثانيا: CellClass
public class Cell implements Interface1 {
private String color;
public String getColor() {
// TODO Auto-generated method stub
return color;
}
public void initializeColor() {
int num =(int)Math.random()*10;
if(num<=6)
color="withe";
else
color="green";
}
public void setColor(String color) {
this.color=color;
}
}
ثالثا: MainClass
import java.util.*;
public class mainClass {
public static void main(String []args){
final String normal="white";
final String abnormal="green";
final String temp="red";
Scanner input = new Scanner(System.in);
System.out.println("Please enter the size of the array");
int size = input.nextInt();
int w;
int g;
Cell[][] cells= new Cell;
for(int x=0; x<cells.length; x++){
for(int y=0; y<cells[x].length; y++){
cells[x][y]=(countCells(cells,x,y,size) > 0);
}
}
System.out.println("Please enter the coordinates of the desired cell");
displayArray(cells,size);
System.out.print("x: ");
int x = input.nextInt();
System.out.print("y: ");
int y = input.nextInt();
int countCells;
System.out.println("Number of cells in the blob is: " + countCells);
}//end main
/*
*The method countCells is a RECURSIVE method whose job is to find
*how many cells are in the same blob as a given cell (i.e cell with
*specified coordinates x and y). The variable int size represents the
*size of the two-dimensional array. This value would have been provided
*by the user at the beginning of the main method.
*Do NOT make any changes to the method declartion.
*/
public static int countCells(Cell [][]cells,int x,int y,int size){
if(x ==0 || y ==0 || x >=cells.length || y >=cells.length){
return 0;
}
else if (cells[x][y].abnormal == "green"){
return 1
+ countCells(cells, x-1, y+1, size)+ countCells(cells, x, y+1, size)
+ countCells(cells, x+1, y+1, size)+ countCells(cells, x-1, y, size)
+ countCells(cells, x+1, y, size)+ countCells(cells, x-1, y-1, size)
+ countCells(cells, x, y-1, size)+ countCells(cells, x+1, y-1,size);
}
return size;
}//countCells()
public static void displayArray(Cell [][]cells,int size){
for(int i=0;i<size;i++){
System.out.println();
System.out.print("-");
for(int j=0;j<size;j++){
System.out.print("-+");
}
System.out.println();
for (int j=0;j<size;j++){
String color=cells[j].getColor();
System.out.print("|"+color.charAt(0));
}
System.out.print("|");
}//outer
System.out.print("\n-");
for(int j=0;j<size;j++){
System.out.print("-+");
}
}//end displayArray()
}
واعتقد ان هذا الكلاس هو الذي يحتوي على الخطأ ولكن لم اتمكن من تعديله .. ارجو مساعدتكم في اقصى وقت ممكن
واعتذر عن عدم ارفاق الصور ,....
