0% found this document useful (0 votes)
45 views8 pages

LABORATORY GUIDE - 7 - ADVANCED PROGRAMMING - I - Arrays - FINAL - PROJECT - 2020-1

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

LABORATORY GUIDE - 7 - ADVANCED PROGRAMMING - I - Arrays - FINAL - PROJECT - 2020-1

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

Prepared by: Eng.

Javier Daza
May 28, 2020 PROGRAMMING Email: [email protected]

Guide No. 7: Java Programming using NetBeans platform

OBJECTIVES

 You will learn to create an array in Java NetBeans programming.


 Write programs with arrays in Java NetBeans platform.

THEORETICAL FOUNDATION1 - Array Declaration In Java Using Netbeans

 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;

 How to declare an array in Java


There are the following ways to declare an array.

1. Assignment operator (=)

By using an assignment operator (=) we assign an array. In this we initialize each


element individually.

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".

Create an ArrayAssignEx.java as follows:


package arrayapp;
public class ArrayAssignEx
{
public static void main(String[] args)
{
// declares an array of integers
int[] x;
// allocates memory for 4 integers
x = new int[4];
// initialize the array element
x[0] = 10;
x[1] = 11;
x[2] = 12;
x[3] = 13;
System.out.println("Index 0: "+ x[0]);
System.out.println("Index 1: "+ x[1]);
System.out.println("Index 2: "+ x[2]);
System.out.println("Iindex 3: "+
x[3]);
}
}
This will produce the following result:
Output
Prepared by: Eng. Javier Daza
May 28, 2020 PROGRAMMING Email: [email protected]

Run the application. You'll get the following output.

2. Using a for loop

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]);
}
}
}

This will produce the following result:


Prepared by: Eng. Javier Daza
May 28, 2020 PROGRAMMING Email: [email protected]

3. Declare at the time of using a list

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 will produce the following result:

4. Declare an array of string

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);
}
}
}

This will produce the following result:


Prepared by: Eng. Javier Daza
May 28, 2020 PROGRAMMING Email: [email protected]

Output
Run the application. You'll get the following output.

 PROPOSED EXERCISES – FINAL PROJECT


Please develop the proposed exercises based on this laboratory guide and the
explanations provided by the teacher in relation to the topic arrays, as follows:

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]

 If the grade is between 4.0 and 4.5, Excellent Work.


 If the grade is between 4.5 and 5.0, Excellent Work.
Showing on the screen, the student his grade and his result in words. Create the
methods that you think are convenient.

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:

Based on the norms, recommendations, parameters, categories and


guidelines agreed upon in class, prepare a report on the development of the
previously proposed programs that include:
1. Cover
2. Objective or introduction
3. Table of contents
4. The content with the elaboration of all the proposed programs should
include: statement of the problem to be solved, pseudocode, program
Prepared by: Eng. Javier Daza
May 28, 2020 PROGRAMMING Email: [email protected]

code, image with the execution of the program, conclusions and


bibliography.
5. Publish the report on the BlackBoard virtual platform before 23:59 on
Wednesday, June 3, 2020, to socialize in the next class session.

You might also like