Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

At the beginning, we practice how to compile and execute a simple program with only main() method. Next we start with a gcd algorithm and

At the beginning, we practice how to compile and execute a simple program with only main() method. Next we start with a gcd algorithm and code it to be a while loop in the main() method. The gcd codes with 3 parts input , process, and output are moved out to Utilility while the main is shortened by a calling instruction Utility.findGCD(int m, int n) with a output return. The new type (object type) of Fraction is created with 3 boxes: Attributes, Constructor(s), and methods especially the toString() to print the attributes of an object. The analysis of many possible constructors are introduced to allow users to create the fractions with different types of input. Moreover the toString() can be modified to display the special fractions in different forms. Project 1 is a continuation of Step 5 The input from keyboard should be moved to Utility The new constructor Fraction(String s) should be created by calling Step 6a, and parsing code of string s from main should be moved to this constructor. Finally, the toString() is modified in parallel to Step 6b.

Project 1: Do all cases of fractions in this page.

""

*"2/6" *"2 /6" "2 / 6" Already done in code.

"4" "20/5" "1 1/2" " 10/15" //char 0

(Test.java, Fraction.java[should work with the fractions], and Utility.java[should contain findGCD])

Have to write code for the remaining fraction. on the test.java.

All fractions should work in Test.java and Fraction.java.

I've already started code on all three java documents just need to finish. Please help wring code for remaining fractions that are not in bold letters. All classes must be linked together ....

here is the following java documents:

//filename: test.java

classs Test { public static void main(String[] args) { /* int i; //declaration i = 10; //init System.out.println("i = " + i);

Fraction f; //init f = new Fraction(2,6); System.out.println(f.num + "/" + f.den);

System.out.println("f = "+ f);

Fraction g = new Fraction(); System.out.println(g" ="+g); */

Scanner myObj = new Scanner(System.in);

System.out.print("Enter a fraction>"); String sFraction = myObj.nextLine(); System.out.println("Fraction= |" + sFract+"|");

//extract num and den to call constructor #1 //trim sFract = sFract.trim(); System.out.println(TrimFraction= |" + sFract+"|");

int pos = sFract.indexOf('/'); System.out.println("position= |" + pos+"|");

String sNum = sFract.substring(0, pos); sNum = sNum.trim(); System.out.println("sNum= |" + sNum+"|");

int inum = Integer.parseInt(sNum); System.out.println("iNum= |" + inum+"|");

String sDen = sFract.substring(pos+1); sDen = sDen.trim(); int iden = Integer.parseInt(Sden); System.out.println("iDum= |" + iden+"|");

Fraction f = new Fraction(inum,iden); System.out.println("f= "+ f); /* Fraction h= new Fraction(" 2 5/4 "); System.out.println("h= "+ h); //output 3 1/4 */ } }

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

//filename: fraction.java

class Fraction //new type //box:declaration (Attributes) //int num; //numerator //int den; //denominator

//box: contructors: initialize the new type Fraction( int m, int n){ //int g =Utility.findGCD(m,n); //num = m/g ; den = n/g

num =m; den =n; }

Fraction(int m){num = m; den = 1;}

Fraction(){num = 0; den =1;0 //no input

//Fraction(String s){ }//via keyboard

//methods static Fraction add(Fraction f, Fraction g){ Fraction h; h = new Fraction(f.num* g.den + g.num * f.den, f.den * g.den ); return h; } public String toString(){ String s; if (den ===1) s = ""+num; else s = num+"/"den; return s; }

}//class

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

//filename: util.java

class Utility {

static int findGCD(int i, int j){ //find gcd of 2 numbers i and j //the larger number is reduced by the smaller //repeat until they are the same while (i != j){ if (i > j)i = i-j; else j = j-i; }//while return i; }//find

public static void main(String args[]){

int i=10; int j=15: System.out.println.("gcd = " + findGCD(i,j) );

}

}//class

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

Database Processing

Authors: David M. Kroenke

12th Edition International Edition

1292023422, 978-1292023427

More Books

Students also viewed these Databases questions