Question: When a programming language does not support a needed data type, a programmer must create an abstract data type (ADT). Examples of data types that

When a programming language does not support a needed data type, a programmer must create an abstract data type (ADT). Examples of data types that might have to be created are Complex Numbers (a + bi) or Fraction (n/d).

These abstract data types created by a programmer would be released to the user community to be used like Integer or Double. That means a class would be created that implements the desired ADT.

You are to create an ADT Fraction in its own file.

Fraction must consist of:

2 state variables num (numerator) and den (denominator).

a default and 2 parameter override constructor so you can use new Fraction(); or new Fraction(a,b);

a toString() that produces output like 1/2

an equals(Fraction f) that compares using: return getNum()*f.getDenom() == f.getNum()*getDenom();

Write an appropriate test program in a separate file. Your test program should read in numerators and denominators and create Fraction instances for comparison

This is my code for the ADT and then the tester in that order... I keep getting compile and run errors.

package fractionadt;

/**

*

* @author Lindsay Cook

*/

public class FractionADT {

int num1;

int denom1;

public FractionADT(int a, int b){

this.num1 = a;

this.denom1 = b;

}

public int getNum1() {

return num1;

}

public int getDenom1() {

return denom1;

}

@Override

public String toString() {

return this.num1 + "/"+ this.denom1;

}

public boolean equals(int num2,int denom2){

if(getNum1()*denom2 == getDenom1()*num2){

return true;

}

else{

return false;

}

}

}

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

package fractionadt;

/**

*

* @author Lindsay Cook

*/

import java.util.Scanner;

public class FractionTester {

static Scanner reader = new Scanner(System.in);

static FractionTester f ;

public static void main(String[] args) {

System.out.println("Enter the numerator for the target fraction: ");

int a = reader.nextInt();

System.out.println("Enter the denominator for the target fraction:");

int b = reader.nextInt();

char e;

f = new Fraction (a , b);

do{

compare();

System.out.println("Would you like to continue (Y/N)?");

e = reader.next().charAt(0);

}while(e =='Y');

if(e =='N'){

System.out.println("Process Completed");

}

}

public static void compare(){

System.out.println("Enter the numerator for the next fraction to test: ");

int c = reader.nextInt();

System.out.println("Enter the denominator for the next fraction to test: ");

int d = reader.nextInt();

if(f.equals(c, d)){

System.out.println(f.toString()+"The fraction you just entered equals the first fraction of:");

}

else{

System.out.println("The fraction you just entered does not equal the first fraction of:"+f.toString());

}

}

private boolean equals(int c, int d) {

throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

}

private static class Fraction extends FractionTester {

public Fraction(int a, int b) {

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!