SCU Programming Portfolio: Java and Greenfoot Activities

Verified

Added on  2025/04/11

|8
|856
|461
AI Summary
Desklib provides past papers and solved assignments for students. This portfolio showcases Java and Greenfoot programming.
Document Page
SOUTHERN CROSS UNIVERSITY
ASSIGNMENT COVER SHEET
Student Name:
Student ID No.:
Unit Name:
Unit Code:
Tutor’s name:
Assignment No.:
Assignment Title:
Due date:
Date submitted:
Declaration:
I have read and understand the Rules Relating to Awards (Rule 3 Section 18 –
Academic Misconduct Including Plagiarism) as contained in the SCU Policy
Library. I understand the penalties that apply for plagiarism and agree to be
bound by these rules. The work I am submitting electronically is entirely my
own work.
Signed:
(please type
your name)
Date:
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Student Name Programming Portfolio Student Number
Activity R4-2
1. Declare a variable called children of type int. Then write an assignment statement that
assigns to this variable the sum of two variables named daughters and sons.
Ans. int children;
int daughters=1;
int sons=1;
children=daughters+sons;
2. Declare a variable named area of type int. Then write an assignment statement that assigns
to the area the product of two variables called width and length. Remember that the
multiplication operator in Java is *.
Ans. int area;
int width=2;
int length=2;
area=length*width;
3. Declare two variables x and y of type int. Assign the values 23 to x and 17 to y. Then write
some code to swap those values (so that afterwards x contains 17 and y contains 23). BE
CAREFUL NOT TO LOSE THE VALUES while you are doing this!
Ans. int x=23;
int y=17;
int temp;
temp=x;
x=y;
y=temp;
1
Document Page
Student Name Programming Portfolio Student Number
Activity R4-3
1. Open the stickman scenario provided ***
2. The Greenfoot class has a method to get the noise level from the computer's microphone.
Find this method in the API documentation. How many parameters does it have?
Ans. 0
3. What is the return type of this method? What does this tell us?
Ans. Integer is the return type of this method. It tells us about the noise level the mic is
received from the surrounding.
4. What are the possible values returned from this method?
Ans. Integer value ranging from 0 to 100.
5. Is this method static or not? What does that tell us?
Ans. This method is a static method as it is called by class name. This method can be
called by a class name anywhere no need to create an instance of class before using
this method.
6. How do we call this method? Write down a correct method call to this method:
Ans. We can call getMicLevel() method by using Greenfoot class name directly as it is a
static method. To call this method, the code used is Greenfoot.getMicLevel().
2
Document Page
Student Name Programming Portfolio Student Number
Activity 6-3
1. Method Signatures:
a. setLocation(int x,int y); : Here x and y are the coordinates of the point in a window.
b. getX(); this method is used to obtain the x coordinate of the point of the actor.
c. getY(); this method is used to obtain the Y coordinate of the point of the actor.
2. setLocation(200,200); this set the location of the actor at 200,200 point.
3
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Student Name Programming Portfolio Student Number
Activity R7-1
1. Declare and initialise a String array called names with the names of three of your classmates,
or if you are an Online student, three of the names from one of the lectures.
Ans. String arr[]={"Harry","Garry","Marry"};
2. Write the code that will play a sound "ouch.wav" 20 times, using a while loop.
Ans. for(int i=0;i<20;i++){
Greenfoot.playSound(“ouch.wav”);
}
3. Consider the following code:
Ans. int x = 3;
nt i = 0;
while (i < 3)
{
x = x + 1;
i = i + 1;
}
4. What is the value of x after this code has executed?
Ans. 6
4
Document Page
Student Name Programming Portfolio Student Number
Activity R8-1
1. Declare and initialise a String array called names with the names of three of your classmates,
or if you are an Online student, three of the names from one of the lectures.
Ans. String names[]={“Jhon”, ”Mike”, ”Henry”};
2. Write the Java/Greenfoot code (not in Greenfoot itself) that will loop through this array,
showing each of the names using the showText method, in a different location each time.
Ans. int i=0;
while(i<names.length){
getWorld().showText(names[i],Greenfoot.getRandomNumber(770) ,
Greenfoot.getRandomNumber(360));
}
3. Write a loop that will add the even numbers from 2 to 2000.
Ans. int i=2;
int Sum=2
while(i<=2000){
Sum=Sum+i;
i=i+2;
}
4. Write a FOR loop that will display the numbers from 199 down to 100.
Ans. int j=10;
for(int i=199;i>=100;i--){
getWorld().showText(“”+i, j,10);
j=j+10;
}
5
Document Page
Student Name Programming Portfolio Student Number
Activity R9-7 Module 9: For-Each Loop
In this activity, we learn the use of for each loop. This loop is used to iterate the whole loop from
starting. Inactivity R9-6.gfar file we used a list of a spider at the certain specified location and then
rotate each spider one by in a for each loop. This loop rotates the spider according to the list order.
6
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Student Name Programming Portfolio Student Number
Activity R9-7 Module 10: Overloading Constructors
Constructor overloading is the concept in which a class constructor is called by two ways or we can
say that a class two or more constructor with a different parameter list. This phenomenon is known
as constructor overloading. In this leaf game, constructor overloading is used to distinguish among
the type of Leaf object from enemy, bonus or normal type.
7
chevron_up_icon
1 out of 8
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]