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,...

  • Help

    (1) Output the user's input. (2 pts) Note: This zyLab outputs a newline after each user-input prompt. For convenience in the examples below, the user's input value is shown on the next line, but such values don't actually appear as output when the program runs. Enter integer: 4 You entered: 4 (2) Extend to output the input squared and cubed. Hint: Compute squared as userNum * userNum. (2 pts) Enter integer: 4 You entered: 4 4 squared is 16  And 4 cubed is 64!!  (3) Extend to get a second user input into userNum2. Output sum and product. (1 pt) Enter integer: 4 You entered: 4 4 squared is 16  And 4 cubed is 64!! Enter another integer: 5 4 + 5 is 9 4 * 5 is 20

  • 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...

  • LAB5 #1. Method and loop Write a method integerPower(base, exponent) that returns the value of baseexponent...

    LAB5 #1. Method and loop Write a method integerPower(base, exponent) that returns the value of baseexponent (2 pts). For example, integerPower(3, 4) returns 81. Assume thatexponent is a positive nonzero integer, and base is an integer. Method integer should use for or while loop to control the calculation. Do not use any Math library methods. Incorporate this method into a program class and invoke this method with different combinations of input values at least 4 times. Please use printf() method...

  • 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...

  • 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 the following programs in Java {Java1 difficulty} [Please create as simple as possible] a. Ask...

    Create the following programs in Java {Java1 difficulty} [Please create as simple as possible] a. Ask the user to enter their favorite number and favorite word. Pass both of these values to a method that will print the favorite word vertically the favorite number of times. b. Ask the user to enter 2 integers. Pass the integers to 2 different methods: one method to add the integers and print the sum (from the method); the second method to multiply the...

  • In C program #include<stdio.h> The first 11 prime integers are 2, 3, 5, 7, 11, 13,...

    In C program #include<stdio.h> The first 11 prime integers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, and 31. A positive integer between 1 and 1000 (inclusive), other than the first 11 prime integers, is prime if it is not divisible by 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, and 31. Write a program that prompts the user to enter a positive integer between 1 and 1000 (inclusive) and that outputs whether the number...

  • c++ please (1) Write a program that prompts the user to enter an integer value N...

    c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...

  • 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 +...

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
ADVERTISEMENT