Fraction Problem.

Added on - 21 Sep 2019

  • Dissertation


  • 26


  • 6874


  • 164


  • 0


Showing pages 1 to 6 of 26 pages
Fraction ProblemCreate a class called Fraction. Provide a constructor that takes 2 integers. Provide methods fortoStringsetFractionequalsaddsubtractmultipledividereduce - make sure you get the previous methods done before worrying about reduce.The following will be your starting point:package fractions;import java.util.*;public class Fraction{private Scanner scan = new Scanner(;private int num=1;private int denom=1;public Fraction(){}public Fraction(int n, int d){// Fill in code (good to use setFraction)}public void setFraction(int n, int d){//Fill in code ... don't forget to reduce}public Fraction add(Fraction op){//Fill in code ... don't forget to reduce// Algebra HINT: a/b + c/d = (a*d + b*c)/(b*d)}public Fraction subtract(Fraction op){//Fill in code ... don't forget to reduce// Algebra HINT: a/b - c/d = (a*d - b*c)/(b*d)}
public Fraction multiply(Fraction op){//Fill in code ... don't forget to reduce// Algebra HINT: a/b * c/d = (a*c)/ (b*d)}public Fraction divide(Fraction op){//Fill in code ... don't forget to reduce// Algebra HINT: a/b / c/d = (a*d)/ (b*c)}private void reduce(){// Pseudo code:// set smaller = minimum ( abs(num), abs(denom));// Loop through the possible divisors: 2, 3, 4, ... smaller// For each possible divisor:// while (num and denom are evenly divisible by divisor)// {// num /= divisor;// denom /= divisor;// smaller /= divisor;// }}public boolean equals(Fraction f){// Assuming all fractions are reduced// Fill in code}public String toString(){// Fill in code}public void readin(String label){while (true) // Keep trying if bad input is received{System.out.print(label);String temp =;temp = temp.trim(); // get rid of white space at the beginning andendint index = temp.indexOf('/');if (index >= 0){String numStr = temp.substring(0, index);String denomStr = temp.substring(index+1);int n = Integer.parseInt(numStr);int d = Integer.parseInt(denomStr);setFraction(n,d);return;}else
System.out.println("Input Fraction missing / ");}//Keep trying until you get it right}public static void main(String[] args){Fraction f1= new Fraction();Fraction f2= new Fraction();Fraction f3=null;Scanner scan = new Scanner(;while(true){System.out.println(); // Add a blank lineSystem.out.print("Enter operation: + - * / q (q ==> quit) : ");String input =;if (input.charAt(0) == 'q')break; // All donef1.readin("Enter Fraction 1: ");f2.readin("Enter Fraction 2: ");System.out.println("f1 = " + f1);System.out.println("f2 = " + f2);if (f1.equals(f2))System.out.println("f1 and f2 are equal");elseSystem.out.println("f1 and f2 are not equal");switch (input.charAt(0)){case '+':f3 = f1.add(f2);System.out.println("f1+f2=" + f3);break;case '-':f3 = f1.subtract(f2);System.out.println("f1-f2=" + f3);break;case '*':f3 = f1.multiply(f2);System.out.println("f1*f2="+f3);break;case '/':f3 = f1.divide(f2);System.out.println("f1/f2="+f3);break;default:System.out.println("Illegal command: " + input );break;}
}// end of while loopSystem.out.println("Bye");} // end of main}Please try to run the following Fraction calculations:(note the answers are filled with ______)Enter operation: + - * / q (q ==> quit) : +Enter Fraction 1: 1/3Enter Fraction 2: 4/6f1 = 1/3f2 = 2/3f1 and f2 are not equalf1+f2=_________Enter operation: + - * / q (q ==> quit) : +Enter Fraction 1: 5/25Enter Fraction 2: 10/50f1 = 1/5f2 = 1/5f1 and f2 are equalf1+f2=_________Enter operation: + - * / q (q ==> quit) : +Enter Fraction 1: 7/30Enter Fraction 2: 11/20f1 = 7/30f2 = 11/20f1 and f2 are not equalf1+f2=_________Enter operation: + - * / q (q ==> quit) : -Enter Fraction 1: 11/12Enter Fraction 2: 4/5f1 = 11/12f2 = 4/5f1 and f2 are not equalf1-f2=_________Enter operation: + - * / q (q ==> quit) : *Enter Fraction 1: 5/6Enter Fraction 2: 1/4f1 = 5/6f2 = 1/4f1 and f2 are not equalf1*f2=_________Enter operation: + - * / q (q ==> quit) : /
Enter Fraction 1: 6/7Enter Fraction 2: 1/12f1 = 6/7f2 = 1/12f1 and f2 are not equalf1/f2=_________Enter operation: + - * / q (q ==> quit) : qByestring_arrayCreate a package named string_array and write a program that creates an array of Strings withthe name of "firstNames".Fill the array with the names(in this order): "George", "Fred", "Sam", "Mary", "Sarah", "Bella","Joy", "Rita", "Marta", "Sue", "Nancy"Print the values of the firstNames array backwards. In otherwords, your output should be:Nancy Sue Marta Rita Joy Bella Sarah Mary Sam Fred GeorgeRun the program and insert the results in the appropriate section of the JH5 worksheet.date_arrayCreate a package named date_array and you will need 2 classes. Create a class called MyDate.This class only needs the following things:Instance variables for month, day and yearConstructor: MyDate(String month, int day, int year) - the code stores the values in 3instance variablesString toString() method - returns a String containing your 3 instance variables.
Create another class called DateArray that creates an array of "MyDate" with the name of"dateArr".The array should have 4 entries and the entries should be filled with MyDate classes representingthe dates:May 16, 1984November 14, 1978September 21, 1980July 3, 1987The DateArray class should print the values of the dateArr array backwards. Your MyDateclasses can be printed using a toString() method in the MyDate class.Run the program and insert the results in the appropriate section of the JH5 worksheet.one_dimensional_array problem (15 points):Create a package named one_dimensional_array and write a class that completes the following"OneDimensionalArrays" class. You will complete the class by filling in code wherever you seethe comment://******* FILL IN CODE *********import java.util.Scanner;public class OneDimensionalArrays {int[] createIntegers(int size_of_array){//******* FILL IN CODE *********// Your code will create an array of ints as large as specified insize_of_array// Fill the array in with the values: 0, 100, 200, 300, ....// Return the array that you just created}void printArray(int[] myArray){//******* FILL IN CODE *********// Print out your array with one number per line. Get the size of the// array from the "myArray" parameter (no hard coding the size)}
You’re reading a preview

To View Complete Document

Become a Desklib Library Member.
Subscribe to our plans

Unlock This Document