Question

Create a Ball enum with the following weights and diameters and must include methods of getCircumference()...

Create a Ball enum with the following weights and diameters and must include methods of getCircumference() and getVolume() to calculate the circumference and the volume of a ball (2 points)

BASEBALL(149, 73)

       BASKETBALL(624, 239)

       FOOTBALL(450 , 216)

       GOLF(46, 43)

       POOL(156, 60)

       SOFTBALL(180, 97)

       TENNIS(59, 64)

       VOLLEYBALL(260, 218)

Test the enum in the main by printing out the name of each ball with its weight and diameter (see example: print out of enum). In addition, print out the circumference of a golf and the volume of baseball in the main class (see example: print out of the circumference and the volume) (1 points)

              example: print out of enum

BASEBALL       149 g 73 mm

BASKETBALL     624 g 239 mm

FOOTBALL       450 g 216 mm

GOLF            46 g 43 mm

POOL           156 g 60 mm

SOFTBALL       180 g 97 mm

TENNIS          59 g 64 mm

       VOLLEYBALL     260 g 218 mm

              example: print out of the circumference and the volume

The circumferent of a golf = 135.02 mm

       The volume of a baseball = 203688.82 mm^3

Create a Rational class  (A rational no. is a number that can be expressed in the form of p/q, where p,q are integers with q !=0 and p and q have no common divisor) . Rational class must include methods of multiply(), divide(), plus(), minus() to do calculation with two rational numbers. In addition, include a method to simplify a rational number to its lowest form, and a toString() method for the lowest forms and its decimal form (see example: print out of the calculation) (4 points)

Test with the following 4 rational numbers in the main

r1 = new Rational(baseball weight, basketball weight)

r2 = new Rational(football weight, golf weight)

r3 = new Rational(pool diameter, softball diameter)

r4 = new Rational(tennis diameter, volleyball diameter)

             In addition, print & simplify the following rational number and calculations (2 points)

15 / 40

r1 + r2

r2 – r3

r3 * r4

r4 / r1

              example: print out of the calculation

15/40 = 3/8 = 0.38

(149/624) + (450/46) = 143827/14352 = 10.02

(450/46) - (60/97) = 20445/2231 = 9.16

(60/97) * (64/218) = 1920/10573 = 0.18

(64/218) / (149/624) = 19968/16241 = 1.23

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Ball enum

public enum Ball {

      // Ball type

      BASEBALL(149, 73), BASKETBALL(624, 239), FOOTBALL(450, 216), GOLF(46, 43), POOL(156, 60), SOFTBALL(180,

                  97), TENNIS(59, 64), VOLLEYBALL(260, 218);

      // Variables

      private int weight;

      private int diameter;

      /**

      * Parameterized constructor

      *

      * @param weight

      *            - weight of the ball

      * @param diameter

      *            - diameter of the ball

      */

      private Ball(int weight, int diameter) {

            this.weight = weight;

            this.diameter = diameter;

      }

     

      /**

      * Returns the weight of the ball

      */

      public int getWeight() {

            return weight;

      }

      /**

      * Returns the diameter of the ball

      */

      public int getDiameter() {

            return diameter;

      }

      /**

      * Returns the circumference of the ball

      */

      public double getCircumference() {

            return 3.14 * this.diameter;

      }

      /**

      * Returns the volume of the ball

      */

      public double getVolume() {

            double radius = this.diameter / 2.0;

            return ((4.0 / 3.0) * Math.PI * radius * radius * radius);

      }

      /**

      * Returns the string representation of ball

      */

      public String display() {

            return String.format("%-15s", name()) + "\t" + this.weight + " g " + this.diameter + " mm";

      }

}

/**

* This represents a Rational class. A rational no. is a number that can be

* expressed in the form of p/q, where p,q are integers with q !=0 and p and q

* have no common divisor

*/

import java.text.DecimalFormat;

public class Rational {

      // Instance variable

      private int numerator;

      private int denominator;

      /**

      * Default Constructor

      */

      public Rational() {

            this(1, 1);

      }

      /**

      * Parameterized constructor

      *

      * @param numerator

      *            - numerator of the Rational number

      * @param denominator

      *            - denominator of the Rational number

      */

      public Rational(int numerator, int denominator) {

            this.numerator = numerator;

            this.denominator = denominator;

      }

      /**

      * Simplifies this rational number to it simplest form

      */

      public void simplify() {

            if (this.numerator == this.denominator) {

                  this.numerator = 1;

                  this.denominator = 1;

            } else {

                  // Find minimum between numerator and denominator

                  int min = (this.numerator < this.denominator) ? this.numerator : this.denominator;

                  // Reduce the fraction by dividing the numerator and denominator by

                  // values [2 to min]

                  for (int i = 2; i <= min; i++) {

                        // Divide by i only if numerator and denominator, both, are

                        // divisible by i

                        while (((this.numerator % i) == 0) && ((this.denominator % i) == 0)) {

                              this.numerator /= i;

                              this.denominator /= i;

                        }

                  }

            }

      }

      /**

      * Returns the numerator

      */

      public int getNumerator() {

            return numerator;

      }

      /**

      * Returns the denominator

      */

      public int getDenominator() {

            return denominator;

      }

      /**

      * Returns the rational number in decimal form

      */

      public String decimalForm() {

            // Create decimal format to display double values

            DecimalFormat df = new DecimalFormat("##0.00");

           

            // Get decimal form

            double result = (getNumerator() * 1.0) / getDenominator();

                       

            return "" + df.format(result);

      }

      /**

      * Returns the simplified rational number as string (numerator /

      * denominator)

      */

      public String simplifiedForm() {

            // Create a new rational number having numerator and denominator as this

            // rational number

            Rational r = new Rational(this.getNumerator(), this.getDenominator());

            // Simplify r

            r.simplify();

            // Return the simplified form as string

            return r.getNumerator() + "/" + r.getDenominator();

      }

      /**

      * Returns the rational number as string (numerator / denominator)

      */

      @Override

      public String toString() {

            return getNumerator() + "/" + getDenominator();

      }

      /**

      * Performs the operation specified by the parameter

      *

      * @param r

      *            - rational number

      * @param operation

      *            - operation to be performed (+, -, *, /)

      * @return - Returns the result as a rational number

      */

      private Rational operate(Rational r, String operation) {

            Rational result = null;

            int n1 = this.getNumerator();

            int n2 = r.getNumerator();

            // Find result of operation

            if (operation.equals("*")) { // Multiply

                  result = new Rational(this.getNumerator() * r.getNumerator(), this.getDenominator() * r.getDenominator());

            } else if (operation.equals("/")) { // Divide

                  result = new Rational(this.getNumerator() * r.getDenominator(), this.getDenominator() * r.getNumerator());

            } else { // For addition and subtraction

                  if (this.getDenominator() != r.getDenominator()) {

                        // Cross multiply the fractions

                        // This way the denominators will be same

                        n1 = this.getNumerator() * r.getDenominator();

                        n2 = r.getNumerator() * this.getDenominator();

                  }

                  if (operation.equals("+"))

                        result = new Rational(n1 + n2, this.getDenominator() * r.getDenominator());

                  else if (operation.equals("-"))

                        result = new Rational(n1 - n2, this.getDenominator() * r.getDenominator());

            }

            return result;

      }

      /**

      * Adds rational number r to this rational number

      *

      * @param r

      *            - rational number to which this rational number is to be added

      */

      public Rational plus(Rational r) {

            return operate(r, "+");

      }

      /**

      * Subtracts rational number r from this rational number

      *

      * @param r

      *            - rational number which is to be subtracted from this rational

      *            number

      */

      public Rational minus(Rational r) {

            return operate(r, "-");

      }

      /**

      * Multiplies rational number r to this rational number

      *

      * @param r

      *            - rational number which is to be multiplied by this rational

      *            number

      */

      public Rational multiply(Rational r) {

            return operate(r, "*");

      }

      /**

      * Divides this rational number by rational number r

      *

      * @param r

      *            - rational number which divides this rational number

      */

      public Rational divide(Rational r) {

            return operate(r, "/");

      }

}

/**

* This class tests the Ball enum.

*/

import java.text.DecimalFormat;

public class EnumTest {

      public static void main(String[] args) {

            // Display all Ball

            for (Ball b : Ball.values()) {

                  System.out.println(b.display());

            }

            // Create decimal format to display double values

            DecimalFormat df = new DecimalFormat("##0.00");

            // Display circumference of a golf ball

            double circumference = Ball.GOLF.getCircumference();

            System.out.println("\nThe circumference of a golf = " + df.format(circumference) + " mm");

            // Display volume of a baseball

            double vol = Ball.BASEBALL.getVolume();

            System.out.println("\nThe volume of a baseball = " + df.format(vol) + " mm^3");

            // Test Rational number class

            System.out.println("\nTESTING RATIONAL CLASS");

           

            // Create a Rational Object

            Rational r = new Rational(15, 40);

           

            // Display rational number

            System.out.println(r + " = " + r.simplifiedForm() + " = "+ r.decimalForm());

            // Create rational numbers

            Rational r1 = new Rational(Ball.BASEBALL.getWeight(), Ball.BASKETBALL.getWeight());

            Rational r2 = new Rational(Ball.FOOTBALL.getWeight(), Ball.GOLF.getWeight());

            Rational r3 = new Rational(Ball.POOL.getDiameter(), Ball.SOFTBALL.getDiameter());

            Rational r4 = new Rational(Ball.TENNIS.getDiameter(), Ball.VOLLEYBALL.getDiameter());

           

            // r1 + r2

            Rational addResult = r1.plus(r2);

            System.out.println("(" + r1 + ") + (" + r2 + ") = " + addResult.simplifiedForm() + " = " + addResult.decimalForm());

           

            // r2 – r3

            Rational subResult = r2.minus(r3);

            System.out.println("(" + r2 + ") + (" + r3 + ") = " + subResult.simplifiedForm() + " = " + subResult.decimalForm());

           

            // r3 * r4

            Rational prodResult = r3.multiply(r4);

            System.out.println("(" + r3 + ") + (" + r4 + ") = " + prodResult.simplifiedForm() + " = " + prodResult.decimalForm());

           

            // r4 / r1

            Rational divResult = r4.divide(r1);

            System.out.println("(" + r4 + ") + (" + r1 + ") = " + divResult.simplifiedForm() + " = " + divResult.decimalForm());

      }

}

SAMPLE OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Create a Ball enum with the following weights and diameters and must include methods of getCircumference()...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can somone show me how to do the 1st problem? Need to find the LS and...

    Can somone show me how to do the 1st problem? Need to find the LS and SS for the fit and the LH and SH for the hole. Fits are all SHAFT BASIS METRIC but the shaft and hole diameters can not be used right out of the table. This is because the 3mm shaft tolerance does not match. You will need to lookup the "Fit" from the table, and then use the LS (Largest Shaft) and SS (Smallest Shaft)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT