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]
3 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]3 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
Submitting Unit 11 Assignment – Step 1 Python CS Fundamentals<br> Need the code to this.
oksano4ka [1.4K]

The Step 1 Python CS Fundamentals  coding work is given in the document attached below.

<h3 /><h3>What are the basic fundamentals of Python?</h3>

The basic steps or lessons in Python is made up of:

  • Kinds of Data types ( such as int, float, strings)
  • The kinds of Compound data structures ( such as lists, tuples, and dictionaries), etc.

Note that by knowing the Step 1  of the Python CS Fundamentals  coding work, one can know what to do next. See the document attached for more information.

Learn more about Coding from

brainly.com/question/16397886

#SPJ1

Download docx
8 0
2 years ago
Which branch of study can help Jessica study the concept of light
Fiesta28 [93]

Answer:

Physics, Quantum Mechanics

Explanation:

Light is a stream of photons with no weight. If you want to dig deeper in this mysterious phenomena, you should study physics and quantum mechanics. Physics takes up things like the theory of relativity, which can be useful in studying spacetime and how light travels through it, and quantum mechanics, being the study of matter in an atomic level, should help you get an understanding on how photons of light travel and why they behave so strangely.

4 0
3 years ago
Lisa and her husband would like to buy a house soon. She continuously pays bills late. How do her actions affect her and others?
stiks02 [169]
Well actually it affects her credit. Home owners would not want to sell a house to someone who doesn't pay bills on time. It shows he/she is unreliable

3 0
3 years ago
Read 2 more answers
An employee was watching Jessica, an IT employee, typing in many different confusing terms. Jessica explained that she needed to
zzz [600]

Answer:

Programming and Software Development

Explanation:

8 0
3 years ago
Read 2 more answers
"The Cloud " is: A A form of word processing B A network for accessing applications from any device C A virtual gaming environme
mestny [16]
I think the answer may be B
7 0
3 years ago
Other questions:
  • Is a growing network of physical objects that have sensors connected to the internet?
    15·1 answer
  • Two words are a ""reverse pair"" if each is the reverse of the other. Write a program that finds all the reverse pairs in the wo
    12·1 answer
  • Why information technology is important in healthcare?
    13·1 answer
  • You give an object a more meaningful name by setting the object’s _________________ property.
    7·1 answer
  • You have a notebook computer and wish to connect to an IEEE 802.11ac wireless network. The computer does not have a built-in WLA
    15·1 answer
  • Using the Internet comes with certain risks that you should be prepared for. True False​
    11·2 answers
  • A normal HDMI signal has a colour depth of 8 bits per each of RGB colour. The maximum number
    13·1 answer
  • For each of the following memory accesses indicate if it will be a cache hit or miss when carried out in sequence as listed. Als
    15·1 answer
  • Pls answer i will give 20 points
    7·1 answer
  • What is the best file manager for windows 10?
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!