Question

The goal of this assignment is to use loop patterns to solve a variety of problems Starting 1. In Eclipse, create a new proje3. Method name: findSmallestNumberinTwoStrings Parameterís): Two String parameters. Each string is made up of numbers separat

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.awt.Color;

public class SearchAnOptimizingLoops {
   /*
   * 1. method to find out the smallest positive number in a String
   *
   * @param: A String of integer numbers separated by spaces
   *
   * @return: An int of smallest positive integer in the String
   */
   public static int findSmallestPositiveNumber(String intStr) {
       // Read each number in the String
       String[] numbers = intStr.split(" ");

       // Loop through each value in numbers
       int numArray[] = new int[numbers.length];
       for (int i = 0; i < numbers.length; i++) {
           // Convert and store the numbers in numArray
           numArray[i] = Integer.parseInt(numbers[i]);
       }

       // Find the smallest positve number
       for (int i = 0; i < numArray.length; i++) {
           for (int j = i + 1; j < numArray.length; j++) {
               // Check for positive number
               if (numArray[i] > 0 && numArray[j] > 0) {
                   if (numArray[i] > numArray[j]) {
                       // Swap the elements
                       int temp = numArray[i];
                       numArray[i] = numArray[j];
                       numArray[j] = temp;
                   }
               }
           }
       }

       // Return the smallest number found at the index 0
       return numArray[0];
   }

   /*
   * 2. Method that compares the words and return the lowest alphabetical word
   *
   * @param: String of words separated by spaces
   *
   * @return: Smallest alphabetical word in the String
   */
   public static String lowestAlphabetically(String wordStr) {
       // Find each word in the String
       String words[] = wordStr.split(" ");

       // Store the first word in the smallest word String
       String smallest = words[0];

       // Loop through each word and find the smallest
       for (int i = 1; i < words.length; i++) {
           // Compare the word at index i with in the smallest, if returns negative update
           // the
           // smallest variable
           if (words[i].compareTo(smallest) < 0)
               smallest = words[i];
       }

       // Return the smallest word
       return smallest;
   }

   /*
   * 3. Method to find the smallest number in two strings
   *
   * @param: Two sStrings with numbers separated by spaces
   *
   * @return: An int having smallest number found
   */
   public static int findSmallestnumberinTwoStrings(String intStr1, String intStr2) {
       // Separate the numbers in two Strings
       String numbers1[] = intStr1.split(" ");
       String numbers2[] = intStr2.split(" ");

       // Create a Single array of numbers
       int numArray[] = new int[numbers1.length + numbers2.length];

       // Store the numbers in numArray
       for (int i = 0; i < numbers1.length; i++)
           numArray[i] = Integer.parseInt(numbers1[i]);

       for (int j = numbers1.length, i = 0; i < numbers2.length; j++, i++)
           numArray[j] = Integer.parseInt(numbers2[i]);

       // find the smallest number
       // Store the first number in numArray in smallest variable
       int smallest = numArray[0];

       // Loop through values in numArray and find the smallest number by comparing to
       // number in smallest variable
       for (int i = 1; i < numArray.length; i++) {
           if (numArray[i] < smallest)
               smallest = numArray[i];
       }

       // Return the smallest number
       return smallest;
   }

   /*
   * 4. Method for scaling the numbers by a factor of 100
   *
   * @param: A String containing numbers from 0 to 100, separated by spaces
   *
   * @return: A String with scaled numbers for the factor of 100 with respect to
   * the largest number
   */
   public static String curveScores(String intStr) {
       // Read each number in the String
       String[] numbers = intStr.split(" ");

       // Loop through each value in numbers
       int numArray[] = new int[numbers.length];
       for (int i = 0; i < numbers.length; i++) {
           // Convert and store the numbers in numArray
           numArray[i] = Integer.parseInt(numbers[i]);
       }

       // Find the largest number
       // Store the number at index 0 as the largest
       int largest = numArray[0];

       // Loop through each value of numArray
       for (int i = 1; i < numArray.length; i++) {
           if (numArray[i] > largest)
               largest = numArray[i];
       }

       // Calculate the scale factor
       int sFactor = 100 - largest;

       // Update each number in numArray with he scale factor
       String num = "";
       for (int i = 0; i < numArray.length; i++) {
           numArray[i] = numArray[i] + sFactor;

           // Create the String with the Scaled numbers
           num += numArray[i] + " ";
       }

       // Return the num String
       return num;
   }

   /*
   * 5. Method to determine if the color is contained in the Picture object
   *
   * @param: Picture Object and Color Object
   *
   * @return boolean value, true if the color is contained in the picture object,
   * otherwise false
   */
   public static boolean containsThisColor(Color pObj, Color cObj) {
       // Compare the picture object with color object
       if (pObj.equals(cObj))
           return true;

       else
           return false;
   }

   // Driver method main
   public static void main(String args[]) {
       System.out.println(findSmallestPositiveNumber("2 -4 5"));
       System.out.println(lowestAlphabetically("cat dog apple fish"));
       System.out.println(findSmallestnumberinTwoStrings("12 3 5", "2 -1 10"));
       System.out.println(curveScores("45 85 90"));
       System.out.println(containsThisColor(new Color(100, 200, 50), new Color(100, 100, 100)));
   }

}

Screenshot of Output:

e eclipse-workspace Chegg/src/SearchAnOptimizingLoops.java - Eclipse File Edit Source RefactorNavigate Seh Project Run Window

Add a comment
Know the answer?
Add Answer to:
The goal of this assignment is to use loop patterns to solve a variety of problems...
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
  • 1.Method name: findSmallestPositiveNumber Parameter(s): A String containing integer numbers separated by spaces. There must be at...

    1.Method name: findSmallestPositiveNumber Parameter(s): A String containing integer numbers separated by spaces. There must be at least one positive number in the String. Return value: An int value that is the smallest number greater than 0 in the input string. Example: findSmallestPositiveNumber("2 -4 5") should return 2. Note: Even though I put this first, I would consider working on this later. The other methods are basically just applying the patterns we looked at in class. This one is similar but...

  • Please I need help. Java language Method name: getScores Return value is a String of numbers...

    Please I need help. Java language Method name: getScores Return value is a String of numbers scaled so that the highest number in the parameter String becomes 100 and all the other numbers are moved up by the same amount. This String should also be numbers separated by spaces with no additional characters. The order of the numbers should stay the same, so that a number in the original string has the equivalent scaled number in the returned string in...

  • Please I need help. Java language Method name: getScores Return value is a String of numbers...

    Please I need help. Java language Method name: getScores Return value is a String of numbers scaled so that the highest number in the parameter String becomes 100 and all the other numbers are moved up by the same amount. This String should also be numbers separated by spaces with no additional characters. The order of the numbers should stay the same, so that a number in the original string has the equivalent scaled number in the returned string in...

  • use Java and it must work for any name String Manipulator Write a program to manipulate...

    use Java and it must work for any name String Manipulator Write a program to manipulate Strings. Store your full name into one String variable. It must include first name, middle name, last name, cach separated by spaces. Spaces could vary. If you do not have a middle name make up one For example the string to be processed could be any of the following John Plain Doe John Plain Doc John Plain Doe Your program must be able to...

  • Deliverable A zipped NetBeans project with 2 classes app ZipCode Address Classes Suggestion: Use Netbeans to...

    Deliverable A zipped NetBeans project with 2 classes app ZipCode Address Classes Suggestion: Use Netbeans to copy your last lab (Lab 03) to a new project called Lab04. Close Lab03. Work on the new Lab04 project then. The Address Class Attributes int number String name String type ZipCode zip String state Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: number - 0 name - "N/A" type...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data...

    FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data as arguments (parameters) for a number of functions you will write. You will need to: Write and test a function square_each(nums) Where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, a return will not be allowed! Write and test a function sum_list(nums) Where nums is a...

  • Java language Any use of java.util.LinkedList is prohibited Objective: The goal of this assignment is to...

    Java language Any use of java.util.LinkedList is prohibited Objective: The goal of this assignment is to practice recursion. ignment: The assignment requires writing recursive methods for some linked list operations. The use of loops in the recursive methods is strictly prohibited in this assignment. That is, you cannot use for, while, and do-while in the recursive methods you will write. You can only use a loop when you will initiate values for a linked list in the main method. You...

  • All files require Javadoc documentation comments at the top to include a description of the program...

    All files require Javadoc documentation comments at the top to include a description of the program and an @author tag with your name  only.    -------------------------------------- Develop an exception class named InvalidMonthException that extends the Exception class.   Instances of the class will be thrown based on the following conditions: The value for a month number is not between 1 and 12 The value for a month name is not January, February, March, … December -------------------------------------- Develop a class named Month.  The class should define an integer...

  • JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using...

    JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using recursion. The method signatures are included in the starter code below, with a more detailed explanation of what function the method should perform. You will be writing the following methods: stringClean() palindromeChecker() reverseString() totalWord() permutation() You will be using tools in the String class like .substring(), .charAt(), and .length() in all of these methods, so be careful with indices. If you get stuck, think...

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