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...
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();...
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods....
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method. Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure...
JAVA QUIZ Question 1 Which of the following is false about a "super" call in a...
JAVA QUIZ Question 1 Which of the following is false about a "super" call in a sub class's constructor? Select one: a. It must be the first statement in the constructor b. If you don't include it Java will c. If you don't include it you must have a 0 parameter constructor coded in the super class or no constructors coded at all in the super class d. The sub class constructor containing the super call and the called super...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class (5 points extra credit) Create another class called CourseSection (20 points extra credit) Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of...
Java Create a new Drive class. * Create a Menu class. * The menu class will...
Java Create a new Drive class. * Create a Menu class. * The menu class will use the previous 4 classes you created in GCD, LCM, FACTORIAL, DIGITS The Menu will display a menu that give you the option of selecting: 1) Greatest Common Denominator 2) Lowest Common Multiple 3) Factorial 4) Number of Digits in an Integer Enter 1,2,3 or 4: When the User enter the choice, then the correct function/method is called for the class and asks the...
Java code Problem 1. Create a Point class to hold x and y values for a...
Java code Problem 1. Create a Point class to hold x and y values for a point. Create methods show(), add() and subtract() to display the Point x and y values, and add and subtract point coordinates. Tip: Keep x and y separate in the calculation. Create another class Shape, which will form the basis of a set of shapes. The Shape class will contain default functions to calculate area and circumference of the shape, and provide the coordinates (Points)...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT