Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Notice how the process is automated, as long as you have the correct: Class name, EXACT spelling 1. Correct return type 2. Correct Method name(s)

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Notice how the process is automated, as long as you have the correct:

  • Class name, EXACT spelling
  • 1. Correct return type
  • 2. Correct Method name(s)
  • 3. Correct parameter list and types
  • Know that zero and negative numbers or data might be possible
  • Prepare for totally different data to be tested by me in the end
2. (25 points) Submit Chapter5.java with methods described below: exercise 5.2 god exercise 5.3 to Binary exercise 5.13 consecutive exercise 5.18 digitSum exercise 5.20 digitRange And here's an example of how I write code to test code: GradeChapter5.java D // CS210 BC public class GradeChapter5 { public static void main(String[] args) { double score=0; if (Chapter5.gcd(927686892, 2) == 2) score+=2.5; // else print comme nt if (Chapter5.gcd(0, 0) == 0) score+=2.5; // else print comment if (Chapter5.toBinary(88).equals("1011000")) score+=2.5; // else pri nt comment if (Chapter5. toBinary().equals("0")) score+=2.5; // else print comm ent if (Chapter5.consecutive(984753,984754,984755)) score+=2.5; // else print comment if (!Chapter5.consecutive(0, 0, 0)) score+=2.5; // else print commen if (Chapter5.digitSum(568679) == 41) score+=2.5; // else print comme if (Chapter5.digitSum(0) == ) score+=2.5; // else print comment if (Chapter5.digitRange(799857) == 5) score+=2.5; // else print comm ent if (Chapter5.digitRange(0) == 1) score+=2.5; // else print comment System.out.println(score); BJP5 Exercise 5.2: gcd Language/Type: Java method basics mod while loops Author: Leslie Ferguson (on 2019/09/19) Write a method named god that accepts two integers as parameters and returns the greatest common divisor of the two greatest common divisor (GCD) of two integers a and b is the largest integer that is a factor of both a and b. The GCD of a 1 is 1, and the GCD of any number and O is that number. One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the following: GCD(A, B) = GCD(B, A % B) GCD(A, 0) = Absolute value of A In other words, if you repeatedly mod A by B and then swap the two values, eventually B will store 0 and A will store the gn common divisor. For example: gcd(24, 84) returns 12, gcd (105, 45) returns 15, and gcd(0, 8) returns 8. NMDOO Type your solution here: 1 public int gcd(int a, int b) { while(b != 0) { int temp = a; a = b; b = temp% b; return Math.abs(a); BJP5 Exercise 5.3: to Binary Language/Type: Author: Java binary numbers method basics mod while Leslie Ferguson (on 2019/09/19) Write a method named toBinary that accepts an integer as a parameter and returns a string of that number's representa For example, the call of toBinary(42) should return "101010". Type your solution here: 1 public String toBinary(int num) { if(num == 0) return "0"; int mask = 0x8000; StringBuilder bin = new StringBuilder(); while(mask != 0 && (num & mask) == 0) { mask >>>= 1; while(mask != 0) { if ((num & mask) != 0) { bin.append('1'); } else { bin.append('0'); mask >>>= 1; return bin.toString(); 22 } Show Header BJP5 Exercise 5.13: consecutive Language/Type: $ Java if/else method basics return Author: Leslie Ferguson (on 2019/09/19) Write a method named consecutive that accepts three integers as parameters and returns true if they are three consecutive numbers that is, if the numbers can be arranged into an order such that there is some integer k such that the parameters' values are k, k+1, and k+2. Your method should return false if the integers are not consecutive. Note that order is not significant; your method should return th same result for the same three integers passed in any order. For example, the calls consecutive(1, 2, 3), consecutive(3, 2, 4), and consecutive(-10, 8, -9) would return true. The calls consecutive(3, 5, 7), consecutive(1, 2, 2), and consecutive(7, 7, 9) would return false. NMOON Type your solution here: 1 public boolean consecutive(int a, int b, int c) { int max = Math.max(a, Math.max(b, c)); int min = Math.min(a, Math.min(b, c)); int mid = a + b + c - max - min; return max == mid + 1 && mid == min + 1; } O Show Header BJP5 Exercise 5.18: digitSum Language/Type: Java method basics mod Author: Marty Stepp (on 2019/09/19) Write a method named digitSum that accepts an integer as a parameter and returns the sum of the digits of that number. For example, digitSum (29107) returns 2+9+1+0+7 or 19. For negative numbers, return the same value that would result if the number were positive. For example, digitSum( -456) returns 4+5+6 or 15. Type your solution here: 1 public int digitSum(int n) { n = Math.abs(n); int sum = 0; NM LOOM 000 while(n > 0) { sum += n % 10; n / = 10; return sum; 11 ] This is a method problem Write a lava method as described Do not write a complete program or class just the method(s) above Show Header BJP5 Exercise 5.20: digitRange Language/Type: Java method basics mod return while loops Author: Marty Stepp (on 2019/09/19) Write a method named digitRange that accepts an integer as a parameter and returns the range of values of its digits. The range is defined as 1 more than the difference between the largest and smallest digit value. For example, the call of digitRange( 68437) would return 6 because the largest digit value is 8 and the smallest is 3, so 8 - 3+ 1 = 6. If the number contains only one digit, return 1. You should solve this problem without using a String. Type your solution here: 1 public int digitRange(int num) { num = Math.abs(num); int max = num % 10; int min = num % 10; num/= 10; while(num > 0) { int digit = num % 10; if(digit > max) { max = digit; } else if(digit

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

Practical Neo4j

Authors: Gregory Jordan

1st Edition

1484200225, 9781484200223

More Books

Students also viewed these Databases questions