(JAVA) Add an exception for divide by zero to your fraction class. Write a progr
ID: 3723468 • Letter: #
Question
(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.");
}
}
Explanation / Answer
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.omg.CORBA.portable.OutputStream;
public class Classifier {
public static void main(String args[]) throws IOException {
boolean flag=true;
while(flag)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter file name");
String filename=sc.nextLine();
System.out.println("Choose binary or text file(b/t)");
char ch=sc.next().charAt(0);
if(ch=='t')
{
System.out.println("Choose read or write(r/w):");
char rw=sc.next().charAt(0);
if(rw=='w')
{
File fout = new File(filename);
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
for (;; ) {
System.out.println("Enter a line of information to write to the file:");
Scanner sc1=new Scanner(System.in);
String s=sc1.nextLine();
bw.write(s);
bw.newLine();
System.out.println("Would you like to enter another line? Y/N only");
char ch1=sc1.next().charAt(0);
if(ch1=='y')
continue;
else
break;
}
bw.close();
}
else
{
FileInputStream fis = null;
BufferedReader reader = null;
try {
fis = new FileInputStream(filename);
reader = new BufferedReader(new InputStreamReader(fis));
System.out.println("Reading File line by line using BufferedReader");
String line = reader.readLine();
while(line != null){
System.out.println(line);
line = reader.readLine();
}
} catch (FileNotFoundException ex) {
// Logger.getLogger(Classifier.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
// Logger.getLogger(Classifier.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
reader.close();
fis.close();
} catch (IOException ex) {
Logger.getLogger(Classifier.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
else
{
System.out.println("Choose read or write(r/w):");
char rw=sc.next().charAt(0);
if(rw=='w')
{
FileOutputStream out = null;
try{
out = new FileOutputStream(filename);
for (;; ) {
System.out.println("Enter a line of information to write to the file:");
Scanner sc11=new Scanner(System.in);
String ss=sc11.nextLine();
byte[] totalBytes = ss.getBytes();
out.write(totalBytes);
System.out.println("Would you like to enter another line? Y/N only");
char ch1=sc11.next().charAt(0);
if(ch1=='y')
continue;
else
break;
}
}
finally{
if(out !=null)
out.close();
}
}
else
{
try {
byte[] buffer = new byte[1000];
FileInputStream inputStream =
new FileInputStream(filename);
int total = 0;
int nRead = 0;
while((nRead = inputStream.read(buffer)) != -1) {
System.out.println(new String(buffer));
total += nRead;
} inputStream.close();
}
catch(FileNotFoundException ex) {
}
catch(IOException ex) {
}
}
}
System.out.println("Continue(y/n)");
Scanner ssss=new Scanner(System.in);
char re=ssss.next().charAt(0);
if(re=='n')
flag=false;
}
System.out.println("Process Completed");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.