Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can I please get some help with this :) will give many thumbs up!! I really do need help figuring this out. Please if you

Can I please get some help with this :) will give many thumbs up!!

I really do need help figuring this out. Please if you could

Fill in the class Rational so that Rational implements Numeric and so Main runs without error with the following output:

The product of 0 remainder 0.3333333333333333 and 1 remainder 0.5 is 0 remainder 0.5 0 remainder 0.3333333333333333 divided by 1 remainder 0.5 results in 0 remainder 0.2222222222222222

The product of 1/3 and 3/2 is 3/6 1/3 divided by 3/2 results in 2/9

A very limited amount of the Rational class is given. You will need to add in getters for the instance variables, and also implement multiply, divide, and toString. There may be further changes that need to be made, but only the Rational class should be changed. Correct arithmetic calculation will count for a very small portion of your score on this question so don't worry about that as much.

public class Main { public static void main(String[] args) { //EXAMPLE 1: TwoPart //Initialize as one third (1/3) Numeric number1 = new TwoPart(0, 1.0/3.0); //Initialize as three halves (3/2) Numeric number2 = new TwoPart(1, 0.5);

Numeric resultTwoPart = number1.multiply(number2); System.out.println("The product of "+number1+" and "+number2+" is "+resultTwoPart); resultTwoPart = number1.divide(number2); System.out.println(number1+" divided by "+number2+" results in "+resultTwoPart); System.out.println(); //EXAMPLE 2: Rational //Initialize as one third (1/3) Numeric fraction1 = new Rational(1,3); //Initialize as three halves (3/2) Numeric fraction2 = new Rational(3,2);

Numeric result = fraction1.multiply(fraction2); System.out.println("The product of "+fraction1+" and "+fraction2+" is "+result); result = fraction1.divide(fraction2); System.out.println(fraction1+" divided by "+fraction2+" results in "+result);

} }

public interface Numeric { public abstract Numeric multiply(Numeric x); public abstract Numeric divide(Numeric x); }

public class Rational { private int numerator; private int denominator; //Constructor public Rational(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; } //Your code goes here: }

p

ublic class TwoPart implements Numeric { private int whole_number; private double remainder; public TwoPart(int whole_number, double remainder) { this.whole_number = whole_number; this.remainder = remainder; } public double getValue(){ return Double.valueOf(whole_number)+remainder; } public Numeric multiply(Numeric other) { TwoPart x = (TwoPart)other; double value = x.getValue(); double result = this.getValue() * value; int whole_part = (int)result; double remainder = result - whole_part; return new TwoPart(whole_part, remainder); }

public Numeric divide(Numeric other) { TwoPart x = (TwoPart)other; double value = x.getValue(); double result = this.getValue() / value; int whole_part = (int)result; double remainder = result - whole_part; return new TwoPart(whole_part, remainder); } public String toString() { return Integer.toString(whole_number)+" remainder "+Double.toString(remainder); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago