Question

JAVA Rational Numbers Create a Rational number class in the same style as the Complex number...

JAVA

Rational Numbers

Create a Rational number class in the same style as the Complex number class created in class. That is, implement the following methods:

  • constructor
  • add
  • sub
  • mul
  • div
  • toString

You must also provide a Main class and main method to fully test your Rational number class.

Homework Answers

Answer #1

class Rational
{
private int num;
private int den;
  
//default constructor
Rational()
{
num = 0;
den = 1;   
}
  
//parameterize constructor
Rational(int n, int d)
{
num = n;
den = d;   
}
  
//method to add two rational number
public Rational add(Rational r)
{
Rational t = new Rational();
t.num = num * r.den + den * r.num;
t.den = den * r.den;
return t;
}
  
//method to subtract two rational number
public Rational sub(Rational r)
{
Rational t = new Rational();
t.num = num * r.den - den * r.num;
t.den = den * r.den;
return t;
}
  
//method to multiply two rational number
public Rational mul(Rational r)
{
Rational t = new Rational();
t.num = num * r.num;
t.den = den * r.den;
return t;
}
  
//method to divide two rational number
public Rational div(Rational r)
{
Rational t = new Rational();
t.num = num * r.den;
t.den = den * r.num;
return t;
}
  
//method to print the rational number
public String toString()
{
return num + "/" + den;
}
}

public class Main
{
   public static void main(String[] args)
   {
   //create object
       Rational r1 = new Rational(3, 5);
       Rational r2 = new Rational(2, 7);
       Rational r3 = new Rational();

//method calling and display result
       r3 = r1.add(r2);
       System.out.println(r1.toString()+" + "+r2.toString()+" = "+r3.toString());
       r3 = r1.sub(r2);
       System.out.println(r1.toString()+" - "+r2.toString()+" = "+r3.toString());
       r3 = r1.mul(r2);
       System.out.println(r1.toString()+" * "+r2.toString()+" = "+r3.toString());
       r3 = r1.div(r2);
       System.out.println(r1.toString()+" / "+r2.toString()+" = "+r3.toString());
   }
}

OUTPUT:

3/5 + 2/7 = 31/35
3/5 - 2/7 = 11/35
3/5 * 2/7 = 6/35
3/5 / 2/7 = 21/10


Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(JAVA) Create a class that represents a Customer. Use good OO programming technique. A Customer has...
(JAVA) Create a class that represents a Customer. Use good OO programming technique. A Customer has a firstName, lastName, customerNumber, emailAddress. You can add some more fields if you want to, but those 4 are a minimum. Override the equals method, and the toString method from the Object class. Implement the Comparable interface. In the main method, create some instances of the Customer class, and demonstrate the use of the accessor and mutator methods, as well as the compareTo method.
Write the program in java Implement a class Product. Create instance variables to store product name...
Write the program in java Implement a class Product. Create instance variables to store product name and price and supply the values through constructor. For example new Product(“Toaster’, 29.95). Create methods, getName, getPrice. Write a method productPrinter that prints the product name and its price after reducing it by $5. Create a main class and necessary constructs in the main class to run the Product class.
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...
Java For this assignment you need to create at least 5 classes. 1.Create a vehicle class...
Java For this assignment you need to create at least 5 classes. 1.Create a vehicle class ( super class) 2,3. Create two sub classes ( car, bus , truck train or any) for vehicle class 4 Create a subclass (sportscar or schoolbus or Goodstrain or any) for car or bus You need to use the following atleast for one class. Use a protected variable Add constructor for superclass and subclass Override a method which displays a description about the vehicle...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
(Use JAVA) For this program, you need to create three classes, one of which is a...
(Use JAVA) For this program, you need to create three classes, one of which is a main class that includes aggregation from the other two classes. Each of the classes must include at least two fields, multiple constructors, accessor and mutator methods, and toString. Methods in your main class must include: 1. .equals 2. .copy 3. One that passes an object other than the .copy method 4. One that returns an object 5. toString that calls the toStrings from the...
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
in JAVA Write a class Store which includes the attributes: store name and sales tax rate....
in JAVA Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Yarn Store, which inherits from Store. A Yarn Store has the following additional attributes: how many skeins of yarn are sold every year and the average price per skein. Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Yarn Store; In the Yarn Store class, also code a method returning the...
Java... Write a class named TestScores. The class constructor should accept an array of test scores...
Java... Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program (create a Driver class in the same file). The program should ask the user to input the number of test scores to...
Coding in Java Create an Airplane class (not abstract) that uses the Vehicle interface in Q1....
Coding in Java Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for all methods should print simple messages to the screen using System.out.println(). Add an integer speed variable that is changed using ChangeSpeed method. ChangeSpeed adds 5 to the speed each time it is called. Create a default constructor that sets the initial speed to 0. Don't create other constructors or any setter/getter methods. // code from Q1: interface Vehicle { void Start();...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT