Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ You are to write the first program(Fraction one) and then you modify it (Fra

ID: 655268 • Letter: C

Question

C++ You are to write the first program(Fraction one) and then you modify it (Fraction2). so we have 2 programms in total.

Fraction 1

1. Imagine that the company you work for is going to create a lot of tutorials on Fractions. You are to create a robust Fraction class that will have all of the following  (all examples are for a fraction half that has a numerator of 1 and a denominator of 2):

*Private integers numerator and denominator ;

*All public getter and setter functions for the numerator and denominator;

*Safeguard that the denominator will NEVER become 0!

*a default constructor with no arguments;

*a constructor that accepts both the numerator and denominator;

*a toDecimal method that returns the decimal value of the fraction, example: 1/2 will be 0.5;

*a toString method that will return the fraction as a string, , example: 1/2 will be "1/2";

*a reduce method that will change the numerator and denominator by finding a common *denominator and reducing the fraction.Example 3/12 becomes 1/4;

Show Sample output showing several different fractions and an example where the user tried to assign 0 to the denominator. Your output should show all of the methods listed above.

Fraction class 2

2. Expand your Fraction class to include all of the following  (all examples are for a fraction half that has a numerator of 1 and a denominator of 2):

*Overload ==, <, <=, >, >= operators (use the decimal values to compare);

*Overload = operator (assigns the numerator and denominator to the fraction);

*.Overload ++ so that 1/3 becomes 2/3 for example.

.*Overload -- so that 2/3 becomes 1/3 for eaxample.

.*It is OK if the numerator is negative.

include a representation of the class and

Sample output showing several different fractions and an example where the user tried to assign 0 to the denominator. Your output should show all of the methods listed above

Explanation / Answer

Program 1:

import java.util.Scanner;

public class FractionTutorial {

    public static void main(String[] args) {

        Fraction ff=new Fraction();

        ff.setter();

        ff.getter();

        ff.toDecimal();

        ff.FtoString();

        ff.reduceFraction();

        System.out.println("Enter the Numerator:");

        Scanner scan=new Scanner(System.in);

        int n=Integer.parseInt(scan.nextLine());

        System.out.println("Enter the Denominator :");

        scan=new Scanner(System.in);

        int d=Integer.parseInt(scan.nextLine());

        if(d==0)

        {

        System.out.println("Enter the Denominator Value other than Zero :");

        scan=new Scanner(System.in);

        d=Integer.parseInt(scan.nextLine());

        }

        Fraction ff1=new Fraction(n,d);

        ff1.getter();

        ff1.toDecimal();

        ff1.FtoString();

        ff1.reduceFraction();

    }

   

}

class Fraction

{

int numerator;

int denominator;

Fraction()

{}

Fraction(int numer,int denom)

{

    numerator=numer;

    denominator=denom;

}

public void setter()

{

    System.out.println("Enter the Numerator:");

        Scanner scan=new Scanner(System.in);

        numerator=Integer.parseInt(scan.nextLine());

    System.out.println("Enter the Denominator :");

        scan=new Scanner(System.in);

        denominator=Integer.parseInt(scan.nextLine());

        int f=divideByZero(denominator);

        if(f==1)

        {

        System.out.println("Enter the Denominator Value other than Zero :");

        scan=new Scanner(System.in);

        denominator=Integer.parseInt(scan.nextLine());

        }

}

public void getter()

{

    System.out.println("Fraction is: ");

    FtoString();

}

public void toDecimal()

{

    float val;

    int f=divideByZero(denominator);

    if(f==0)

    { val=numerator/denominator;

    System.out.println("Decimal Value of the Fraction: "+val); }

    else

     System.out.println("Fraction cannot be done. Denominator is zero. So divide by zero error will occur");  

}

public void FtoString()

{

    String S=Integer.toString(numerator)+"/"+Integer.toString(denominator);

    System.out.println("String Representation of the Fraction: "+S);   

}

public int divideByZero(int denom)

{

    int f=0;

    if(denom==0)

        f=1;

    return f;

}

public void reduceFraction()

{

int f=divideByZero(denominator);

if(f==1)

System.out.println("Reduction cannot be done. Divide by zero error");  

else if(denominator==1){

System.out.println("no need for reduction. Reduced Answer Same as Fraction");   

FtoString();   

}   

else

{

   for(int j=2;j<=denominator;j++)

   {

       if((numerator%j)==0)

       {

           numerator/=j;

           denominator/=j;

       }    

   }

   FtoString();

}

}

}

Output: 1

Enter the Numerator:

10

Enter the Denominator :

0

Enter the Denominator Value other than Zero :

5

Fraction is:

String Representation of the Fraction: 10/5

Decimal Value of the Fraction: 2.0

String Representation of the Fraction: 10/5

After reduction

String Representation of the Fraction: 5/2

Output 2:

Enter the Numerator:

15

Enter the Denominator :

9

Fraction is:

String Representation of the Fraction: 15/9

Decimal Value of the Fraction: 1.0

String Representation of the Fraction: 15/9

After reduction

String Representation of the Fraction: 5/3

Program 2:

Java doesn't provide freedom to programmer, to overload the standard operators.

So the program cannot be written

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote