LABORATORY GUIDE - 7 - ADVANCED PROGRAMMING - I - Arrays - FINAL - PROJECT - 2020-1
LABORATORY GUIDE - 7 - ADVANCED PROGRAMMING - I - Arrays - FINAL - PROJECT - 2020-1
Javier Daza
May 28, 2020 PROGRAMMING Email: [email protected]
OBJECTIVES
What is array
In general, an array is a group of items having the same features or we can say that
are of the same kind, like types of cars, bicycles, or any group having the same property.
In terms of computer programming languages, an array is a group of objects or a
collection of variables that are all of the same type. An array is a collective name given to a
group of similar quantities. All elements of any given array must be of the same type, in
other words we can't have an array of 20 numbers, of which 10 are doubles and 10 are
floats.
Arrays can be of multiple dimensions like:
one-dimensional array called a vector
two-dimensional array called a matrix
and multidimensional arrays.
1
http://cort.as/-C9LK - http://cort.as/-C9LT - http://cort.as/-C9Lb - http://cort.as/-C9Lu - http://cort.as/-C9Lz - http://cort.as/-C9M0 - http://cort.as/-
C9M6 - http://cort.as/-C9MD - http://cort.as/-C9Lu - http://cort.as/-C9Mn
Prepared by: Eng. Javier Daza
May 28, 2020 PROGRAMMING Email: [email protected]
Java has the ability to define a set of variables of the same type grouped under the
same name, and distinguish them using a numerical index.
To define a matrix in java is to define a variable or attribute, but when specifying the
type, what we do is put a pair of brackets [] to indicate that what we are defining is a matrix.
For example:
public int [] theValues;
Example
Open the NetBeans IDE then open the file menu and select "New Project" then choose
"Java application" then enter your project name (for example I choose "ArrayApp") and click
on "Ok".
By using a for loop, Java reduces coding lines, since we don't need to use different
outputs for each index.
Create "ArrayUserAssignEx.java" as follows:
package arrayapp;
public class ArrayUserAssignEx
{
public static void main(String[] args)
{
// declares an array of integers
int[] x= new int[4];
x[0]=10;
x[1]=11;
x[2]=12;
x[3]=13;
for(int i=0;i<4;i++)
{
System.out.println("Elements: "+ x[i]);
}
}
}
It is possible to fill an array at the time of declaration. By using this we don't need
to initialize each element separately so it will help in reducing the code and it is beneficial
for large array lists.
ArrayDeclarationEx.java
package arrayapp;
public class ArrayDeclarationEx
{
public static void main(String[] args)
{
// declares an array of integers it takes memory according to elements size
int[] x= { 10, 11, 12, 13};
for(int i=0;i<4;i++)
{
System.out.println("Elements: "+ x[i]);
}
}
}
Prepared by: Eng. Javier Daza
May 28, 2020 PROGRAMMING Email: [email protected]
This type of declaration deals with a string array. Like prints the name of an animal,
human being, or any other group.
ArrayStringEx.java
package arrayapp;
import java.util.*;
public class ArrayStringEx
{
public static void main(String[] args)
{
String[] str = {"elephant", "cow", "goat", "dog", "lion"};
List<String> strList = Arrays.asList(str);
for (String a : strList)
{
System.out.println(a);
}
}
}
Output
Run the application. You'll get the following output.
1. Write a program in Java with NetBeans that creates a one-dimensional array with
a size of 10, insert the numeric values you want in the way you want and show
the average values of the matrix on the screen.
2. Write a program in Java with NetBeans that creates a one-dimensional array
where you indicate the size by keyboard and create a function that fills the array
with the multiples of a number requested by keyboard. For example, if I define an
array of size 5 and select a 3 in the function, the array will contain 3, 6, 9, 12, 15.
Display them on the screen using a different function.
3. Write a program in Java with NetBeans that creates an array of numbers and
another of 10-position String where we will insert notes between 0 and 5.0
(control that inserts a valid note), being able to be the note in the array of
numbers, in the Strings the names of the students.
Next, create an array of String where we insert the result of the note with words.
If the grade is between 0 and 2.99, study more.
If the grade is between 3.0 and 3.4, Acceptable Work.
If the grade is between 3.5 and 4.0, Good Work.
Prepared by: Eng. Javier Daza
May 28, 2020 PROGRAMMING Email: [email protected]
4. Write a program in Java with NetBeans that creates an array containing N notes
from 0 to 5 generated randomly and shown on the screen, according to the
grade obtained, indicate how many students are:
Deficient 0-2.99
Regular 3-3,5
Good 3.5-4.0
Very good 4.0-4.5
Excellent 4.5-5.0
5. Write a program in Java with NetBeans that creates an array of characters from
'A' to 'Z' (only upper case). Then, you can ask for positions of the array by
keyboard and if the position is correct, it will be added to a string that will be
shown at the end, it will stop inserting when a -1 is entered.
For example, if I write the following numbers
0 // Add the 'A'
5 // Add the 'F'
25 // Add the 'Z'
50 // Error, insert another number
-1 // end
Resulting string: AFZ
IT IS REQUESTED: