Question

In: Computer Science

For some reason another programmer has asked you to write a function. The function is called...

For some reason another programmer has asked you to write a function.

The function is called getWarYear ( ) It has one parameter , an integer.

if it is 1 return a random number in the range of 1914 to 1919.

if it is 2 return a random number in the range of 1938 to 1945.

Since the function is returning an year the data type of the return value is an integer.

Solutions

Expert Solution

import java.util.Random;

public class RandomYear {
   public static void main(String[] args) {
       System.out.println(getWarYear(1));
       System.out.println(getWarYear(2));

   }

   private static int getWarYear(int n) {
       int min = 0, max = 0;
       // setting min and max values based on given string
       if (n == 1) {
           min = 1914;
           max = 1919;
       }
       if (n == 2) {
           min = 1938;
           max = 1945;
       }
       // generating random number in range
       Random r = new Random();
       return r.nextInt((max - min) + 1) + min;

   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

Programmer defined Function / Arrays: 1.Write a function for printing an array. void print_array (int arr[],...
Programmer defined Function / Arrays: 1.Write a function for printing an array. void print_array (int arr[], int size); 2. Write a function to generate an array of random integers. void random_array (int arr[], int size, int range); The scale of numbers should be 1 to range. 3. Write a function to find the sum of a sequence of number. int sum (int arr[], int size); 4. Write a function rotate(arr[], d, n) that rotates arr[] of size n by d...
As a programmer in a java project, you have been asked by your project manager to...
As a programmer in a java project, you have been asked by your project manager to describe the most efficient way to store the following assigned numbers 10,20,30,1000,200,350 for an operation which involves on calculation such as sum and average.
For this problem, you will be asked to write some code to accomplish a particular task...
For this problem, you will be asked to write some code to accomplish a particular task given the code fragment below. Each task may depend on the tasks that came before it. Your code must be syntactically correct. None of your operations for this problem may affect S::m_unique. class S { public: S(int init, int size, int id) :m_num(init), m_size(size) {                m_unique = new int[m_num]; for (int i = 0; i < m_num; i++)       m_unique[i] = id;...
Write a function called price_of_rocks. It has no parameters. In a while loop, get a rock...
Write a function called price_of_rocks. It has no parameters. In a while loop, get a rock type and a weight from the user. Keep a running total of the price for all requested rocks. Repeat until the user wants to quit. Quartz crystals cost $23 per pound. Garnets cost $160 per pound. Meteorite costs $15.50 per gram. Assume the user enters weights in the units as above. Return the total price of all of the material. Using Python For this...
In C Write a main function with a function call to a function called GetLetter which...
In C Write a main function with a function call to a function called GetLetter which has two double arguments/parameters. The function returns a character. Declare and initialize the necessary data variables and assign values needed to make it executable and to prevent a loss of information
In Python Create a function called ℎ?????. The function has as arguments a list called ??????...
In Python Create a function called ℎ?????. The function has as arguments a list called ?????? and a list call center. • List ?????? contains lists that represent points. o For example, if ?????? = [[4,2], [3,2], [6,1]], the list [4,2] represents the point with coordinate ? at 4 and y coordinate at 2, and so on for the other lists. Assume that all lists within points contain two numbers (that is, they have x, y coordinates). • List ??????...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
Write a function called is_equal(). This function will take as an argument two integers. Call the...
Write a function called is_equal(). This function will take as an argument two integers. Call the is_equal function and supply two integers. You do not need to get the integers from the keyboard. Compare the two integers. If the two integers are equal, then print "equal". If the two integers are not equal, print "not equal". Call the function from your main code to execute the function. Sample output: equal Sample output: not equal
This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
Write a program called whilebun.py, in which the user is asked to guess the name of...
Write a program called whilebun.py, in which the user is asked to guess the name of your favorite DBVac vacuum cleaner model. Your program should: · Include a comment in the first line with your name. · Include comments describing each major section of code. · Set a variable called vacname to be equal to the name of your favorite DBVac vacuum cleaner model. · Ask the user to guess the name. Allow the user to make up to three...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT