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: Early Objects (6th Edition)
- Date: Lab Objectives This lab was designed to reinforce the following programming concepts: Using repetition structures. Using decision making Using /0 files. Exercise#1: Counting Number of Digits Write a Java program that inputs one integer number from the user, determine the number af individual digits and prin the number of digits and the digits separated from one another by a tab space. For example, if the user types in the numbe 42339, the program should print "There are 5 digits, which are 4 2339", Sample Input/Output Enter an integer value: 12345678 There are(8 aigite, wrich are(1)2 3 45676 num/13 Exercise#2: Sales A mal-order house sells five products whose retail arices are as follows: Procuct 1. $2. sa; product 2, 54.50; product 3, 34. 481 Eroduct 4. $4.99 and product 5. $6.87. Write a Java program that reads a series of pairs as foliows: product number and product name. The quantity sold is generated at randam between O and 50. Your program should n a svitch statem.ent to…arrow_forwardName format: You must use String object and not StringBuffer or StringBuilder. Many documents use a specific format for a person's name. Write a program that take one Line as an input. Example: Scanner input = new Scanner(System.in); System.out.println(“Enter firstName middleName lastName separated by at least one blank, It may have more than one blank separating firstName middleName lastName."); Input sample : Pat Silly Doe || Note these are separated by at least one blank. String line = input.nextLine() Ex: If the input is: Pat Silly Doe the output is: Doe, P.S. If the input has the form: firstName lastName the output is: lastName, firstInitial. Ex: If the input is: Julia Clark the output is: Clark, J. Please submit the .java source file and the sample run in a text file. Thanksarrow_forwardRest of code in image / This is a bad programming style since it is using goto. // This is an spagetti code and not working.// Use function to display menu, and display game rules,// Use different color for text display.// fix it so it works any way you like./*HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE); // Write 16 lines in 16 different colors. for (int color = 0; color < 16; color++) { SetConsoleTextAttribute (screen, color); cout << " Hello World!" << endl; Sleep(400); // Pause between lines to watch them appear } // Restore the normal text color) SetConsoleTextAttribute(screen, 7);*/#include <iostream>#include <windows.h>using namespace std;int main(){ //textbackground(WHITE); //textcolor(RED); system("cls"); char ch, a[20], ch2; int num = 100, rnum, guess, count, ch1, c = 0; cout << "**********************************************************"<<endl; cout << "*…arrow_forward
- Anagram words Write a C code that asks users to enter two words, one after the other and checks if these words are anagrams (permutations of the same letters). For instance, the words “smartest” and “mattress” are anagrams. Examplea Run 1: Please enter first word: lookplease enter the second word: coolSorry! “cool” and “look” are not anagrams. Run 2: Please enter first word: masters please enter the second word: smartes Yes! “masters” and “smartes” are anagrams. Hint: If you sort the two words that are anagrams, they will become identical! For instance, “smartest” and “masters” both sort to “aemrsstt” You can use all functions in <string.h> Sorting a string is exactly similar to sorting an array. You just need to know the length of the string and make sure you do not touch the terminating null.arrow_forwardComplete the loop so that it displays all mountains that are over 15000 feet. The blank ____ on the code below indicates where your statement will be placed. var mountains = ["Mount Everest" : 29029, "Nanga Parbat" : 26660, "Kanlaon" : 8087, "Pico de Orizaba" : 18491, "Sabalan" : 15784]for ____ { if elevation > 15000 { print("\(mountain): \(elevation) feet.") } }arrow_forwardC# - One sentece answer - short answer - only a statementarrow_forward
- Word Sleuth Puzzle Programarrow_forward* CENGAGE MINDTAP Programming Exercise 8-3A Instructions CarCareChoice.java + Write an application for Cody's Car Care Shop that shows a user a list of available services: oil change, tire rotation, battery check, or brake inspection. Allow the user to enter a string that corresponds to one of the options, and display the option and its price as $25, $22, $15, or $5, accordingly. Display Invalid Entry if the user enters an invalid item. An example of the program is shown below: Enter selection: oil change tire rotation battery check brake inspection battery check battery check price is $15 Grading Write your Java code in the area on the right. Use the Run button to compile and run the code. Clicking the Run Checks button will run pre-configured tests against your code to calculate a grade. Once you are happy with your results, click the Submit button to record your score.arrow_forward99.arrow_forward
- testScore Array: Student1 = 75Student2 = 24Student3 = 73Student4 = 65Student5 = 86Student6 = 45Student7 = 56Student8 = 94 Write code in C# to process the student's result from the array(testScore) based on the condition below and display all the score that are below the passing marks onto a ListBox (lstFailList). Condition: The student is passed if the grade is more than 50.arrow_forwardLearning Objectives: Python Programming Use loop over a stringReplace characters within a stringFunction (declaration and call)Write code in modular formInstructions Create a function only_chars that takes the line of text and returns the text line excluding the spaces, periods, exclamation points, or commas. Create a program that will read the input line, use the function, and then print the line, without spaces, periods, exclamation marks, or commas and their length. Ex: If the input is: Listen, Mr. Jones, calm down. the output is: ListenMrJonescalmdown21Do not forget if __name__ == "__main__" section!arrow_forwardTreasure Hunter description You are in front of a cave that has treasures. The cave canrepresented in a grid which has rows numbered from 1to , and of the columns numbered from 1 to . For this problem, define (?, )is the tile that is in the -th row and -column.There is a character in each tile, which indicates the type of that tile.Tiles can be floors, walls, or treasures that are sequentially representedwith the characters '.' (period), '#' (hashmark), and '*' (asterisk). You can passfloor and treasure tiles, but can't get past wall tiles.Initially, you are in a tile (??, ). You want to visit all the treasure squares, andtake the treasure. If you visit the treasure chest, then treasurewill be instantly taken, then the tile turns into a floor.In a move, if you are in a tile (?, ), then you can move tosquares immediately above (? 1, ), right (?, + 1), bottom (? + 1, ), and left (?, 1) of thecurrent plot. The tile you visit must not be off the grid, and must not be awall patch.Determine…arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,