1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Ivanshal [37]
2 years ago
12

Create a Rational number class in Java using the same style as the Complex number class created in class.(The in class example c

ode is below) That is, implement the following methods:constructoradd submuldivtoStringYou must also provide a Main class and main method to fully test your Rational number class.Example code:public class Main { public static void main(String[] args) { Complex a = new Complex(2.0, 3.0); Complex b = new Complex(1.0, 2.0); System.out.println(a + " + " + b + " = " + a.add(b)); System.out.println(a + " - " + b + " = " + a.sub(b)); System.out.println(a + " * " + b + " = " + a.mul(b)); System.out.println(a + " / " + b + " = " + a.div(b)); }}class Complex { public Complex(double re, double im) { real = re; imag = im; } public Complex add(Complex o) { return new Complex(real + o.real, imag + o.imag); } private Complex conjugate() { return new Complex(real, -imag); } public Complex div(Complex o) { Complex top = mul(o.conjugate()); Complex bot = o.mul(o.conjugate()); return new Complex(top.real / bot.real, top.imag / bot.real); } public Complex mul(Complex o) { return new Complex(real * o.real - imag * o.imag, real * o.imag + imag * o.real); } public Complex sub(Complex o) { return new Complex(real - o.real, imag - o.imag); } public String toString() { return "(" + real + " + " + imag + "i)"; } private double real; private double imag;}
Computers and Technology
1 answer:
Elis [28]2 years ago
6 0

Answer:

Explanation:

/*

*    This program adds, subtracts, multiplies, and divides two rational numbers.

*/

public class Main {

  //Main method

   public static void main(String[] args) {

       Rational a = new Rational(3, 4);   // input for first rational number

       Rational b = new Rational(2 , 5);   // input for second rational number

       System.out.println(a + " + " + b + " = " + a.add(b));

       System.out.println(a + " - " + b + " = " + a.sub(b));

       System.out.println(a + " * " + b + " = " + a.mul(b));

       System.out.println(a + " / " + b + " = " + a.div(b));

   }

}

class Rational {

   public Rational(int num, int den) {

      numerator = num;

      denominator = den;

     

      //ensures a non-zero denominator

      if (den == 0)

          den =1;

     

      //stores a negative sign in numerator

      if (den < 0) {

          num = num * -1;

          den = den * -1;

      }

   }

   //return numerator

   public int getNumerator() {

     

      return numerator;

   }

 

   //return denominator

   public int getDenominator() {

     

      return denominator;

   }

 

   //return reciprocal method

   public Rational reciprocal() {

     

      return new Rational(denominator, numerator);

   }

   /*   Addition Class

    *        Find common denominator by multiplying the denominators

    *        Store number values (numerator / common denominator) as num1 and num2

    *        Sum both numbers

    */

   public Rational add(Rational r2) {

      int comDen = denominator * r2.getDenominator();

      int num1 = numerator * r2.getDenominator();

      int num2 = r2.getNumerator() * denominator;

      int sum = num1 + num2;

      return new Rational (sum, comDen);

   }

   //Subtraction Class - same implementation as Addition Class

   public Rational sub(Rational r2) {

   

     int comDen = denominator * r2.getDenominator();

     int num1 = numerator * r2.getDenominator();

     int num2 = r2.getNumerator() * denominator;

     int difference = num1 - num2;

     return new Rational (difference, comDen);

  }

   

  /*   Multiplication Class

   *        Multiply numerator with r2 numerator

   *        Multiply denominator with r2 denominator

   */

  public Rational mul(Rational r2) {

   

     int num = numerator * r2.getNumerator();

     int den = denominator * r2.getDenominator();

     return new Rational (num, den);

  }

  /*   Divide Class

   *        Multiply number by r2 reciprocal

   */

  public Rational div(Rational r2) {

      return mul (r2.reciprocal());

  }

  /*   toString Method

   *        Returns rational number as a string - in parenthesis if a fraction

   */

  public String toString() {

     

      return (((numerator == 0) ? "0" : ( (denominator == 1) ? numerator + "": "(" + numerator + "/" + denominator + ")")));

 

      /* Logic for toString method

      * if (num == 0) {

              return "0";

          } else {

              if (denominator == 1){

                  return numerator;

              } else {

                  return numerator + "/" + denominator;

              }

      */

   }

  // Declare variables

  private int numerator, denominator;

 

}

output

(3/4) + (2/5) = (23/20)                                                                                                                                      

(3/4) - (2/5) = (7/20)                                                                                                                                      

(3/4) * (2/5) = (6/20)                                                                                                                                      

(3/4) / (2/5) = (15/8)                                                                                                                                      

You might be interested in
How do I make Dank memes
Dmitrij [34]

Well bro you see what you gotta do is be a gamer.

HIT OR MISS

I GUESS THEY NEVER MISS

H U H ?

YOU GOT A BF?

I BET HE DOESNT KISS YA

M U A H


It be like that sometimes b r u h

6 0
3 years ago
Read 2 more answers
Find and record a set of instructions containing a repetition clause (e.g., instructions on a shampoo bottle, a recipe, etc.). H
Leto [7]

This question belongs to scratch programming. This programming language has various instructions to carry out various tasks. There are different types of repeat statement available. This statement or instruction allows the user / programmer to repeat certain line of statements to a number of times. Here, according to the question, we need to use “Repeat after me”

If you take a music note, the tempo, timing and pitch needs to be mentioned clearly and “:” represents that a particular note to be repeated only once.

3 0
3 years ago
Assume that you have an ArrayList variable named a containing 4 elements, and an object named element that is the correct type t
jonny [76]

Answer:

Option (4) is the correct answer.

Explanation:

In Java programming language ,array collection starts from 0 index location and ends in a size-1 index location. So to access the last elements the user needs to use a[Size-1] statement. so to modify the value of the last location of the array the user needs to use "a[size-1]= element;".

But when the user wants to add some new value to the end of the array list collection then he needs to use the statement--

a.add(element); //where add is a function, element is a value and a is a array list object.

Another option is invalid because--

  • Option 1 is not the correct because "a[3]=element;" modify the value of the 3rd element of the array.
  • Option 2 gives a compile-time error because add functions bracts are not closed.
  • Option 3 gives the error because a[4] gives the location of the 5th element of the array but the above question says that a is defined with 4 elements.
7 0
2 years ago
Write a query to display the department name, location name, number of employees, and the average salary for all employees in th
Zigmanuir [339]

Answer:

is hjs yen ; he jawjwjwjwjw

6 0
2 years ago
What wired channel, commonly used for cable tv, consists of an insulated copper wire wrapped in a solid or braided shield placed
Airida [17]
the answer is A coaxial cable
8 0
2 years ago
Other questions:
  • Which statement describes the word "iterative"?
    7·2 answers
  • 24 bit or 16 million colors is often called?
    7·1 answer
  • Your friend sends you a computer game. after installing the game on your computer, you realize that it plays very slowly. you kn
    14·1 answer
  • Suppose one hundred stores participated in the
    14·1 answer
  • Write a function called missing_letters that takes a string parameter and returns a new string with all the letters of the alpha
    7·1 answer
  • What connectors are available for components to be connected externally to the motherboard
    12·2 answers
  • Using C, Write a program that reads a series of strings from standard input and prints only those strings beginning with the let
    8·1 answer
  • Owen is writing a program that will lock his computer for one hour if the incorrect password is entered more than five times. Ha
    14·1 answer
  • Is e commerce a challenge or opportunity to the freight forwarder
    8·1 answer
  • 1. Name the three kinds of Eraser in Photoshop. <br>​
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!