Array:
An array is defined as a group that consists of similar data types.
- The array size must be specified by an “int” value and not long or short.
- In Java, all arrays are allocated dynamically.
- An array is always indexed, starting from 0.
Declaring an array reference and creating an array:
- To insert elements to an array, first an array has to be created by using new operator and then the reference variable is assigned.
- The syntax is as follows,
arrayRefVar = new elementType[arraySize]
- From the above statement it is clear that, an array is created using new elementType[arraySize] and then the reference to the newly created array is assigned to the variable arrayRefVar.
- It is possible to declare an array variable, create an array and assign the reference to a variable in a single statement itself.
The syntax will be as follows,
elementType[]arrayRefVar = new elementType[arraySize];
elementType arrayRefVar[]= new elementType[arraySize);
Example:
double myFile = new double[20];
The statement given above declares an array variable myFile and then creates an array that consists of 20 elements which are of the datatype double and at last assign the reference to myFile.
Explanation of Solution
b. Loop to display each element in the array:
//Class definition
public class Sample {
// define main function
public static void main(String[] args) {
// Declaration of string array
String[] planets = {"Mercury", "Venus", "Earth", "Mars"};
// Loop to read each element in the planets array
for(int i = 0; i<planets.length; i++)
// Display the contents of the planet array
System...
Explanation of Solution
c. Loop to display first character of the elements present in the array:
//Class definition
public class Sample {
// define main function
public static void main(String[] args) {
// Declaration of string array
String[] planets = {"Mercury", "Venus", "Earth", "Mars"};
// Loop to read each element in the planets array
for(int i = 0; i<planets.length; i++)
// Display the contents of the planet array
System.out.println(planets[i]...
Want to see the full answer?
Check out a sample textbook solutionChapter 7 Solutions
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
- C++; In all different cases please create a const int SIZE_EX_1 = 10; // type of declaration. Do not hard code numbers into. #1) Fill an array of 10 elements with the digits 1 to 10. Then print the contents of the array. (Do not fill and print in one loop )arrow_forwardThe strLetters array is declared using the Dim strLetters() As String = {"E", "A","C", "G"} statement. Which of the following will sort the array as follows: G, E, C, A?a. Array.Sort(strLetters) Array.Reverse(strLetters)b. Array.Reverse(strLetters) Array.Sort(strLetters)c. Array.SortDescending(strLetters)d. both a and barrow_forward(c++)1. Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking your weight in kilograms and dividing by the square of your height in meters. Write a program that lets a teacher to calculate the BMI of 20 students. The program should use three one dimensional arrays named studWeight that holds the weight (in kilograms), studHeight that holds the height (in meters) and studBMI that holds the calculated BMI for each student. The program contains the following functions: a) readData: This function receives the studWeight and studHeight arrays from the main function. It reads the weight and height for all the students and stored them in the studWeight and studHeight arrays, respectively. b) calBMI: This function receives one element of the studWeight and studHeight arrays. It calculates the BMI of each student based on her/his weight and height, and then store the BMI in the studBMI array. c) display: This function displays the BMI of the students.arrow_forward
- Declare a two-dimensional char array that is 25 by 25 that represents the entire ocean and an If statement that prints “HIT” if a torpedo hits a ship given the coordinates X and Y. Write a C++ program that will read in a file representing a game board with 25 lines where each line has 25 characters corresponding to the description above. An example file might look like: ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~#####~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~#~~~~~~~~~~ ~~~~~~~~~~~~~~#~~~~~~~~~~ ~~~~~~~~~~~~~~#~~~~~~~~~~ ~~~~~~~~~~~~~~#~~~~~~~~~~ ~~~~~~~~~~~~~~#~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~#####~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~######~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~#####~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~#~~~~~ ~~~~~~~~~~~~~~~~~~~#~~~~~ ~~~~~~~~~~~~~~~~~~~#~~~~~ ~~~~~~~~~~~~~~~~~~~#~~~~~ ~~~######~~~~~~~~~~#~~~~~…arrow_forwardPART B. Write a program, Passwords, that contains an array of five names and another array of five passwords(your choice). Your program should ask the user to enter a name and a password. If the user enters the correctname with its related password, grant access (e.g. display “Access granted!”). IF the user enters the wrongname/password (can be either or both), allow the user to re-try only once. You don’t have to use nested loops.If the user still cannot enter the correct name/password, display the following: “Sorry, yourusername/password does not match our database. Contact the administrator.arrow_forwardPART B. Write a program, Passwords, that contains an array of five names and another array of five passwords(your choice). Your program should ask the user to enter a name and a password. If the user enters the correctname with its related password, grant access (e.g. display “Access granted!”). IF the user enters the wrongname/password (can be either or both), allow the user to re-try only once. You don’t have to use nested loops.If the user still cannot enter the correct name/password, display the following: “Sorry, yourusername/password does not match our database. Contact the administrator.” Scanner in = new Scanner(System.in);// Arrays for usernames and corresponding passwords.// The username at index 0 corresponds to the password at index 0,// the username at index 1 corresponds to the password at index 1,// and so on.// You can customize the login info by modifying the arrays below:String[] usernames = {"user1", "user2", "user3", "user4", "user5"};String[] passwords =…arrow_forward
- c++ 1. Does the element of an array have a default value? 2. Which of the following statements are valid array declarations? double d[30]; char[30] r;arrow_forwardConsider the following C++ code. string seasons[4] = {"Winter", "Spring", "Summer", "Fall"};string *strPtr;strPtr = new string[5];for (int i = 0; i < 4; i++)strPtr[i] = seasons[i];a. Write a C++ code that outputs the contents of the array to which strPtr points.b. Write the C++ statement that deallocates the memory space occupied by the array to which strPtr points.arrow_forwardLargest/Smallest Array Values Write a program that lets the user enter 10 values into an array. The program should then display the largest and smallest values stored in the array. Do question 7(above) again this time using 5 functions. void getValues(int [], int); void displayValues(const int[], int); int largest(const int[],int); int smallest(const int[],int); void displayLargestSmallest(int,int);arrow_forward
- Given the following array declaration:int array[30];Note that you DON’T know the values in the array.Use a for loop to increment the contents of the even indexed (0, 2, 4, 6, 8 …) elements by the value of 4.Output the first component of array.Set of the value of the fourth component of array to 7 multiplied by the value of the 30th component of array plus 8.arrow_forwardNote: Please use WHILE LOOP and print all the contents of the array. Use java language. Days of the Week. Create an array of Strings which are initialized to the 7 daysof the week. Use while-loop and print all the contents of the array.arrow_forwardThis is my C++ Codes for the problem but it has errors when I run it. Can you fix the errors? Please still use header files iostream. This is a topic under Data Structures: Arrays. //Start of program#include <iostream>#define TRUE 0#define FALSE 1 using namespace std; int main(){//Declaration of array int A[50]; //Display the first element of the array Acout << "The value of the first element of the array A is: " << A[0] << endl; //Store the value 26 in 25th element of array AA[24] = 26; //Store the computed value in 10th element of array AA[9] = (A[39] * 3) + 10;cout << "The value of the 10th element of the array A is: " << A[9] << endl; int n = 0; //Subscript of array A //For loopfor (int i = 0; i < n; i++){if (n / 2 == 0 || n / 3 == 0) //If subscript is a multiple of 2 or 3{cout << A[i] << " " << endl; //Display the value of an element of array A} if (n / 2 != 0 || n / 3 != 0) //If subscript is not a multiple of 2 or…arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage