Docsity

Lab Exercise Questions - Problem Solving and Programming Concepts | CIS 121, Lab Reports of Computer Science

Toggle sidebar
bg1
CIS 121 Problem-solving and Programming Concepts II
Lab Exercise: Help with programming assignment
The following code is from chapter 7 of your textbook. Understanding this code should
help you with your current programming assignment. Your assignment requires you to have each
animal on a separate thread. Key in the following code, fill in the missing output statements,
compile and run it. What happens? How would you convert this to a multithreaded program?
// Exercise 7.41 Solution
// Race.java
// Program simulates the race between
// the tortoise and the hare
public class Race {
static final int RACE_END = 70;
static int tortoise = 1;
static int hare = 1;
public static void main( String args[] )
{
int timer = 0;
System.out.println( "ON YOUR MARK, GET SET" );
System.out.println( "BANG !!!!" );
System.out.println( "AND THEY'RE OFF !!!!" );
while ( tortoise != RACE_END && hare != RACE_END ) {
moveHare();
moveTortoise();
printCurrentPositions();
++timer;
}
if ( tortoise >= hare )
//add code for what happens here
//if while loop ends, what does that mean?
//What to print here?
}
public static void moveTortoise()
{
int x = ( int ) ( 1 + Math.random() * 10 );
int tortoiseMoves[] = { 3, 6 };
if ( x >= 1 && x <= 5 ) // fast plod
tortoise += tortoiseMoves[ 0 ];
else if ( x == 6 || x == 7 ) // slip
tortoise -= tortoiseMoves[ 1 ];
else // slow plod
++tortoise;
pf2

Partial preview of the text

Download Lab Exercise Questions - Problem Solving and Programming Concepts | CIS 121 and more Lab Reports Computer Science in PDF only on Docsity!

CIS 121 Problem-solving and Programming Concepts II

Lab Exercise: Help with programming assignment

The following code is from chapter 7 of your textbook. Understanding this code should

help you with your current programming assignment. Your assignment requires you to have each

animal on a separate thread. Key in the following code, fill in the missing output statements,

compile and run it. What happens? How would you convert this to a multithreaded program?

// Exercise 7.41 Solution // Race.java // Program simulates the race between // the tortoise and the hare public class Race { static final int RACE_END = 70; static int tortoise = 1; static int hare = 1; public static void main( String args[] ) { int timer = 0; System.out.println( "ON YOUR MARK, GET SET" ); System.out.println( "BANG !!!!" ); System.out.println( "AND THEY'RE OFF !!!!" ); while ( tortoise != RACE_END && hare != RACE_END ) { moveHare(); moveTortoise(); printCurrentPositions(); ++timer; } if ( tortoise >= hare )

//add code for what happens here

//if while loop ends, what does that mean?

//What to print here?

public static void moveTortoise() { int x = ( int ) ( 1 + Math.random() * 10 ); int tortoiseMoves[] = { 3, 6 }; if ( x >= 1 && x <= 5 ) // fast plod tortoise += tortoiseMoves[ 0 ]; else if ( x == 6 || x == 7 ) // slip tortoise -= tortoiseMoves[ 1 ]; else // slow plod ++tortoise;

if ( tortoise < 1 ) tortoise = 1; else if ( tortoise > RACE_END ) tortoise = RACE_END; } public static void moveHare() { int y = ( int ) ( 1 + Math.random() * 10 ); int hareMoves[] = { 9, 12, 2 }; if ( y == 3 || y == 4 ) // big hop hare += hareMoves[ 0 ]; else if ( y == 5 ) // big slip hare -= hareMoves[ 1 ]; else if ( y >= 6 && y <= 8 ) // small hop ++hare; else if ( y > 8 ) // small slip hare -= hareMoves[ 2 ]; if ( hare < 1 ) hare = 1; else if ( hare > RACE_END ) hare = RACE_END; } public static void printCurrentPositions() { for ( int count = 1; count <= RACE_END; count++ ) if ( count == tortoise && count == hare )

//What do you print here? What happens if the above

//condition is true?

else if ( count == hare )

//What happens if this is true?

//What code goes here?

else if ( count == tortoise )

//What happens if this is true?

//What code goes here?

else

//What happens if this is true?

//What code goes here?

System.out.println(); } }