Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

L ELEC 2543 Object-Oriented Programming and Data Structures Programming Exercise 2a Topics: Java Classes Released: Jan 26. 2024 (Friday) Due: 9:00am, Feb 6, 2024 (Tuesday)

image text in transcribed
L ELEC 2543 Object-Oriented Programming and Data Structures Programming Exercise 2a Topics: Java Classes Released: Jan 26. 2024 (Friday) Due: 9:00am, Feb 6, 2024 (Tuesday) Overview: This exercise introduces how to define a class to represent a type of objects. You are required to use the provided methods and write some methods that return some values. Java method syntax is the same as C++. Define a class called Fraction which satisfies the following: 1. It consists of two data variables, one for representing the numerator and the other for the denominator of a fraction. [What data type would you use for them?] Use private as the visibility modifier. No other data variables are allowed. 2. There is only one constructor and it accepts two integers as parameters. The first parameter represents the numerator and the second parameter represents the denominator. You should modify the numerator and denominator values kept in your data variables in your class according to the following: a If 0 is supplied as the numerator, denominator is changed to 1 in the constructor b. If 0 is supplied as the denominator with non-zero numerator, set the fraction to the default value (numerator = 1 and denominator = 2) Note: 0/0 should become 0/1 according to Rule (2) c. For negative fractions, always make the numerator negative and the denominator positive d. The fraction should be kept in its simplified form. That is, fraction 3/6 should be stored as 12. A method for finding the greatest common divisor ( Method Gap) has been provided. 3. Method wa lue () returns the numerical value of the traction in type double. For example, the value of fraction of #, is 0.5. 4. Method Cat sing!) is defined so that the fraction is printed in format numerator /denominator. Negative sign is put in front of the numerator if appropriate. Positive sign is not needed. 5. Make sure you define the appropriate return type (if needed) for each method. Use public visibility modifier for all the methods you have to develop. After you have defined your Fraction dass, run file Test Fraction . java to test whether your implementation is correct. To run Test.Fraction. java, import the file into the same Eclipse project as Fraction. java. Open TestFraction. java and click the run button go Belpse

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_2

Step: 3

blur-text-image_3

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

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions