Question: The question that I posted here is only part 2 of this module 2 project as I've done part 1. There are other ways to
The question that I posted here is only part 2 of this module 2 project as I've done part 1. There are other ways to solve this questions but I prefer to use the guidance that is provided from this question. Can you help me ? Thanks. Btw, I am halfway solving this question and this are the code I've written :
import java.util.Scanner; public class FractionCalculator { public static void main(String[] args) { isNumber(); } public static String getOperation() { System.out.println(" Please enter an operation (+, -, /, *, = or Q to quit): "); Scanner input = new Scanner(System.in); String operation = input.nextLine(); int x = 0; while (x == 0) { if (operation.equals("+") || operation.equals("-") || operation.equals("/") || operation.equals("*") || operation.equals("=")) { x++; } else if (operation.equalsIgnoreCase("q")) { System.exit(0); } else { System.out.println("Invalid input (+, -, /, *, = or Q to quit)"); operation = input.nextLine(); } } return operation; } // public static boolean validFraction() { // ; // } public static String isNumber() { Scanner input = new Scanner(System.in); String isNumber = input.nextLine(); if (isNumber.matches("^[0-9]+$") && isNumber.length() > 0) { System.out.println("Input correct"); } else { System.out.println("Invalid input. Insert only numbers."); return isNumber; } return isNumber; } }
Module 2 Project - Fraction Calculator
This project is designed to help you practice building your own object class and testing it with a client class. You will be creating two classes, one called Fraction and the other called FractionCalculator. The Fraction class is an object that holds information about a fraction (numerator and denominator). It will have several constructors and both private and public methods implementing the behavior of a fraction. The FractionCalculator class is a class that will allow the user to enter in fractions and operations, calculating and displaying the result. It will run until the user tells it to quit. When this program is complete, you wont have to second guess your fraction arithmetic ever again!
Methods
| Method to implement | parameter | return | description |
|---|---|---|---|
| getOperation() | Scanner input | String | Asks the user to enter in a valid mathematical operation. If the user enters anything except "+", "- |
| ", "/", "*", "=", "q", or "Q" it should re-prompt them until there is valid input. | |||
| validFraction() | String input | boolean | returns true if the parameter is in the form "a/b" where a is any int and b is any positive int |
| getFraction() | Scanner input | Fraction | It prompts the user for a String that is a validFraction. If they enter any thing that is not a valid Fraction, it should re-prompt them until it is valid |
getOperation()
Here is example output from a call to getOperation():
At the end of this run, getOperation would have returned *.
validFraction()
Some things to be mindful of when implementing the validFraction() method:
- The first character may or may not be a "-" character. If a negative shows up anywhere else, then it is not a valid fraction. It may be helpful to remove the "-" character if there is one.
- If there is no "/" character, then every character in the string must be a number (if you removed the "-" sign).
- If there is a "/" character, then it may be helpful to create substrings for the numerator and denominator.
- Both substrings must be non-empty.
- Both must be entirely made of numbers.
- The denominator cannot be "0".
Hint 1: It may be useful to create a helper method isNumber() that takes a String as input and returns true if every character in the String is a number 0-9 and false otherwise. This method can also check for empty strings. Hint 2: Once you determine whether or not the Strings are numbers, you may find the Integer.parseInt() method helpful.
getFraction()
Here is example output from a call to getFraction(). If the user enters any thing that is not a valid Fraction, it should re-prompt them until it is valid:
This call would return a new Fraction object equal to -31/1. No user input should throw an exception! If you are getting exceptions, then it is likely your validFraction method isnt correct.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
