Question

Write a new program called TrickOr Treat that will do the following 1. In a while loop, say Trick or Treat! and allow the user to type in the name of a candy and store it in an ArrayList. Once the user types Trick, then the loop should stop. Then, print out the total number of candies on the screen. (See example below) [1 points] 2. Write a method called countSnickers that will take an ArrayList of candy as an argument, and return the total number of Snickers candy you have You should print out this value in the main method. [2 points] 3. Overload the countSnickers method to take a conventional array (not ArrayList) of candy as an argument, and return the total number of Snickers candy you have. We will not use this method, just create it only. [1 points] 4. Write another method called search that will take two arguments: an ArrayList of candy and the name of one candy. In the main method, ask the user which candy do you want to search for, and then pass the ArrayList along with the name of the candy that the user typed to the search method The search method will loop through the array and print out how many of those candies exist (See example below). [2 points]
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;

import java.util.Scanner;

public class TrickOrTreat {

public static void main(String[] args) {

//Declaring variables

String name;

ArrayList<String> candies = new ArrayList<String>();

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

/* This while loop continues to execute

* until the user enters "Trick"

*/

while (true) {

System.out.print("Trick or Treat :");

name = sc.nextLine();

if (name.equalsIgnoreCase("Trick")) {

break;

} else {

//Adding to the Array list

candies.add(name);

}

}

//calling the method

int snickersCnt = countSnickers(candies);

System.out.println("No of Snickers :" + snickersCnt);

System.out.print("Enter a candy name to search :");

name = sc.nextLine();

//calling the method

search(candies, name);

}

//This method will count no of Snickers and return the count to the caller

private static int countSnickers(ArrayList<String> candies) {

int cnt = 0;

for (int i = 0; i < candies.size(); i++) {

if (candies.get(i).equalsIgnoreCase("Snickers"))

cnt++;

}

return cnt;

}

//This method will count no of Search candy and display the count

private static void search(ArrayList<String> candies, String name) {

int cnt = 0;

for (int i = 0; i < candies.size(); i++) {

if (candies.get(i).equalsIgnoreCase(name))

cnt++;

}

System.out.println("No of '" + name + "' candies are :" + cnt);

}

}

OUTPUT

Trick or Treat :Riesen
Trick or Treat :Bit o Honey
Trick or Treat :SweetTarts
Trick or Treat :Starburst
Trick or Treat :Baby Ruth
Trick or Treat :Milk Duds
Trick or Treat :Jolly Rancher
Trick or Treat :Snickers
Trick or Treat :Milk Duds
Trick or Treat :Snickers
Trick or Treat :Skittles
Trick or Treat :Snickers
Trick or Treat :Skittles
Trick or Treat :SweetTarts
Trick or Treat :Jawbreakers
Trick or Treat :Snickers
Trick or Treat :Trick
Enter a candy name to search :Skittles
No of Snickers :4
No of 'Skittles' candies are :2

_______________

3)

Overloaded countSnickers() method:

int countSnickers(String candies[])

{

int cnt=0;

for(int i=0;i<candies.length;i++)

{

if(candies[i].equalsIgnoreCase("Snickers"))

cnt++;

}

return cnt;

}

Add a comment
Know the answer?
Add Answer to:
Write a new program called TrickOr Treat that will do the following 1. In a while...
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
  • 7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food...

    7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food to represent a Lunch food item. Fields include name, calories, carbs In a main method (of a different class), ask the user "How many things are you going to eat for lunch?". Loop through each food item and prompt the user to enter the name, calories, and grams of carbs for that food item. Create a Food object with this data, and store each...

  • 1.You are to write a program name search.java that will do the following: 2.You are to...

    1.You are to write a program name search.java that will do the following: 2.You are to create 3 arrays - prompt the user for a number that is greater than 100 that will serve as the size for the arrays (all 3 arrays will have the same user input size). Call them A, B & C. 3.Generate this amount of random numbers to fill these arrays – the random numbers must range from 1 to 99. 4.Write 1 sequential search...

  • Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes...

    Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes three integer values into the first three positions of an array of integers calls a method you write called doubleEachValue that takes an array of integers (the one whose values you hard-coded in main) as its only argument and returns an ArrayList of integers, with each value in returned ArrayList equal to double the correspondingly indexed value in the array that is passed in...

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • Step 1. Start Netbeans and create a project called ArrayLab Step 2. Write the main method...

    Step 1. Start Netbeans and create a project called ArrayLab Step 2. Write the main method  Declare and initialize an array called myNums int myNums = {3, 8, 12, 4, 2, 9, 6};  Declare an int called largestNum and set it to 0.  Write a for loop that prints the array for (int index = 0; index < myNums.length; index++) { System.out.println(myNums[index] + “ “); }  Write a for loop that prints the array backwards ...

  • Program Overview This brief exercise is designed for you to consider how reference variables behave when...

    Program Overview This brief exercise is designed for you to consider how reference variables behave when you are passing them as arguments by-value. Instructions Name your class References.java. 1. Implement the following methods. 1.1 Method name: readStudentNames Purpose: This method accepts an array of strings as a parameter. It loops through the array and asks the user to input names. It populates the array of strings with the names. Parameters: an array of Strings, stringArray Return type: void In the...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

  • Document2 Tell me ign Layout References Mailings Review View 1. Write a program called Lab18C that...

    Document2 Tell me ign Layout References Mailings Review View 1. Write a program called Lab18C that searches for a number in an array. T a. Write a bool function named search that receives 3 parameters: an int array, an int containing the length of the array, and an int number to search for. Return true if the number is in the array and false otherwise. b. Write a void function called fillArray with the array as a parameter that uses...

  • I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according...

    I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the...

  • Lab Objectives Be able to write methods Be able to call methods Be able to declare...

    Lab Objectives Be able to write methods Be able to call methods Be able to declare arrays Be able to fill an array using a loop Be able to access and process data in an array Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing...

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