Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


COMP9103 Week 2 Tutorial


Loops and Arrays

As with any programming language there is some form of iteration. Within the java language we have access to for and while keywords which allow you to create loops.


while loop

A while loop requires a condition to be true for it to commence and continue iterating. The while loop structure follows while(condition), after this component is defined, the following code block will continue to be executed.


for loop

For loops within the java language have two prolific uses. The common C-like method of defining variables, condition and update. This is represented in the form:

The alternative method is an iterator based method which will allow the programmer to express a loop using a binding and collection. This is represented in the following form:


Question 1: Count vowels

You are to write a program that will count the number of vowels that exist in a given string.


Question 2: Draw a tree

You are tasked with writing a function that will draw a tree to standard output.


Arrays

Arrays are defined as contiguous blocks of memory that contain a number of elements. Memory is allocated based on the size of the type and the number of elements defined. When using primitive typed arrays, all elements are aligned next to each other while reference typed arrays will contain a memory reference. The size of a memory reference is the size of the address space size of the CPU on your computer. This is commonly referred to as the pointer size.

Elements of primitive typed arrays will always contain a value. In previous instances of java (<= Java 6), did not gaurantee to be initialise each element to 0.

Elements of reference typed arrays will by default be initialised to null. By default, each element within the array is a memory reference and does not have any usable data. To set an element within a referenced typed array, you are able to set an element to null or to an object (either directly initialising it with new or through aliasing).


Question 3: Contains

Write a method that will check if an element exists within an array.

Example:


Question 4: Count

Extend your previous question to return the number of times an element exists in an array.

Example:


Question 5: Count duplicates

Given an array, count the number of duplicates that exist in a given array.

Example:


Question 6: Array Union

Given two integer arrays, return an array that is the union between two arrays.

Example:


Extension: Writing and executing a bash script

In a bash script you can put all of your commands to execute your java program. To simply compile and run your java program, do the following:

● Create a file with the extension .sh (let’s say run.sh)

● In the .sh file, write the commands to compile and execute your java code like

   javac <FileName>.java

   java <FileName>

● Then type the following in the terminal to execute your sh file

   bash run.sh


Question 7: Assessed Task: Online Task 1

Remember you are required to complete a Online Task within the due date. Go to EdStem for this unit and click on Lessons to find out the task and the due date. This is a marked task.