• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with Constructors program  RSS feed

 
kelly mike
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea what i'm suppose to do and my teacher wont help me. I FINISH THE FIRST PART BUT HAVE NO CLUE AVOUT THE SECOND PART???
----------------------------------------------------------------------------
PROGRAM PART1 -- THE DONE PART
----------------------------------------------------------------------------
Write a program with a Rational class.

The purpose of the Rational class is to manipulate rational number operations. A rational number is a number that can be expressed in the form A / B where A and B are both whole numbers (no fractions or decimals) and B � 0.

Your Rational class needs to declare two data attributes:
1)num for numerator and
2)den for denominator.

Only one constructor is required, which uses two parameters entered at the keyboard. The first parameter is the numerator and the second parameter is the denominator.

The Rational class requires three additional methods:

getDecimal -- the getDecimal method returns the decimal value of the current rational number that is stored. If the number 3 / 4 is stored, then getDecimal will return 0.75.

getNum -- method getNum returns the numerator.

getDen -- method getDen returns the denominator.


�The main method should create 1 object and display the rational number using the specified Rational class methods.
----------------------------------------------------------------------------


/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
!!!THIS IS THE PART I DONT GET!!!
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
----------------------------------------------------------------------------
Part B
----------------------------------------------------------------------------
CAN ANYONE HELP
Add the reduce method (a helper method) to the Rational class.

oThe reduce method is used to reduce the rational number to the smallest possible fraction. This means that a number that is entered as 22/33 will be reduced and stored as 2/3.

oreduce uses another helper method, getGCF.

�To compute the Greatest Common Factor (GCF) use Euclid's Algorithm. This 2000-year-old algorithm is suited very well for the sequential instructions required by a computer program.

�Euclid's Algorithm is shown below using 120 and 108 as sample numbers to compute the GCF of 12.


Algorithm StepsSample Problem
Step 1:
Start with two integers
Integer1 is 120
Integer2 is 108
Step 2:
Divide Integer1 by Integer2 and compute the remainder.
120 / 108 = 1
The remainder = 12
Step 3:
If the remainder equals 0, you are
finished. The GCF is Integer2.

The remainder is not 0
You are not finished.
Step 4:
If the remainder is not 0 then
Integer1 is replaced by Integer2 and
Integer2 is replaced by remainder
Integer1 is now 108
Integer2 is now 12
Step 5:
Go to Step2:

Step 2:
Divide Integer1 by Integer2 and compute the remainder.
108 / 12 = 9
The remainder = 0
Step 3:
If the remainder equals 0, you are
finished. The GCF is Integer2.
The remainder is 0
You are finished and the GCF = 12
oOnce the greatest common factor is figured, the reduce method will use it to reduce the fraction.

oThink about the most logical place to call the reduce method�


You also need to improve the constructor (with two-parameters).

oYour program needs to protect against the possibility of entering illegal values for a rational number.

oEntering zero as the denominator value should NOT be allowed.

oIn the event zero is entered, the denominator needs to be initialized to one and an error message needs to be displayed, which states Illegal denominator; altered to 1.


You also need to add method getOriginal.

oThis method returns the original fraction, as it was entered before reduction. (returns the value as a String� for printing)

oThis also means that the Rational class needs to add two additional attributes to store the original values.

oThis will allow your program to display the fraction before reduction.


You may also want to add method getRational.

oThis method returns the reduced fraction. (returns as a String)

oThis will allow your program to display the fraction after reduction.


�The main method should create 1 object and display the rational number using the appropriate Rational class methods.

oIt should display both the original rational number and the reduced value too (even if they are the same).
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest that you examine the "Algorithm Steps Sample Problem" section provided in the assignment again. Your professor literally hand held you through how to do it.

Henry
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, the key thing about any program is to break it into manageable steps and solve them one by one. I wish my instructors had been so kind and direct in the assistance they provided me
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!