Question

In: Computer Science

Write one Java program and satisfy the following requirements: Write a method called cube that accepts...

Write one Java program and satisfy the following requirements:

  1. Write a method called cube that accepts one integer parameter and returns that value raised to the third power.

  1. Write a method called randomNumber that returns a random floating-point number in the range of [-20.0, 50.0). (hints: use Random class in the method)

  1. Write a method called findMax that accepts three floating-point number as parameters and returns the largest one.(hints: use conditional statement in the method)

  1. Overload findMax that accepts one integer parameter representing the amount, the method will read the floating-point number one by one within the amount and return the largest value. (hints: use Scanner class and loop statement in the method)

5. In the main method, invoke the above methods and output their results.

Solutions

Expert Solution

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


Related Solutions

Write one Java program and satisfy the following requirements: Rewrite the following if -else if -...
Write one Java program and satisfy the following requirements: Rewrite the following if -else if - else statement as an equivalent switch statement.   if (letter == 'A' | | letter == 'a')         System.out.println("Excellent");         else if (letter == 'B' | | letter == 'b')                System.out.println("You can do better");         else if (letter == 'C' | | letter == 'c')                System.out.println("Try harder");         else if (letter == 'D' | | letter == 'd')                System.out.println("Try much harder");        ...
***JAVA PROGRAM Write a method called shrink that accepts an array of integers (that the user...
***JAVA PROGRAM Write a method called shrink that accepts an array of integers (that the user inputs) as a parameter and returns a new array containing the result of replacing each pair of integers with the sum of that pair. For example, if an array called list stores the values {7, 2, 8, 9, 4, 15, 7, 1, 9, 10}, then the call of shrink(list) should return a new array containing {9, 17, 19, 8, 19}. The first pair from...
In Java Write a method called findMax that accepts three floating-point number as parameters and returns...
In Java Write a method called findMax that accepts three floating-point number as parameters and returns the largest one.(hints: use conditional statement in the method)
Write a program in Java which performs the sort operation. The main method accepts ten numbers...
Write a program in Java which performs the sort operation. The main method accepts ten numbers in an array and passes that to the method sort. The method sort accepts and sorts the numbers in ascending and descending order. The method display shows the result. You can use integers or floating point numbers.
Needs to be in JAVA. Write a Java program that accepts the total amount of cars...
Needs to be in JAVA. Write a Java program that accepts the total amount of cars sold and total sales amount of a car salesperson for a given month. The salesperson’s paycheck is computed as follows: a. Every sales person gets 10% (commission) of total sales b. Sales totals greater than $50,000 get 5% of total sales amount c. 8 or more cars sold earns the salesperson an extra 3% Please remove 30% (taxes) of the gross pay and list...
1. Write a Java program from scratch that meets the following requirements: a. The program is...
1. Write a Java program from scratch that meets the following requirements: a. The program is in a file called Duplicates.java that defines a class called Duplicates (upper/lower case matters) b. The program includes a Java method called noDuplicates that takes an array of integers and returns true if all the integers in that array are distinct (i.e., no duplicates). Otherwise it returns false. Make sure the method is public static. example tests: noDuplicates({}) returns true noDuplicates({-1, 1}) returns true...
Q1: Write a method called testArray that accepts a one-dimensional array of any size and prints...
Q1: Write a method called testArray that accepts a one-dimensional array of any size and prints the following summary information: a) all array elements at an even index b) every fifth element c) all elements but the first and last Write a program that demonstrates this method by using an array of 20 random integers from 100 to 200. Q2: Write a method called displayGrid that accepts a two-dimensional array of integer values from 0 to 99 and prints this...
Write a java program. The program requirements are: the program will let the user input a...
Write a java program. The program requirements are: the program will let the user input a starting number, such as 2.  It will generate ten multiplication problems ranging from 2x1 to 2x10.  For each problem, the user will be prompted to enter the correct answer. The program should check the answer and should not let the user advance to the next question until the correct answer is given to the question being asked currently. After testing a total of 10 multiplication problems,...
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube...
JAVA PROGRAMMING Write a program that ask the user for a number then prints the cube of the number. The program should be called CubeNumbers. Use a value returning method with parameter, call the method myCube.
3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT