Rational numbers are numbers that can be represented as a fraction p / q where p is an integer number and q is a positive integer (q != 0). Design and implement a Java class RationalNumber for representing such numbers. Implement methods to add and mutliply rational numbers. Implement a method for return the value of a rational number as a double value. Write a driver class to test your class. here is the class details:

Constructor Summary: RationalNumber(int numerator, int denominator) Creates a new RationalNumber with given numerator and denominator. methods summary: RationalNumber add(RationalNumber rationaleNumber) Adds the given rationaleNumber to this RationalenNumber (the object that you created from RationalNumber Class) and returns the sum as a new RationalNumber. int getDenominator() Returns the denominator of this RationalNumber. double getDoubleValue() Returns the value of this RationalNumber as a double value. int getNumerator() Returns the numerator of this RationalNumber. RationalNumber multiply(RationalNumber rationalNumber) Multiplies the given rationalNumber to this RationalNumber (the object that you created from RationalNumber Class) and returns the product as a new RationalNumber

View Answers

April 25, 2011 at 4:05 PM

public class RationalClass { private int nr; private int dr; public int getDenominator() { return dr; } public int getNumerator() { return nr; } public RationalClass(int numerator, int denominator) { if (denominator == 0) { throw new RuntimeException("Denominator is zero"); } int no = rationalNumber(numerator, denominator); nr = numerator / no; dr = denominator / no; } public RationalClass add(RationalClass number) { int numerator = (this.nr * number.dr) + (this.dr * number.nr); int denominator = this.dr * number.dr; return new RationalClass(numerator, denominator); } public RationalClass multiply(RationalClass number) { return new RationalClass(this.nr * number.nr, this.dr * number.dr); } private static int rationalNumber(int p, int q) { if (q == 0) { return p; } else { return rationalNumber(q, p % q); } } public double getDoubleValue() { double num = Double.parseDouble(Integer.toString(getNumerator())); double den = Double.parseDouble(Integer.toString(getDenominator())); double value = num / den; return value; } public String toString() { return Double.toString(getDoubleValue()); } public static void main(String[] args) { RationalClass a, b, c; a = new RationalClass(1, 2); b = new RationalClass(1, 2); c = a.add(b); System.out.println("Addition= " + c); a = new RationalClass(1, 2); b = new RationalClass(1, 2); c = a.multiply(b); System.out.println("Multiplication= " + c); } }

April 25, 2011 at 4:05 PM

Ads

Related Tutorials/Questions & Answers:

Help With Java... Write a java program that can take a positive integer greater than 2 as input and write out the number of times one must repeatedly divide this number by 2 before getting a value less than 2

java help! I need help starting off a program I am trying to write for class. The class should contain a private instance variable to represent temperature in Degrees Celsius and it should have the following methods

Advertisements

help in java Rational Numbers Rational numbers are numbers that can be represented as a fraction p / q where p is an integer number and q is a positive integer (q != 0). Design and implement a Java class RationalNumber

help in java Write an application that prompt the user to enter a number to use as an array size, and then attempt to declare an array using the entered size. Java generates a NegativeArraySizeException if wou attempt to create

java help How to Open CSV Files in a Microsoft Excel Application Using Java Code with example pgm

help for java 1.convert the following Pseudo code to java if/else structure if student?s grade is greater than or equal to 90 Print ?A? else if student?s grade is greater than or equal to 80 Print ?B? else

help in java 3.Flights Design and implement a class called Flight that represents an airline flight. It should contain instance data that represents the airline name, flight number, and the flight's origin and destination cities

help in java Write a program that will evaluate simple expressions such as 17 + 3 and 3.14159 * 4.7. The expressions are to be typed in by the user. The input always consist of a number, followed by an operator, followed

help in java Design and implement a class called Sphere that contains instance data that represents the sphere's diameter. Define the Sphere constructor to accept and initialize the diameter, and include getter and setter

help in java The factorial of a nonnegative integer n is written n! (pronounced Ã¢â?¬Å? n factorialÃ¢â?¬Â?) and is defined as follows: n!=n . (n-1) . (n-2) . .... . 1 (for values of n greater than or equal to 1) and n!=1 (for n

help in java Gross And Dozens Design and implement a class called GrossAndDozens to convert a given number of eggs into the number of gross, the number of dozens, and to the number of left over eggs. If you have N eggs, then you

help in java Shopping cart Create 3 shopping carts from a Cart class. Each class should have four items in it, a title, a price for the 4 items and the number of each. Your shopping cart should use private variables, so use

help in java Team Roster Create a program called TeamRoster that uses a class called Roster. Roster should contain a team name and the names of all... or help me on this pleas.. 7. Gross And Dozens Design and implement a class

java help 1) build class(es) for the dresses, choose (dress ID, color, price, designer)( link lists) 2) construct the driver class by establishing two separate objects for each sister 3) let them both to enter the dresses

java help Rational numbers are numbers that can be represented as a fraction p / q where p is an integer number and q is a positive integer (q != 0). Design and implement a Java class RationalNumber for representing such numbers

java,java,java,help Dear people, can anyone help me complete this program import java.util.*; public class StringDemo { static String a="{a=100;b=200;c=300}"; public static void main(String args[]) { String b[]=a.split

help in java Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have a number

help in java Temperature Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have

help in java Create 3 shopping carts from a Cart class. Each class should have four items in it, a title, a price for the 4 items and the number of each. Your shopping cart should use private variables, so use getter and setter

help in java Define a class named Money whose objects represent amounts of U.S. money. The class will have two instance variables of type int for the dollars and cents in the amount of money. Include a constructor with two

help in java Create a class named Order that performs order processing of a single item. The class has five instance variables (fields) : customer name, customer number, quantity ordered, unit price, and total price. Define

Java help Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit. Use a Floating-point number for the temperature and a character for the scale: either 'C' for Celsius or 'F

Java Help Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit. Use a Floating-point number for the temperature and a character for the scale: either 'C' for Celsius or 'F

java help Design and implement a class called DateWriter that has three instance variables: a string for month, an integer for day, and an integer for year. The class should has a constructor to accept and initialize the values

Intro Java help with Double I have to evaluate a math expression using double in Java. I can input the code fine for the expression, but I don't know how to view the answer that it gives me. help

Need Help in Java programming Hello. Can someone please help me with writing the following program Java program that gives assignment details such as:assignment number,assignment name,due date,submission date,percentage marks

New to Java Please help Hi I need help, can some one help me.... Thanks! If you are new in java, then you need to learn core java concepts.So go through the following link: Core Java Tutorials Here, you will find

Need help on JAVA JSP Hi, I have never worked on java and I have been given an assignment where I have to fix existing issues in the tool(created using JAVA JSP). e.g. Tool does not adjust with the resolution of the screen

Java Programming Help Hello ! I m the student of last year and i need your help to complete my java project. I going to make a site like youtube... with controls .. I Just want to use jsp and applet in my project . Please help me

Java programing help Can you help me create this program? Write a console program that repeatedly prompts the user to enter data until they type done (any case, Upper, Lower, or Mixed). As they enter the data, assign

Java application Help? Write an application that accepts a users password from the keyboard. When the entered password is less then six characters more then 10 characters, or does not contain at lease one letter and one digit