0% found this document useful (0 votes)
74 views30 pages

3.4 Array 0903

The document discusses a Java program that calculates gross pay for multiple employees. It begins with a while loop that takes employee hours as input until -1 is entered. It then converts the while loop to a for loop. The rest of the document discusses arrays, including declaring and initializing arrays, accessing array elements using indexes, and using for loops to assign values to all elements of an array.

Uploaded by

Debbie Choong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views30 pages

3.4 Array 0903

The document discusses a Java program that calculates gross pay for multiple employees. It begins with a while loop that takes employee hours as input until -1 is entered. It then converts the while loop to a for loop. The rest of the document discusses arrays, including declaring and initializing arrays, accessing array elements using indexes, and using for loops to assign values to all elements of an array.

Uploaded by

Debbie Choong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

SC025 COMPUTER SCIENCE

LECTURE 1 W8 2020/2021
QUOTE OF THE WEEK

“If you cannot do great


things, do small things in a
great way.”
Napoleon Hill
The Wheels & More store has several part-time employees; they are paid RM10 per hour. The store manager
wants a program that calculates and displays the gross pay amount for as many employees as needed without
having to run the program more than once. Because the number of hours an employee worked can be a
positive number only, the store manager will indicate that he is finished with the program by entering a
negative number (in this case -1) as the number of hours.
import java.util.Scanner;
class Salary{
public static void main (String [] args){
Scanner sc = new Scanner (System.in);
int hour;
double salary;

System.out.print(“ Enter hour: “);


hour = sc.nextInt();

while (hour != -1){


salary = hour * 10;
System.out.println(“ salary: “ + salary);
System.out.print(“ Enter hour: “);
hour = sc.nextInt();
}
}
}
Convert the while structure source code into for structure source code.
import java.util.Scanner;
class Salary{
public static void main (String [] args){
Scanner sc = new Scanner (System.in);
int hour;
double salary;

System.out.print(“ Enter hour: “);


hour = sc.nextInt();

while (hour != -1){


salary = hour * 10;
System.out.println(“ salary: “ + salary);
System.out.print(“ Enter hour: “);
hour = sc.nextInt();
}
}
}
import java.util.Scanner;
class Salary{
public static void main (String [] args){
Scanner sc = new Scanner (System.in);
int hour;
double salary;

System.out.print(“ Enter hour: “);


hour = sc.nextInt();

while (hour != -1){


salary = hour * 10;
System.out.println(“ salary: “ + salary);
System.out.print(“ Enter hour: “);
hour = sc.nextInt();
}
}
}
3.4 Array
Learning Outcomes
At the end of this topic, you should be able to:
a) Define array and relate with memory allocation
b) Explain accessing arrays – initialize, input, process
and output.
Introduction to Array
• Variables that we always used are for individual values such as
numbers and strings.

• With array, we’ll learn how to store multiple values of the


same type using single variable.
CREATING ARRAYS
■ Arrays look just like other variables, except they are
followed by square brackets ([ ])
DECLARE A VARIABLE
BASIC TYPE ARRAY TYPE
int counts; int [ ] counts;
✔single variable ✔single variable
✔single data type ✔single data type
✔for individual value ✔for multiple value
Variable marks Array marks
double marks; double[ ] marks;
store one value of marks store multiple values of the same type of
marks

marks=97.5; marks={97.5,41.8,36.5,100,75.3,57.8,67.5};
One value assign to variable Multiple values assign to array marks
marks Values of array marks known as element
Introduction to Array
An array is an ordered sequence of data of the same type.

ages 41 37 56 28 42

marks 97.5 41.8 36.5 100 75.3 57.8 67.5

grades ‘C’ ‘A’ ‘B’ ‘A’ ‘D’ ‘F’ ‘B’

names “Ibrahim” “Maryam” “Rahman” “Rahim”

months “Jan” “Feb” “Mar” “Apr” “May” “Jun”


Introduction to Array
Components of an array consists of:
◦ Data type
◦ Size
◦ Index (start from 0)
◦ Elements (values in the array)
Example:
double[] marks={97.5,41.8,36.5,100,75.3,57.8,67.5};
Memory
location
97.5 41.8 36.5 100 75.3 57.8 67.5 elements

[0] [1] [2] [3] [4] [5] [6] index

marks variable name


Arrays
To create an array, you have to:
1. declare an array with an data type ,
2. create the array itself
1. Declare Array
◦ To declare array with a data type :

Format : datatype [ ] arraynames;


int[] age;
double[] marks;
2. Create Array
To create array:
Format : arraynames = new datatype [size];
[size] refers to number of elements in array

age = new int[5];


marks = new double[7];
Or Declare and Create Array
To create array:
Format : datatype [ ] arraynames = new datatype [size];
[size] refers to number of elements in array

int[] age = new int[5];


double[] marks = new double[7];
CREATING ARRAYS
1. DECLARE AN ARRAY WITH A DATA TYPE
double[ ] values; int[ ] counts;
✔variable : values ✔variable : counts
✔data type : double ✔data type : integer
2. CREATING THE ARRAY
values = new double [ 7 ]; counts = new int [ 5 ];
✔makes values refer to an ✔makes counts refer to an
array of double, where the array of int, where the
number of elements in number of elements in counts
values is seven. is five.
CREATING ARRAYS
■ You can use any integer expression for the size of an array, as
long as the value is positive or zero element.
DECLARE A VARIABLE AND CREATE ARRAY USING VALID ELEMENTS
double [ ] values; int [ ] counts;
values = new double [7]; counts = new int[5];

double [ ] values = new double[7]; int [ ] counts = new int[5];

DECLARE A VARIABLE AND CREATE ARRAY USING INVALID ELEMENTS

double [ ] values; int [ ]counts = new int[-8];


values = new double[-3];
After-lecture task

1.
int [ ] mark = new int [7];
Label the components of the array declaration above.
Array name:
Data type:
Size:
2. Declare and create an array to store the weights of your five friends.
3.4 Array
Learning Outcomes
At the end of this topic, you should be able to:
a) Define array and relate with memory allocation
b) Explain accessing arrays – initialize, input, process and output.
3. Initialize array value
■ When you create an array of ints, the elements are initialized to
zero by default.

Declare a variable int[ ]counts;


counts = new int[5];
Create array
All elements of the count array
initialized to zero counts 0 0 0 0 0
0 1 2 3 4
3. Initialize array value
Large number inside the box
(red color) are the elements of counts 0 0 0 0 0
0 1 2 3 4

the array.
Small numbers outside the
boxes are the indexes (or
indices) used to identify each
counts 0 0 0 0 0
location in the array. 0 1 2 3 4

Notice that the index of the first


element is 0, not 1.
3. Initialize array value
(A) You also has an option to initialize the value of elements by
yourself. For example :
Declare a count array and make
it refers to an array of five int[ ]counts = {1,2,-3,4,5}
elements

All elements of the count array


equal to the value you initialize counts 1 2 -3 4 5
0 1 2 3 4
above
3. assign array value
(B) You can use the [ ] operator anywhere in an expression:
ASSIGNMENT STATEMENT STATE DIAGRAM AFTER ASSIGNMENT STATEMENT
counts[0] = 7;
counts 7 0 0 0 0
0 1 2 3 4

counts[1] = counts[0] *2;


counts 7 14 0 0 0
0 1 2 3 4

counts[2]++;
counts 7 14 1 0 0
0 1 2 3 4

counts[0] = counts[1] + counts[4];


counts 14 14 1 0 0
0 1 2 3 4
3. assign array value
(C) You can use for construct to assign value into array
double [ ] a; // declare the array
a = new double [3]; // create the array
for (int count = 0 ; count < 3; count++) // elements are indexed from 0 to 2
a[count] = 0.0; // initialized all elements to 0.0

OR

double [ ] a = new double [3]; // declare & create the array


for (int count = 0 ; count < 3; count++) // elements are indexed from 0 to 2
a[count] = 0.0; // initialized all elements to 0.0

OR

double [ ] a = new double [3]; // declare & create the array


for (int count = 0 ; count < a.length; count++) // elements are indexed from 0 to 2
a[count] = 0.0; // initialized all elements to 0.0
THEREFORE, instead of assigning the elements one
by one like this:
double [ ] a = new double [3];
a[0] = 0.0;
a[1] = 0.0;
a[2] = 0.0;

You can use the for construct as shown before!


Entering Data into Arrays
(D) You can use for construct to enter value into array
import java.util.Scanner;
class EnterDataArray
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
double[] marks;
marks = new double[6];
for(int i = 0; i < marks.length ; i++)
{
marks[i] = sc.nextDouble();
}
}
}
Accessing Elements in an Array
int[] age = {7,21,5,9};
int counter = 0;
while (counter < age.length)
{
System.out.println("Age "+ age[counter]);
counter = counter + 1;
}
Displaying Elements in an Array

class PrintArray
{
public static void main(String[] args)
{
int[] arr = {1,2,3,4,5,6,7};
for (int i=0; i<arr.length; i++)
{
System.out.print(arr[i]);
}
}
}
ACCESSING ARRAYS – output/display
■ If we want to display all elements of the array, we can use a
for loop as example below.
class PrintArray{
public static void main(String[] args){
int[] arr = {1,2,3,4,5,6,7};
for (int i=0; i < arr.length; i++) {
System.out.print(arr[i]);
}
}
}

You might also like