Chapter 4 Programming Assignment 7
Write an application, RationalOperations_YI.java that lets the user add, subtract, multiply, or divide two fractions. Use the Rational class in your implementation. Implement the driver class to test all operations. Have a loop prompting the user for operators and operands.
//******************************************************************** // RationalNumbers.java Author: Lewis/Loftus/Cocking // BeRational Assignment: change this driver so the user can input Rational // numbers and selects the operations // Prompt the user for the rational RationalNumbers // Prompt the user for the operations // Driver to exercise the use of multiple Rational objects. //******************************************************************** public class RationalNumbers { //----------------------------------------------------------------- // Creates some rational number objects and performs various // operations on them. //----------------------------------------------------------------- public static void main (String[] args) { Rational r1 = new Rational (6, 8); Rational r2 = new Rational (1, 3); Rational r3, r4, r5, r6, r7; System.out.println ("First rational number: " + r1); System.out.println ("Second rational number: " + r2); if (r1.equals(r2)) System.out.println ("r1 and r2 are equal."); else System.out.println ("r1 and r2 are NOT equal."); r3 = r1.reciprocal(); System.out.println ("The reciprocal of r1 is: " + r3); r4 = r1.add(r2); r5 = r1.subtract(r2); r6 = r1.multiply(r2); r7 = r1.divide(r2); System.out.println ("r1 + r2: " + r4); System.out.println ("r1 - r2: " + r5); System.out.println ("r1 * r2: " + r6); System.out.println ("r1 / r2: " + r7); } }
Chapter 4 Programming Assignment 8
Modify the Student class (Listing 4.13) with the following changes:
a. Each student object also contains the scores for three tests.
b. Provide a constructor that sets all instance values based on parameter values. Overload the constructor so that each test score starts out at zero.
c. Provide a method called setTestScore that accepts two parameters: the test number (1 through 3) and the score.
d. Provide a method called getTestScore that accepts the test number and returns the score.
e. Provide a method called average that computes and returns the average test score for this student.
f. Modify the toString method so that the test scores and average are included in the description of the student.
g. Modify the driver class main method to exercise the new Student methods.