Question
I have provided a main method. Please add your fraction class. You need constructors, add, subtract, multiply, and divide methods, and a toString method. Your
I have provided a main method. Please add your fraction class. You need constructors, add, subtract, multiply, and divide methods, and a toString method. Your toString method needs to return the numerator followed by / followed by the denominator - NO spaces. DO NOT make any attempt to reduce the fractions (we shall do that later).
Please add comments throughout.
import java.util.Scanner;
public class H4 {
public static class Fraction { } public static void main(String[] args) { Fraction f= new Fraction(); //test default constructor System.out.println(f); //test mutators f.setNumerator(4); f.setDenominator(5); //test accessors System.out.println("numerator is " + f.getNumerator() + " and denominator is " + f.getDenominator()); System.out.println(f); //test constructor Fraction g= new Fraction(8,19); System.out.println(g); Scanner keyboard = new Scanner (System.in); //get the data for the next fraction and construct it int n1=keyboard.nextInt(); int d1=keyboard.nextInt(); Fraction one=new Fraction (n1,d1); //get the data for the next fraction and construct it n1=keyboard.nextInt(); d1=keyboard.nextInt(); Fraction two=new Fraction (n1,d1); //test methods for add, subtract, multiply, divide System.out.println(one + " + " + two + " = " + one.add(two)); System.out.println(one + " - " + two + " = " + one.subtract(two)); System.out.println(one + " * " + two + " = " + one.multiply(two)); System.out.println(one + " divided by " + two + " = " + one.divide(two)); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started