Thanks for posting the question, we are glad to help you. Here is the implemented parse() method. I have demonstrated how you can use space, *, newline, tab as separator to seperate into 2 numbers and return a Fraction class object.
// use the below pattern to split a line using space, newline, tab backslash or * character
s.useDelimiter(" |/|\n|\t|\*");
Refer the screenshot for the output
Thank you !
__________________________________________________________________________________________________
import java.util.Scanner;
public class Codechef {
static class Fraction {
private int numerator, denominator;
public Fraction(int num, int denom) {
// TODO Auto-generated constructor stub
numerator = num;
denominator = denom;
}
public String toString() {
return numerator + "/" + denominator;
}
}
public static Fraction parse(String input) {
Scanner s = new Scanner(input);
// use the below pattern to split a line using space, newline, tab backslash or * character
s.useDelimiter(" |/|\n|\t|\*");
int num=Integer.parseInt(s.next().trim());
int denom=Integer.parseInt(s.next().trim());
s.close();
return new Fraction(num, denom);
}
public static void main(String[] args) {
Fraction tabSeparator = parse("2 5");
System.out.println(tabSeparator);
Fraction newLineSeparator = parse("12
65");
System.out.println(newLineSeparator);
Fraction spaceSeparator = parse("37 99");
System.out.println(spaceSeparator);
Fraction starSeparator = parse("1*7");
System.out.println(starSeparator);
}
}
__________________________________________________________________________________________________

: )
Write in Java Implement the parse method and test it by calling with three different strings...
For the add method in Fraction class, fill the following blank. public Fraction add(Fraction f) { int num = numerator * f.getDenominator() + f.getNumerator() * denominator; int denom = denominator * f.getDenominator(); return ww new Fraction(num, denom) num/denom (double)num/(double)denom (int)num/denom
Question 34 (2 points) For the add method in Fraction class, fill the following blank. public Fraction add(Fraction f) { int num = numerator * f.getDenominator() + f.getNumerator() * denominator; int denom = denominator * f.getDenominator: return } new Fraction(num, denom) O num/denom (double)num/(double)denom (int)num/denom Previous Page Next Page
Write a Java method that should take an ArrayList as a parameter, print its element in the reverse order. The main function that calls the method is the following: import java.util.*; public class ReverseGen { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("How many numbers do you want to input?"); int num = input.nextInt(); ArrayList<Double> d = new ArrayList<Double>(num); for(int i = 0 ; i < num; i++){ System.out.print("Enter...
Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...
Hi everyone! I need help on my Java assignment. I need to create a method that is called sumIt that will sum two values and will return the value.It should also invoke cubeIt from the sum of the two values. I need to change the currecnt program that I have to make it input two values from the console. The method has to be called sumIt and will return to the sum that will produce the cube of the sum...
Implement the function hasDuplicates. Input will be given as a line containing a positive integer n, followed by n rows, each containing a string. The output should be either the word true if there are any duplicates among these strings or false if there are not. Your code should work well on long lists of strings. Use the following set-up to write the code in JAVA import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) {...
Refer to this header file: // Fraction class // This class represents a fraction a / b class Fraction { public: // Constructors Fraction(); // sets numerator to 1 and denominator to 1 Fraction(int num, int denom); // Setters void setNumerator(int num); void setDenominator(int denom); // getters int getNumerator()const {return num;} int getDenominator()const {return denom;} double getDecimal(){return static_cast<double> num / denom;} private: int num, denom; }; 1.Write the code for the non-member overloaded << operator that will display all of...
Java Write a Fraction class that implements these methods: add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction. multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction. divide ─ This method receives a Fraction parameter and divides the parameter fraction by the calling object fraction. print ─ This method prints the fraction using fraction notation (1/4, 21/14, etc.)...
Need help with following assignment written in Java. For this assignment, you’re going to create a new class named “Fraction” for managing rational numbers (fractions). (You should not submit a main program with the Fraction class; however, you'll definitely want to write one for your own testing purposes.) Details are as follows: CLASS DEFINITION CONSTRUCTORS: Fraction(int num,int denom) num/denom creates a new number whose value is Fraction(int num) METHODS (assume x is a Fraction): creates a new number whose value...
Write a program that stores a phrase as an array of words, and then prints it backwards. The main method calls method getInput , which asks the user how many words there are, stores them in an array, and returns this array. printBackwards then takes this array of words, and prints it in reverse. Code Example: import java.util.Scanner; public class L17Num1{ public static void main(String[] args) { String[] sArray=getInput(); printBackwards(sArray); } public static String[] getInput() { System.out.println("How many...