The following can use loops, strings, arrays, and functions. The code must be ab
ID: 668873 • Letter: T
Question
The following can use loops, strings, arrays, and functions. The code must be able to accept and perform calculations on the whole number portion of a mixed number if necessary (as in the '2' in 2 1/4):
You are developing a Fraction structure for Teacher’s Pet Software. The structure contains
three public data fields for whole number, numerator, and denominator. Using the same
structure, write the functions described below:
» An enterFractionValue()function that declares a local Fraction object and
prompts the user to enter values for the Fraction. Do not allow the user to enter a
value of 0 for the denominator of any Fraction; continue to prompt the user for a
denominator value until a valid one is entered. The function returns a data-filled
Fraction object to the calling function.
» A reduceFraction()function that accepts a Fraction object and reduces it to proper
form, returning the reduced Fraction. For example, a Fraction entering the function as 0
2/6 would be reduced to 0 1/3, and a Fraction entered as 4 18/4 would be reduced to 8 1/2.
» A displayFraction()function that displays any Fraction object passed to it. This
function displays a fraction with a slash between the numerator and denominator.
» A main()function that declares a Fraction object and continues to get Fraction values
from the user until the user enters a Fraction with value 0 (both the whole number and
the numerator are 0). For each Fraction entered, display the Fraction, reduce the
Fraction, and display the Fraction again.
Explanation / Answer
//language is not specified so writing program in java
import java.util.Scanner;
public class Fraction {
private int wholenumber;
private int numerator;
private int denominator;
public static String slash ="/";
public static void main(String[] args) throws Exception{
Fraction fc = new Fraction();
while(true) {
fc.enterFractionValue();
if(fc.wholenumber == 0 && fc.numerator == 0)
break;
fc.reduceFraction();
fc.displayFraction();
}
}
public void enterFractionValue() {
Scanner sc = new Scanner(System.in);
// System.out.println("Enter a fractional value [ Format should be like 4 2/3] ");
System.out.println("Enter whole number in the fraction");
wholenumber = sc.nextInt();
System.out.println("Enter numerator in the fraction");
numerator = sc.nextInt();
System.out.println("Enter denominator in the fraction");
int d;
while(true) {
d = sc.nextInt();
if(d==0)
System.out.println("Denominator should not be 0");
else {
denominator = d;
break;
}
}
}
public void reduceFraction() {
if(denominator % numerator == 0 ) {
denominator = denominator / numerator;
numerator = 1;
} else if(numerator % denominator == 0 ) {
wholenumber += numerator / denominator;
numerator = 0;
denominator = 1;
} else {
int hcf = getHCF(denominator, numerator);
if(hcf > 1) {
denominator /= hcf;
numerator /= hcf;
if(denominator < numerator) {
int n = numerator/denominator;
wholenumber += n;
numerator = numerator % denominator;
}
}
}
}
public void displayFraction() {
System.out.println(" Entered fraction : "+ wholenumber+" "+ numerator+Fraction.slash+denominator);
}
private int getHCF(int a, int b) {
int hcf=0;
int min = a > b? b: a;
for(int i=min; i >= 1; i--)
{
if(a%i == 0 && b%i == 0)
{
hcf = i;
break;
}
}
return hcf;
}
public int getWholenumber() {
return wholenumber;
}
public void setWholenumber(int wholenumber) {
this.wholenumber = wholenumber;
}
public int getNumerator() {
return numerator;
}
public void setNumerator(int numerator) {
this.numerator = numerator;
}
public int getDenominator() {
return denominator;
}
public void setDenominator(int denominator) {
this.denominator = denominator;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.