In: Computer Science
Write one Java program and satisfy the following requirements:
5. In the main method, invoke the above methods and output their results.
import java.util.Scanner;
import java.util.Random;
import java.lang.Math;
public class Main
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter any
number n: ");
//read input for a,b,c
int n=in.nextInt();
System.out.println("cube of n
="+cube(n));
System.out.println("Random number
generated:"+randomNumber());
//read input for a,b,c and call
findMAx with 3 parameters
System.out.println("Enter 3 float
numbers :");
float a=in.nextFloat();
float b=in.nextFloat();
float c=in.nextFloat();
System.out.println("Maximum of a ,b
and c is :"+findMax(a,b,c));
//call find MAx with one
parameter
System.out.println("Enter size of
array :");
int amount=in.nextInt();
System.out.println("Maximum of
given list is :"+ findMax(amount));
}
static int cube(int n)
{
return (n*n*n);
}
static float randomNumber()
{
Random r=new Random();
float
n=r.nextFloat()*(float)(50.0-(-21.0))-(float)20.0;
return n;
}
static float findMax(float a,float b,float c)
{
if(a>b)
{
if(a>c)
return a;
else
return c;
}
else
{
if(b>c)
return b;
else
return c;
}
}
static float findMax(int amount)
{
float []array=new float[amount];
Scanner in=new Scanner(System.in);
System.out.println("Enter "+amount+"Elements to array:
");
for(int i=0;i<amount;i++) //to read
array elements
array[i]=in.nextFloat();
float max=array[0];
for(int i=1;i<amount;i++)
{
if(max<array[i])
max=array[i];
}
return max;
}//close findMax
function
}//close class