Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java program please help! Write a class to implement rational numbers. Rational numbers are numbers that can be represented as a fraction: p/ q, where

Java program please help!

image text in transcribedimage text in transcribedimage text in transcribed

Write a class to implement 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). The p is called the numerator, and the q is called the denominator. Your class will need to provide fields for the parts of a rational number along with methods to work with the fields. Requirements 1) If your code does not compile, you will receive an automatic score of 0. It is YOUR responsibility to come see ME and ONLY me for help if you need it. Please ask for any clarification in the assignment if you do not understand something or for any uncertainty. 2) (0.5 point) The ADT source code file must be named Rational.java. You will also need a source code file for an exception class that must be named InvalidDenominatorException.java and a source code file for a program to test the Rational class named RationalDemo.java. You will need to create a single Eclipse project and create these three files in the project. NOTE: You must turn in all three files! 3) (1 point) You should have a Javadoc comment at the top of each file with a description of the class and your name. 4) (2 points) Programming Practices, Organization, Design. The code must follow good programming practices, be well organized, and be well designed. This includes good layout, consistent and clean indenting using spaces, lines 80 characters or less, etc. See Blackboard for the information on good programming practices that you are expected to follow. I recommend you open the files in a text editor before submitting to make certain the layout is clean and consistent. InvalidDenominatorException: (2 points) Define an exception class for the Rational class. 1) (2 points) You will need to write and submit the code for InvalidDenominatorException. See pages 23 - 27 in the textbook for more information on how to write the exception class. Rational: (11 total points) The Rational class must have the following. 1) (0.5 point) The class must correctly define the attributes of an object; the numerator and the denominator. 2) (2 points) The class must have a constructor that accepts the numerator and denominator of the rational number as int arguments. If the denominator argument value is negative, throw an InvalidDenominatorException exception that has an appropriate error message. Note that you are not handling this exception, but throwing the exception when the denominator value is invalid. 3) (2 points) The class must have a constructor that accepts the numerator and denominator of the rational number as String arguments. If the denominator argument value is negative, throw an InvalidDenominatorException exception that has an appropriate error message. 4) (1 point) Provide a copy constructor for the class. 5) (1 point) The class must have the accessor methods as specified in the UML diagram. 6) (0.5 point) The class must have a method named getDecimal that returns the value of the rational number as a decimal (floating-point) number. 7) (1 point) Provide an equals method for the class. 8) (1 point) Provide a copy method for the class. 9) (1 point) Provide a toString method that produces the rational number in the fraction form, such as 1/2 or-2/7. 10) (1 point) You must have complete Javadoc comments directly above all constructors and methods with a description of the method and any necessary tags and descriptions. Rational numerator : int denominator : int + Rational (num : int, den : int) + Rational (num : String, den : String) + Rational (rational : Rational) + getNumerator(); int + getDenominator() : int + getDecimal() : double + equals (rational : Rational) : boolean + copy () : Rational + toString(): String RationalDemo: (3.5 total points) The RationalDemo program must do the following: 1) (0.25 point) Create a Rational object using the first constructor. The rational number must have the value 1/12. Output the status of the object using toString. Check the output to make certain it is correct and matches the description for toString! If the output is not correct you will not receive credit! 2) (0.25 point) Create a second Rational object using the second constructor. The rational number must have the value -3/12. Output status of the object using toString. Check the output to make certain it is correct and matches the description for toString! If the output is not correct you will not receive credit! 3) (0.25 point) Create a third Rational object using the second constructor. The rational number must have the value 1/3. Output status of the object using toString. Check the output to make certain it is correct and matches the description for toString! If the output is not correct you will not receive credit! 4) (0.25 point) Create a fourth Rational object using the second constructor. The rational number must have the value -3/15. Output status of the object using toString. Check the output to make certain it is correct and matches the description for toString! If the output is not correct you will not receive credit! 5) (0.25 point) Create a fifth Rational object that is a copy of the first Rational object using the copy constructor. 6) (0.25 point) Create a sixth Rational object that is a copy of the second Rational object using the copy method. 7) (0.5 point) Compare the first object with the second object, third object, fourth object, fifth object, and sixth object using the equals method. Output the result from the equals method and check to see if it is correct. 8) (0.5 point) Compare the second object with the third object, fourth object, fifth object, and sixth see if it is correct. 8) (0.5 point) Compare the second object with the third object, fourth object, fifth object, and sixth object using the equals method. Output the result from the equals method and check to see if it is correct. 9) (0.5 point) Compare the third object with the fourth object, fifth object, and sixth object using the equals method. Output the result. Output the result that from the equals method and check to see if it is correct. 10) (0.5 point) Compare the fourth object with the fifth object and sixth object using the equals method. Output the result. Output the result from the equals method and check to see if it is correct. 11) You will also want to create an object that has a denominator of O to make certain your Rational class correctly throws a InvalidDenominatorException. Do not leave this code in your submitted program, use this only to test InvalidDenominatorException

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inference Control In Statistical Databases From Theory To Practice Lncs 2316

Authors: Josep Domingo-Ferrer

2002nd Edition

3540436146, 978-3540436140

More Books

Students also viewed these Databases questions