Solved–Assignment 9 –Solution

$35.00 $24.00

Preparation   Read Lesson 8   Read text chapter 8. This week you really do need to read all of it, except maybe 8.16.   For Credit   Assignment 9.1 [15 points]   Complete all of the MyProgrammingLab exercises for chapter 8.   Assignment 9.2 [80 points]   Write a Fraction class whose objects will…

You’ll get a: . zip file solution

 

 
Categorys:

Description

5/5 – (2 votes)

Preparation

 

  1. Read Lesson 8

 

  1. Read text chapter 8. This week you really do need to read all of it, except maybe 8.16.

 

For Credit

 

Assignment 9.1 [15 points]

 

Complete all of the MyProgrammingLab exercises for chapter 8.

 

Assignment 9.2 [80 points]

 

Write a Fraction class whose objects will represent fractions. You should provide the following member functions:

 

  1. Two constructors, a parameter-less constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction.

 

  1. Arithmetic operations that add, subtract, multiply, and divide Fraction These should be implemented as value returning methods that return a Fraction object. They should be

named addedTo, subtract, multipliedBy, and dividedBy.

 

  1. A boolean operation named isEqualTo that compares two Fraction objects for equality.

 

  1. An output operation named print that displays the value of a Fractionobject on the screen in the form numerator/denominator.

 

Your class should have exactly two private data members, one to represent the numerator of the fraction being represented, and one to represent the denominator of the fraction being represented.

 

When adding or subtracting Fractions, remember that you must first find the common denominator. The easy way to do this is to multiply the denominators together and use that product as the common denominator.

 

I am providing a client program for you below. You should copy and paste this into a file and use it as your client program. The output that should be produced when the provided client program is run with your class is also given below, so that you can check your results. Since you are not writing the client program, you are not required to include comments in it.

 

I strongly suggest that you design your class incrementally. For example, you should first implement only the constructors and the output function, and then test what you have so far. Once this code has been thoroughly debugged, you should add additional member functions, testing each one thoroughly as it is added. You might do this by creating your own client program to test the code at each stage; however, it would probably be better to use the provided client program and comment out code that relates to member functions that you have not yet implemented.

 

As you can see from the sample output given below, you are not required to change improper fractions into mixed numbers for printing. Just print it as an improper fraction. You are, however, required to reduce fractions, as illustrated in the sample output. Make sure that your class will reduce ANY fraction, not just the fractions that are tested in the provided client program. Fractions should not be simply reduced upon output, they should be stored in reduced form at all times. In other words, you should ensure that all fraction objects are reduced before the end of any member function. You are also not required to deal with negative numbers, either in the numerator or the denominator.

 

You must create your own algorithm for reducing fractions. Don’t look up an already existing algorithm for reducing fractions or finding GCF. The point here is to have you practice solving the problem on your own. In particular, don’t use Euclid’s algorithm. Don’t worry too much about efficiency, just create something of your own that works correctly on ANY fraction.

 

Here is the client program. It must be in a separate file from your class.

 

public class ChangeThisClassName {

public static void main(String[] args) {

Fraction f1 = new Fraction(9,8);

Fraction f2 = new Fraction(2,3);

 

Fraction result = new Fraction();

 

System.out.print(“The result starts off at “);

result.print();

 

System.out.println();

 

System.out.print(“The product of “);

f1.print();

System.out.print(” and “);

f2.print();

System.out.print(” is “);

result = f1.multipliedBy(f2);

result.print();

System.out.println();

 

System.out.print(“The quotient of “);

f1.print();

System.out.print(” and “);

f2.print();

System.out.print(” is “);

result = f1.dividedBy(f2);

result.print();

System.out.println();

 

System.out.print(“The sum of “);

f1.print();

System.out.print(” and “);

f2.print();

System.out.print(” is “);

result = f1.addedTo(f2);

result.print();

System.out.println();

 

System.out.print(“The difference of “);

f1.print();

System.out.print(” and “);

f2.print();

System.out.print(” is “);

result = f1.subtract(f2);

result.print();

System.out.println();

 

if (f1.isEqualTo(f2)){

System.out.println(“The two Fractions are equal.”); } else {

System.out.println(“The two Fractions are not equal.”);

 

}

 

Fraction f3 = new Fraction(12, 8);

Fraction f4 = new Fraction(202, 303);

result = f3.multipliedBy(f4);

System.out.print(“The product of “);

f3.print();

System.out.print(” and “);

f4.print();

System.out.print(” is “);

result.print();

System.out.println();

 

}

}

 

This client should produce the output shown here:

 

The result starts off at 0/1

The product of 9/8 and 2/3 is 3/4

The quotient of 9/8 and 2/3 is 27/16

The sum of 9/8 and 2/3 is 43/24

The difference of 9/8 and 2/3 is 11/24

The two fractions are not equal.

 

The product of 3/2 and 2/3 is 1/1

 

You may not change the client program in any way, except that you may change the name of the main class. Changing the client program in any other way will result in a grade of 0 on the assignment.

 

Submit Your Work

 

Execute the program and copy/paste the output into the bottom of your Fraction.java file, making it into a comment. Send an email to ta.sanmateo@gmail.com with the subject “CIS 254 a9”. Attach your source code file to the email. Don’t submit the client program. In the body of the email let me know whether the programs work as required.

 

Keep in mind that if your code does not compile you will receive a 0.

 

Note that the gmail email account is for homework submission only. The account is not monitored for any other purpose.

 

Assignment 9.3 [5 points]

 

Participate in the assignment 9 discussion. This could involve asking a question, answering another student’s question, giving an example of something that you struggled with and then overcame (or didn’t!), giving an example of something you found particularly cool, or any other constructive way you can think of to participate.