Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(JAVA) Add an exception for divide by zero to your fraction class. Write a program to test it. Example Output: --------------------Configuration: -------------------- Please enter the

(JAVA)

Add an exception for divide by zero to your fraction class. Write a program to test it.

Example Output:

--------------------Configuration: --------------------

Please enter the numerator: 1

Please enter the denominator: 0

Denominator cannot be zero.

Please enter the numerator: Note, this must be handled by DenominatorIsZeroException.java.

__________________________ Write a program that reads and writes from binary or text files.

Example Output:

--------------------Configuration: --------------------

Enter the file name: kenb

Choose binary or text file(b/t): b

Choose read or write(r/w): w

Enter a line of information to write to the file: lasdklj

Would you like to enter another line? Y/N only

n

Continue? (y/n)y

Enter the file name: kenb

Choose binary or text file(b/t): b

Choose read or write(r/w): r

File contains: lasdklj

Continue? (y/n)y

Enter the file name: kent

Choose binary or text file(b/t): t

Choose read or write(r/w): w

Enter a line of information to write to the file: OOP

Would you like to enter another line? Y/N only

Y

Enter a line of information to write to the file:

Java, C++ not C. Would you like to enter another line? Y/N only n

Continue? (y/n)y

Enter the file name: kent

Choose binary or text file(b/t): t

Choose read or write(r/w):r

File contains:

OOP

Java, C++ not C.

Continue? (y/n)n

Process completed.

______________________________________________________

(Referenced Fraction program)

Complete Program:

// File: FractionADT.java public interface FractionADT { public int getNum(); public int getDenom(); public boolean equals(Fraction f); public String toString(); }

---------------------------------------------------------------------------------------

// File: Fraction.java public class Fraction implements FractionADT { private int num; private int den; public Fraction() { num = 0; den = 1; } public Fraction(int numerator, int denominator) { num = numerator; den = denominator; } public int getNum() { return num; } public int getDenom() { return den; } public boolean equals(Fraction f) { return getNum()* f.getDenom() == f.getNum()*getDenom(); } public String toString() { return num + "/" + den; } }

--------------------------------------------------------------------------------------------------------

// File: FractionTester.java import java.util.Scanner; public class FractionTester { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n1, d1, n2, d2; char choice; System.out.print("Enter the numerator for the target fraction: "); n1 = input.nextInt(); System.out.print("Enter the denominator for the target fraction: "); d1 = input.nextInt(); Fraction f1 = new Fraction(n1, d1); do { System.out.print(" Enter the numerator for the next fraction to test: "); n2 = input.nextInt(); System.out.print("Enter the denominator for the next fraction to test: "); d2 = input.nextInt(); Fraction f2 = new Fraction(n2, d2); if(f1.equals(f2)) System.out.println("The fraction you just entered equals the first fraction of " + f1 + "."); else System.out.println("The fraction you just entered does not equal the first fraction of " + f1 + "."); System.out.print("Would you like to continue (Y/N)? "); choice = input.next().charAt(0); }while(choice == 'y' || choice == 'Y'); System.out.println("Process completed."); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Second International Conference Acids Hue City Vietnam March 2010 Proceedings Part 1 Lnai 5990

Authors: Manh Thanh Le ,Jerzy Swiatek ,Ngoc Thanh Nguyen

2010th Edition

3642121446, 978-3642121449

More Books

Students also viewed these Databases questions