Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA problem I need help fixing the code to make it pass. public class Task1 { public static void main(String[] args){ int lower = 10;

JAVA problem

I need help fixing the code to make it pass.

public class Task1 { public static void main(String[] args){ int lower = 10; int higher = 20; int best = mostDivisible(lower, higher); // Let's print out what the value is politely. System.out.println("the number from "+lower+" to "+higher + " inclusive with the most divisors is " + best); }

/** * given the lower and upper bound on a range of numbers, * which one has the most divisors? * * @param low the lowest number to consider (included) * @param high the highest number to consider (included) * * @return the number in range that has the most divisors */ public static int mostDivisible(int low, int high){ // store the best answer candidate in ans, and its number of divisors in numDivs. int ans = low; int numDivs = 0; // check each possible answer candidate to see if it's better than any before it. for (int i=low; i < high; i++){ // calculate current number's divisors. int currentDivs; // look at each possible divisor and include it in the count if it counts. for (int d=1; d<=i; d++){ if (d%i=0){ currentDivs++; } } if (currentDivs>numDivs) ans = i; numDivs = currentDivs; } return ans; } }

AND THE TESTER:

import org.junit.*; import static org.junit.Assert.*; import java.util.*; public class Task1Tests { public static void main(String args[]){ org.junit.runner.JUnitCore.main("Task1Tests"); } @Test public void task1_01 (){ assertEquals( 12, Task1.mostDivisible( 10, 20)); } @Test public void task1_02 (){ assertEquals( 30, Task1.mostDivisible( 29, 31)); } @Test public void task1_03 (){ assertEquals( 6, Task1.mostDivisible( 6, 8)); } @Test public void task1_04 (){ assertEquals( 60, Task1.mostDivisible( 1, 100)); } @Test public void task1_05 (){ assertEquals( 12, Task1.mostDivisible( 5, 12)); } @Test public void task1_06 (){ assertEquals( 12, Task1.mostDivisible( 5, 13)); } @Test public void task1_07 (){ assertEquals( 12, Task1.mostDivisible( 12, 14)); } @Test public void task1_08 (){ assertEquals( 6, Task1.mostDivisible( 5, 10)); } @Test public void task1_09 (){ assertEquals( 6, Task1.mostDivisible( 6, 10)); } @Test public void task1_10 (){ assertEquals( 6, Task1.mostDivisible( 6, 11)); } @Test public void task1_11 (){ assertEquals( 12, Task1.mostDivisible( 6, 12)); } @Test public void task1_12 (){ assertEquals( 1, Task1.mostDivisible( 1, 1)); } @Test public void task1_13 (){ assertEquals( 10, Task1.mostDivisible( 10, 10)); } @Test public void task1_14 (){ assertEquals( 100, Task1.mostDivisible(100, 100)); } @Test public void task1_15 (){ assertEquals( 5, Task1.mostDivisible( 5, 4)); } @Test public void task1_16 (){ assertEquals( 13, Task1.mostDivisible( 13, 12)); } @Test public void task1_17 (){ assertEquals( 100, Task1.mostDivisible(100, 1)); } @Test public void task1_18 (){ assertEquals( 840, Task1.mostDivisible( 1, 1000)); } @Test public void task1_19 (){ assertEquals( 60, Task1.mostDivisible( 29, 111)); } @Test public void task1_20 (){ assertEquals( 40, Task1.mostDivisible( 40, 45)); } }

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

Step: 3

blur-text-image

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

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Luc Perkins, Eric Redmond, Jim Wilson

2nd Edition

1680502530, 978-1680502534

More Books

Students also viewed these Databases questions