Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Suppose that you are working for an online service dedicated to animal lovers. V

ID: 3726490 • Letter: S

Question

Suppose that you are working for an online service dedicated to animal lovers. Visitors

to your website can post comments and you desire to ‘like’ any comments that ONLY

includes animals like cat, dog, horse, or bird.

Complete the program (shown below) that reads a string of characters from the

keyboard and determines whether it contains one or more of these animals. For

example, if the user enters

My dog is as big as a horse

Your program will output to the display

I like this user because he likes: dog horse

If the user’s input does not include an animal, the program will output to the display

I don’t like this user

Upper and lowercase does not matter. Therefore, dog and dOg to same.

Following is the program to be completed. Nothing needs to be done to the main

method, however the checkForLikes method must be completed.

import java.util.Scanner;

/**

* Project 2 - Animal Lover

* @author

*/

public class Project2 {

public static void main(String[] args) {

//The following is for testing

checkForLikes("I have a Dog and a cat");

checkForLikes("I don't have an animal");

//The following is for user input

Scanner keyboard = new Scanner(System.in);

String input = keyboard.nextLine();

checkForLikes(input);

}

public static void checkForLikes(String message) {

// Your code goes here

}

}

Page 2

This project is concerned with making multiple decisions and the String class will be

most helpful.

The following specifications and guidelines also apply:

1. Make sure that your project meets all of the requirements set forth.

2. Be sure that it has your name in it. All work must be original and your own!

No credit will be given for plagiarized work.

3. Even if you are not successful in completing the project, submit what you

have as partial credit will be given for programs that would work if minor

corrections in logic are made.

4. Any work that is submitted late is subject to the rules set forth in the

syllabus!

Explanation / Answer

import java.util.Scanner;

/**

* Project 2 - Animal Lover

* @author

*/

public class Project2 {

  

   // add more animals as per your requrement

   private static String[] animalList = {"cat", "horse", "dog"};

   public static void main(String[] args) {

       //The following is for testing

       checkForLikes("I have a Dog and a cat");

       checkForLikes("I don't have an animal");

       //The following is for user input

       Scanner keyboard = new Scanner(System.in);

       String input = keyboard.nextLine();

       checkForLikes(input);

      

       keyboard.close();

   }

   public static void checkForLikes(String message) {

       String animals = "";

       message = message.toLowerCase();

      

       for(String a : animalList) {

           if(message.contains(a))

               animals = animals + a+" ";

       }

      

       if(animals.isEmpty()) {

           System.out.println("I don’t like this user");

       }else{

           System.out.println("I like this user because he likes: "+animals);

       }

   }

}

/*

Sample run:

I like this user because he likes: cat dog

I don’t like this user

My dog is as big as a horse

I like this user because he likes: horse dog

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote