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

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...
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.
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...
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
The program should be able to do the following: In Java accepts one command line parameter....
The program should be able to do the following: In Java accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated by a single whitespace. reads the integers from the text file in part a into an array of integers. sort the integers...
Create a java program that will do the following: Create a method called getInt.Allow the user...
Create a java program that will do the following: Create a method called getInt.Allow the user to enter up to 20 student names,and for each student 3 quiz scores (in the range 0-100). Once input is done, display each student’s name, their three quiz scores, and their quiz score average, one student per line. The output table does not need to line up perfectly in columns.Use dialog boxes for all input and output.Use the method to input the three scores.Parameter...
XML and JAVA Write a Java program that meets these requirements. It is important you follow...
XML and JAVA Write a Java program that meets these requirements. It is important you follow these requirements closely. • Create a NetBeans project named LastnameAssign1. Change Lastname to your last name. For example, my project would be named NicholsonAssign1. • In the Java file, print a welcome message that includes your full name. • The program should prompt for an XML filename to write to o The filename entered must end with .xml and have at least one letter...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4. For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT