Question

In: Computer Science

Given the main method of a driver class, write a Fraction class. Include the following instance...

Given the main method of a driver class, write a Fraction class. Include the following instance methods:

add,

multiply,

print,

printAsDouble,

and a separate accessor method for each instance variable.

    1. Write a Fraction class that implements these methods:
  • add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction.
  • multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction.
  • print ─ This method prints the fraction using fraction notation (1/4, 21/14, etc.)
  • printAsDouble ─ This method prints the fraction as a double (0.25, 1.5, etc.)

Separate accessor methods for each instance variable in the Fraction class.

Provide a driver class, FractionDemo,   that demonstrates this Fraction class. The driver class should contain this main method:

public static void main(String[] args)

{

Scanner stdIn = new Scanner(System.in);

Fraction c, d, x;       // Fraction objects

System.out.println("Enter numerator; then denominator.");

c = new Fraction(stdIn.nextInt(), stdIn.nextInt());

c.print();

System.out.println("Enter numerator; then denominator.");

d = new Fraction(stdIn.nextInt(), stdIn.nextInt());

d.print();

x = new Fraction();     // create a fraction for number 0

System.out.println("Sum:");

x.add(c).add(d);

x.print();

x.printAsDouble();

x = new Fraction(1, 1); // create a fraction for number 1

System.out.println("Product:");

x.multiply(c).multiply(d);

x.print();

x.printAsDouble();

System.out.println("Enter numerator; then denominator.");

x = new Fraction(stdIn.nextInt(), stdIn.nextInt());

x.printAsDouble();

} // end main

Note that this demonstration driver does not call the accessor methods. That’s OK.   Accessor methods are often implemented regardless of whether there’s an immediate need for them. That’s because they are very useful methods in general and providing them means that future code can use them when the need arises.

Sample session:

Enter numerator; then denominator.

5

8

5/8

Enter numerator; then denominator.

4

10

4/10

Sum:

82/80

1.025

Product:

20/80

0.25

Enter numerator; then denominator.

6

0

infinity

  1. Modify the code so that it can handle negative numerators and negative denominators, and provide a helping method that performs fraction reduction.

Sample session using negative numbers and reduction:

Enter numerator; then denominator.

-5

-8

5/8

Enter numerator; then denominator.

4

-10

-2/5

Sum:

9/40

0.225

Product:

-1/4

-0.25

Enter numerator; then denominator.

0

-0

indeterminate

Solutions

Expert Solution

Fraction.java

public class Fraction {
   private int numerator;
   private int denominator;
  
   Fraction(){
       numerator = 0;
       denominator = 1;
   }
   Fraction(int n, int d){
       //If both numerator and denominator are negative
       if(n<0 && d<0) {
           this.numerator = -n;
           this.denominator = -d;
       }
       //If numerator is positive, denominator is negative
       else if (n>0 && d<0){
           numerator = -n;
           denominator = -d;
       }
       //Otherwise
       else {
           numerator = n;
           denominator = d;
       }
   }
  
   public int getNumerator() {
       return numerator;
   }
   public int getDenominator() {
       return denominator;
   }
        //Adds Fraction f to the current Fraction object, and returns new Fraction
   public Fraction add(Fraction f) {
       int num = this.numerator*f.denominator + this.denominator*f.numerator;
       int denom = this.denominator*f.denominator;
       this.numerator = num;
       this.denominator = denom;
       return this;
   }
   //Multiplies Fraction f to the current Fraction object, and returns new Fraction
   public Fraction multiply(Fraction f) {
       int num = this.numerator * f.numerator;
       int denom = this.denominator * f.denominator;
       this.numerator = num;
       this.denominator = denom;
       return this;
   }
      
   public void print() {
       if(numerator == 0 && denominator == 0)
           System.out.println("Indeterminate");
       else if (denominator==0)
           System.out.println("Infinity");
       else
           System.out.println(numerator + "/" + denominator);
   }
  
   public void printAsDouble() {
       System.out.println((double)numerator/denominator);
   }
}

FractioDemo.java

import java.util.Scanner;

public class FractionDemo {

   public static void main(String[] args) {
       Scanner stdIn = new Scanner(System.in);
       Fraction c, d, x;       // Fraction objects
       System.out.println("Enter numerator; then denominator.");
       c = new Fraction(stdIn.nextInt(), stdIn.nextInt());
       c.print();
       System.out.println("Enter numerator; then denominator.");
       d = new Fraction(stdIn.nextInt(), stdIn.nextInt());
       d.print();
       x = new Fraction();     // create a fraction for number 0
       System.out.println("Sum:");
       x.add(c).add(d);
       x.print();
       x.printAsDouble();
      
       x = new Fraction(1, 1); // create a fraction for number 1
       System.out.println("Product:");
       x.multiply(c).multiply(d);
       x.print();
       x.printAsDouble();
       System.out.println("Enter numerator; then denominator.");
       x = new Fraction(stdIn.nextInt(), stdIn.nextInt());
       x.printAsDouble();      
   }

}


Related Solutions

3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
For python Write a new method for the Fraction class called mixed() which returns a string...
For python Write a new method for the Fraction class called mixed() which returns a string that writes out the Fraction in mixed number form. Here are some examples: f1 = Fraction(1, 2) print(f1.mixed()) # should print "1/2" f2 = Fraction(3, 2) print(f2.mixed()) # should return "1 and 1/2" f3 = Fraction(5, 1) print(f3.mixed()) # should return "5" def gcd(m, n): while m % n != 0: oldm = m oldn = n m = oldn n = oldm %...
Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
Question - Write a Client class with a main method that tests the data structures as...
Question - Write a Client class with a main method that tests the data structures as follows: For the ArrayStack, LinkedStack, ArrayQueue, LinkedQueue, and ArrayList: Perform a timing test for each of these data structures. Each timing test should measure in nanoseconds how long it takes to add and remove N Integers from the structure. N should vary from 10 to 1,000,000,000 increasing N by a factor of 10 for each test. Depending on your system you may run out...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # #...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # # - meat # - to_go # - rice # - beans # - extra_meat (default: False) # - guacamole (default: False) # - cheese (default: False) # - pico (default: False) # - corn (default: False) # #The constructor should let any of these attributes be #changed when the object is instantiated. The attributes #with a default value should be optional. Both positional #and...
Write a public class call CountInts. class CountInts has a main method. CountInts must have two...
Write a public class call CountInts. class CountInts has a main method. CountInts must have two methods: count and displayResults. 1. method count takes an array, aa, of ints and counts how many times each integer appears in aa. The integers can only be from 0 to 100 inclusive. 2. method display results displays the results of the count 3. use the template provided. insert your code in the places indicated and don't change anything else. Examples % java CountInts...
Define Loan Class – Add to your project. And write program in main method to test...
Define Loan Class – Add to your project. And write program in main method to test it. Note: Assume year is number of years, rate is the annual interest rate and P is principle is loan amount, then the total payment is Total payment = P *(1+ rate/12)^ year*12; Monthly Payment = TotalPayment/(year*12); java
IN C++, ONE FILE Also include the main class that just runs these functions, thanks. Write...
IN C++, ONE FILE Also include the main class that just runs these functions, thanks. Write a recursive function to produce a pattern of n lines of asterisks. The first line contains one asterisk, the next line contains two, and so on, up to the nth line, which contains n asterisks. For example, if the non-negative integer is 5, the pattern generated is: * ** *** **** ***** Prototype: void pattern(unsigned n); Write a recursive function, sum_range that finds the...
Error: Main method is not static in class ArrayReview, please define the main method as: public...
Error: Main method is not static in class ArrayReview, please define the main method as: public static void main(String[] args) please help me fast: import java.util. Random; import java.util.Scanner; //ArrayReview class class ArrayReview { int array[];    //constructor ArrayReview (int n) { array = new int[n]; //populating array Random r = new Random(); for (int i=0;i<n; i++) array[i] = r.nextInt (); } //getter method return integer at given index int getElement (int i) { return array[i]; }    //method to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT