Question

In: Computer Science

Hey, I'm stuck on this assignment for AP Comp Sci Java. I don't know how to...

Hey, I'm stuck on this assignment for AP Comp Sci Java. I don't know how to start.

An array of String objects, words, has been properly declared and initialized. Each element of words contains a String consisting of lowercase letters (a–z).

Write a code segment that uses an enhanced for loop to print all elements of words that end with "ing". As an example, if words contains {"ten", "fading", "post", "card", "thunder", "hinge", "trailing", "batting"}, then the following output should be produced by the code segment.

fading

trailing

batting

Solutions

Expert Solution

public class JavaApplication20 {

    public static void main(String[] args) {

        //initialize array

        String words[] = {"ten", "fading", "post", "card", "thunder", "hinge", "trailing", "batting"};

        //enhanced for loop to loop through the array

        for (String word : words) {

            //lastIndexOf() method returns the position of the last found occurrence

            //of the substring specified in a string

            if (word.lastIndexOf("ing") == word.length() - 3) {

                System.out.println(word);

            }

        }

    }

}

Code screenshot

Output


Related Solutions

I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
Hey! I'm stuck with this task from the Princeton course on Coursera. Write a program Minesweeper.java...
Hey! I'm stuck with this task from the Princeton course on Coursera. Write a program Minesweeper.java that takes three integer command-line arguments m, n, and k and prints an m-by-n grid of cells with k mines, using asterisks for mines and integers for the neighboring mine counts (with two space characters between each cell). To do so, Generate an m-by-n grid of cells, with exactly k of the mn cells containing mines, uniformly at random. For each cell not containing...
I'm in a freshmen level physics class now, so I don't know much, but something I...
I'm in a freshmen level physics class now, so I don't know much, but something I heard today intrigued me. My TA was talking about how at the research facility he worked at, they were able to accelerate some certain particle to "99.99% the speed of light". I said why not 100%, and I didn't quite understand his explanation, but he said it wasn't possible. This confused me. Since the speed of light is a finite number, why can we...
How do I write this method correctly? What Ihave is a Java project assignment where I'm...
How do I write this method correctly? What Ihave is a Java project assignment where I'm supposed to create a Roshambo game. One of the classes that the project is supposed to have is a class called "RoshamboApp that let the user play 1 of 2 AI opponents, a "Bart" class, and a "Lisa" class. The instructions say "Create a class names RoshamboApp that allows player1 to play Bart or Lisa as shown in the console output. Rock should beat...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
I know the what the answers are but I don't know how to get them. Can...
I know the what the answers are but I don't know how to get them. Can you please explain the process? Thank you. Part VII. Discontinued Operations and Earnings per Share (11 points) Todd Corporation had pre-tax income for 2017 of $2,500,000. On December 31, 2017, Boyd disposed of a component of its business that represented a strategic shift in operation. That component had a Loss on Discontinued Operations of $450,000 (pre-tax). Boyd received $1,000,000 net cash proceeds from the...
I'm Getting an "unindented error" Please fix the bolded codes. Because I don't know whats going...
I'm Getting an "unindented error" Please fix the bolded codes. Because I don't know whats going on. Thank You. # This program exercises lists. # The following files must be in the same folder: # abstractcollection.py # abstractlist.py # arraylist.py # arrays.py # linkedlist.py # node.py # input.txt - the input text file. # Input: input.txt # This file must be in the same folder. # To keep things simple: # This file contains no punctuation. # This file contains...
I have a lab assignment that I'm not sure how to do. The experiment is a...
I have a lab assignment that I'm not sure how to do. The experiment is a cart moving 60cm distance and there is a fan on top of it making it have a mass of .56kg. Every trial there is 100g added to the cart. For this part, the time is kept the same. 1. If the force provided by the fan was the same for each run and we have chosen the same time interval, how does the impulse...
Hey! I'm having trouble answering this for my assignment. Thank you so much in advance. 1)...
Hey! I'm having trouble answering this for my assignment. Thank you so much in advance. 1) Which type of vessels, arteries or veins, has more muscle fibers? What is the functional significance of this? 2a) In general, we have no conscious control over smooth muscle or cardiac muscle function, whereas we can consciously control to some extent all skeletal muscles. Can you consciously control your breathing? What does this tell you about the muscle type of the diaphragm? 2b) What...
I have the formulas for how to calculate ROI, Margin and Turnover. But I don't know...
I have the formulas for how to calculate ROI, Margin and Turnover. But I don't know if I am using the right numbers and it gets frustrating. A worked out calculation for each would really be helpful so I know what numbers from what years you plugged in. Your time is valuable and would really appreciate the help. If the response is correct, I would not mind giving a like! Return on Investment, Margin, Turnover Ready Electronics is facing stiff...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT