1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
OlgaM077 [116]
3 years ago
15

Write a Comparator that compares String objects by the number of words they contain. Consider any nonwhitespace string of charac

ters to be a word. For example, "hello" comes before "I see", which comes before "You can do it"
Computers and Technology
1 answer:
Readme [11.4K]3 years ago
8 0

Answer:

import java.util.Scanner;

public class num12 {

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

       System.out.println("Enter the first String");

       String word1 = in.nextLine();

       System.out.println("Enter the second String");

       String word2 = in.nextLine();

       System.out.println("Enter the third String");

       String word3 = in.nextLine();

       //Remove all white spaces

        String cword1 = word1.replace(" ","");

       String cword2 = word2.replace(" ","");

       String cword3 = word3.replace(" ","");

       //Comparing the string by their lengths

       if(cword1.length()>cword2.length()&&cword1.length()>cword3.length()){

           System.out.println(word1+" Is the longest");

       }

       else if(cword2.length()>cword1.length()&&cword2.length()>cword3.length()){

           System.out.println(word2+" Is the longest");

       }

       else{

           System.out.println(cword3+" Is the longest");

       }

   }

}

Explanation:

Using Java Programming Language

Use the Scanner Class to obtain the String values from the user

Save them in different variables

Use the replace() method in java to remove white space from any of the string entered

Using if and else statements compare the lengths of the strings (word.length()) returns the length of the word.

Print out the word that is longest

NOTE I have compared three Strings, comparing two would have been more straigth forward

You might be interested in
Wrtie a program in which we will pass a value N. N can be positive or negative. If N is positive then output all values from N d
Triss [41]

Answer:

Following are the answer to this question:

x=int(input("Enter number: "))#defining x variable that input value from user end

if x< 0:#defining if block that check x value is less then 0

   while x<0:#defining while loop print up to the value

       print(x)#print value

       x+= 1#add values by 1

elif x>0:#defining elif block to check value x is greater than 0

   while x>0:#defining while loop to print down to value

       print(x)#print value

       x-= 1#subtract value by 1

Output:

when input is a positive value

Enter number: 5

5

4

3

2

1

when input is a negative value

Enter number: -5

-5

-4

-3

-2

-1

Explanation:

  • In the given python code, x variable is declared that input the value from the user end, in the next step if and elseif block is declared that calculates and prints its value.
  • In the if block, it checks value is negative it uses the while loop to prints its values in the down to value form.
  • In the elif block, it checks the positive it uses the while loop to prints its values into the up to values form.  
5 0
3 years ago
Remembering on which continent 5 different countries are located
Illusion [34]

Answer:

South america.

Explanation:

8 0
3 years ago
Using bearings will cause more friction. Group of answer choices True False
Olegator [25]

Answer:

False

Explanation:

6 0
3 years ago
A ________ power station uses the energy from a small piece of metal called Uranium ​
grigory [225]

Answer:

nuclear power plant

Explanation:

A nuclear reactor, or power plant, is a series of machines that can control nuclear fission to produce electricity. The fuel that nuclear reactors use to produce nuclear fission is pellets of the element uranium. In a nuclear reactor, atoms of uranium are forced to break apart.

3 0
3 years ago
Read 2 more answers
Prolog uses ____. Select one: a. lowercase for variable names, and uppercase for constants and functions b. uppercase for variab
Afina-wow [57]

Answer:

b) uppercase for variable names, and lowercase for constants and functions  

Explanation:

Given

Programming language: prolog

Required

The case type for variables, constants and functions

In prolog,

Variable names begin with uppercase

e.g. Name, NAME

While constants and functions begin with lowercase

e.g. name, addnumbers()

<em>Hence, (b) is correct</em>

6 0
3 years ago
Other questions:
  • PLEASE HURRY What data unit is addressed based on the IP address of the recipient? a. packet b. frame c. segment d. section
    9·2 answers
  • In 2-3 paragraphs, identify at least two ways technology and innovation have affected global economic competition. Provide a spe
    5·1 answer
  • In the space below, write the formula that needs to be added to the blank cells under the fourth column of the table.
    13·1 answer
  • How did the invention of the printing press lead eventually to an increase in the diversity of religious expression?
    7·1 answer
  • Write a class called (d) Teacher that has just a main method. The main method should construct a GradeBook for a class with two
    7·1 answer
  • A _____ defines what must take place, not how it will be accomplished.​
    12·1 answer
  • A solid understanding of __________ is the foundation of verbal communication
    7·1 answer
  • Which of these parts serves as the rear cross structure of a vehicle?
    12·1 answer
  • Select the correct answer.
    11·1 answer
  • Why do my airpods keep disconnecting from my laptop
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!