okay, so the first class of this program is the Fraction class.
and which I supposed to create:
1. method to input numerator & denominator of fraction
2. and a method to reduce the fraction to its smallest stage.
example:
input: 6/8
reduce to: 3/4 (both are devided by the gcd which is 2)
I think the problem is around the reduce method... about the gcd.
please tell me what's wrong.
[PHP]public class Fraction
{
// num: numerator of fraction, den: denominator of fraction
private int num;
private int den;
/**
* Constructor for objects of class fraction
*/
public Fraction()
{
//default constructor (1/1)
num = 1;
den = 1;
}
/**
* With num =1 and den = 2 it will return "1/2"
*
* @return A fraction constructed of numerator and denominator
*/
public String toString()
{
//returning a string of the fraction showing numerator over denominator
return num + "/" + den;
}
public int getNum () //accessor method, returns the value of the numerator
{
return num;
}
public int getDen ()//accessor method, returns the value of the denominator
{
return den;
}
public void setFraction (int inNum, int inDen) //mutator method, reassigns the values of the numerator & denominator
{
num = inNum;
den = inDen;
}
public void reduce ()
{
int gcd =1;
int x;
if ( num < den )
{
x=num;
}
else
{
x=den;
}
while ( !((num%gcd)==0) && ((den%gcd)==0) )
{
x--;
gcd = x;
}
num = num / gcd;
den = den / gcd;
}
}[/PHP]
thanks.
2 5975 JosAH 11,448
Recognized Expert MVP
Just a few tips: make that class immutable and don't make that gcd() method
a public method. If a user wants another fraction she should create a new one
and not fiddle with another fraction. After your test class works properly change
the int numerator and int denominator to BigIntegers. That default constructor
isn't of much use, i.e. new Fraction(1, 1) isn't much more typing. Only store
normalized fractions, i.e. the constructor should deal with the (private) gcd()
method and store normalized numerators and denominators.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: mwh |
last post by:
Hi. If you remember, I posted Expressons Help. Now I am making a
calculator with javascript. I can't get this to work:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<title>Calculator</title>
<script language="Javascript">
<!-- Begin Hiding
var total = 0
|
by: Fao |
last post by:
Hello, I am having some problems with inheritance. The compiler does
not not return any error messages, but when I execute the program, it
only allows me to enter the number, but nothing else happend. I think
the problem may be in my input function or in the main function.
If anyone out there can help me it woul be greatly appreciated.
Here is the code:
#include <iostream>
|
by: el jefe |
last post by:
hey guys, this is my assignment but i have no godly idea where to start. can anyone help?
the assignment:
A stock tank is a metal or plastic tank that ranchers and farmers use to provide water for their livestock. The tanks are usually made of metal or plastic, and come in three basic shapes; rectangular, “oval”, and circular. The rectangular and circular tanks are shaped as you expect, but the “oval” tank is actually a rectangle with...
|
by: firstcustomer |
last post by:
Hi,
Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that
someone will be able to point me to a ready-made solution to my
problem!
A friend of mine (honest!) is wanting to have on his site, a Javascript
Calculator for working out the cost of what they want, for example:
1 widget and 2 widglets = £5.00
|
by: phjones |
last post by:
This is not a class project.The program below is to display mortgage interest paid for each payment over the term of the loan and loan balance.It is program using array. However, I am receiving the following error message:
--------------------Configuration: <Default>--------------------
C:\Program Files\Xinox Software\JCreatorV4LE\MyProjects\mortgageloan\src\mortgageloan.java:30: operator > cannot be applied to double,int
...
| |
by: clairelee0322 |
last post by:
This program doesn't run because some problem with the reduce fraction.
My reduce fraction is all right but when I tried to put that in this program, It has some errors.
Or maybe the switch menu is wrong? because I put another swith menu in the main switch menu.
I guess the problem is In the division - fraction section
the error says something like cases are skipped by something.. I don't remember because I only get the chances to use C++...
|
by: Kevin McKinley |
last post by:
Below i've put the code for a program that i wrote. I need help on lines 384-403. If you run this program you will notice on the first tab when have it produce an answer the $ is surrounded with {$}. How can i get rid of that?
from Tkinter import *
class MyApp:
def __init__(self, parent):
self.myparent = parent
|
by: Guilherme Polo |
last post by:
On Wed, Sep 3, 2008 at 8:57 PM, Kevin McKinley <kem1723@yahoo.comwrote:
Come on.. "help on lines 384-403", that is not a good way to look for
help. You are supposed to post some minimal code that demonstrates the
problem.
Anyway, this demonstrates what you are getting (independent of python version):
import Tkinter
|
by: blamp |
last post by:
Im writing a fraction calculator program I finished the program but keep getting an error on the do while function. can someone help
heres the program
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std; // Internal storage
int choice;
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |