Create a new class named Fraction that models fractions, such as 2/3 or 45/9. Implement the following API: Constructor: Fraction(int numerator, int denominator) . String toString() so instances of the class can be printed. The format used above is sufficient. Fraction simplify() This method returns a new Fraction that is arithmetically equal to that given, but for which the numerator and denominator have no common factors. Create a testing program, TestFraction, that prompts the user to provide the numerator and denominator of a fraction, creates the corresponding Fraction object, then prints both it and a simplified version. If the denominator is 1, it should be omitted when printed. (This test should take place in your toString method.) Provide a transcript of five executions of TestFraction, using different values for numerator and denominator that demonstrate that your program runs correctly.
Screenshot
----------------------------------------------
Program
import java.util.Scanner;
//Create a class fraction
class FractionClass{
//Instance variables
private int numerator;
private int denominator;
//Constructor
public FractionClass(int num,int denom){
numerator=num;
if(denom==0) {
System.out.println("Denominator cannot be zero");
denominator=1;
}
else {
denominator=denom;
}
}
public int calculateGCD(int numerator, int
denominator) {
if (numerator % denominator == 0)
{
return denominator;
}
return calculateGCD(denominator,
numerator % denominator);
}
//To simplify the fraction
public FractionClass simplify() {
FractionClass fraction=new
FractionClass(numerator, denominator);
int gcd = calculateGCD(numerator,
denominator);
fraction.setNumerator(numerator /
gcd);
fraction.setDenominator(denominator
/gcd);
return fraction;
}
//Getters and setters
public int getNumerator() {
return numerator;
}
public void setNumerator(int numerator) {
this.numerator = numerator;
}
public int getDenominator() {
return denominator;
}
public void setDenominator(int denom) {
if(denom==0) {
System.out.println("Denominator cannot be zero");
denominator=1;
}
else {
this.denominator=denom;
}
}
//Override toString method
public String toString() {
String str="";
str+="Fraction = ";
str+=numerator;
if(denominator!=1) {
str+="/"+denominator+"\n";
}
FractionClass f=simplify();
str+="Simplified Fraction =
";
str+=f.numerator;
if(f.denominator!=1) {
str+="/"+f.denominator+"\n";
}
return str;
}
}
//Test class
public class FractionTest {
public static void main(String[] args) {
//Call test
testFraction();
}
//Test method
public static void testFraction() {
//For input read
Scanner sc=new
Scanner(System.in);
//Loop for 5 tests
for(int i=0;i<5;i++) {
System.out.print("Enter numerator: ");
int
num=sc.nextInt();
System.out.print("Enter denominator: ");
int
denom=sc.nextInt();
FractionClass
f=new FractionClass(num,denom);
System.out.println(f+"\n");
}
}
}
-------------------------------------------------
Output
Enter numerator: 10
Enter denominator: 5
Fraction = 10/5
Simplified Fraction = 2
Enter numerator: 5
Enter denominator: 10
Fraction = 5/10
Simplified Fraction = 1/2
Enter numerator: 3
Enter denominator: 2
Fraction = 3/2
Simplified Fraction = 3/2
Enter numerator: 3
Enter denominator: 0
Denominator cannot be zero
Fraction = 3Simplified Fraction = 3
Enter numerator: 3
Enter denominator: 1
Fraction = 3Simplified Fraction = 3
Create a new class named Fraction that models fractions, such as 2/3 or 45/9. Implement the...
In C# programming. Create a fractions class that represents fractions in the form a/b Your class should implement the following members: int Numerator int Denominator Fraction(int numerator, int denominator) creates a new Fraction double ToDecimal() returns the fraction as a double Fraction Add(Fraction f) adds the fraction to the one passed in and simplifies the result Fraction Multiply(Fraction f) multiplies the fraction by the one passed in and simplifies the result Fraction Simplify() simplifies the...
I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...
Assignment 1, Fraction Instructions For this assignment, you will create a class to store fractions. The class will hold two integer values: a numerator and a denominator. For your class we will stick to positive fractions, meaning the numerator and the denominator must both be greater than 0. In previous assignments, we had a requirement that your class be named Main. In this assignment, the class is required to be named Fraction. To get started, download the template file Fraction.java...
java code
Write a class called FractionObject that represents a fraction with an integer numerator and denominator as fields. A Fraction Object object should have the following methods: A constructor with int numerator, int denominator as parameters - Constructs a new fraction object to represent the ratio (numerator/denominator). The denominator cannot be 0, so output a message if O is passed and set the numerator and denominator to 1. A constructor with no parameters - Constructs a new FractionObject to...
This in in C# There are two classes, class Fraction and class FractionDemo The user needs to be able to input a value for the numerator and the denominator Create a Fraction class with private fields that hold a positive int numerator and a positive int denominator. In addition, create Properties for each field with the set mutator such that the numerator is greater than or equal to 0 and the denominator is greater than 0 (illegal values should be...
Create a fraction class. This will have two attributes, a numerator and a denominator, both int. This class will have constructors accessors and mutators (for the attributes) a toString method which will allow us to print the fraction in the form 3/4 an Add method so we can add two fractions a subtract method (subtracts one fraction from the other) a multiply method (multiply two fractions) a divide method (divides one fraction by the other) DO NOT (for now) worry...
C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook. Provide the following capabilities: Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions (by dividing the numerator and the denominator by their greatest common divisor) that are not in reduced form, and avoids negative denominators. Overload the addition, subtraction, multiplication, and division operators for this class. Overload the relational and equality operators for this class. Provide a function...
Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two fractions are equal, and convert a fraction to...
Lab 1.java only Goal: This lab will give you experience with defining and using classes and fields, and with conditionals and recursive functions. Getting Started --------------- Read the Fraction.java class into a text editor and compile it filling in the command javac -g Fraction.java. The program should compile without errors. In a shell window, run the program using "java Fraction". The program should run, although it will print fractions in a non-reduced form, like 12/20. Part I: Constructors (1 point)...
In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition. \**************Homework 5 code*****************************/ #include<iostream> using namespace std; class Fraction { private: int wholeNumber, numerator, denominator; public: //get methods int getWholeNumber() { return wholeNumber; } int getNumerator() { return numerator; } int getDenominator() { return denominator; } Fraction()// default constructor { int w,n,d; cout<<"\nEnter whole number : "; cin>>w; cout<<"\nEnter numerator : "; cin>>n; cout<<"\nEnter denominator : "; cin>>d; while(d == 0)...