اجرای یک برنامه جاوا

me.fatima

عضو جدید
کاربر ممتاز
سلام

من این مسئله رو تو کتاب Java How to Program دیدم ( فصل 7 ، سوال 21 آخر فصل ) و اصلا منظور سوال رو متوجه نشدم. ممنون میشم اگه کسی کمکم کنه ( من تازه اول راه برنامه نویسی)

سوال به زبان پارسی :

7.21 :

زبان Logo که از محبوبیت خاصی در مدارس ابتدایی برخوردار است ، از مفهوم لاک پشت گرافیکی استفاده کرده است. تصور کنید که یک لاک پشت مکانیکی در فضایی حرکت می کند که در کنترل یک برنامه جاوا است. لاک پشت مدادی را به یکی از دو جهت ، بالا یا پایین حرکت می دهد. زمانیکه مداد به طرف پایین کشیده می شود، لاک پشت اشکالی را ترسیم می کند. با حرکت مداد به سمت بالا، لاک پشت آزادانه بدون ترسیم چیزی حرکت می کند. در این مسئله می خواهیم حرکت لاک پشت را شبیه سازی کرده و صفحه طراحی کامپیوتری شده ای را هم برای آن ایجاد کنید.

از آرایه 20 در 20 بنام floor استفاده کنید که در ابتدا با صفر مقداردهی اولیه شده است. دستورات از آرایه ای خوانده شود که حاوی آنها است. همیشه مسیر و موقعیت جاری لاک پشت را خواه مداد بالا باشد یا پایین ، داشته باشید. فرض کنید لاک پشت از موقعیت ( 0,0 ) با مداد بالا حرکت خود را آغاز می کند. مجموعه دستورات لاک پشت که باید برنامه مبادرت به پردازش آنها کند در جدول زیر آورده شده اند. فرض کنید که لاک پشت جای در نزدیکی مرکز صفحه قرار دارد. برنامه زیر باید یک مربع 12 در 12 ترسیم و چاپ کرده و با بالا رفتن مداد به کار پایان دهد :
2
5,12
3
5,12
3
5,12
3
5,12
1
6
9

همانطوری که لاک پشت با مداد پایین حرکت می کند ، عناصر مقتضی آرایه floor با 1 تنظیم می شوند. زمانیکه دستور 6 اعمال می شود( چاپ )، هرجا که 1 در آرایه وجود داشته باشد ، یک کاراکتر ستاره یا کاراکتر دلخواه به نمایش در آید. هرجا که صفر در آرایه وجود داشته باشد ، یک جای خالی به نمایش درآید. برنامه ای بنویسید که لاک گرافیکی را با قابلیت های فوق پیاده سازی کند.


[FONT=&quot]دستور[/FONT]​
[FONT=&quot]مفهوم دستور[/FONT]
1​
[FONT=&quot]مداد بالا[/FONT]
2​
[FONT=&quot]مداد پایین[/FONT]​
3​
[FONT=&quot]گردش به راست[/FONT]​
4​
[FONT=&quot]گردش به چپ[/FONT]​
5,10​
Move forward 10 spaces (replace 10​
for a different number of spaces)​
6​
Display the 20-by-20 array​
9​
End of data (sentinel)


سوال به انگلیسی :

Turtle Graphics) The Logo language made the concept of turtle graphics famous. Imagine a)

mechanical turtle that walks around the room under the control of a Java application. The turtle
holds a pen in one of two positions, up or down. While the pen is down, the turtle traces out shapes
as it moves, and while the pen is up, the turtle moves about freely without writing anything. In this
problem, you will simulate the operation of the turtle and create a computerized sketchpad.


Use a 20-by-20 array floor that is initialized to zeros. Read commands from an array that
contains them. Keep track of the current position of the turtle at all times and whether the pen is
currently up or down. Assume that the turtle always starts at position (0, 0) of the floor with its
pen up. The set of turtle commands your application must process are shown in Fig. 7.31.
Suppose that the turtle is somewhere near the center of the floor. The following “program”
would draw and display a 12-by-12 square, leaving the pen in the up position:

2
5,12
3
5,12
3
5,12
3
5,12
1
6
9


As the turtle moves with the pen down, set the appropriate elements of array floor to 1s. When the
6 command (display the array) is given, wherever there is a 1 in the array, display an asterisk or any
character you choose. Wherever there is a 0, display a blank.
Write an application to implement the turtle graphics capabilities discussed here. Write several
turtle graphics programs to draw interesting shapes. Add other commands to increase the power of
your turtle graphics language.



تو اینترنت به انگلیسی هم جستجو کردم یه جا دیدم که کدش رو گذاشته بودند :


import java.util.Scanner;

public class Turtle {
/**
* Directions: 0 right, 1 down, 2 left, 3 up
*/
private static short direction = 0;
private static boolean penDown;
private static int turtleX = 0, turtleY = 0;
private static int[][] floor = new int[20][20];

public static void main(String[] args) {
initFloor(floor);
Scanner in = new Scanner(System.in);
printMenu();
int nextCommand = in.nextInt();
while (nextCommand != 9) {
switch (nextCommand) {
case 1:
penDown = false;
break;
case 2:
penDown = true;
break;
case 3:
direction++;
break;
case 4:
direction--;
break;
case 5:
System.out.println("How many steps do you want to move?");
int move = in.nextInt();
if (move <= 10)
while (--move != 0)
move();
break;
case 6:
printArray();
break;
default:
System.err.println("Unknow command, please try again:\n");
break;
}
move();
System.out.println("What's next?");
nextCommand = in.nextInt();
}
}

private static void initFloor(int[][] floor) {
for (int i = 0; i < floor.length; i++) {
for (int j = 0; j < floor.length; j++) {
floor[j] = 0;
}
}
}

private static void printMenu() {
System.out
.println("Commands List:\n\n\t1 Pen up\n"
+ "\t2 Pen down\n"
+ "\t3 Turn right\n"
+ "\t4 Turn left\n"
+ "\t5 to 10 Move forward 10 spaces (replace 10 for a different number of spaces)\n"
+ "\t6 Display the 20-by-20 array\n"
+ "\t9 End of data (sentinel)Please enter a command number:\n");
}

private static void printArray() {
for (int i = 0; i < floor.length; i++) {
for (int j = 0; j < floor.length; j++) {
System.out.print(floor[j]);
System.out.print(" ");
}
System.out.println();
}
}

private static void move() {
switch (direction) {
case 0:
turtleX++;
break;
case 1:
turtleY++;
break;
case 2:
turtleX--;
break;
case 3:
turtleY--;
break;
default:
if (direction < 0)
direction = 3;
else
direction = 4;
move();
break;
}
if (penDown) {
if (turtleX < 20 && turtleY < 20)
floor[turtleX][turtleY] = 1;
else {
direction -= 2;
move();
}
}
}
}

که من تو Net Beans نتونستم اجراش کنم.
 

ASILTASH

عضو جدید
سلام

من این مسئله رو تو کتاب Java How to Program دیدم ( فصل 7 ، سوال 21 آخر فصل ) و اصلا منظور سوال رو متوجه نشدم. ممنون میشم اگه کسی کمکم کنه ( من تازه اول راه برنامه نویسی)

سوال به زبان پارسی :

7.21 :

زبان Logo که از محبوبیت خاصی در مدارس ابتدایی برخوردار است ، از مفهوم لاک پشت گرافیکی استفاده کرده است. تصور کنید که یک لاک پشت مکانیکی در فضایی حرکت می کند که در کنترل یک برنامه جاوا است. لاک پشت مدادی را به یکی از دو جهت ، بالا یا پایین حرکت می دهد. زمانیکه مداد به طرف پایین کشیده می شود، لاک پشت اشکالی را ترسیم می کند. با حرکت مداد به سمت بالا، لاک پشت آزادانه بدون ترسیم چیزی حرکت می کند. در این مسئله می خواهیم حرکت لاک پشت را شبیه سازی کرده و صفحه طراحی کامپیوتری شده ای را هم برای آن ایجاد کنید.

از آرایه 20 در 20 بنام floor استفاده کنید که در ابتدا با صفر مقداردهی اولیه شده است. دستورات از آرایه ای خوانده شود که حاوی آنها است. همیشه مسیر و موقعیت جاری لاک پشت را خواه مداد بالا باشد یا پایین ، داشته باشید. فرض کنید لاک پشت از موقعیت ( 0,0 ) با مداد بالا حرکت خود را آغاز می کند. مجموعه دستورات لاک پشت که باید برنامه مبادرت به پردازش آنها کند در جدول زیر آورده شده اند. فرض کنید که لاک پشت جای در نزدیکی مرکز صفحه قرار دارد. برنامه زیر باید یک مربع 12 در 12 ترسیم و چاپ کرده و با بالا رفتن مداد به کار پایان دهد :
2
5,12
3
5,12
3
5,12
3
5,12
1
6
9

همانطوری که لاک پشت با مداد پایین حرکت می کند ، عناصر مقتضی آرایه floor با 1 تنظیم می شوند. زمانیکه دستور 6 اعمال می شود( چاپ )، هرجا که 1 در آرایه وجود داشته باشد ، یک کاراکتر ستاره یا کاراکتر دلخواه به نمایش در آید. هرجا که صفر در آرایه وجود داشته باشد ، یک جای خالی به نمایش درآید. برنامه ای بنویسید که لاک گرافیکی را با قابلیت های فوق پیاده سازی کند.


دستور
مفهوم دستور
1​
مداد بالا
2​
مداد پایین
3​
گردش به راست
4​
گردش به چپ
5,10​
Move forward 10 spaces (replace 10​
for a different number of spaces)​
6​
Display the 20-by-20 array​
9​
End of data (sentinel)


سوال به انگلیسی :

Turtle Graphics) The Logo language made the concept of turtle graphics famous. Imagine a)

mechanical turtle that walks around the room under the control of a Java application. The turtle
holds a pen in one of two positions, up or down. While the pen is down, the turtle traces out shapes
as it moves, and while the pen is up, the turtle moves about freely without writing anything. In this
problem, you will simulate the operation of the turtle and create a computerized sketchpad.


Use a 20-by-20 array floor that is initialized to zeros. Read commands from an array that
contains them. Keep track of the current position of the turtle at all times and whether the pen is
currently up or down. Assume that the turtle always starts at position (0, 0) of the floor with its
pen up. The set of turtle commands your application must process are shown in Fig. 7.31.
Suppose that the turtle is somewhere near the center of the floor. The following “program”
would draw and display a 12-by-12 square, leaving the pen in the up position:

2
5,12
3
5,12
3
5,12
3
5,12
1
6
9


As the turtle moves with the pen down, set the appropriate elements of array floor to 1s. When the
6 command (display the array) is given, wherever there is a 1 in the array, display an asterisk or any
character you choose. Wherever there is a 0, display a blank.
Write an application to implement the turtle graphics capabilities discussed here. Write several
turtle graphics programs to draw interesting shapes. Add other commands to increase the power of
your turtle graphics language.



تو اینترنت به انگلیسی هم جستجو کردم یه جا دیدم که کدش رو گذاشته بودند :


import java.util.Scanner;

public class Turtle {
/**
* Directions: 0 right, 1 down, 2 left, 3 up
*/
private static short direction = 0;
private static boolean penDown;
private static int turtleX = 0, turtleY = 0;
private static int[][] floor = new int[20][20];

public static void main(String[] args) {
initFloor(floor);
Scanner in = new Scanner(System.in);
printMenu();
int nextCommand = in.nextInt();
while (nextCommand != 9) {
switch (nextCommand) {
case 1:
penDown = false;
break;
case 2:
penDown = true;
break;
case 3:
direction++;
break;
case 4:
direction--;
break;
case 5:
System.out.println("How many steps do you want to move?");
int move = in.nextInt();
if (move <= 10)
while (--move != 0)
move();
break;
case 6:
printArray();
break;
default:
System.err.println("Unknow command, please try again:\n");
break;
}
move();
System.out.println("What's next?");
nextCommand = in.nextInt();
}
}

private static void initFloor(int[][] floor) {
for (int i = 0; i < floor.length; i++) {
for (int j = 0; j < floor.length; j++) {
floor[j] = 0;
}
}
}

private static void printMenu() {
System.out
.println("Commands List:\n\n\t1 Pen up\n"
+ "\t2 Pen down\n"
+ "\t3 Turn right\n"
+ "\t4 Turn left\n"
+ "\t5 to 10 Move forward 10 spaces (replace 10 for a different number of spaces)\n"
+ "\t6 Display the 20-by-20 array\n"
+ "\t9 End of data (sentinel)Please enter a command number:\n");
}

private static void printArray() {
for (int i = 0; i < floor.length; i++) {
for (int j = 0; j < floor.length; j++) {
System.out.print(floor[j]);
System.out.print(" ");
}
System.out.println();
}
}

private static void move() {
switch (direction) {
case 0:
turtleX++;
break;
case 1:
turtleY++;
break;
case 2:
turtleX--;
break;
case 3:
turtleY--;
break;
default:
if (direction < 0)
direction = 3;
else
direction = 4;
move();
break;
}
if (penDown) {
if (turtleX < 20 && turtleY < 20)
floor[turtleX][turtleY] = 1;
else {
direction -= 2;
move();
}
}
}
}

که من تو Net Beans نتونستم اجراش کنم.


سلام تو کتاب وو که تو تاپیک اول راه جاوا معرفی کردم نویسنده خودش یه پکیج به نام galapagos درست کرده که چیزهایی که مد نظر شماست و با امکانات بیشتر توش هست مثلا میتونید اشکال گرافیکی رسم کنید و...
بهتون پیشنهاد میکنم این پکیجو دانلود کنید .یه سری مثال داره که اگه اونارو اجرا کنین تا حدودی روال کار و دستوراتش دستتون میاد.(میتونید از همون وبلاگ دانلود کنین)
 

me.fatima

عضو جدید
کاربر ممتاز
سلام تو کتاب وو که تو تاپیک اول راه جاوا معرفی کردم نویسنده خودش یه پکیج به نام galapagos درست کرده که چیزهایی که مد نظر شماست و با امکانات بیشتر توش هست مثلا میتونید اشکال گرافیکی رسم کنید و...
بهتون پیشنهاد میکنم این پکیجو دانلود کنید .یه سری مثال داره که اگه اونارو اجرا کنین تا حدودی روال کار و دستوراتش دستتون میاد.(میتونید از همون وبلاگ دانلود کنین)

باشه . ممنون . اگه تونستم دانلود میکنم ...
 

Similar threads

بالا