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

The project description: As a programmer, you have been asked to write a Java application, using...
The project description: As a programmer, you have been asked to write a Java application, using OOP concepts, for a Hospital with the following requirements: • The Hospital has several employees and each one of them has an ID (int), name (string), address (string), mobile phone number (string), email (string) and salary (double) with suitable data types. • The employees are divided into: o Administration staff: who have in addition to the previous information their position (string). o Doctor: who...
As a programmer, you have been asked to write a Java application, using OOP concepts, for...
As a programmer, you have been asked to write a Java application, using OOP concepts, for a Hospital with the following requirements: • The Hospital has several employees and each one of them has an ID (int), name (string), address (string), mobile phone number (string), email (string) and salary (double) with suitable data types. • The employees are divided into: o Administration staff: who have in addition to the previous information their position (string). o Doctor: who have also a...
(C++) In a file called pp7c.cpp, write a function called printMoney that has one parameter, a...
(C++) In a file called pp7c.cpp, write a function called printMoney that has one parameter, a double, and it prints this parameter out formatted like a dollar amount with $ and exactly 2 digits to the right of the decimal. Write a driver that declares an array of monetary amounts like this: double amounts[MAX_AMOUNTS]; and uses a while or do while loop to ask the user for monetary amounts with -1 for the amount as a sentinel value to put...
Having trouble getting started on this Overview As a programmer, you have be asked by a...
Having trouble getting started on this Overview As a programmer, you have be asked by a good friend to create a program that will allow them to keep track of their personal expenses. They want to be able to enter their expenses for the month, store them in a file, and then have the ability to retrieve them later. However, your friend only wants certain people to have the ability to view his or her expenses, so they are requesting...
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.
# Write a function called `get_state_data` that allows you to specify a state, # then saves...
# Write a function called `get_state_data` that allows you to specify a state, # then saves a .csv file (`STATE_data.csv`) with observations from that state # This includes data about the state, as well as the counties in the state # You should use the full any.drinking dataset in this function (not just 2012) # Demonstrate that you function works by passing "Utah" to the function state_Utah <- get_state_data(Utah) ############################ Binge drinking Dataset ############################ # In this section, you will...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT