Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help with this java class Class Fraction java.lang.Object mypackages.types.Fraction public class Fraction extends java.lang.Object Class representing a fraction, split into two parts, integer numerator

need help with this java class

Class Fraction

java.lang.Object

mypackages.types.Fraction

public class Fraction extends java.lang.Object Class representing a fraction, split into two parts, integer numerator and denominator. Fractions cannot have 0 as a denominator.

Field Summary

Fields Modifier and Type Field and Description private int denominator Denominator of Fraction. private int numerator Numerator of Fraction.

Constructor Summary

Constructors Constructor and Description Fraction(int numeratorIn, int denominatorIn) Fraction constructor, accepts numerator and denominator values.

Method Summary

All MethodsInstance MethodsConcrete Methods Modifier and Type Method and Description boolean equals(java.lang.Object o2) double getDecimal() Returns the decimal value of this Fraction. void setDenominator(int denominatorIn) Sets the denominator of the Fraction. void setNumerator(int numeratorIn) Sets the numerator of the Fraction. java.lang.String toString() Creates and returns common representation of Fraction: numerator / denominator.

Methods inherited from class java.lang.Object

clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Field Detail

numerator

private int numerator Numerator of Fraction.

denominator

private int denominator Denominator of Fraction.

Constructor Detail

Fraction

public Fraction(int numeratorIn, int denominatorIn) throws ZeroDenominatorException Fraction constructor, accepts numerator and denominator values. Ensures denominator is not 0 by calling upon setDenominator which throws ZeroDenominatorException, rethrows this Exceptions.

Parameters: numeratorIn - Numerator of Fraction. denominatorIn - Denominator of Fraction. Throws: ZeroDenominatorException - Rethrown Exception from setDenominator.

Method Detail

setNumerator

public void setNumerator(int numeratorIn) Sets the numerator of the Fraction.

Parameters: numeratorIn - Incoming value of the numerator.

setDenominator

public void setDenominator(int denominatorIn) throws ZeroDenominatorException Sets the denominator of the Fraction.

Parameters: denominatorIn - Incoming value of the denominator. Throws: ZeroDenominatorException - Throws ZeroDenominator Exception if denominatorIn is 0.

getDecimal

public double getDecimal() Returns the decimal value of this Fraction.

Returns: Decimal value of the Fraction by dividing numerator by denominator.

equals

public boolean equals(java.lang.Object o2)

Overrides: equals in class java.lang.Object

toString

public java.lang.String toString() Creates and returns common representation of Fraction: numerator / denominator.

Overrides: toString in class java.lang.Object Returns: Fraction as a String.

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

More Books

Students also viewed these Databases questions