What is wrong?-posted again and better to understand

Okay. I saw a few answers. So here is again the program I have to do and the whole code I made:
-Create a class called Rational for performing arithmetic with fractions. Write a driver program to test your class.
Use integer variables to represent the private instance variables of the class�the numerator and the denominator. Provide a constructor method that enables an object of this class to be initialized when it is declared. The constructor should store the fraction in reduced form (i.e., the fraction
2/4
would be stored in the object as 1 in the numerator and 2 in the denominator). Provide a no- argument constructor with default values in case no initializers are provided. Provide public methods for each of the following:
-Addition of two Rational numbers. The result of the addition should be stored in reduced form.
Subtraction of two Rational numbers. The result of the subtraction should be stored in reduced form.
-Multiplication of two Rational numbers. The result of the multiplication should be stored in reduced form.
-Division of two Rational numbers. The result of the division should be stored in reduced form.
-Printing Rational numbers in the form a/b, where a is the numerator and b is the denominator.
-Printing Rational numbers in floating-point format. (Consider providing formatting capabilities that enable the user of the class to specify the number of digits of precision to the right of the decimal point.)
MY CODE:
// Definition of class Rational
public class Rational extends Object {
// variables
private int numerator;
private int denominator;
// Rational constructor with no arguments:
// numerator initialized to 0 and denominator initialized to 1
public Rational()
this( 0/1, 0/1 );
// Rational constructor: numerator initialized to n
// and denominator initialized to 1
public Rational( int n )
this( n/1, n/1 );
// Rational constructor: numerator initialized to n
// and denominator initialized to d
public Rational( int n, int d )
numerator = n;
denominator = d;
// Add two Rational numbers
public Rational sum( Rational right )
return new Rational( ( numerator * right.denominator + denominator * right.numerator ) / denominator * right.denominator );
// Subtract two Rational numbers
public Rational subtract( Rational right )
return new Rational( ( numerator * right.denominator - denominator * right.numerator ) / denominator * right.denominator );
// Multiply two Rational numbers
public Rational multiply( Rational right )
return new Rational( ( numerator * right.numerator ) / denominator * right.denominator );
// Divide two Rational numbers
public Rational divide( Rational right )
return new Rational( ( numerator * right.denominator ) / denominator * right.numerator );
// Reduce the fraction
private void reduce()
int gcd = 0;
int smaller = Math.min( numerator, denominator );
for ( int x = 2; x <= smaller; x++ )
if ( numerator % x == 0 && denominator % x == 0 )
gcd = x;
if ( gcd != 0 )
numerator /= gcd;
denominator /= gcd;
} // end method reduce
// Return String representation of a Rational number
public String toString()
return numerator + "/" + denominator;
// Return floating-point String representation of a Rational number
public String toFloatString()
return Double.toString(
( double ) numerator / denominator );
} // end public class Rational
This is the test program I wrote:
// Java core packages
import java.awt.*;
import java.awt.event.*;
// Java extension packages
import javax.swing.*;
public class RationalTest extends JApplet implements ActionListener
private Rational a, b;
// GUI components
private JLabel nlabel1, nlabel2, dlabel1, dlabel2;
private JTextField numer1Field, numer2Field, denom1Field, denom2Field;
private JButton additButton, subtractButton, multiplyButton, divideButton;
// set up applet's GUI
public void init()
// obtain content pane and set its layout to FlowLayout
Container container = getContentPane();
container.setLayout( new FlowLayout() );
// create nlabel1, numer1Field and attach them to content pane
nlabel1 = new JLabel( "Enter numerator 1:" );
numer1Field = new JTextField( 5 );
container.add( nlabel1 );
container.add( numer1Field );
// create dlabel1, denomField1 and attach them to content pane
dlabel1 = new JLabel( "Enter denominator 1:" );
denom1Field = new JTextField( 5 );
container.add( dlabel1 );
container.add( denom1Field );
// create nlabel2, numer2Field and attach them to content pane
nlabel2 = new JLabel( "Enter numerator 2:" );
numer2Field = new JTextField( 5 );
container.add( nlabel2 );
container.add( numer2Field );
// create dlabel2, denomField2 and attach them to content pane
dlabel2 = new JLabel( "Enter denominator 2:" );
denom2Field = new JTextField( 5 );
container.add( dlabel2 );
container.add( denom2Field );
// set up additButton and attach it to content pane
additButton = new JButton( "Add" );
additButton.addActionListener( this );
container.add( additButton );
// set up subtractButton and attach it to content pane
subtractButton = new JButton( "Subtract" );
subtractButton.addActionListener( this );
container.add( subtractButton );
// set up multiplyButton and attach it to content pane
multiplyButton = new JButton( "Multiply" );
multiplyButton.addActionListener( this );
container.add( multiplyButton );
// set up divideButton and attach it to content pane
divideButton = new JButton( "Divide" );
divideButton.addActionListener( this );
container.add( divideButton );
} // end method init
// handle button events
public void actionPerformed( ActionEvent actionEvent )
Rational r;
a = new Rational( Integer.parseInt( numer1Field.getText() ),
Integer.parseInt( denom1Field.getText() ) );
b = new Rational( Integer.parseInt( numer2Field.getText() ),
Integer.parseInt( denom2Field.getText() ) );
// process additButton event
if ( actionEvent.getSource() == additButton )
r = a.sum( b );
showStatus( "a + b = " + r + " = " + r.toFloatString() );
// process subtractButton event
else if ( actionEvent.getSource() == subtractButton )
r = a.subtract( b );
showStatus( "a - b = " + r + " = " + r.toFloatString() );
// process multiplyButton event
else if ( actionEvent.getSource() == multiplyButton )
r = a.multiply( b );
showStatus( "a * b = " + r + " = " + r.toFloatString() );
// process divideButton event
else if ( actionEvent.getSource() == divideButton )
r = a.divide( b );
showStatus( "a / b = " + r + " = " + r.toFloatString() );
} // end method actionPerformed
} // end public class RationalTest
The html for the applet is:
<html>
<applet code = "RationalTest.class" width = "400" height = "100">
</applet>
</html>
I decided post all this, so you guys have a better idea of what is this about.
The program compiles without errors, but the calculations (add, subtract, multiply and divide) don't work well.
If I follow one of the advices someone gave me here [enclose between parenthesis what follows to the symbol "/" in the calculation for the denominator. It would be, for example for the addition:
return new Rational( ( numerator * right.denominator + denominator * right.numerator ) / ( denominator * right.denominator ) ); ],
I always get the output, a+b = 0/0 = NAN
Do you guys think the problem is in the calculations? is it maybe in the reduce method? Well, my instructor told me that the reduce method works okay. And I still don't know/see what is wrong with the calculations.
Again, the instructor is telling me that

The problem is that the sum, subtract, multiply, and divide methods all use integer arithmetic, so anything less than 1 gets rounded to zero. Try replacing the division symbol '/' by a comma in each method.
Also, your two-argument constructor doesn't reduce the fraction before storing it, as the assignment stated it should. Also, your one-argument constructor doesn't do what the comments state; it initializes both numerator and denominator to n. If n==0, then you get 0/0 as you have discovered.

Similar Messages

  • It says that "there was a problem connecting to the server". What's wrong with this, and how can I deal with this problem?

    I just got my new iPad Mini2, and when I choose "sign in with your apple ID", it says that "there was a problem connecting to the server". What's wrong with this, and how can I deal with this problem?

    1. Turn router off for 30 seconds and on again
    2. Settings>General>Reset>Reset Network Settings

  • TS2570 My Mac is stuck on grey screen what's wrong with it and am I going to lose everything

    My Mac is stuck on grey screen what's wrong with it and am I going to lose everything

    Hopefully you have a backup, but...
    Could be many things, we should start with this...
    "Try Disk Utility
    1. Insert the Mac OS X Install disc, then restart the computer while holding the C key.
    2. When your computer finishes starting up from the disc, choose Disk Utility from the Installer menu at top of the screen. (In Mac OS X 10.4 or later, you must select your language first.)
    *Important: Do not click Continue in the first screen of the Installer. If you do, you must restart from the disc again to access Disk Utility.*
    3. Click the First Aid tab.
    4. Select your Mac OS X volume.
    5. Click Repair Disk, (not Repair Permissions). Disk Utility checks and repairs the disk."
    http://docs.info.apple.com/article.html?artnum=106214
    Then try a Safe Boot, (holding Shift key down at bootup), run Disk Utility in Applications>Utilities, then highlight your drive, click on Repair Permissions, reboot when it completes.
    (Safe boot may stay on the gray radian for a long time, let it go, it's trying to repair the Hard Drive.)
    If perchance you can't find your install Disc, at least try it from the Safe Boot part onward.

  • HT1688 My iphone 5 won't charge and it's in perfect condition, I dont drop it and it's not cracked. I tried multiple chargers, none of them are damaged and my outlets work with other things so the problem is my phone. What's wrong with it and what should

    My iphone 5 won't charge and it's in perfect condition, I dont drop it and it's not cracked. I tried multiple chargers, none of them are damaged and my outlets work with other things so the problem is my phone. What's wrong with it and what should I do? Please help me I need my phone for work.

    Make sure there's nothing blocking a contact in the charging port of the phone.

  • My iPhone 5 screen is black and will not come back on. The phone still works when paired with my vehicles Bluetooth what's wrong with it, and can I fix it?

    My iPhone 5 screen is black and will not come back on. The phone still works when paired with my vehicles Bluetooth what's wrong with it, and can I fix it?

    I have a similar problem.  I restored my iPhone 5 to the latest available OS.  After completing the process the phone never restarted.  The phone was fully charged when I restored it but now refuses to charge past the red battery icon.  I have been able to get the phone in UFD mode several times and have been able to load the new OS on the phone.  The Apple logo does come on and the software fully loads.  iTunes tells me the phone will restart but never does.  I left it to charge all night but it never appeared to load the battery.  This morning I went to the Grand Central Terminal apple store for help.  A woman at the store plugged the phone in but it never charged.  She absolutely refused to believe that I got my phone in UFD mode and loaded any software on.  I don't think it is a battery issue.  The only solution I was given was to buy a new phone.  Repairing it was out of the question.  At this point the phone is completely useless.  Thanks in advance for the help.

  • My optical drive will not read any music cds now or recognize a blank cd in the drive. it might read some data discs. what is wrong with it and how do i fix it?

    My optical drive will not read any music cds or recognize a blank cd. it might read some data cds. what is wrong with it and how can i fix it?

    The optics might be dirty. Try running a cleaning CD through it and see if that makes a difference.
    If not, it will have to be replace.
    Allan

  • What is the posting program and what kind of it is?

    HI,
    what is the posting program and what kind of it is?

    Hi,
    I am not sure in what context you are talking about. In case of a IDOC, there will be a function module which picks the data in the IDOC and posts the same to the respecitve application.
    If you require more details explain the context of the same.
    Regards,
    Ravi
    Note : Please close the thread, if the question is answered

  • I bought 120 gold in Haypi dragon on Mar 12 and 15 2012 , but I havenot received them yet.What is wrong with it ,and when i can reveive them ?

    I bought 120 gold in Haypi dragon on Mar 12 and 15 2012 , but I havenot received them yet.What is wrong with it ,and when i can reveive them ?

    For in-app purchases you should contact the developer's customer support, assuming there is one.

  • What's wrong with Logic and Soundtrack Pro

    Just spent a frustrating day trying to record with Soundtrack Pro and Logic
    Soundtrack is working on and off
    Problems:
    1. Even if sounds are loaded in the multitrack editor, no sound when playing. Only the Solo (S) button make a track sound. No idea what's wrong?
    2. Trying to record directly into a multitrack sometimes works, and sometimes not.
    3. When making another attempt to record, the noise of death appeared (same as the long thread on Logic.
    Finally I just recorded in Amadeus Pro. At least that software works. It just crazy. Look I accept there is a learning curve but this s just crazy
    Apple is becoming an arrogant company that is becoming more interested in creating gadgets that providing tools that work
    Sorry for the rant

    BenB wrote:
    I've worked on both Windows and OS X (Unix), with a few apps over the years. No one can possible work with every app on every hardware configuration possible on every OS available. So to make the statement you actually have is preposterous.
    That said, Logic is the most used software in project studios. Mixing engineers like ProTools better. But the two have way more market share than anything else.
    Hey Ben,
    Sorry to bump your fanboy applecart... (don't mean that as derogatory as it may sound, I liked the play on words)
    From everything that's happening here and the west coast (USA) many of the Logic users I know have switched to other software, the two Logic studios here have gone to ProTools mainly because Logic version 8 was so unstable, there are no more Logic studios here. The studio with the most business uses a Radar system, the second busiest studio is running Sonar. There is a fellow with a mobile system using a MBP and Logic but he uses version 7.
    The University here took bids 3 years ago and opted for PC/Nuendo/RME setups.
    So yes, Logic may indeed have the most project studio percentage but since every jerk can download it for free what is the quality coming out of these project studios? My point is, for professionals, Logic is becoming less and less of an option, most everyone that used it professionally has been disappointed in Apples support of the product, a long time between upgrades and except for user boards like this... literally zero professional support. You know there's problems when you call up Apple and know far more about Logic than the support staff. It seems like both of those are starting to improve but I think it's a little too late. Also... there's still the "blast of white noise" problem that hasn't been addressed, no professional studio can take that kind of chance.
    Apple is using Logic to sell hardware, smart business, absolutely. However, cutting the price in half, removing the copy protection and throwing in the kitchen sink plus seemingly not giving a d@mn that the program is pirated leaves a very bad taste in my mouth.
    pancenter-

  • My macbook is extremely hot and the fan is worryingly loud, what is wrong with it and how do i stop this from happening?

    For soem reason if I actually dare to use my macbook the fan kicks in and is so loud that I am unable to use skype or other voice programs without a headset because of its volume, also it gets very hot very fast, so hot that if I rest it on my knees it burns.  I normally have it on a desk and the area is well ventialted.  I have no print queues and rarely have more than two programs running at a time.  Can anyone offer a suggestion of what is wrong and how I can fix this please?  One of the main reasons I bought one in the first place was that they are so quiet when shown them in the apple store, but this is worryingly loud... 

    Thanks, it seems that it doesn't like to play games, which is a bit of a bummer as i bought this specifically because it was capable of doing that.  To be fair, it is capable... I just need to get used to a sound like sitting by a washing machine on spin cycle...  Wish they would make people aware of this in the Apple Store, its very misleading and not at all desireable.

  • No matter what website i go to mozilla goes straight to problem loading page. i clearly have internet connection but it won't go to any websites. what's wrong with it and what should i do?

    i had this problem before. i went to the IT department in my school and they fixed but i dont know how. now it's happening again and i don't know what to do. i've been using internet explorer so i do have internet connection, but i mozilla is way faster and more reliable so i really need this problem to get fixed please and thank you.

    Do a malware check with some malware scanning programs on the Windows computer.<br />
    You need to scan with all programs because each program detects different malware.<br />
    Make sure that you update each program to get the latest version of their databases before doing a scan.
    *http://www.malwarebytes.org/mbam.php - Malwarebytes' Anti-Malware
    *http://www.superantispyware.com/ - SuperAntispyware
    *http://www.microsoft.com/security/scanner/en-us/default.aspx - Microsoft Safety Scanner
    *http://www.microsoft.com/windows/products/winfamily/defender/default.mspx - Windows Defender: Home Page
    *http://www.safer-networking.org/en/index.html - Spybot Search & Destroy
    You can also do a check for a rootkit infection with TDSSKiller.
    *http://support.kaspersky.com/viruses/solutions?qid=208280684
    See also:
    *"Spyware on Windows": http://kb.mozillazine.org/Popups_not_blocked

  • Why is it when using my iPhone 4s front camera, the screen sometimes turn black or have blue stripes on it? What's wrong with it and what should I do?

    When using my iPhone 4s front camera, the screen sometimes become black or have blue stripes on it! Is this a hardware or software problem? Can this issue be covered by my warranty? I bought it inmid-december last year, and this problem came at around Late January. It's a rare case, only came about 5 times, but this really concerns me. What's wrong with it? Thank you so much for your concern.

    thank you for your reply
    I brought it to the store and they agreed to give me an exchange. thank goodness!
    I have a new question: I've now transferred my back up datas to my new iphone. They wanted me to register, so I registered (as my old apple ID) and then they asked if I want to restore backup, I did that too. after the restore back up process, the only things that aren't there were my music and apps! what should I do to get them into my new iPhone?

  • What's wrong with Siri and Voice dictation?

    Neither Siri or voice dictation is working today.
    Any idea what the problem is?

    I was having problems with dictation for days - the 3 dots would just vibrate a second, and then disappear.  It wasn't an internet connection issue at all.
    Finally found a (bizarre) solution on another site - use Siri to dicate a text (e.g., activate Siri; tell it to send a text to ________; then she'll ask you for the content and you say what you want), then tell her to send it.
    Voila, now if you go to use dictation, it works again.  (At least, that bizarre method worked for me -- I tried dictating an email and it didn't work; used Siri to send a text; then went back and tried dictation again and it was working perfectly.)

  • What's wrong with iCloud and all that. See below

    What's wrong with iCloud tonight I am
    Sending texts and it is going as a normal text
    Not iMessage it was working earlier. Now
    Gas stopped working all of a sudden. If
    Anyone Is experiencing the same please let
    Me know. Also it is saying when I turn on and log
    In waiting for activation. I've never had this before.

    There is an earlier thread that was answered by an Apple employee that states they are seeing the iMessage and Facetime servers are down. It is unknown when they will return, however it generally is not an extended period of time.

  • What's wrong? Verify and compare Cisco 2901 config after loading old config from Cisco 2801

    Hi Cisco Community / Friends,
    I am new to this site though I have cisco account for many years. I am a CCNA ,I  passed my certification on January 2013 I seldom use and utilized my skills on networking becuase of my type of work. I am Project Eng'r working in a System integrator company . Anyway, I would like to ask assistance on the configurations of my Cisco router for this gov't projects.. Here's the situation.
    We have a new project for the VSAT Comm'n of  Coast Watch Station ,  The VSAT was installed 7 years ago. The VSAT was only used for a year by this Gov't agency because of  subscription issue. Now, they wants to revive and use their VSAT facilities for the Coast watch monitoring. Now, some of this routers are working up to now and for some site  are already defective so I need to replace the old 2801 router with a new equivalent model which is Cisco 2901. My plan was just to load the old config into the new Cisco 2901 router. However, after loading it to the new router, I am a little worried because I've got some errors received. I load the old config by copying the old files, edit it in notepad, and load the config using Secure CRT (terminal emulator). When I copy the old config of cisco 2801 to new router cisco 2901 , below are the command not recognized on Cisco 2901. What's wrong ? What are these commands for? 
    Appreciate your comments and help on this matter.. Thank You very much
    Note: I Attached the original config from Cisco 2801 and the other file is the config after I load the config file to Cisco 2901.
    (Errors see below)
    CWS_4_Pandami(config-erm)#mmi polling-interval 60
                                                           ^
    % Invalid input detected at '^' marker.
    CWS_4_Pandami(config-erm)#no mmi auto-configure
                                                           ^
    % Invalid input detected at '^' marker.
    CWS_4_Pandami(config-erm)#no mmi pvc
                                                           ^
    % Invalid input detected at '^' marker.
    CWS_4_Pandami(config-erm)#mmi snmp-timeout 180
                                                            ^
    % Invalid input detected at '^' marker.
    CWS_4_Pandami(config-if)#interface GigabitEthernet0/1
    CWS_4_Pandami(config-if)# description ===CWS4 SAT Modem===
    CWS_4_Pandami(config-if)# bandwidth 256
    CWS_4_Pandami(config-if)# ip address 192.168.42.1 255.255.255.0
    CWS_4_Pandami(config-if)# duplex auto
    CWS_4_Pandami(config-if)# speed auto
    CWS_4_Pandami(config-if)# priority-group 1
                                                        ^
    % Invalid input detected at '^' marker.
    CWS_4_Pandami(config)#access-list 100 permit ip any any dscp cs5
    CWS_4_Pandami(config)#priority-list 1 protocol ip high list 100
                                                    ^
    % Invalid input detected at '^' marker.

    Hi
    From Cisco's website:
    The Modem Management Interface (MMI) is software that enables auto-provisioning for the Cisco 827 routers. The MMI uses a fixed PVC to communicate with the Proxy Element (PE) residing on the digital subscriber line access multiplexer (DSLAM). Using MMI, the Cisco 827 router updates the running image and downloads the prescribed configuration using a configuration file or configuration values in a provisioning information database.
    The customer premise equipment (CPE) can be automatically configured using the Cisco DSL CPE download, but it can be configured only with the image provisioning feature.
    So because this is your device, you don't want to use MMI anyways.
    And "priority-list" is QoS. Probably that QoS-command is old and removed, because now QoS is configured using class-maps and policy-maps.

Maybe you are looking for

  • Japanese Data Corruption in SOAP Response

    Hello, I'm in an environment where I need to pass Japanese data through a SOAP service using an Element. When I return the Element in response tor my SOAP request I'm receiving corrupted data in the XML. The XML data of the Element on the server side

  • How to display cascaded dropdownlist using MVC4?

    I have two tables in DB to populate cascading dropdownlist. On change of value in first dropdown, second dropdown has to be populated using second table. While i tried below code, i am getting the same data for country dropdownlist. countryid is prim

  • CS3/CS4 Creative Suites Web Premium installation conflicts

    Scenario: I installed Creative Suite CS4 Web Premium using Windows XP Pro with SP3 and all the required computer specifications while I still had Creative Suite CS3 Web Premium on the computer. All went well for a while and then Flash CS3 started act

  • BDT & EEWB

    I am working on a task to add custom fields to a standard SAP BP tab.  I was going to use EEWB to update BUT000 and create the custom tab in BP, then use BDT to move the fields over to the SAP standard tab.  I have never done this before, so here are

  • Having to re-enable sharing on 2008R2 server after each reboot.

    I have a 2008R2 EE server that has some foder shares located on a drive. We have an application that uses those shares but evertime the server reboots I have to go in and manually re-enable the sharing for the shares to reappear. Has anyone seen this