Write a program that does the following in java You ask the

Write a program that does the following in java:

You ask the user how many dogs they have by inputting the number of dogs from the keyboard.

Depending on the number of dogs, your program will employ a switch or if statement construct:

If the user has no dogs, it prints to the console “You need a friend.”

If the user has one dog, it prints to the console “You have a friend.”

If the user has two dogs or more, it prints to the console “Make sure you are the alpha dog.”

Your program then asks the user for the names of the dogs (assume that the maximum number of dogs to consider is three).

Your program prints the first letter of each dog\'s name capitalized followed by a period into a single line on screen following the text \"Have fun with \". E.g., \"erwin\" as dog name results in output \"Have fun with E.\"

Solution

import java.util.Scanner;

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

       Scanner scanner = null;
       try {
           scanner = new Scanner(System.in);
           System.out.print(\"Enter the number of Dogs:\");
           int dogs = scanner.nextInt();
           switch (dogs) {
           case 0:
               System.out.println(\"You need a friend.\");
               break;
           case 1:
               System.out.println(\"You have a friend.\");
               break;
           case 2:
               System.out.println(\"Make sure you are the alpha dog.\");
               break;
           default:
               break;
           }
           if (dogs > 0) {

               String[] names = new String[dogs];
               for (int i = 0; i < names.length; i++) {
                   System.out.print(\"Enter the name of dog \" + (i + 1) + \":\");
                   names[i] = scanner.next();
               }
               for (int i = 0; i < names.length; i++) {
                   System.out.print(\"Have fun with \"
                           + names[i].substring(0, 1).toUpperCase() + \".\");

               }

           }

       } catch (Exception e) {
           // TODO: handle exception
       }
   }
}

OUTPUT:

Enter the number of Dogs:1
You have a friend.
Enter the name of dog 1:erwin
Have fun with E.

Write a program that does the following in java: You ask the user how many dogs they have by inputting the number of dogs from the keyboard. Depending on the nu
Write a program that does the following in java: You ask the user how many dogs they have by inputting the number of dogs from the keyboard. Depending on the nu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site