In: Computer Science
1 Write a Java method that takes an integer, n, as input and returns a reference to an array of n random doubles between 100.0 and 200.0. Just write the method.
2. You have a class A:
public class A
{
int i, double d;
public A(double d)
{
this.d=d;
this.i=10;
}
}
import java.io.*;
public class A
{
int i;
double d;
public A(double d)
{
this.d=d;
this.i=10;
}
public double[] random(int n)
{
double min=100,max=200;
double []res=new double[n];
//initilaize n values to array
for(int j=0;j<n;j++)
{
//if you need to round off to two decimal places use this
// Math.round(a * 100.0) / 100.0; a should be R.H.s of below
statement and 100 is for 2 decimals(2 zero's)
res[j]=Math.random()*(max-min+1)+min;
}
return res;
}
public static void main(String[] args) {
Main m=new Main(200);
double []res=m.random(10);
for(int j=0;j<10;j++)
{
System.out.print(res[j]+" ");
}
}
}
I don't know why the constructor is present and i,d are declared but i gave the method for it
comment if any doubts