Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package a4; import java.awt.Color; public class SearchAndOptimizingLoops { /* * 1. method to find out the smallest positive number in a String * * @param:

package a4;

import java.awt.Color;

public class SearchAndOptimizingLoops {

/*

* 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

{

// Convert and store the numbers in numArray

numArray[i] = Integer.parseInt(numbers[i]);

}

// Find the smallest positve number

for (int i = 0; i

{

for (int j = i + 1; 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

{

// Compare the word at index i with in the smallest, if returns negative update

// the

// smallest variable

if (words[i].compareTo(smallest)

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

numArray[i] = Integer.parseInt(numbers1[i]);

for (int j = numbers1.length, i = 0; 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

{

if (numArray[i]

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

{

// 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

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[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(Picture pObj, Color cObj)

{

// Compare the picture object with color object

Color cObj2 = cObj;

if (pObj.equals(cObj2))

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"));

Picture testPicture = new Picture("Arches.jpg");

System.out.println(containsThisColor(testPicture, new Color(100, 100, 100)));

}

}

image text in transcribedimage text in transcribedPlease fix the existing code , so it works

Test curveScores with '50 60 70 80 90' (0.0/9.0) Test Failed! curvescores failed with '50 60 70 80 90' expected : but was : at a4.SearchAndOptimizingLoopsTest.testcurveScoresBasic:82 (SearchAndOptimizingLoopsTest.java) Test containsThisColor with white image and white color (0.0/9.0) Test Failed! containsThiscolor failed with white image and white color expected: but was: at a4.SearchAndoptimizingLoopsTest.containsThisColorTrue:124 (SearchAndoptimizingLoopsTest.java)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Using Entity Relationship Diagrams

Authors: Sikha Saha Bagui, Richard Walsh Earp

3rd Edition

103201718X, 978-1032017181

More Books

Students also viewed these Databases questions

Question

Question 2

Answered: 1 week ago

Question

Organize and support your main points

Answered: 1 week ago

Question

Move smoothly from point to point

Answered: 1 week ago

Question

Outlining Your Speech?

Answered: 1 week ago