Question

Problem Description: Create a mathematics library with the following
functionality.
 calculate the absolute value of an integer number
 calculate the power given an integer base and a positive integer exponent
 calculate the square of an integer number
 determine if an integer is an even
 determine if an integer is prime
Write a Java program to do the following:
1. Calculate the absolute value, square, cube (i.e., 3rd power) of the integers 2
through 7.
2. Determine if the integers from 2 through 7 are even or primes.
Requirements:
In addition to fulfilling the conditions described above your program must also

1. Have the main class named ‘MathLibs’
2. Have user defined methods to perform the bulleted actions listed above.
3. Operate without receiving any input from the user or a file.
4. You are not allowed you use any existing math methods (e.g., Math.pow,
etc).
5. Your output should match up with the sample output provided below.
Hints:
1. Use Lab 7 as a starting point.
2. To calculate the nth power of a number, multiply the number by itself n
times (e.g., 3 to the 5 power is 3 * 3 * 3 * 3 * 3)
3. To determine if a number n is a prime, any positive integer less than n will
not divide evenly into it (except for 1). There is also a video over checking if a
number is prime.
4. Start by writing all of the methods. Then in the main method, have a for
loop which runs from 2 to 7 and calls methods.

Sample output: Abs. value of 2 is 2 2 squared is 4 2 cubed is 8 Is 2 even? true Is 2 prime true Abs. value of 3 is 3 3 squared is 9 3 cubed is 27 Is 3 even? false Is 3 prime true Abs. value of 4 is 4 4 squared is 16 4 cubed is 64 Is 4 even? true Is 4 prime false Abs value of 5 is 5 5 squared is 25 5 cubed is 125 Is 5 even? false Is 5 prime true Abs value of 6 is 6 6 squared is 36 6 cubed is 216 Is 6 even true Is 6 prime false Abs. value of 7 is 7 7 squared is 49 7 cubed is 343 Is 7 even? false Is 7 prime true

Remember: Use good programming practices such as proper use of white space, sufficient in-line comments to explain your code

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for you:

public class MathLibs {

public static void main(String[] args) {

/* Print some stats on -5, 0 and 5
for(int i = -5; i <=5; i += 5) {
System.out.println("|" + i + "| is " + absValue(i));
System.out.println("Is " + i + " odd? " + isOdd(i));
printMagnitude(i);
System.out.println("---");
}*/
for(int i = 2; i <= 7; i++) {
{
    System.out.println("Abs. value of " + i + " is " + absValue(i));
   System.out.println(i + " squared is " + squared(i));
   System.out.println(i + " cubed is " + cubed(i));
   System.out.println("Is " + i + " even? " + isEven(i));
   System.out.println("Is " + i + " prime? " + isPrime(i));
   System.out.println("---");
}  
}
}

/**************************************************************************
* absValue - Calculates the absolute value of an integer number
*
* int x - The number whose absolute value is to be determined
* return int - The absolute value (i.e., distance from 0) for x, the given int
*/
public static int absValue(int x) {

if(x < 0)
   return -x;
return x;  
}
  
/**************************************************************************
* squared - Calculates the square of an integer number
*
* int x - The number whose square is to be determined
* return int - The square value for x, the given int
*/
public static int squared(int x) {

return x * x;
}
  
/**************************************************************************
* cubed - Calculates the cube of an integer number
*
* int x - The number whose cube is to be determined
* return int - The cube value for x, the given int
*/
public static int cubed(int x) {

return x * x * x;
}
  
/**************************************************************************
* isEven - Determines if an integer value is even
*   
* int x - The number whose evenness is to be checked
* return boolean - Indicates if the given integer is even.
*/
public static boolean isEven(int x) {
  
if(x % 2 == 0)
   return true;
return false;  
}
  
/**************************************************************************
* isPrime - Determines if an integer value is prime
*   
* int x - The number whose primeness is to be checked
* return boolean - Indicates if the given integer is prime.
*/
public static boolean isPrime(int x) {
  
boolean flag = false;
for(int i = 2; i <= x/2; i++)
   if(x % i == 0)
       return false;
return true;    
}
  
/**************************************************************************
* isOdd - Determines if an integer value is odd
*   
* int x - The number whose oddness is to be checked
* return boolean - Indicates if the given integer is odd.
*/
public static boolean isOdd(int x) {
  
if(x % 2 == 0)
   return false;
return true;  
}
  
  
/**************************************************************************
* printMagnitude - Prints a message to the display containing
* the distance of an integer value from 0 (i.e., magnitude)
*   
* int x - The number whose magnitude is to be determined and displayed
*/
public static void printMagnitude(int x) {
               System.out.println("The distance of " + x + " from 0 is: " + absValue(x));
  
}
  
  
}

And the output screenshot is:

Terminal Shell Edit View Window Help イ日0 < >し令도 4) 100%00 Sat 11 Mar 02:18 ANANDA KUMAR THUMMAPUDI e E Currently Open Docum A

Add a comment
Know the answer?
Add Answer to:
Problem Description: Create a mathematics library with the following functionality.  calculate the absolute value of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • w Problem Statement: In mathematics, a series is, roughly speaking, a description of the operation of...

    w Problem Statement: In mathematics, a series is, roughly speaking, a description of the operation of adding infinitely many quantities, one after the other, to a given starting quantity. The study of series is a major part of calculus and its generalization, mathematical analysis. Mathematically, the n" number of a mathematical series is defined as follows: T. -Σ . . Write a CH program that: Reads a positive integer k from the user. If the user enters a negative value,...

  • Convert the following C fragment to equivalent MIPS assembly language that calculate and print the absolute...

    Convert the following C fragment to equivalent MIPS assembly language that calculate and print the absolute value for any integer input entered by the user. The output must be something like: Enter any integer number: -5 The absolute value is: 5

  • For Exercises 1-15, prove or disprove the given statement. 1. The product of any three consecutive...

    For Exercises 1-15, prove or disprove the given statement. 1. The product of any three consecutive integers is even. 2. The sum of any three consecutive integers is even. 3. The product of an integer and its square is even. 4. The sum of an integer and its cube is even. 5. Any positive integer can be written as the sum of the squares of two integers. 6. For a positive integer 7. For every prime number n, n +...

  • Diagonal Difference HackerRank Pseudocode and C++: Given a square matrix, calculate the absolute difference between the...

    Diagonal Difference HackerRank Pseudocode and C++: Given a square matrix, calculate the absolute difference between the sums of its diagonals. Function Description Complete the diagonalDifference function described below to calculate the absolute difference between diagonal sums. diagonalDifference( integer: a_size_rows, integer: a_size_cols, integer array: arr) Parameters: a_size_rows: number of rows in array a_size_cols: number of columns in array a: array of integers to process Returns: integer value that was calculated Constraints -100 < = elements of the matrix < = 100...

  • QUESTION 12 Write a program that would prompt the user to enter an integer. The program...

    QUESTION 12 Write a program that would prompt the user to enter an integer. The program then would displays a table of squares and cubes from 1 to the value entered by the user. The program should prompt the user to continue if they wish. Name your class NumberPowers.java, add header and sample output as block comments and uploaded it to this link. Use these formulas for calculating squares and cubes are: square = x * x cube = x...

  • Absolute value is always... 1. Positive 2. Non-negative Every fraction is a rational number. True or...

    Absolute value is always... 1. Positive 2. Non-negative Every fraction is a rational number. True or False? The sum of 2/3 and 5/7 is 7/10. True or False? The quotient of two fractions cannot be a whole number True or False? The set of whole numbers is a subset of the set of rational numbers. True or False? Which of these fractions is between 7/13 and 14/19 ? 1. a. 6/17 2. b. 9/16 3. c. 1/2 4. d. None...

  • Create a class named Program10. Your program should have the following 4 methods (other helper methods...

    Create a class named Program10. Your program should have the following 4 methods (other helper methods are allowed, but these four methods must be implemented as specified). You need to define a global scanner object and only use it inside the main method and the getValues method, since other methods do not get any values from the input. getValues: This method does not have any input parameter and returns an array of integers. This method prompts the user with a...

  • Write a C++ program that does the following : 1. Accepts array size from the keyboard....

    Write a C++ program that does the following : 1. Accepts array size from the keyboard. The size must be positive integer that is >= 5 and <= 15 inclusive . 2. Use the size from step 1 in order to create an integer array. Populate the created array with random integer values between 10 and 200. 3. Display the generated array. 4. Write a recursive function that displays the array in reverse order . 5. Write a recursive function...

  • C+ using pointers 2. Determine the following information about each value in a list of positive...

    C+ using pointers 2. Determine the following information about each value in a list of positive integers. Is the value a multiple of 7, 11, or 13? a. b. Is the sum of the digits odd or even? (parameter output returns 0 for even, 1 for odd) Is the value a prime number? (parameter output returns 0 nonprime, 1 for prime) c. You should write three functions: 1)input, 2) processing, 3)output 1) Input wil have one output parameter of type...

  • using matlab(applied numerical) 7. (5 points) Create a 4x6 matrix of random integers, each in the...

    using matlab(applied numerical) 7. (5 points) Create a 4x6 matrix of random integers, each in the range from -5 to 5; store it in a variable, ΥΧ. (a) Find the absolute value of rx. (b) Find the smallest and highest numbers in rx. (c) Find the sum of the row and sum of the columns of rx. Hint: use abs, min, max, and sum. 7. (5 points) Create a 4 x 6 matrix of random integers, each in the range...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT