QuestionQuestion

1. Write complete code for the method named countNeg, whose documentation is provided below. You must use recursion.   

/**
* Count the number of negatives
*
* @param nums int[]
* @param n int
* @return int the number of negative values in the
*                first n components of the array nums   
   */

Write complete code for the method named isAllNeg, whose documentation is provided below. You must use recursion.   

/**
* Check if all components are negatives
*
* @param nums int[]
* @param n int
* @return boolean true if and only if the first n
         * components of the array nums are
*             all negative; false, otherwise   
*/

2. Consider the following method. The numbers on left indicate line numbers.

1 public int min (int[] nums)
2 {
3     int smallest = nums[0];
4     for (int index = 1; index < nums.length; index++)
5        if (nums[index] < smallest)
6             smallest = nums[index];
7     return smallest;
8 }

Write Java documentation for the method similar to what is written in Question 1, describing the purpose, parameters, and the returned value.

Suppose the array nums has six components. Create a test case for the nums array such that the assignment statement in line 6 is never executed.

nums[0]    nums[1]    nums[2]    nums[3]    nums[4]    nums[5]

Suppose the array nums has six components. Create a test case for the nums array such that the assignment statement in line 6 is executed every time though the loop.

nums[0]    nums[1]    nums[2]    nums[3]    nums[4]    nums[5]

3. Write a complete main method for which a file name is provided as a parameter. That is, args[0] provides the file name. The program opens the file, reads it. The file contains several integers. The program computes and prints the sum total of the integers. Show the try and catch for exception handling in the context of opening the file. A sample output of the program is as follows:

The sum total of all integers in the file is 124. 

4. Consider a class named Time1 and another class named Time2 that extends Time1. Both keep time in military format, with hour in the range 0-23.
a. Write Java code for Time1. It has two instance variables hour and minute.   It has a constructor that takes two parameters and sets the time. (Use mod operator to ensure that hour is in the range 0-23 and minute is in the range 0-59. Do not adjust hour just because minute >= 60.) The class has two getter methods.
b. Write Java code for Time2 to extend Time1. It has an extra instance variable named second. It has a constructor with three parameters. It has an extra getter method. (Use mod operator to ensure that second is in the range 0-59. Do not adjust minute just because second >= 60.)

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

1. Write complete code for the method named countNeg, whose documentation is provided below. You must use recursion.   

/**
* Count the number of negatives
*
* @param nums int[]
* @param n int
* @return int the number of negative values in the
*                first n components of the array nums   
   */

static int countNeg(int[] num, int n) {
       if (n < 0 || n >= num.length) {
            return 0;
       }
       if (num[n] < 0) {
            return 1 + countNeg(num, n + 1);
       }
       return countNeg(num, n + 1);
    }...

By purchasing this solution you'll be able to access the following files:
Solution.docx.

$30.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats