Concept explainers
Explanation of Solution
a. Declaring string array:
The statement which is used to declare “String” array with the values “Mercury”, “Venus&...
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[] scientists = {"Einstein", "Newton", "Copernicus", "Kepler"};
// Loop to read each element in scientists array
for(int i = 0; i<scientists.length; i++)
// Display contents of the scientists array
System...
Explanation of Solution
c. Loop to display total length of the all strings present in the array:
//Class definition
public class Sample {
// define main function
public static void main(String[] args) {
// Declaration of string array
String[] scientists = {"Einstein", "Newton", "Copernicus", "Kepler"};
// Declare and initialize the variable
int total = 0;
// Loop to read each element in scientists array
for(int i = 0; i<scientists.length; i++)
/* Using the length() method read the length of strings present in the array */
total += scientists[i]...
Want to see the full answer?
Check out a sample textbook solutionChapter 7 Solutions
Starting Out with Java: Early Objects (6th Edition)
- (Electrical eng.) Write a program that stores the following resistance values in an array named resistance: 16, 27, 39, 56, and 81. Your program should also create two arrays named current and power, each capable of storing five double-precision numbers. Using a for loop nd a cin statement, have your program accept five user-input numbers in the current array when the program is run. Your program should store the product of the values of the squares of the current array and the resistance array in the power array. For example, use power[1]=resistance[1]pow(current[1],2). Your program should then display the following output (fill in the chart):arrow_forward(Electrical eng.) Write a program that specifies three one-dimensional arrays named current, resistance, and volts. Each array should be capable of holding 10 elements. Using a for loop, input values for the current and resistance arrays. The entries in the volts array should be the product of the corresponding values in the current and resistance arrays (sovolts[i]=current[i]resistance[i]). After all the data has been entered, display the following output, with the appropriate value under each column heading: CurrentResistance Voltsarrow_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
- Consider 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_forwardIdentify error(s), if any, in the following array declarations. If a statement is incorrect, provide the correct statement. a. int primeNum[99];b. int testScores[0];c. string names[60];d. int list100[0..99];e. double[50] gpa;f. const double LENGTH = 26;double list[LENGTH - 1];g. const long SIZE = 100;int list[2 * SIZE];arrow_forwardUsing the two arrays shown below,write a program that prompts the user to enter an integer between 1 and 12 andthen displays the months and its number of days corresponding to the integerentered. Your program should display “wrong number” if the user enters awrong number by catching ArrayIndexOutOfBoundsException. String[] months = {"January", "February", "March", "April","May", "June","July", "August", "September", "October","November", "December"};int[] dom = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};arrow_forward
- Please include your result in your code. 1. 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. The expectedresult is: 2. Create a Calculator program. The operations included in the calculator areaddition, subtraction, multiplication and division. Also include buttons for clearingthe calculator screen, for the decimal point and equals operator. Keep your codesimple and organized. Be as creative as you want. You may either use AWT orSwing for this exercise.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_forwardJAVA Programming Problem 5 – CharacterArray Write a program which will store the following string into a character array: 6901 Sudley Road Manassas VA Your program will display each original character, determine and display whether the character is a digit or a letter, and redisplay the character according to the directions below. Directions Create a class called CharacterArray. The scanner class is not required. Initialize the character array with the given string. Write a program which will accept an alpha numeric string at the keyboard. Store the string, of any length, into a character array, for example: RBDB Manassas Campus 6901 Sudley Road Manassas VA 20109 For each visible character: Display the original character Determine and display whether the character is a digit or a letter If the character is a lower case letter, redisplay the character in upper case If the character is a upper case letter, redisplay the character in lower case If the character is a digit, display it…arrow_forward
- Don't copy from google or any other source Write a program that finds and shows the minimum and maximum values of an array filled with 100 random integer numbers belonging to [1000,2000]. In C++ please!arrow_forwardDeclare 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_forwardWrite a function that receives a string consisting of several lines of text (paragraph) andreturns an array indicating the number of occurrences of each letter of the alphabet in thetext.void countLetters(char* str, int*& array, int & size) note: Do not use any String or math libraries (such as cmath, cstring, string etc) and also do notuse built-in function (such as strlen, strcmp etc).arrow_forward
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
- Programming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,