SlideShare a Scribd company logo
1 of 3
Download to read offline
Complete the following code segment to handle potential zero denominators. When there is a
zero denominator, use "zero denominator" as the message to pass to the exception object. You
may get the message from an exception object by using e.getMessage().
Sample run
Input:
Output:
Test
import java.util.Scanner;
import chapter11.fraction.Fraction;
import chapter11.fraction.exception.ZeroDenominatorException;
public class Test {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
try {
Fraction fraction = new Fraction(stdin.nextInt(), stdin.nextInt());
fraction.print();
fraction = new Fraction(stdin.nextInt(), stdin.nextInt());
fraction.print();
fraction = new Fraction(stdin.nextInt(), stdin.nextInt());
fraction.print();
fraction = new Fraction(stdin.nextInt(), stdin.nextInt());
fraction.print();
}
// code starts here
/code ends here
}
}
ZeroDenominatorException
package chapter11.fraction.exception;
// code starts here
// code ends here
Fraction
package chapter11.fraction;
import chapter11.fraction.exception.ZeroDenominatorException;
public class Fraction {
private int num;
private int denom;
// code starts here
// code ends here
public Fraction() throws ZeroDenominatorException {
this (1,1);
}
public Fraction add(Fraction frac) throws ZeroDenominatorException {
int n = this.num * frac.denom + this.denom * frac.num;
int d = this.denom * frac.denom;
return new (n, d);
}
public Fraction sub(Fraction frac) throws ZeroDenominatorException {
int n = this.num * frac.denom - this.denom * frac.num;
int d = this.denom * frac.denom;
return new (n, d);
}
public Fraction mult(Fraction frac) throws ZeroDenominatorException {
int n = this.num * frac.num;
int d = this.denom * frac.denom;
return new (n, d);
}
public Fraction div(Fraction frac) throws ZeroDenominatorException {
int n = this.num * frac.num;
int d = this.denom * frac.denom;
return new (n, d);
}
public void print() {
if (num != 0 && denom != 0) {
System.out.println(num + "/" + denom);
} else if (num ==0) {
System.out.print(0);
} else {
System.out.println("NaF");
}
}
public int getNum() {
return this.num;
}
public int getDenom() {
return this.denom;
}
}

More Related Content

Similar to Complete the following code segment to handle potential zero denomin.pdf

Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdfarshiartpalace
 
An input file A1-txt is given which contains a list of integer values-.docx
An input file A1-txt is given which contains a list of integer values-.docxAn input file A1-txt is given which contains a list of integer values-.docx
An input file A1-txt is given which contains a list of integer values-.docxlauracallander
 
JAVA Question : Programming Assignment
JAVA Question : Programming AssignmentJAVA Question : Programming Assignment
JAVA Question : Programming AssignmentCoding Assignment Help
 
Lab Task 2Use the below code and fill it in where appropriate to .pdf
Lab Task 2Use the below code and fill it in where appropriate to .pdfLab Task 2Use the below code and fill it in where appropriate to .pdf
Lab Task 2Use the below code and fill it in where appropriate to .pdfpmajanta
 
Throwing.javaimport java.util.InputMismatchException; import jav.pdf
Throwing.javaimport java.util.InputMismatchException; import jav.pdfThrowing.javaimport java.util.InputMismatchException; import jav.pdf
Throwing.javaimport java.util.InputMismatchException; import jav.pdfaquazac
 
PrimeRange.java import java.util.Scanner;public class PrimeRan.pdf
PrimeRange.java import java.util.Scanner;public class PrimeRan.pdfPrimeRange.java import java.util.Scanner;public class PrimeRan.pdf
PrimeRange.java import java.util.Scanner;public class PrimeRan.pdfAnkitchhabra28
 
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdfDigits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdfannapurnnatextailes
 

Similar to Complete the following code segment to handle potential zero denomin.pdf (11)

UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
STS4022 Exceptional_Handling
STS4022  Exceptional_HandlingSTS4022  Exceptional_Handling
STS4022 Exceptional_Handling
 
Lab4
Lab4Lab4
Lab4
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
 
An input file A1-txt is given which contains a list of integer values-.docx
An input file A1-txt is given which contains a list of integer values-.docxAn input file A1-txt is given which contains a list of integer values-.docx
An input file A1-txt is given which contains a list of integer values-.docx
 
JAVA Question : Programming Assignment
JAVA Question : Programming AssignmentJAVA Question : Programming Assignment
JAVA Question : Programming Assignment
 
Lab Task 2Use the below code and fill it in where appropriate to .pdf
Lab Task 2Use the below code and fill it in where appropriate to .pdfLab Task 2Use the below code and fill it in where appropriate to .pdf
Lab Task 2Use the below code and fill it in where appropriate to .pdf
 
Throwing.javaimport java.util.InputMismatchException; import jav.pdf
Throwing.javaimport java.util.InputMismatchException; import jav.pdfThrowing.javaimport java.util.InputMismatchException; import jav.pdf
Throwing.javaimport java.util.InputMismatchException; import jav.pdf
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
 
PrimeRange.java import java.util.Scanner;public class PrimeRan.pdf
PrimeRange.java import java.util.Scanner;public class PrimeRan.pdfPrimeRange.java import java.util.Scanner;public class PrimeRan.pdf
PrimeRange.java import java.util.Scanner;public class PrimeRan.pdf
 
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdfDigits.javapackage week04;import java.util.Scanner; public cla.pdf
Digits.javapackage week04;import java.util.Scanner; public cla.pdf
 

More from americanopticalscbe

Nanial ano 28 ic cinclo and hac the followinn inromo and ovnoncoc in .pdf
 Nanial ano 28 ic cinclo and hac the followinn inromo and ovnoncoc in .pdf Nanial ano 28 ic cinclo and hac the followinn inromo and ovnoncoc in .pdf
Nanial ano 28 ic cinclo and hac the followinn inromo and ovnoncoc in .pdfamericanopticalscbe
 
myList is an object of type ListFromOneString. myList currently con.pdf
 myList is an object of type ListFromOneString. myList currently con.pdf myList is an object of type ListFromOneString. myList currently con.pdf
myList is an object of type ListFromOneString. myList currently con.pdfamericanopticalscbe
 
Name Draw the front on the following sarface imap, using rgproprixte.pdf
 Name Draw the front on the following sarface imap, using rgproprixte.pdf Name Draw the front on the following sarface imap, using rgproprixte.pdf
Name Draw the front on the following sarface imap, using rgproprixte.pdfamericanopticalscbe
 
My private pilot training took place in 1977 in a Cessna 150 from Rya.pdf
 My private pilot training took place in 1977 in a Cessna 150 from Rya.pdf My private pilot training took place in 1977 in a Cessna 150 from Rya.pdf
My private pilot training took place in 1977 in a Cessna 150 from Rya.pdfamericanopticalscbe
 
Multiple-Step Income Statement On March 31,20Y4, the balances of the .pdf
 Multiple-Step Income Statement On March 31,20Y4, the balances of the .pdf Multiple-Step Income Statement On March 31,20Y4, the balances of the .pdf
Multiple-Step Income Statement On March 31,20Y4, the balances of the .pdfamericanopticalscbe
 
Multiple Choice decrease AD2 to AD1 Increase AS1 to AS2 Increase A.pdf
 Multiple Choice decrease AD2 to AD1 Increase AS1 to AS2 Increase A.pdf Multiple Choice decrease AD2 to AD1 Increase AS1 to AS2 Increase A.pdf
Multiple Choice decrease AD2 to AD1 Increase AS1 to AS2 Increase A.pdfamericanopticalscbe
 
Mycocepurus ant and social parasite Figure. A queen of the social par.pdf
 Mycocepurus ant and social parasite Figure. A queen of the social par.pdf Mycocepurus ant and social parasite Figure. A queen of the social par.pdf
Mycocepurus ant and social parasite Figure. A queen of the social par.pdfamericanopticalscbe
 
Multiple choice Select the correct answer from the list provided. 17.pdf
 Multiple choice Select the correct answer from the list provided. 17.pdf Multiple choice Select the correct answer from the list provided. 17.pdf
Multiple choice Select the correct answer from the list provided. 17.pdfamericanopticalscbe
 
Nash Inc s EPS is $7.27 and its dividend payout ratio is 0.7 . If th.pdf
 Nash Inc s EPS is $7.27 and its dividend payout ratio is 0.7 . If th.pdf Nash Inc s EPS is $7.27 and its dividend payout ratio is 0.7 . If th.pdf
Nash Inc s EPS is $7.27 and its dividend payout ratio is 0.7 . If th.pdfamericanopticalscbe
 
M�tiple Chaice Porffolio investment moitly represents the sole and pu.pdf
 M�tiple Chaice Porffolio investment moitly represents the sole and pu.pdf M�tiple Chaice Porffolio investment moitly represents the sole and pu.pdf
M�tiple Chaice Porffolio investment moitly represents the sole and pu.pdfamericanopticalscbe
 
Naren is dating a girl named Felicity. Naren and Felicity both cautio.pdf
 Naren is dating a girl named Felicity. Naren and Felicity both cautio.pdf Naren is dating a girl named Felicity. Naren and Felicity both cautio.pdf
Naren is dating a girl named Felicity. Naren and Felicity both cautio.pdfamericanopticalscbe
 
My code below is working fine but I just need help with the Unique.pdf
 My code below is working fine but I just need help with the Unique.pdf My code below is working fine but I just need help with the Unique.pdf
My code below is working fine but I just need help with the Unique.pdfamericanopticalscbe
 
Muliple Chaice a pice hugw than 56 a nice .pdf
 Muliple Chaice a pice hugw than 56 a nice .pdf Muliple Chaice a pice hugw than 56 a nice .pdf
Muliple Chaice a pice hugw than 56 a nice .pdfamericanopticalscbe
 
Comprehensive Illustration (Estimated Time 60 to 75 Minutes) On Jan.pdf
Comprehensive Illustration (Estimated Time 60 to 75 Minutes) On Jan.pdfComprehensive Illustration (Estimated Time 60 to 75 Minutes) On Jan.pdf
Comprehensive Illustration (Estimated Time 60 to 75 Minutes) On Jan.pdfamericanopticalscbe
 
Comprehensive Problem 1 - Part 1 Taxpayer information,2022 Form 104.pdf
Comprehensive Problem 1 - Part 1 Taxpayer information,2022 Form 104.pdfComprehensive Problem 1 - Part 1 Taxpayer information,2022 Form 104.pdf
Comprehensive Problem 1 - Part 1 Taxpayer information,2022 Form 104.pdfamericanopticalscbe
 
Comportamiento organizacional 2-1 Describa las dos formas princip.pdf
Comportamiento organizacional 2-1 Describa las dos formas princip.pdfComportamiento organizacional 2-1 Describa las dos formas princip.pdf
Comportamiento organizacional 2-1 Describa las dos formas princip.pdfamericanopticalscbe
 
Compare DNA sequence between species to build a phylogeny. There are.pdf
Compare DNA sequence between species to build a phylogeny. There are.pdfCompare DNA sequence between species to build a phylogeny. There are.pdf
Compare DNA sequence between species to build a phylogeny. There are.pdfamericanopticalscbe
 
Compare lists and tuples. Which one is not rightLists can be edi.pdf
Compare lists and tuples. Which one is not rightLists can be edi.pdfCompare lists and tuples. Which one is not rightLists can be edi.pdf
Compare lists and tuples. Which one is not rightLists can be edi.pdfamericanopticalscbe
 
Consider the following data for a closed economyY =$13trillio.pdf
Consider the following data for a closed economyY =$13trillio.pdfConsider the following data for a closed economyY =$13trillio.pdf
Consider the following data for a closed economyY =$13trillio.pdfamericanopticalscbe
 
Consider the following (AI-generated) response to the question Why .pdf
Consider the following (AI-generated) response to the question Why .pdfConsider the following (AI-generated) response to the question Why .pdf
Consider the following (AI-generated) response to the question Why .pdfamericanopticalscbe
 

More from americanopticalscbe (20)

Nanial ano 28 ic cinclo and hac the followinn inromo and ovnoncoc in .pdf
 Nanial ano 28 ic cinclo and hac the followinn inromo and ovnoncoc in .pdf Nanial ano 28 ic cinclo and hac the followinn inromo and ovnoncoc in .pdf
Nanial ano 28 ic cinclo and hac the followinn inromo and ovnoncoc in .pdf
 
myList is an object of type ListFromOneString. myList currently con.pdf
 myList is an object of type ListFromOneString. myList currently con.pdf myList is an object of type ListFromOneString. myList currently con.pdf
myList is an object of type ListFromOneString. myList currently con.pdf
 
Name Draw the front on the following sarface imap, using rgproprixte.pdf
 Name Draw the front on the following sarface imap, using rgproprixte.pdf Name Draw the front on the following sarface imap, using rgproprixte.pdf
Name Draw the front on the following sarface imap, using rgproprixte.pdf
 
My private pilot training took place in 1977 in a Cessna 150 from Rya.pdf
 My private pilot training took place in 1977 in a Cessna 150 from Rya.pdf My private pilot training took place in 1977 in a Cessna 150 from Rya.pdf
My private pilot training took place in 1977 in a Cessna 150 from Rya.pdf
 
Multiple-Step Income Statement On March 31,20Y4, the balances of the .pdf
 Multiple-Step Income Statement On March 31,20Y4, the balances of the .pdf Multiple-Step Income Statement On March 31,20Y4, the balances of the .pdf
Multiple-Step Income Statement On March 31,20Y4, the balances of the .pdf
 
Multiple Choice decrease AD2 to AD1 Increase AS1 to AS2 Increase A.pdf
 Multiple Choice decrease AD2 to AD1 Increase AS1 to AS2 Increase A.pdf Multiple Choice decrease AD2 to AD1 Increase AS1 to AS2 Increase A.pdf
Multiple Choice decrease AD2 to AD1 Increase AS1 to AS2 Increase A.pdf
 
Mycocepurus ant and social parasite Figure. A queen of the social par.pdf
 Mycocepurus ant and social parasite Figure. A queen of the social par.pdf Mycocepurus ant and social parasite Figure. A queen of the social par.pdf
Mycocepurus ant and social parasite Figure. A queen of the social par.pdf
 
Multiple choice Select the correct answer from the list provided. 17.pdf
 Multiple choice Select the correct answer from the list provided. 17.pdf Multiple choice Select the correct answer from the list provided. 17.pdf
Multiple choice Select the correct answer from the list provided. 17.pdf
 
Nash Inc s EPS is $7.27 and its dividend payout ratio is 0.7 . If th.pdf
 Nash Inc s EPS is $7.27 and its dividend payout ratio is 0.7 . If th.pdf Nash Inc s EPS is $7.27 and its dividend payout ratio is 0.7 . If th.pdf
Nash Inc s EPS is $7.27 and its dividend payout ratio is 0.7 . If th.pdf
 
M�tiple Chaice Porffolio investment moitly represents the sole and pu.pdf
 M�tiple Chaice Porffolio investment moitly represents the sole and pu.pdf M�tiple Chaice Porffolio investment moitly represents the sole and pu.pdf
M�tiple Chaice Porffolio investment moitly represents the sole and pu.pdf
 
Naren is dating a girl named Felicity. Naren and Felicity both cautio.pdf
 Naren is dating a girl named Felicity. Naren and Felicity both cautio.pdf Naren is dating a girl named Felicity. Naren and Felicity both cautio.pdf
Naren is dating a girl named Felicity. Naren and Felicity both cautio.pdf
 
My code below is working fine but I just need help with the Unique.pdf
 My code below is working fine but I just need help with the Unique.pdf My code below is working fine but I just need help with the Unique.pdf
My code below is working fine but I just need help with the Unique.pdf
 
Muliple Chaice a pice hugw than 56 a nice .pdf
 Muliple Chaice a pice hugw than 56 a nice .pdf Muliple Chaice a pice hugw than 56 a nice .pdf
Muliple Chaice a pice hugw than 56 a nice .pdf
 
Comprehensive Illustration (Estimated Time 60 to 75 Minutes) On Jan.pdf
Comprehensive Illustration (Estimated Time 60 to 75 Minutes) On Jan.pdfComprehensive Illustration (Estimated Time 60 to 75 Minutes) On Jan.pdf
Comprehensive Illustration (Estimated Time 60 to 75 Minutes) On Jan.pdf
 
Comprehensive Problem 1 - Part 1 Taxpayer information,2022 Form 104.pdf
Comprehensive Problem 1 - Part 1 Taxpayer information,2022 Form 104.pdfComprehensive Problem 1 - Part 1 Taxpayer information,2022 Form 104.pdf
Comprehensive Problem 1 - Part 1 Taxpayer information,2022 Form 104.pdf
 
Comportamiento organizacional 2-1 Describa las dos formas princip.pdf
Comportamiento organizacional 2-1 Describa las dos formas princip.pdfComportamiento organizacional 2-1 Describa las dos formas princip.pdf
Comportamiento organizacional 2-1 Describa las dos formas princip.pdf
 
Compare DNA sequence between species to build a phylogeny. There are.pdf
Compare DNA sequence between species to build a phylogeny. There are.pdfCompare DNA sequence between species to build a phylogeny. There are.pdf
Compare DNA sequence between species to build a phylogeny. There are.pdf
 
Compare lists and tuples. Which one is not rightLists can be edi.pdf
Compare lists and tuples. Which one is not rightLists can be edi.pdfCompare lists and tuples. Which one is not rightLists can be edi.pdf
Compare lists and tuples. Which one is not rightLists can be edi.pdf
 
Consider the following data for a closed economyY =$13trillio.pdf
Consider the following data for a closed economyY =$13trillio.pdfConsider the following data for a closed economyY =$13trillio.pdf
Consider the following data for a closed economyY =$13trillio.pdf
 
Consider the following (AI-generated) response to the question Why .pdf
Consider the following (AI-generated) response to the question Why .pdfConsider the following (AI-generated) response to the question Why .pdf
Consider the following (AI-generated) response to the question Why .pdf
 

Recently uploaded

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Recently uploaded (20)

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

Complete the following code segment to handle potential zero denomin.pdf

  • 1. Complete the following code segment to handle potential zero denominators. When there is a zero denominator, use "zero denominator" as the message to pass to the exception object. You may get the message from an exception object by using e.getMessage(). Sample run Input: Output: Test import java.util.Scanner; import chapter11.fraction.Fraction; import chapter11.fraction.exception.ZeroDenominatorException; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); try { Fraction fraction = new Fraction(stdin.nextInt(), stdin.nextInt()); fraction.print(); fraction = new Fraction(stdin.nextInt(), stdin.nextInt()); fraction.print(); fraction = new Fraction(stdin.nextInt(), stdin.nextInt()); fraction.print(); fraction = new Fraction(stdin.nextInt(), stdin.nextInt()); fraction.print(); } // code starts here /code ends here } } ZeroDenominatorException package chapter11.fraction.exception; // code starts here // code ends here Fraction package chapter11.fraction; import chapter11.fraction.exception.ZeroDenominatorException;
  • 2. public class Fraction { private int num; private int denom; // code starts here // code ends here public Fraction() throws ZeroDenominatorException { this (1,1); } public Fraction add(Fraction frac) throws ZeroDenominatorException { int n = this.num * frac.denom + this.denom * frac.num; int d = this.denom * frac.denom; return new (n, d); } public Fraction sub(Fraction frac) throws ZeroDenominatorException { int n = this.num * frac.denom - this.denom * frac.num; int d = this.denom * frac.denom; return new (n, d); } public Fraction mult(Fraction frac) throws ZeroDenominatorException { int n = this.num * frac.num; int d = this.denom * frac.denom; return new (n, d); } public Fraction div(Fraction frac) throws ZeroDenominatorException { int n = this.num * frac.num; int d = this.denom * frac.denom; return new (n, d); } public void print() { if (num != 0 && denom != 0) { System.out.println(num + "/" + denom); } else if (num ==0) { System.out.print(0); } else { System.out.println("NaF"); }
  • 3. } public int getNum() { return this.num; } public int getDenom() { return this.denom; } }