Next, write the following method that returns the number of digits in the fractional part of a double value. For example, 12.345 has 3 digits in the fractional part, 1.2345 has 4 digits in the fractional part, 12.0 has 0 digits in the fractional part i.e. this double value is essentially an integer.
You should round the fractional part (not the whole value, what happens if you round the whole value? Test it and see) of the value parameter so that it contains a maximum of 6 decimal places and hence the maximum possible value returned by the following method is 6.
public static
int FractionDigits(double value)
Here are some sample usage of this method along with their corresponding outputs.
System.out.println(FractionDigits(12.0)); //output: 0
System.out.println(FractionDigits(12.1230000)); //output: 3
System.out.println(FractionDigits(12.01204000)); //output: 5
System.out.println(FractionDigits(12.12345678)); //output: 6
System.out.println(FractionDigits(12.99999999)); //output: 0
System.out.println(FractionDigits(12.99999919)); //output: 6
Test this FractionDigits method on the values you placed in doubles.txt by reading the double values from that file and outputting the FractionDigits on the screen.
Thanks for the question.
Here is the completed code for this problem. Comments are included so that you understand whats going on. Let me know if you have any doubts or if you need anything to change.
Thank You !!
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class Fractions {
// input and output file
private static final String FILE = "doubles.txt";
// takes in the number of doubles to generate and writes them to the file
public static void randomDouble(int numberCount) throws IOException {
PrintWriter writer = new PrintWriter(new FileWriter(FILE));
Random random = new Random();
for (int i = 1; i <= numberCount; i++) {
writer.write(String.valueOf(random.nextDouble()) + "\r\n");
}
writer.flush();
writer.close();
System.out.println("file created successfully.");
}
// returns the number of digits in the fractional part
public static int FractionDigits(double value) {
int integerPart = (int) (value);
value -= integerPart;
value = (double) (Math.round(value * 1000000) / 1000000.0);
integerPart = (int) (value);
if (value - integerPart == 0) return 0;
else return String.valueOf(value).length() - 2;
}
public static void main(String[] args) {
try {
randomDouble(10);
displayFractionDigits();
} catch (IOException e) {
System.out.println("Unable to write to file: " + FILE);
}
System.out.println(FractionDigits(12.0)); //output: 0
System.out.println(FractionDigits(12.1230000)); //output: 3
System.out.println(FractionDigits(12.01204000)); //output: 5
System.out.println(FractionDigits(12.12345678)); //output: 6
System.out.println(FractionDigits(12.99999999)); //output: 0
System.out.println(FractionDigits(12.99999919)); //output: 6
}
// method reads the numbers from doubles.txt file
private static void displayFractionDigits() throws FileNotFoundException {
Scanner inputFile = new Scanner(new File(FILE));
while (inputFile.hasNext()) {
double number = Double.parseDouble(inputFile.nextLine());
int digits = FractionDigits(number);
System.out.println("Number: " + number + " contains " + digits + " fractional parts.");
}
inputFile.close();
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Write a method named RandomDoubles that takes a parameter N as its argument and generates N...
/** * Write a method named outOfOrder that takes as a parameter an array of double and returns a value of type int. * This method will test the array for being out of order, meaning that the array violates the conditions: * array[0] <= arrayValues[0] <= arrayValues[2] <=... * * The method returns -1 if the elemnts are not out of order; otherwise , it returns the index of the first element of the array that is out of...
JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...
JAVA~ 1. Write a static method named countOdd(my_array) that returns the number of odd integers in a given array. If there are no odd numbers in the array, the method should return 0. If the array is empty, the method should also return 0. For example: Test Result System.out.println(countOdd(new int[]{2, 3, 5, 6})); 2 System.out.println(countOdd(new int[]{2})); 0 System.out.println(countOdd(new int[]{})); 0 System.out.println(countOdd(new int[]{-7, 2, 3, 8, 6, 6, 75, 38, 3, 2})); 4 public static int ----------------------------------------------------------------------------------------------------------- 2. Write a static...
help
Question 12 (20 points) Write a function named inverse that takes a single parameter, a dictionary. In this key is a student, represented by a string. The value of each key is a list of courses, each represented by a string, in which the student is enrolled. dictionary each The function inverse should compute and return a dictionary in which each key is a course and the associated value is a list of students enrolled in that course For...
Write four overloaded methods called randomize. Each method will return a random number based on the parameters that it receives: randomize() - Returns a random int between min and max inclusive. Must have two int parameters. randomize() - Returns a random int between 0 and max inclusive. Must have one int parameter. randomize() - Returns a random double between min and max. Must have two double parameters. randomize() - Returns a random double between 0 and max. Must have one...
Write a method in java named isValidEmail that takes a string as input parameter, and returns true if that string represents a valid email address, or false otherwise. An email address is considered valid if it follows this format “user123@domain.ext”, where: user123 represents a sequence of word characters (i.e., letters, digits, or underscore) whose length is between 1 and 10 (inclusive), but the first character must be a letter domain represents a sequence of alphanumeric characters (i.e., letters...
Write a method named printReverse() that accepts an int array as its parameter and prints that array in reverse order. This method should not have a return statement (void). - All elements should be printed on the same line, separated by a space - A new line should be printed after the entire array prints - If the array passed in held the values: {3,2,1} - Output: 1 2 3 //newline printed here
A java exercise please!
Write a static method named swapHavles that takes an ArrayList of integers as a a parameter and returns a new ArrayList that has every element in the second half of the original ArrayList swapped with every element in the first half of the original ArrayList. Note: • You can assume that the ArrayList passed to this method as a parameter always contains an even number of elements and will always contain at least two elements. For...
Write a static method named sumOf that takes an array of int values as an argument (you may safely assume that the array contains at least 1 element). This method should return the sum of all values in the array. Examples: Given the following array declaration and definition. int[] theArray = {0, 0, 0, 0, 0, 0}; sumOf(theArray) will return 0 Given the following array declaration and definition. int[] theArray = {1, 1, 1, 1, 1, 1}; sumOf(theArray) will return...
1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...