The goal of this assignment is to use loop patterns to solve a variety of proble
ID: 3665451 • Letter: T
Question
The goal of this assignment is to use loop patterns to solve a variety of problems.
To get started, follow these instructions:
In Eclipse, create a new package called assign3. Add a new class called LoopPatterns to this package.
In your LoopPatterns class, implement the ten static methods specified below. Each method has a Scanner object as one of its parameters and operates on information provided by that Scanner object. As we have seen in class, a Scanner object can obtain text from a string, the keyboard, or a file.
This is an example of creating a Scanner object that takes input from a string:
This is an example of creating a Scanner object that takes input from the keyboard (i.e., console):
This is an example of creating a Scanner object that takes input from a file:
You should write a main method that tests each of your methods using Scanner objects created however you like. Write at least four tests per method that cover a variety of possible scenarios, taking particular care to test boundary/edge cases.
None of your methods may print anything to the console. Of course, the one exception is your main method, in which you test the other methods. Leave all testing code in main for grading.
Ensure that you have used meaningful variables names, consistent indentation/spacing, and appropriate comments. The class and all methods should be commented using Javadoc. The class-level comment must contain your name. Use additional comments, as needed. Overall, your program should have good programming style.
Export your LoopPatterns.java file and submit it using the button below.
--Method name: sum
Parameter: a Scanner object containing integers
Return value: the sum of all integers
Example: sum(new Scanner("5 1 1 -2 70")) should return 75
Note: Your method should work even if the Scanner object contains no integers.
--Method name: searchWord
Parameters (in order): a Scanner object containing words, a word to search for
Return value: true if the word occurs, false otherwise
Example: searchWord(new Scanner("apple bee cat bee cat dog"), "tac") should return false
Notes: Your method should work even if the Scanner object contains no words. Recall that because String is a special data type, we should not compare for equality using ==.
--Method name: average
Parameter: a Scanner object containing double values
Return value: the average value
Example: average(new Scanner("3.2 9.0 -7.1 104.5")) should return 27.4
Note: You may assume that the Scanner object contains at least one value.
--Method name: moreWordsOfEvenLength
Parameter: a Scanner object containing words
Return value: true if there are more words of even length than words of odd length, false otherwise
Example: moreWordsOfEvenLength(new Scanner("I love Java")) should return true
Note: Your method should work even if the Scanner object contains no words.
--Method name: emphasize
Parameter: a Scanner object containing words
Return value: a String where anything between a pair of asterisks is all uppercase
Example: emphasize(new Scanner("I am so *angry* right now! ")) should return I am so ANGRY right now!
Notes: Your method should work even if the Scanner object contains no words.
The asterisks will only come in pairs. There will never be an odd number of asterisks.
--Method name: firstLetters
Parameter: a Scanner object containing words
Return value: a String made from the first letter of every word (in order)
Example: firstLetters(new Scanner("Let me Google that for you")) should return "LmGtfy"
Notes: Your method should work even if the Scanner object contains no words.
--Method name: countDivBy3
Parameter: a Scanner object containing integers
Return value: the number of integers that are evenly divisible by 3
Example: countDivBy3(new Scanner("2 -30 9 55 -10 48")) should return 3
Note: Your method should work even if the Scanner object contains no integers.
--Method name: firstWord
Parameter: a Scanner object containing words
Return value: the word that comes first in lexicographical order
Example: firstWord(new Scanner("one two three four")) should return "four"
Notes: You may assume that the Scanner object contains at least one word. Use the compareTo method of String for comparing two words. Read the API as needed to learn about its parameters and return value.
--Method name: countInRange
Parameters (in order): a Scanner object containing double values, a double value A, a double value B
Return value: the number of values that are between A and B (inclusive)
Example: countInRange(new Scanner("-11.8 -2.0 9.25 3.0 5.0"), -2.0, 5.1) should return 3
Note: Your method should work even if the Scanner object contains no values.
--Method name: sumOfTangents
Parameter: a Scanner object containing double values
Return value: the sum of the trigonometric tangents of the values
Example: sumOfTangents(new Scanner("4.3 6.5 8.7")) should return about 1.620568
Notes: You may assume that the Scanner object contains at least one value.
NOTE: The base assignment does not get input from or give output to the user. The course staff will grade the programs of all CS 1410 students with the same set of calls to each of the ten methods. Therefore, it is critical that your program contains all method names, return types, and parameter lists exactly as instructed, and that there are no print statements in any method except main. Otherwise, some or all of the method calls written by the course staff will fail for your program, even if the code contained in the bodies of your methods is correct.
EXTRA CHALLENGE: A correct and complete program that meets the specifications above and has good programming style will receive 95% of the credit for this programming assignment. The last 5% of credit depends on completion of an extra challenge, which gives interested and motivated students the opportunity to go above and beyond the basic requirements. Note that the amount of work / time / intellect required to complete the extra challenge is more than for the base assignment, yet is worth much less credit.
Create a new class called LoopPatternsExtra in package assign3.
Add one static method to LoopPatternsExtra, as specified below. Of course, you should use the main method for testing. Do not add any other methods to this class.
Method name: removeLetters
Parameters (in order): a String S1, a String S2
Return value: a String the same as S1 but with all of the letters in S2 removed
Example: removeLetters("Mississippi River", "is") should return "Mpp Rver"
Notes: Your method should work even if S1, S2, or both are empty. You are to remove each of the letters contained in S2 from S1, not just the entire substring S2. (E.g., the letter i and the letter s are removed in the example, even if "is" does not appear together).
Explanation / Answer
LoopPatterns.java
package assign3;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
/**
* LoopPatterns class
*
* @author YourName
*
*/
public class LoopPatterns {
public static void main(String[] args) {
sum(new Scanner("5 1 1 -2 70"));
searchWord(new Scanner("apple bee cat bee cat dog"), "tac");
average(new Scanner("3.2 9.0 -7.1 104.5"));
moreWordsOfEvenLength(new Scanner("I love Java"));
emphasize(new Scanner("I am so *angry* right now! "));
firstLetters(new Scanner("Let me Google that for you"));
countDivBy3(new Scanner("2 -30 9 55 -10 48"));
firstWord(new Scanner("one two three four"));
countInRange(new Scanner("-11.8 -2.0 9.25 3.0 5.0"), -2.0, 5.1);
sumOfTangents(new Scanner("4.3 6.5 8.7"));
}
/**
* Method to sum given integers
*
* @param scanner
* @return totalSum
*/
public static int sum(Scanner scanner) {
int totalSum = 0;
while (scanner.hasNext()) {
totalSum += scanner.nextInt();
}
return totalSum;
}
/**
* Method to search a word in a given sentence
*
* @param scanner
* @param serachWord
* @return true/false
*/
public static boolean searchWord(Scanner scanner, String serachWord) {
StringBuffer completeSentence = new StringBuffer();
while (scanner.hasNext()) {
completeSentence.append(scanner.next());
}
if (null != serachWord
&& serachWord.contains(completeSentence.toString())) {
return true;
}
return false;
}
/**
* Method to calculate the average of given numbers
*
* @param scanner
* @return average
*/
public static double average(Scanner scanner) {
double totalSum = 0.0;
int count = 0;
while (scanner.hasNext()) {
totalSum += scanner.nextDouble();
count++;
}
double average = totalSum / count;
return average;
}
/**
* Method to find if a sentence is having more number of words with even
* length than odd length
*
* @param scanner
*
* @return true/false
*/
public static boolean moreWordsOfEvenLength(Scanner scanner) {
int evenCount = 0;
int oddCount = 0;
while (scanner.hasNext()) {
int length = scanner.next().length();
if (length % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
}
if (evenCount > oddCount) {
return true;
}
return false;
}
/**
* Method to make a word emphasize when it is between asterisks
*
* @param scanner
* @return newSentence
*/
public static String emphasize(Scanner scanner) {
StringBuffer newSentence = new StringBuffer();
while (scanner.hasNext()) {
String word = scanner.next();
if (word.charAt(0) == '*' && word.charAt(word.length() - 1) == '*') {
newSentence.append(word.substring(1, word.length() - 1)
.toUpperCase() + " ");
} else {
newSentence.append(word + " ");
}
}
return newSentence.toString();
}
/**
* method to make a word by taking every first letter of word in a sentence
*
* @param scanner
* @return newWord
*/
public static String firstLetters(Scanner scanner) {
StringBuffer newWord = new StringBuffer();
while (scanner.hasNext()) {
newWord.append(scanner.next().charAt(0));
}
return newWord.toString();
}
/**
* Method to check how many numbers are divisible by 3
*
* @param scanner
* @return count
*/
public static int countDivBy3(Scanner scanner) {
int count = 0;
int input = 0;
while (scanner.hasNext()) {
input = scanner.nextInt();
if (input % 3 == 0) {
count++;
}
}
return count;
}
/**
* Method to return a word that comes first in lexicographical order
*
* @param scanner
* @return word
*/
public static String firstWord(Scanner scanner) {
List<String> wordsList = new ArrayList<String>();
String word = null;
while (scanner.hasNext()) {
wordsList.add(scanner.next());
}
Collections.sort(wordsList);
if (null != wordsList && !wordsList.isEmpty()) {
word = wordsList.get(0);
}
return word;
}
/**
* Method to check how many numbers are present in a given range
*
* @param scanner
* @param d
* @param e
* @return count
*/
public static int countInRange(Scanner scanner, double d, double e) {
int count = 0;
double input = 0.0;
while (scanner.hasNext()) {
input = scanner.nextDouble();
if (input <= d || input >= e) {
count++;
}
}
return count;
}
/**
* Method to get sum of given tangents numbers
*
* @param scanner
* @return sumOfTangents
*/
public static double sumOfTangents(Scanner scanner) {
double sumOfTangents = 0.0;
while (scanner.hasNext()) {
sumOfTangents += Math.tan(scanner.nextDouble());
}
return sumOfTangents;
}
}
LoopPatternsExtra.java
package assign3;
import java.util.BitSet;
/**
* LoopPatternsExtra class to remove characters which are present in string 2
* from string 1
*
* @author YourName
*
*/
public class LoopPatternsExtra {
public static void main(String[] args) {
removeLetters("Mississippi River", "is");
}
/**
* Method to remove characters which are present in string 2 from string1
*
* @param param1
* @param param2
* @return string
*/
public static String removeLetters(String param1, String param2) {
BitSet charSet = new BitSet();
String newString = null;
for (char c : param2.toCharArray())
charSet.set(c);
StringBuilder outputStr = new StringBuilder();
for (char c : param1.toCharArray())
if (charSet.get(c) == false)
outputStr.append(c);
newString = outputStr.toString();
return newString;
}
}
Note:As it clearly mentioned in program we should not print any inforamtion console ,so iam not placing the output screen shot.Please feel free to ask if you have any doubts.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.