Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please help with the following Java programs . We are using regular notepad and running programs on command prompt. here are the instructions

Can you please help with the following Java programs. We are using regular notepad and running programs on command prompt. here are the instructions I was given for Homework: In have included what I've done so far I need help finishing the programs. Thank in advance for your help.

Create a program that reduces fractions. Link all three classes together. test,java, fraction.java, and utility.java.

Do not duplicate code together , as you can see on my code in test.java i have already added the first portion of the Homework which is the fraction "2/6". I need to create additional programs in the test.java to run the following "4" whole number, "20/5" fraction, "1 l/2" Improper Fraction (It's 1 and the letter l/2 not the number 1) , and " 10/15" (0 is the char 0 not number). Using (Scanner):String Methods as I did with the first fraction 2/6. All thee classes need to be linked and both the test.java and fraction.java should work with all the fractions. Not sure if my utility.java is correct or not please help. "" *"2/6" *"2 /6" "2 / 6" "4" "20/5" "1 1/2" " 10/15" //char 0

Output (display) -simple forms "2/4" => 1/2 "5 2/6" => 5 1/3 " 4/3 " => 1 1/3

Project 1: Do all cases in this page. Submit your zip file of the sources (Test.java, Fraction.java[should work with the fractions], and Utility.java[should contain findGCD])

image text in transcribedimage text in transcribedimage text in transcribed

//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 /ew type //box:declaration (Attributes) //int num; /umerator //int den; //denominator

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

num =m; den =n; }

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

Fraction(){num = 0; den =1;0 /o 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

testjava Notepad Fit Edit Format View Help class Test public static void main(String U args) { int i; //declaration 1 - 19; //init System.out.println(""+ 1); Fraction f; /it f-new Fraction (2,6); System.out.println(f.nu/f.den); System.out.println("f); Fractions - new Fraction(); System.out.println("+ B); Scanner myOb) - Scanner(System.in); System.out.println("Enter a Fraction); String sFrac - myObj.nextLine(); System.out.printin("Fractions Fract"1"); Wextract num and den to call constructor 81 Altrim 1/5FracsFract.trim(); System.out.println("Trinfractions Fract:"1"); Int possFract.indexOf("/"); System.out.println("position 1. pos+"|"). String som - Fract.substringco, pos); - Montrim(); System.out.println("1" st"); int inum - Integer.parseInt(s); System.out.printiniuinus"); String Den - Fract.substring(pos+1); Den - Dentis) Int den - Integer.parseInt(s); System.out.printinium "Ident" "); Fraction new Fractionine, den); System.out.println(".0); W/ Class El Window X *Utility.java - Notepad File Edit Format View Help //path C:\Program Files\Java\jdk-15.0.2 bin 17 filename: Utility.java class Utility { static int findGCD(int i, int i) { // find god of two numbers i and j // the larger number is reduced by the smaller H/ repeat until they are the same while (i !- ) { if (i >j) i -i-; else j - j-i; } return i; } public static void main(String args[]) { int i = 18; int - 15; System.out.println ("ged + findGCD(1,1)); } In 3 Col 1 100% Windows (CRLA "Fraction.java - Notepad File Edit Format View Help declaration: int i; char o double d; primitive HOW TO CREATE NEWTYPE (object type) Java Prog */ class Fraction{ /ew type //box: declarations (Attributes) int num; /umerator int den; //denominator //box: contructors: initialize the new type Fraction(int m, int n){ nummi den - }//constructor Fraction(int m){ num-m; den - 1; //constructor //method public String toString() { String s; if (den 1-1) 5. num.""+den; else 3*** *num; returns } }//class in 1 Col 1 100% Windows (CRL UTH testjava Notepad Fit Edit Format View Help class Test public static void main(String U args) { int i; //declaration 1 - 19; //init System.out.println(""+ 1); Fraction f; /it f-new Fraction (2,6); System.out.println(f.nu/f.den); System.out.println("f); Fractions - new Fraction(); System.out.println("+ B); Scanner myOb) - Scanner(System.in); System.out.println("Enter a Fraction); String sFrac - myObj.nextLine(); System.out.printin("Fractions Fract"1"); Wextract num and den to call constructor 81 Altrim 1/5FracsFract.trim(); System.out.println("Trinfractions Fract:"1"); Int possFract.indexOf("/"); System.out.println("position 1. pos+"|"). String som - Fract.substringco, pos); - Montrim(); System.out.println("1" st"); int inum - Integer.parseInt(s); System.out.printiniuinus"); String Den - Fract.substring(pos+1); Den - Dentis) Int den - Integer.parseInt(s); System.out.printinium "Ident" "); Fraction new Fractionine, den); System.out.println(".0); W/ Class El Window X *Utility.java - Notepad File Edit Format View Help //path C:\Program Files\Java\jdk-15.0.2 bin 17 filename: Utility.java class Utility { static int findGCD(int i, int i) { // find god of two numbers i and j // the larger number is reduced by the smaller H/ repeat until they are the same while (i !- ) { if (i >j) i -i-; else j - j-i; } return i; } public static void main(String args[]) { int i = 18; int - 15; System.out.println ("ged + findGCD(1,1)); } In 3 Col 1 100% Windows (CRLA "Fraction.java - Notepad File Edit Format View Help declaration: int i; char o double d; primitive HOW TO CREATE NEWTYPE (object type) Java Prog */ class Fraction{ /ew type //box: declarations (Attributes) int num; /umerator int den; //denominator //box: contructors: initialize the new type Fraction(int m, int n){ nummi den - }//constructor Fraction(int m){ num-m; den - 1; //constructor //method public String toString() { String s; if (den 1-1) 5. num.""+den; else 3*** *num; returns } }//class in 1 Col 1 100% Windows (CRL UTH

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 Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago