Add an exception for divide by zero to your fraction class. Write a program to t
ID: 3931135 • Letter: A
Question
Add an exception for divide by zero to your fraction class. Write a program to test it.
Example Output:
--------------------Configuration: <Default>--------------------
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: <Default>--------------------
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.
Explanation / Answer
import java.lang.ArithmeticException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class DivideByZeroException
{
public class Divide extends ArithmeticException
{
String message;
public Divide( String message )
{
this.message = message;
}
public String toString()
{
return( message );
}
}
public static void main(String[] args)
{
DivideByZeroException demo = new DivideByZeroException();
demo.go();
}
public void go()
{
Scanner s = new Scanner(System.in);
int num, deno, ans;
try
{
System.out.print("Please enter the numerator: ");
num = s.nextInt();
System.out.println("Please enter the denomenator: ");
deno = s.nextInt();
ans = quotient( num, deno );
System.out.println( "The answer is " + ans );
}
catch ( InputMismatchException ime )
{
System.out.println("Please enter an integer");
}
catch ( Divide asd )
{
System.out.println( asd.toString() );
}
}
public int quotient( int num, int deno) throws Divide
{
if ( deno == 0 )
throw new Divide("Denominator cannot be zero.");
return ( num / deno );
}
}
2)
#include <stdio.h>
#include <string.h>
int main()
{
char fname[20],type,operation,input[100],c;
FILE *fp;
printf("Enter the file name: ");
scanf(" %s",fname);
printf("Choose binary or text file(b/t): ");
scanf(" %c",&type);
if(type=='b')
{
char ch='y';
strcat(fname,".bin");
printf("Choose read or write (r/w): ");
scanf(" %c",&operation);
if(operation=='w')
{
fp=fopen(fname,"wb");
if(fp == NULL)
printf("Cannot open file!");
do{
printf("Enter a line of information to write to the file: ");
scanf(" %s",input);
fwrite(input, sizeof(input[0]), sizeof(input)/sizeof(input[0]), fp);
printf("Would you like to enter another line (y/n): ");
scanf(" %c",&ch);
}while(ch!='n');
fclose(fp);
}
else if(operation=='r')
{
fp=fopen(fname,"rb");
if (fp == NULL)
printf("Cannot open file!");
c = fgetc(fp);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fp);
}
fclose(fp);
}
}
else if (type=='t')
{
char ch='y';
strcat(fname,".txt");
printf("Choose read or write (r/w): ");
scanf(" %c",&operation);
if(operation=='w')
{
fp=fopen(fname,"w");
if(fp == NULL)
printf("Cannot open file!");
do{
printf("Enter a line of information to write to the file: ");
scanf(" %s",input);
fprintf(fp," %s", input);
printf("Would you like to enter another line (y/n): ");
scanf(" %c",&ch);
}while(ch!='n');
fclose(fp);
}
else if(operation=='r')
{
fp=fopen(fname,"r");
if (fp == NULL)
printf("Cannot open file!");
c = fgetc(fp);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fp);
}
fclose(fp);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.