SlideShare a Scribd company logo
1 of 4
Download to read offline
Java Program- Wrtie a Fraction class
Given the main method of a driver class, write a Fraction class. Include the following instance
methods:
add,
multiply,
print,
printAsDouble,
and a separate accessor method for each instance variable.
Write a Fraction class that implements these methods:
add This method receives a Fraction parameter and adds the parameter fraction to the calling
object fraction.
multiply This method receives a Fraction parameter and multiplies the parameter fraction by the
calling object fraction.
print This method prints the fraction using fraction notation (1/4, 21/14, etc.)
printAsDouble This method prints the fraction as a double (0.25, 1.5, etc.)
Separate accessor methods for each instance variable in the Fraction class.
Provide a driver class, FractionDemo, that demonstrates this Fraction class. The driver class
should contain this main method:
/***********************************************************
* FractionDriver.java
* Dean & Dean
*
* This class acts as a driver for the Fraction class.
***********************************************************/
import java.util.Scanner;
public class FractionDriver {
/**
* @param args
*/
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
Fraction c, d, x; // Fraction objects
System.out.println("Enter numerator; then denominator.");
c = new Fraction(stdIn.nextInt(), stdIn.nextInt());
c.print();
System.out.println("Enter numerator; then denominator.");
d = new Fraction(stdIn.nextInt(), stdIn.nextInt());
d.print();
x = new Fraction(); // create a fraction for number 0
System.out.println("Sum:");
x.add(c).add(d);
x.print();
x.printAsDouble();
x = new Fraction(1, 1); // create a fraction for number 1
System.out.println("Product:");
x.multiply(c).multiply(d);
x.print();
x.printAsDouble();
System.out.println("Enter numerator; then denominator.");
x = new Fraction(stdIn.nextInt(), stdIn.nextInt());
x.printAsDouble();
} //end main
} // end Fraction Driver
Note that this demonstration driver does not call the accessor methods. That’s OK. Accessor
methods are often implemented regardless of whether there’s an immediate need for them. That’s
because they are very useful methods in general and providing them means that future code can
use them when the need arises.
Sample session:
Enter numerator; then denominator.
5
8
5/8
Enter numerator; then denominator.
4
10
4/10
Sum:
82/80
1.025
Product:
20/80
0.25
Enter numerator; then denominator.
6
0
infinity
Solution
public Building(int numFloors)
{
numberOfFloors = numFloors;
current = 1;
requestedFloor = 0;
}
public Fraction add ( Fraction A)
{
Fraction X=new Fraction(( numerator*A.denominator + A.numerator*denominator ),
numerator*A.numerator );
X.reduce();
return X;
}
public Fraction multiply( Fraction otherFraction )
{
return new Fraction( otherFraction.getNumerator() * this.getNumerator(),
otherFraction.getDenominator() * this.getDenominator() );
}
public void print()
{
System.out.println(numerator + "/"+denominator);
}
public void printAsDouble()
{
double x= (double)numerator/(double)denominator;
System.out.println(x);
}
private void reduce()
int x=denominator < numerator? denominator:numerator;
while(x>1)
{
if( (numerator%x==0) && (numerator%x==0)) break;
x--;
}
denominator /=x;
numerator /=x;
}

More Related Content

Similar to Java Program- Wrtie a Fraction classGiven the main method of a dri.pdf

Hey I need help creating this code using Visual Studio (Basic) 2015.pdf
Hey I need help creating this code using Visual Studio (Basic) 2015.pdfHey I need help creating this code using Visual Studio (Basic) 2015.pdf
Hey I need help creating this code using Visual Studio (Basic) 2015.pdf
forwardcom41
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
aromanets
 
Chapter 2.4
Chapter 2.4Chapter 2.4
Chapter 2.4
sotlsoc
 
import java.util.Scanner;Henry Cutler ID 1234 7202.docx
import java.util.Scanner;Henry Cutler ID 1234  7202.docximport java.util.Scanner;Henry Cutler ID 1234  7202.docx
import java.util.Scanner;Henry Cutler ID 1234 7202.docx
wilcockiris
 

Similar to Java Program- Wrtie a Fraction classGiven the main method of a dri.pdf (20)

Vb (1)
Vb (1)Vb (1)
Vb (1)
 
Hey I need help creating this code using Visual Studio (Basic) 2015.pdf
Hey I need help creating this code using Visual Studio (Basic) 2015.pdfHey I need help creating this code using Visual Studio (Basic) 2015.pdf
Hey I need help creating this code using Visual Studio (Basic) 2015.pdf
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
 
Estructura secuencial -garcia
Estructura secuencial -garciaEstructura secuencial -garcia
Estructura secuencial -garcia
 
CIS 407 STUDY Inspiring Innovation--cis407study.com
CIS 407 STUDY Inspiring Innovation--cis407study.comCIS 407 STUDY Inspiring Innovation--cis407study.com
CIS 407 STUDY Inspiring Innovation--cis407study.com
 
5 Rmi Print
5  Rmi Print5  Rmi Print
5 Rmi Print
 
Chapter 2.4
Chapter 2.4Chapter 2.4
Chapter 2.4
 
import java.util.Scanner;Henry Cutler ID 1234 7202.docx
import java.util.Scanner;Henry Cutler ID 1234  7202.docximport java.util.Scanner;Henry Cutler ID 1234  7202.docx
import java.util.Scanner;Henry Cutler ID 1234 7202.docx
 
Functions in c
Functions in cFunctions in c
Functions in c
 
L03vars
L03varsL03vars
L03vars
 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manual
 
Methods intro-1.0
Methods intro-1.0Methods intro-1.0
Methods intro-1.0
 
Problemas secuenciales.
Problemas secuenciales.Problemas secuenciales.
Problemas secuenciales.
 
Problemas secuenciales.
Problemas secuenciales.Problemas secuenciales.
Problemas secuenciales.
 
Problemas secuenciales.
Problemas secuenciales.Problemas secuenciales.
Problemas secuenciales.
 
Problemas secuenciales.
Problemas secuenciales.Problemas secuenciales.
Problemas secuenciales.
 
Problemas secuenciales.
Problemas secuenciales.Problemas secuenciales.
Problemas secuenciales.
 

More from archanaemporium

Identify a article about a communicable or noncommunicable disease i.pdf
Identify a article about a communicable or noncommunicable disease i.pdfIdentify a article about a communicable or noncommunicable disease i.pdf
Identify a article about a communicable or noncommunicable disease i.pdf
archanaemporium
 
i safari File Edit View History Bookmarks Window Help 67 D. Thu 101.pdf
i safari File Edit View History Bookmarks Window Help 67 D. Thu 101.pdfi safari File Edit View History Bookmarks Window Help 67 D. Thu 101.pdf
i safari File Edit View History Bookmarks Window Help 67 D. Thu 101.pdf
archanaemporium
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
archanaemporium
 
How can Internet technologies be involved in improving a process in .pdf
How can Internet technologies be involved in improving a process in .pdfHow can Internet technologies be involved in improving a process in .pdf
How can Internet technologies be involved in improving a process in .pdf
archanaemporium
 
Hypothesize a way for a virus to evade a host defense & then devise .pdf
Hypothesize a way for a virus to evade a host defense & then devise .pdfHypothesize a way for a virus to evade a host defense & then devise .pdf
Hypothesize a way for a virus to evade a host defense & then devise .pdf
archanaemporium
 
Haploid. After is complete, the resulting gametes are haploid. This m.pdf
Haploid. After is complete, the resulting gametes are haploid. This m.pdfHaploid. After is complete, the resulting gametes are haploid. This m.pdf
Haploid. After is complete, the resulting gametes are haploid. This m.pdf
archanaemporium
 
FTP and TFTP are primarily file transfer protocols. What is the main.pdf
FTP and TFTP are primarily file transfer protocols. What is the main.pdfFTP and TFTP are primarily file transfer protocols. What is the main.pdf
FTP and TFTP are primarily file transfer protocols. What is the main.pdf
archanaemporium
 
DOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdf
DOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdfDOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdf
DOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdf
archanaemporium
 
Discuss in detail how two different Progressive reformers tackled th.pdf
Discuss in detail how two different Progressive reformers tackled th.pdfDiscuss in detail how two different Progressive reformers tackled th.pdf
Discuss in detail how two different Progressive reformers tackled th.pdf
archanaemporium
 

More from archanaemporium (20)

Identify a article about a communicable or noncommunicable disease i.pdf
Identify a article about a communicable or noncommunicable disease i.pdfIdentify a article about a communicable or noncommunicable disease i.pdf
Identify a article about a communicable or noncommunicable disease i.pdf
 
i safari File Edit View History Bookmarks Window Help 67 D. Thu 101.pdf
i safari File Edit View History Bookmarks Window Help 67 D. Thu 101.pdfi safari File Edit View History Bookmarks Window Help 67 D. Thu 101.pdf
i safari File Edit View History Bookmarks Window Help 67 D. Thu 101.pdf
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
 
I know that water molecules attract each other, but why do water mol.pdf
I know that water molecules attract each other, but why do water mol.pdfI know that water molecules attract each other, but why do water mol.pdf
I know that water molecules attract each other, but why do water mol.pdf
 
How can Internet technologies be involved in improving a process in .pdf
How can Internet technologies be involved in improving a process in .pdfHow can Internet technologies be involved in improving a process in .pdf
How can Internet technologies be involved in improving a process in .pdf
 
How many paths are there in a tree with n nodesHow many paths a.pdf
How many paths are there in a tree with n nodesHow many paths a.pdfHow many paths are there in a tree with n nodesHow many paths a.pdf
How many paths are there in a tree with n nodesHow many paths a.pdf
 
Hypothesize a way for a virus to evade a host defense & then devise .pdf
Hypothesize a way for a virus to evade a host defense & then devise .pdfHypothesize a way for a virus to evade a host defense & then devise .pdf
Hypothesize a way for a virus to evade a host defense & then devise .pdf
 
How many ways can letters of the word SINGAPORE be arranged such that.pdf
How many ways can letters of the word SINGAPORE be arranged such that.pdfHow many ways can letters of the word SINGAPORE be arranged such that.pdf
How many ways can letters of the word SINGAPORE be arranged such that.pdf
 
Haploid. After is complete, the resulting gametes are haploid. This m.pdf
Haploid. After is complete, the resulting gametes are haploid. This m.pdfHaploid. After is complete, the resulting gametes are haploid. This m.pdf
Haploid. After is complete, the resulting gametes are haploid. This m.pdf
 
FTP and TFTP are primarily file transfer protocols. What is the main.pdf
FTP and TFTP are primarily file transfer protocols. What is the main.pdfFTP and TFTP are primarily file transfer protocols. What is the main.pdf
FTP and TFTP are primarily file transfer protocols. What is the main.pdf
 
Given Starter Fileimport java.util.Arrays; Encapsulates.pdf
Given Starter Fileimport java.util.Arrays;   Encapsulates.pdfGiven Starter Fileimport java.util.Arrays;   Encapsulates.pdf
Given Starter Fileimport java.util.Arrays; Encapsulates.pdf
 
Express the verbal representation for the function f symbolically. M.pdf
Express the verbal representation for the function f symbolically.  M.pdfExpress the verbal representation for the function f symbolically.  M.pdf
Express the verbal representation for the function f symbolically. M.pdf
 
Economic resources have a price above zero because...A. there are .pdf
Economic resources have a price above zero because...A. there are .pdfEconomic resources have a price above zero because...A. there are .pdf
Economic resources have a price above zero because...A. there are .pdf
 
DOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdf
DOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdfDOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdf
DOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdf
 
Discuss in detail how two different Progressive reformers tackled th.pdf
Discuss in detail how two different Progressive reformers tackled th.pdfDiscuss in detail how two different Progressive reformers tackled th.pdf
Discuss in detail how two different Progressive reformers tackled th.pdf
 
Describe the major parts of the nervous system and their functions..pdf
Describe the major parts of the nervous system and their functions..pdfDescribe the major parts of the nervous system and their functions..pdf
Describe the major parts of the nervous system and their functions..pdf
 
DEFINE gene enhancer and gene promoter (3-4 sentences each).Sol.pdf
DEFINE gene enhancer and gene promoter (3-4 sentences each).Sol.pdfDEFINE gene enhancer and gene promoter (3-4 sentences each).Sol.pdf
DEFINE gene enhancer and gene promoter (3-4 sentences each).Sol.pdf
 
Cisco Systems, Inc. offers a switching technology known as Multi-Lay.pdf
Cisco Systems, Inc. offers a switching technology known as Multi-Lay.pdfCisco Systems, Inc. offers a switching technology known as Multi-Lay.pdf
Cisco Systems, Inc. offers a switching technology known as Multi-Lay.pdf
 
C# ProgramingHow a derived class that inherits attributes and beha.pdf
C# ProgramingHow a derived class that inherits attributes and beha.pdfC# ProgramingHow a derived class that inherits attributes and beha.pdf
C# ProgramingHow a derived class that inherits attributes and beha.pdf
 
Assume that x and y are already defined as being of type int . Write.pdf
Assume that x and y are already defined as being of type int . Write.pdfAssume that x and y are already defined as being of type int . Write.pdf
Assume that x and y are already defined as being of type int . Write.pdf
 

Recently uploaded

Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
cupulin
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 

Recently uploaded (20)

e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 

Java Program- Wrtie a Fraction classGiven the main method of a dri.pdf

  • 1. Java Program- Wrtie a Fraction class Given the main method of a driver class, write a Fraction class. Include the following instance methods: add, multiply, print, printAsDouble, and a separate accessor method for each instance variable. Write a Fraction class that implements these methods: add This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction. multiply This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction. print This method prints the fraction using fraction notation (1/4, 21/14, etc.) printAsDouble This method prints the fraction as a double (0.25, 1.5, etc.) Separate accessor methods for each instance variable in the Fraction class. Provide a driver class, FractionDemo, that demonstrates this Fraction class. The driver class should contain this main method: /*********************************************************** * FractionDriver.java * Dean & Dean * * This class acts as a driver for the Fraction class. ***********************************************************/ import java.util.Scanner; public class FractionDriver { /** * @param args */ public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); Fraction c, d, x; // Fraction objects System.out.println("Enter numerator; then denominator."); c = new Fraction(stdIn.nextInt(), stdIn.nextInt());
  • 2. c.print(); System.out.println("Enter numerator; then denominator."); d = new Fraction(stdIn.nextInt(), stdIn.nextInt()); d.print(); x = new Fraction(); // create a fraction for number 0 System.out.println("Sum:"); x.add(c).add(d); x.print(); x.printAsDouble(); x = new Fraction(1, 1); // create a fraction for number 1 System.out.println("Product:"); x.multiply(c).multiply(d); x.print(); x.printAsDouble(); System.out.println("Enter numerator; then denominator."); x = new Fraction(stdIn.nextInt(), stdIn.nextInt()); x.printAsDouble(); } //end main } // end Fraction Driver Note that this demonstration driver does not call the accessor methods. That’s OK. Accessor methods are often implemented regardless of whether there’s an immediate need for them. That’s because they are very useful methods in general and providing them means that future code can use them when the need arises. Sample session: Enter numerator; then denominator. 5 8 5/8 Enter numerator; then denominator. 4 10 4/10 Sum: 82/80
  • 3. 1.025 Product: 20/80 0.25 Enter numerator; then denominator. 6 0 infinity Solution public Building(int numFloors) { numberOfFloors = numFloors; current = 1; requestedFloor = 0; } public Fraction add ( Fraction A) { Fraction X=new Fraction(( numerator*A.denominator + A.numerator*denominator ), numerator*A.numerator ); X.reduce(); return X; } public Fraction multiply( Fraction otherFraction ) { return new Fraction( otherFraction.getNumerator() * this.getNumerator(), otherFraction.getDenominator() * this.getDenominator() ); } public void print() { System.out.println(numerator + "/"+denominator); } public void printAsDouble() { double x= (double)numerator/(double)denominator;
  • 4. System.out.println(x); } private void reduce() int x=denominator < numerator? denominator:numerator; while(x>1) { if( (numerator%x==0) && (numerator%x==0)) break; x--; } denominator /=x; numerator /=x; }