首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
讲解CSC71001编程、Programming程序辅导、Java、c++编程讲解 解析Java程序|解析SPSS
项目预算:
开发周期:
发布时间:
要求地区:
Programming I
CSC71001 Module 8 - Page 1
Module 8
More about Loops
Introduction
Note that it is assumed that you have completed the work up until this point, and are familiar
with creating objects, adding code, using setImage and other Greenfoot class and object
methods. If you have not completed the work up until now, please go back and make sure you
are familiar with it all, before going further.
This week we will not be using your textbook. We will instead be working on some example files
to concentrate on the mechanics of looping and using loops to produce various results.
Study materials
Materials required:
• The free Greenfoot programming environment;
• Files in "Module08" folder on MySCU site;
• Internet access to access your lecture, and any other material.
CSC71001 Module 8 - Page 2
Objectives
On completion of this topic, you should be able to:
• create and use for loops
• create and use constant values
• create use nested loops (loops within loops)
• build different patterns of objects in the world, using loops and other coding constructs
Concepts
• for loops
• initialisation
• terminating condition
• increment
• decrement
• variable++
• variable--
• constant
• static final
CSC71001 Module 8 - Page 3
Building an army
Before you start
Watch 'Concept Video 8-1 Review of Module 7
Our purpose this week will be to build an “army” of objects in various configurations, using loops.
Along the way, we will use various concepts that you have encountered in the previous modules,
as well as new concepts.
Activity 8-1 A sample scenario
Open the scenario 8-1.gfar from the MySCU site.
Write down the classes in the scenario:
____________________________________________________________________________
Open the Alien class in the editor. Fill out the comment at the top of the class. You will notice that
this class doesn't do anything (yet).
Open the SpaceWorld class. Change the comment at the top of the class to your name and your
version number.
Adding an alien
The first step is to add some aliens into our scenario.
To add objects to our world, we use the addObject method inherited from the World class.
If you check the Greenfoot documentation (Help Menu -> Greenfoot Class Documentation), you
will see the signature of the addObject method:
addObject(Actor object, int x, int y)
We can add a new Alien object at a random location in the world using the following code:
addObject(new Alien(), Greenfoot.getRandomNumber(600),
Greenfoot.getRandomNumber(400));
Activity 8-2 Adding an object
Using the scenario from Activity 8-1, add code in the SpaceWorld constructor to add one object in
a random location in the world.
Test by pressing the reset button several times.
CSC71001 Module 8 - Page 4
Adding more aliens
We could now add more aliens manually, by adding more lines of code (repeating them). There is
a simpler way to do this – a loop. If you remember in the last module we looked at while loops,
which repeat until a particular condition is true.
While loops have an initialisation, a condition that will terminate the loop when it becomes false
and an increment or decrement that will change the loop counter/variable.
A while loop always looks like this:
//declaration of a loop variable
while (condition)
{
// activity of the loop
// incrementing or decrementing the loop variable
}
An example is:
int i = 0;
int sum = 0;
while (i < 6)
{
sum = sum + i;
i = i + 1;
}
Activity 8-3
1. What would the value of sum be at the end of this loop? _____________
2. What would the value of i be at the end of this loop? ______________
3. How many times is the body of the loop executed? _______________
If we wanted to use a while loop to create a number of objects, we could combine the code that
we have used in Activity 8-2, with a while loop.
Activity 8-4 Adding several random aliens using a while loop
Using the scenario from Activity 8-2, alter the SpaceWorld constructor with a while loop to add 8
aliens in random locations in the world.
Test by pressing the reset button several times.
Watch 'Concept Video 8-2 Debugging While Loops
CSC71001 Module 8 - Page 5
Increments and Decrements
Until now, we have been adding one to the current value of a variable using:
i = i + 1;
We have been deleting one from the current value of a variable using:
i = i – 1;
Looking more carefully at this, we can see that we are querying the current value of a particular
variable, adding (or subtracting) one to it, and then storing the result in the same variable,
overwriting the previous value. This activity is so common, that there is a shortcut provided in
Java and many other languages for this operation.
i = i + 1; can be shortened to i++;
i = i – 1; can be shortened to i—-;
Our code in the SpaceWorld constructor can be shortened to:
int alienCount = 0;
while (alienCount < 8)
{
addObject(new Alien(), Greenfoot.getRandomNumber(600),
Greenfoot.getRandomNumber(400));
alienCount++;
}
Activity 8-5 Adding several random aliens using shortened code
Using the scenario from Activity 8-4, alter the SpaceWorld constructor while loop to use the simpler
increment. Test that it is still working correctly by pressing the reset button several times.
We can now refactor this code by creating a new SpaceWorld method called "createAliens".
Create a new method createAliens that does not return anything, and does not have any
parameters. Place the alien creation code in this method, and call it from the SpaceWorld
constructor. Test your code with the reset button several times.
CSC71001 Module 8 - Page 6
Many things can go wrong with While loops. If we forget to initialise the loop variable, or forget
to increment the loop variable, we can easily end up with an infinite loop, or one that does not
execute at all.
Next we are going to look at a different type of loop – a FOR loop. If you understand the
mechanics of the WHILE loop, then you shouldn’t have any trouble with the FOR loop.
FOR loops
Watch 'Concept Video 8-3 For Loops
FOR loops have this structure:
for (initialisation ; condition ; increment/decrement)
{
// body of the loop
}
The three parts that need further explanation are the:
initialisation: This takes the place of the initialisation statement that normally sits outside a while
loop. For example, we could use:
int i=0;
condition: This is the condition that when it becomes false, terminates the loop. An example:
i < 8;
increment/decrement: This is where the loop variable is increased or decreased. It doesn’t have
to be by 1 each time, but it must be included, for the for loop to progress.
An example of a FOR loop:
int myNumber = 10;
for (int i=0; i<6; i++)
{
myNumber = myNumber + i;
}
Activity 8-6 Adding several random aliens using a for loop
Using the scenario from Activity 8-5, alter the createAliens method to use a for loop. Test that it is
still working correctly by pressing the reset button several times. Make sure you comment your
createAliens method.
Non-random placement
Usually our games involving armies of enemies do not have random placement of those enemies.
An army is usually arranged in rows and columns. There is something menacing about a perfect
formation moving towards us relentlessly!
CSC71001 Module 8 - Page 7
Our next task is to alter our for loop to arrange our aliens in a line. To do this, we remove our
random number code, and instead calculate our x and y each time.
Our y value should be fixed. This will mean that our aliens all have the same height across the
screen. Our x value will change for each alien.
If our first alien is sitting at (40, 80), then the second one will be sitting at (40+a, 80) and the third
at (40+2a, 80) and so on.
How do we find the value of “a”? You can see from the diagram above that “a” is the width of the
alien graphic plus a little gap. In this case, our graphic is 60 pixels wide, so if we set our “a” as 70,
then we will place our aliens across the screen.
The first alien will be placed at (40 + 0*70, 80).
The second alien will be placed at (40 + 1*70, 80).
The third alien will be placed at (40 + 2*70, 80).
The fourth alien will be placed at (40 + 3*70, 80) and so on
We can now rewrite our for loop as:
for (int alienCount=0; alienCount < 8; alienCount++)
{
addObject(new Alien(), 40 + (70 * alienCount), 80);
}
Activity 8-7 Adding a line of aliens
Using the scenario from Activity 8-6, alter the for loop in the createAliens method, so that the aliens
are in a line. Are the aliens positioned well? If not, make adjustments until the aliens are positioned
as in the screenshot below.
40 40+a 40+2a 40+3a
CSC71001 Module 8 - Page 8
Reversing the order
We could also reverse the order that we place the alien spaceships. We could start with
alienCount at 8, and make the loop decrement (alienCount--) each time. Think about what the
condition should be within the loop statement, and the starting x value.
Activity 8-8 Adding a line of aliens
Using the scenario from Activity 8-7, alter the for loop in the createAliens method, so that the aliens
are in a line, with the right-most alien positioned first. Are the aliens positioned well? If not, make
adjustments until the aliens are positioned as in the previous screenshot.
Diagonals
We can position our aliens in a diagonal line by changing both our x and y values in each iteration
of the loop.
A first try might look like this:
for (int alienCount=0; alienCount < 8; alienCount++)
{
addObject(new Alien(), 53+(65*alienCount), 20+(65*alienCount));
}
This guesses that the first alien object will sit at (53, 20).
Activity 8-9 Adding a diagonal line of aliens
Using the scenario from Activity 8-8, alter the for loop in the createAliens method, so that the aliens
are in a diagonal line, from top left to bottom right. Make adjustments until the aliens are positioned
as in the screenshot below.
CSC71001 Module 8 - Page 9
Activity 8-10 Adding a diagonal line of aliens
Using the scenario from Activity 8-9, alter the for loop to make a diagonal from the top right to the
bottom left. Hint: you will need to decrement in the for loop.
Creating the army (part 1)
So far we have one line of aliens. We could easily add another two loops, with greater values of y,
to give us another two rows of alien objects.
public void createAliens()
{
// first row of aliens
for (int alienCount=0; alienCount < 8; alienCount++)
{
addObject(new Alien(), 48+(73*alienCount), 70);
}
// second row of aliens
for (int alienCount=0; alienCount < 8; alienCount++)
{
addObject(new Alien(), 48+(73*alienCount), 130);
}
// third row of aliens
for (int alienCount=0; alienCount < 8; alienCount++)
{
addObject(new Alien(), 48+(73*alienCount), 190);
}
}
Activity 8-11 Adding two more rows of aliens
Using the scenario from Activity 8-10, add another two for loops to make three rows of aliens.
CSC71001 Module 8 - Page 10
Repeating the repeat!
You may have noticed that we are repeating some code, with only small changes. When we do
this normally, we use a loop to cut down on code – but we are already using a loop.
We can solve this by putting a loop within a loop. This is also known as using “nested” loops
where one loop is inside another loop.
Let’s trace through an example:
int myNumber = 0;
for (int oCounter=0; oCounter<3; oCounter++) // outer loop
{
myNumber = myNumber + oCounter;
// inner loop
for (int iCounter=10; iCounter<12; iCounter++)
{
myNumber = myNumber + iCounter;
}
}
One way we can trace through this is with a tracing table.
myNumber oCounter myNumber oCounter++ oCounter<3 iCounter myNumber iCounter++ iCounter<12
0 0 0 10 10 11 True
11 21 12 False
1 True
21 1 22 10 32 11 True
11 43 12 False
2 True
43 2 45 10 55 11 True
11 66 12 False
3 False
------- --------- --------- --------- (End of loops)
myNumber would result as 66.
Activity 8-12 Loop within a loop
Using the scenario from Activity 8-10, add another loop that results in three rows of aliens.
What would you need to change to make 4 rows of aliens?
CSC71001 Module 8 - Page 11
Constants
Watch 'Concept Video 8-4 Constants
So far in this unit we have been using numbers wherever we like. For example, our initial code for
the row of aliens was:
public void createAliens()
{
for (int alienCount=0; alienCount < 8; alienCount++)
{
addObject(new Alien(), 48+(73*alienCount), 80);
}
}
If we change the size of our world, we would have to go through all our code and find all the spots
that are dependent on the size of the world. We would also have to change our code if the size of
the aliens was changed – and that would require searching through code to find which numbers
we had used that were dependent on these dimensions.
It is much better practice to use constant values that are set in one place, and then can be used
throughout. You might be thinking that you could store a value in a variable, and then use that
variable throughout your code. This is possible, but it involves the danger that the variable could
accidentally be changed.
Java provides for a static final variable that can be used as a constant. Once set, the value of this
stays the same throughout the program and cannot be changed. We usually give these constants
a name that has all capitals to distinguish these constants from normal variables.
For example, the offset from the top of the world might be called “Y_OFFSET”, and would be
declared like this:
public static final int Y_OFFSET = 80;
The rest is just like a normal variable declaration.
We would then use the constant wherever we use the y offset value:
addObject(new Alien(), 48+(73*alienCount), Y_OFFSET);
If we want to change the y offset, we can change it in one place. Other possible constants may be
X_OFFSET
X_GAP
NUM_COLUMNS
NUM_ROWS
You would declare these constants outside the methods – i.e. make them class constants.
CSC71001 Module 8 - Page 12
Activity 8-13 Using constants
Using the scenario from Activity 8-12, change your code so you are using constants for any values
that make sense.
Summary
During this topic we have built another application completely, using the techniques you have
learned previously and a few new techniques. You have used FOUR new constructs/concepts that
you will use over and over while programming:
• State variables to make decisions based on the state of an application or object at a
particular time;
• Using parameters in constructor methods to set up initial values of the object – including
the image used, and any other information needed that differs between objects;
• While loops that repeat a section of code over and over, until a given condition is false; and
• the concept of array variables that store a number of related values in one array variable.
The workshop activities are to be included in your portfolio under a separate folder “Module 8”.
CSC71001 Module 8 - Page 13
Workshop activities
Activity R8-14 Create a new scenario
Create a new scenario called ActivityR8-14. Choose a background that you like and create a new
Actor with a logical class name, using a small image (smaller than the alien image). Remember
that classes are named with a capital letter as their first letter.
Using a loop within a loop (either a for loop within a for loop, or a while loop within a while loop),
create an army of objects from the new Actor class.
You can, if you wish, offset the objects so they are exactly in a grid formation.
Activity R8-15 Your Initials
Save your scenario from ActivityR8-14 as YourNameR8-15. Using the same Class as previously,
create a frame of objects against the four sides of the World.
Create a constructor in your actor sub-class which takes an integer as a parameter. This
constructor will set an image based on the value passed to this constructor.
For example, instead of using
new Alien()
I might use
new Alien(1) or
new Alien(2)
and use the parameter to decide which image to set.
In your World class, using at least one loop, create a army of objects in the shape of the letter "M".
I can choose any type of object form the letter. If I used stars the letter M may look like this:
(Hopefully your attempt is better than mine with the stars!)
Optional extras:
You may wish to code your objects, so they change state (and spin, move, or turn) on a keypress.
To do this, you could add your objects to an array as they are created, and then loop through the
array, calling that method for each object.
You should try and use nested loops, a loop within another loop, to create the border.
Getting images:
A good place to get sprites to use for your Greenfoot projects is Google images - using the search
"sprites png" or "sprite sheet png". Of course, you need to be careful that you have permission to
use the images, but these are great to use. You can use the free online image editor at
www.pixlr.com to crop them to size.
CSC71001 Module 8 - Page 14
**** Activity R8-1 Portfolio Activity ***
The following activities assume that you have completed the previous initials Scenario
workshop activities. You will use the concepts from those activities in the next few activities.
In this activity, you will update the game you created for Assessment 2 - Practical skills. Each
activity, when it is completed, should be saved as a gfar in your portfolio folder.
Here, you will need to create a different type of food to add to your game (you already have 1 food
type with its own image).
In your Food class:
1. Create two (2) additional instance variables, which will be of Greenfoot image type. These
two variables will be used to hold images for the Food objects. The first variable can hold
the original Food image, and the second can hold the new image for your chosen theme.
2. If you do not have one already, create a default Food constructor that takes no parameters
and does not initialise any variables or properties eg: public Food(){ //no code in here }
3. Create a second Food constructor that has one integer parameter. This Food constructor
will set the image of the food to either the default image or the new food image. Passing the
value 1 as an argument will set the default image. Passing the value 2 will mean that the
second image will be set. You may refer to the concept code videos on setting up a
constructor that uses parameters.
Make sure that you test your game.
Save your scenario to your Portfolio as ActivityR8-1_rmason10.gfar. Replace rmason10 with your
username for MySCU.
**** Activity R8-2 Portfolio Activity ***
In this activity, you will continue to update your existing game created for Assessment 2 - Practical
skills. When this activity is completed, you should save it as a gfar in your portfolio folder.
In your World class;
1. Comment out or remove the while loops from Module 7.
2. Using at least one for-loop, create Food objects to form the shape of one of your initials.
Each new Food object should be created using the default Food constructor.
3. Using two nested for-loops (a loop within another loop), create a border around your scene
of Food objects. You will use one nested for-loop to create the top and the bottom border
and the other nested for-loop to create the left and right borders.
Make sure that you test your game.
Save your scenario to your Portfolio as ActivityR8-2_rmason10.gfar. Replace rmason10 with your
username for MySCU
CSC71001 Module 8 - Page 15
**** Activity R8-3 Portfolio Activity ***
This activity will update your existing game you created for Assessment 2 - Practical skills. When
this activity is completed, you should save it as a gfar in your portfolio folder.
In your World class;
1. you will need to change the default constructor calls in the for-loops to use the second Food
constructor.
2. When you call the second Food constructor, the argument you will pass will be a random
number of either 1 or 2.
Note: for testing purposes, instead of using the default new Food(), you might use new Food(1)
or new Food(2) i.e. not random. You can add random functionality to your code once tested.
Save your program in the Portfolio as ActivityR8-3_rmason10.gfar. Replace rmason10 with your
username for MySCU
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
urba6006代写、java/c++编程语...
2024-12-26
代做program、代写python编程语...
2024-12-26
代写dts207tc、sql编程语言代做
2024-12-25
cs209a代做、java程序设计代写
2024-12-25
cs305程序代做、代写python程序...
2024-12-25
代写csc1001、代做python设计程...
2024-12-24
代写practice test preparatio...
2024-12-24
代写bre2031 – environmental...
2024-12-24
代写ece5550: applied kalman ...
2024-12-24
代做conmgnt 7049 – measurem...
2024-12-24
代写ece3700j introduction to...
2024-12-24
代做adad9311 designing the e...
2024-12-24
代做comp5618 - applied cyber...
2024-12-24
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!