Question

In: Computer Science

Java Programing: Predefined mathematical methods that are part of the class Math in the package java.lang...

Java Programing:

Predefined mathematical methods that are part of the class Math in the package java.lang include those below.

method name

description

abs( m )

returns the absolute value of m

ceil( m )

rounds m to the smallest integer not less than m

floor( m )

rounds m to the largest integer not greater than m

max( m , n )

returns the larger of m and n

min( m , n )

returns the smaller of m and n

pow( m , n )

returns m raised to the power n

round( m )

returns a value which is the integer closest to m

sqrt( m )

returns a value which is the square root of m

Evaluate each of the following, which include Math predefined methods.

(1)       _____ Math.abs( 6.0 )              (2)       _____ Math.abs( - 6.0 )

(3)       _____ Math.ceil( 10.25 )           (4)       _____ Math.ceil( - 6.8 )

(5)       _____ Math.floor( - 5.1 )           (6)       _____ Math.floor( 7.9 )

(7)       _____ Math.pow( 5 , 2 )             (8)       _____ Math.pow( 2 , 5 )

(9)       _____ Math.max( 1.5 , 2 )           (10)     _____ Math. min( 3 , 0.5 )

(11)     _____ Math.min( 2 , 1.5 )           (12)     _____ Math. max( 0.5 , 3 )

(13)     _____ Math.sqrt( 16.0 )            (14)     _____ Math. round( 0.6 )

(15)     _____ Math.ceil( Math.pow( 3 , 0.5 ) )

(16)     _____ Math.floor( Math.pow( 1.5 , 2 ) )   

(17)     _____ Math.ceil( Math.floor( 3.5 ) )

(18)     _____ Math.min( Math.max( 3.0 , 2 ), 2.5)

(19)     _____ Math.max( Math.min( 2.0 , 3 ), 3.0)

(20)     _____ Math.sqrt( Math.pow( 2.0 , 3 ) )    

(21)     _____ Math.pow( Math.sqrt( 9 ) , 2 ) )

(22)     _____ Math.round( Math.max( 2.1 , 3.1 ) )

(23)     _____ Math.abs( Math.abs( 8.0 ) )

(24)     _____ Math.min( Math.abs( 1.5 , 2 ) )

(25)     _____ Math.ceil( Math.pow( 1 , 0.5 ) )

Solutions

Expert Solution

import java.io.*;

public class math{
public static void main(String args[])
{

System.out.println("Solving some problem using inbuilt funstion of the math library:");
//Math.abs function can return the absolute value of int,double,float & long
System.out.println("Math.abs(6.0) :");
System.out.println(Math.abs(6.0));
System.out.println("Math.abs(- 6.0) :");
System.out.println(Math.abs(- 6.0));
System.out.println("Math.ceil(10.25) :");
//Math.ceil function returns the smallest double value that is greater than or equal to the argument and is equal to a mathematical integer.
System.out.println(Math.ceil(10.25));
System.out.println("Math.ceil(- 6.8) :");
System.out.println(Math.ceil(-6.8));
System.out.println("Math.floor(- 5.1) :");
//Math.floor function returns the largest double value that is less than or equal to the argument and is equal to a mathematical integer
System.out.println(Math.floor(-5.1));
System.out.println("Math.floor(7.9) :");
System.out.println(Math.floor(7.9));
//Math.pow returns the value of the first value raised to the power of the second
System.out.println("Math.pow(5,2) :");
System.out.println(Math.pow(5,2));
System.out.println("Math.pow(2,5) :");
System.out.println(Math.pow(2,5));
//Math.max function returns the maximum of the two values (it can be int/float/long/double)
System.out.println("Math.max(1.5,2) :");
System.out.println(Math.max(1.5,2));
//Math.max function returns the maximum of the two values (it can be int/float/long/double)
System.out.println("Math.min(3,0.5) :");
System.out.println(Math.min(3,0.5));
System.out.println("Math.min(2, 1.5) :");
System.out.println(Math.min(2,1.5));
System.out.println("Math.max(0.5,3) :");
System.out.println(Math.max(0.5,3));
//Math.sqrt function returns the correctly rounded positive square root of a double value.
System.out.println("Math.sqrt(16.0) :");
System.out.println(Math.sqrt(16.0));
//Math.round method returns the closest long to the argument.
System.out.println("Math.round(0.6) :");
System.out.println(Math.round(0.6));
System.out.println(Math.pow( 2.0 , 3 ));

System.out.println("Math.ceil(Math.pow( 3 , 0.5 )) :" );
System.out.println(Math.ceil( Math.pow( 3 , 0.5 ) ));
/*Math.pow will evaluate to 1.7320508075688772 as it will calculate 3 raised to the power 0.5
* then this value is passed to ceil function which rounds up the value to 2.0
*/
System.out.println("Math.floor(Math.pow(1.5 ,2)) :");
System.out.println(Math.floor( Math.pow( 1.5 , 2 ) ));
/*Math.pow will evaluate to 2.25 as it will calculate 1.5 raised to the power 2
* then this value is passed to ceil function which rounds up the value to 2.0
*/
System.out.println("Math.ceil(Math.floor(3.5)) : ");
System.out.println(Math.ceil(Math.floor(3.5)));
/*Math.floor will evaluate to 3.0 as it will round down the value 3.5
* then this value is passed to ceil function which makes no change because the value rounded
* up value of 3.0 is also 3.0 had it been 3.1 it would have made it 4.0
*/
System.out.println("Math.min(Math.max(3.0,2), 2.5) :");
System.out.println( Math.min( Math.max( 3.0 , 2 ), 2.5) );
/*Math.max will evaluate to 3.0 as it will compare the values 3.0 and 2 and return the larger one
* then this value is passed to min function which calculates the minimum value amongst 3.0 and 2.5
* and hence the entire expression evaluates to 2.5
*/
System.out.println("Math.max(Math.min(2.0,3),3.0) : ");
System.out.println( Math.max( Math.min( 2.0 , 3 ), 3.0));
/*Math.min will evaluate to 2.0 as it will compare the values 2.0 and 3 and return the smaller one which is 2.0
* then this value is passed to max function which compares the two values 2.0 and 3.0 and returns
* the maximum value amongst 2.0 and 3.0
* and hence the entire expression evaluates to 3.0
*/
System.out.println("Math.sqrt(Math.pow(2.0,3))");
System.out.println( Math.sqrt( Math.pow( 2.0 , 3 ) ));
/*Math.pow will evaluate to 8.0 as it will calculate 2.0 raised to the power 3
* then this value is passed to sqrt function which caculates the square root of 8.0
* and hence the entire expression evaluates to 2.8284271247461903
*/
System.out.println( "Math.pow(Math.sqrt( 9 ),2)");
System.out.println( Math.pow( Math.sqrt( 9 ) , 2 ) );
/*Math.sqrt will evaluate to 3.0 as it will calculate the square root of 9 which comes out to be 3.0
* then this value is passed to pow function which caculates the 3.0 raised to the power 2
* and hence the entire expression evaluates to 9.0
*/
//Similar expressions....
System.out.println("Math.round(Math.max(2.1,3.1))");
System.out.println(Math.round(Math.max( 2.1 , 3.1 ) ));
System.out.println("Math.abs(Math.abs(8.0)) :");
System.out.println(Math.abs(Math.abs( 8.0 )));
System.out.println("Math.min(Math.abs(1.5),2) : ");
System.out.println(Math.min(Math.abs( 1.5 ), 2 ) );
System.out.println("Math.ceil( Math.pow(1 ,0.5 )) :" );
System.out.println( Math.ceil( Math.pow( 1 , 0.5 ) ) );

}
}


Related Solutions

Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs...
Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs to support simple arithmetic functions including taking the sum, difference, and the product of the division of the two. Do not include a main() method in the Fraction class. The Fraction class will implement the following class methods: Fraction add (Fraction f1, Fraction f2); // f1 + f2 and returns a new Fraction Fraction sub (Fraction f1, Fraction f2); // f1 - f2 and...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
Create a Java class with the name (identifier) MyProgram and place it in the Questions package....
Create a Java class with the name (identifier) MyProgram and place it in the Questions package. You will need to create a main method for this class and place all of your code inside of that method. Your program should complete the following steps: - Prompt the user to enter a name for a Student. - Use the Scanner class to read in the user input and store the result in a variable. - Use the Random class to generate...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
package design; public class FortuneEmployee { /** * FortuneEmployee class has a main methods where you...
package design; public class FortuneEmployee { /** * FortuneEmployee class has a main methods where you will be able to create Object from * EmployeeInfo class to use fields and attributes.Demonstrate as many methods as possible * to use with proper business work flow.Think as a Software Architect, Product Designer and * as a Software Developer.(employee.info.system) package is given as an outline,you need to elaborate * more to design an application that will meet for fortune 500 Employee Information *...
What is a package? What package is the Scanner class in? What package is the String class in?
What is a package? What package is the Scanner class in? What package is the String class in? 
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for the following: 1. Name the class SalonServices 2. Add private data fields: a. salonServiceDescription – This field is a String type b. price - This field is a double type 3. Create two methods that will set the field values. a. The first method setSalonServiceDescription() accepts a String parameter defined as service and assigns it to the salonServiceDescription. The method is not static b....
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT