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
What is an important theme in "Once Upon a Time"?
Maurinko [17]

Answer:

The disintegration of South African society.

Explanation:

Nadine Gordimer's "Once Upon A Time" revolves around the racist "Apartheid" time of South African society where the whites 'dominate' the blacks. the story focuses on the societal issues between the two races and how it is detrimental to the overall lives of the people involved.

One important theme of the story is racism, <em>"Apartheid"</em> which seemingly dictates who is superior and who is not. The huge divide in not only the living spheres of the two groups of citizens but also in their approaches to society is indicative of the discriminative attitude and perception of the people, especially with the whites claiming themselves to be superior and 'civilized'. The book deals largely with the colonial mindset of the whites while also the structural bigotry apparent in the mindset of the people.

Thus, the correct answer is the second option.

3 0
3 years ago
Marcus creates a HTML webpage on his favourite game, football. On clicking the word “football” another website containing the in
shtirl [24]
I can’t see the following tags that he could’ve possibly used
3 0
3 years ago
i need help or good site for my project topic: identification of problems hindering effective teaching and learning of computer
attashe74 [19]

Answer:

they learn how to be independent

and how to create their website at the young age and who make money and create jods

like me I'm learning IT at katlehong secondary school

and I'm now to create my website

4 0
3 years ago
Eric walks 7 km East in 2 hours and then 2.5 km West in 1 hour.
Sidana [21]
Ok what is the question this is just a statement on what it is eric did.
7 0
3 years ago
What job does a back-end developer perform?
Diano4ka-milaya [45]

Answer:

Handles what happens behind the scenes on websites

Explanation:

The website back end is made up of server, an application, and a database. The back-end developer builds and coordinate these components together. Back end developers manage the technical side of the websites (that is what the users do not see on the website).

The back end developer needs to know how to program using PHP, java, Python, Ruby, as well as use tools such as MySQL, Oracle, and SQL Server.

6 0
3 years ago
Other questions:
  • Mail merge can be used in business to complete which of the following tasks
    15·2 answers
  • Choose the type of critical thinking demonstrated in the example:
    8·2 answers
  • Survey Q. Non-scoring: What role is played in the team? (1 correct answer)
    14·1 answer
  • What is the function of the Status bar in Outlook 2010?
    9·1 answer
  • Where do you go to create a workbook?​
    8·1 answer
  • Llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll
    7·1 answer
  • What is entered into a cell that is typically numeric and can be used for calculations?
    9·2 answers
  • Which of the following about Java arrays is true?
    14·1 answer
  • Storage that is wiped clean when power is cut off from a device is known as _____.
    13·1 answer
  • During an IFR flight in IMC, you enter a holding pattern (at a fix that is not the same as the approach fix) with an EFC time of
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!