Question

Class DemoArray: 1. Create an array of type String , name it words, and initialize it...

Class DemoArray: 1. Create an array of type String , name it words, and initialize it with the following words: one, two, three, four, five, four, three, two, one 2. Use a method of class Arrays to print the array. Make sure to use a label 3. At this point we are going to fix the misspelled word three. Use a loop to access the elements of the array. One by one check whether the current element happens to be three. If that is the case, assign the correctly spelled word three to that array element 4. Again print the result. This time there should be no rectangular brackets nor commas

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the required program along with the comments and output:

import java.util.Arrays;

class DemoArray{
    public static void main(String[] args) {

        String[] words = {"one", "two", "three", "four", "five", "four", "three", "two", "one"};    //initialize words array with the given words

        System.out.println("Array Contents Before: "+ Arrays.toString(words));  //print the array contents using Arrays class method

        for(int i=0; i<words.length; i++) { //iterate over each element in the words array
            if(words[i].contains("three"))  //check for misspelled word three
                words[i] = "three"; //set the correct word
        }

        System.out.print("\nArray Contents After : ");  //print after correction
        for(int i=0; i<words.length; i++) {
            System.out.print(words[i] + " ");
        }

    }
}

------------------------------

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Class DemoArray: 1. Create an array of type String , name it words, and initialize it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program - create a 1-d array. Its data type is integer. It has...

    Write a C++ program - create a 1-d array. Its data type is integer. It has 10 elements. - initialize this array by 10 integer numbers. Five elements of the array are positive, and others are negative. - use a while loop to manipulate the array as below. - If the value of an element is positive, the value is doubled. Otherwise, the value is incremented by 3. - print the new value of each element. - use a for...

  • Create a new ArrayPractice project in Eclipse and a class named ArrayPractice.java. Put everything that follows...

    Create a new ArrayPractice project in Eclipse and a class named ArrayPractice.java. Put everything that follows into the main method except the method arrayAverage. Create a 5 element array of doubles called grades that contains the following numbers in this order: 81.2, 92.5, 48.9, 78.8, and 45.5. Create a 7 element array of ints called numbers that contains the following numbers in this order: 12, 42, 33, 67, 92, 58, and 33. Create a 9 element array of Strings called...

  • JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate...

    JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate passing array reference to a method;        III. Demonstrate creating array of objects that prints out only x 0.0 for all objects. PROJECT 5C - ARRAYS Create a new file called " EventArray5C". There are some "challenging" directions in this project. . 1.Prepare a document box (tell me that task(s) the application is to accomplish, how it will accomplish the tasks, the author of...

  • I am working on my java but I keep getting this one wrong. Method Name: arrayContains...

    I am working on my java but I keep getting this one wrong. Method Name: arrayContains Access modifier: Private Parameters: 1 integer named number Return type: boolean Purpose: This method returns a true or a false indicating if the number is present in an array or not. In order for this method to work, there must be an array already declared. Therefore, in the CLASS BLOCK, declare an array as follows: static int[] listof Numbers = {1, 2, 3, 4,...

  • java: 1d arrays PLEASE NEED HELp THANK YOU!!! One dimensional (1D) array 1. Create an array...

    java: 1d arrays PLEASE NEED HELp THANK YOU!!! One dimensional (1D) array 1. Create an array of 1000 integers. Name the array: x 2. Assign 95 to the ninth element, 25 to the twentieth element of array x. 3. Assign the sum of the ninth and the twentieth element to the sixtieth element of array x. 4. Display the sixtieth element of the array. 5. Use the for statement to generate all indexes to read and display all elements in...

  • Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring...

    Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem...

  • Create a UML diagram to help design the class described in exercise 3 below. Do this...

    Create a UML diagram to help design the class described in exercise 3 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Baby class object; should they be private or public? Determine what class methods are required; should they be private or public? 3. Write Java code for a Baby class. A Baby has a name of type String and an age of type integer. Supply two...

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

  • Can someone finish this class for me? There are comment instructions with what to do in...

    Can someone finish this class for me? There are comment instructions with what to do in the methods already. public class ArrayDemo   {                               public void demonstrateTask1(){                                System.out.println("Demonstrate   Task   1");                                                               int[]   numbers =   null;   //   note   that   numbers   references   nothing   (null)   initially                           ...

  • Java Object Array With 2 Elements In 1 Object

    1. Create a UML diagram to help design the class described in Q2 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Person class object; should they be private or public? Determine what class methods are required; should they be private or public?2. Write Java code for a Person class. A Person has a name of type String and an age of type integer.Supply two constructors: one will be...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT