Question

Write in Java Implement the parse method and test it by calling with three different strings and by printing the results. The

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

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);

   }

}
__________________________________________________________________________________________________

ar MedLib,javb EhwPM 回WPMDrver nCodechef.javexx Console 2/65 1 public class Codechef i static class Fraction privste int nuse

: )

Add a comment
Know the answer?
Add Answer to:
Write in Java Implement the parse method and test it by calling with three different strings...
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
  • For the add method in Fraction class, fill the following blank. public Fraction add(Fraction f) {...

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

    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

  • Need help with Java for Fraction exercise

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

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

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

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

  • Need help with following assignment written in Java. For this assignment, you’re going to create...

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

  • Rational will be our parent class that I included to this post, Implement a sub-class MixedRational....

    Rational will be our parent class that I included to this post, Implement a sub-class MixedRational. This class should Implement not limited to: 1) a Constructor with a mathematically proper whole, numerator and denominator values as parameters. 2) You will override the: toString, add, subtract, multiply, and divide methods. You may need to implement some additional methods, enabling utilization of methods from rational. I have included a MixedRational class with the method headers that I used to meet these expectations....

  • 5. Write a static method "f(n)" in the space provide, that returns O if n is...

    5. Write a static method "f(n)" in the space provide, that returns O if n is even, and if n is odd, returns 1 or -1 according as n is greater than or less than 0. importjava.util.Scanner; public class Q_05 Your "f(n)" method: public static void main(String args[]) mana int n; Scanner input = new Scanner(System.in); System.out.print("Please enetrn: "); n=input.nextInt(); System.out.println(f(n)); "Method f(n)" 7. Write a static method "max" in the space provide, that returns the maximum value from 3...

  • Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for...

    Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for sample program: 3.5 import java.util.Scanner; public class HourToMinConv {    public static void outputMinutesAsHours(double origMinutes) {       /* Your solution goes here */    }    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       double minutes;       minutes = scnr.nextDouble();       outputMinutesAsHours(minutes); // Will be run with 210.0, 3600.0, and 0.0.       System.out.println("");    } } 2....

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