Question: Consider a simple OOP application which defines a PythogoreanTriple class and tests it with a driver. A Pythagorean Triple is a set of three numbers

Consider a simple OOP application which defines a PythogoreanTriple class and tests it with a driver. A Pythagorean Triple is a set of three numbers which can be the three sides of a right triangle. In this example, it is possible for two instances of my PythTriple class to be equivalent, but with side A and side B reversed, but this is not important for this purpose.

PythTriple.java:

package pythex;

public class PythTriple { private double sideA; private double sideB; private double hyp;

public void inputTwoSides(double sideAIn, double sideBIn) { sideA = sideAIn; sideB = sideBIn; hyp = Math.sqrt(Math.pow(sideA, 2) + Math.pow(sideB, 2)); }

public void inputOneSideAndHyp(double sideAIn, double hypIn) { sideA = sideAIn; hyp = hypIn; sideB = Math.sqrt(Math.pow(hyp, 2) - Math.pow(sideA, 2)); }

@Override public String toString() { return "PythTriple [sideA=" + String.format("%5.3f", sideA) + ", sideB=" + String.format("%5.3f", sideB) + ", hyp=" + String.format("%5.3f", hyp) + "]"; }

}

OOPPythDriver.java:

package pythex;

public class OOPPythDriver { public static void main(String[] args) { PythTriple[] pyths = new PythTriple[3]; PythTriple pt1 = new PythTriple(); pt1.inputOneSideAndHyp(3,5); PythTriple pt2 = new PythTriple(); pt2.inputOneSideAndHyp(1, Math.sqrt(2)); PythTriple pt3 = new PythTriple(); pt3.inputTwoSides(2, 3); pyths[0] = pt1; pyths[1] = pt2; pyths[2] = pt3; for(PythTriple p: pyths) System.out.println(p); } }

Paste this code into your Eclipse workspace and run it to see what it does, and read the code until you understand it.

Note that this small application relies heavily on OOP:

a) The data is represented using a Java class, with one instance of the class representing one Pythagorean triple.

b) The driver uses several of PythagoreanTriple's instance methods.

Assignment: Rewrite this application in Java as follows. All Java code must be in a class, but do not use OOP techniques beyond that.

  • All your code should be in a single class, ImpPythTripEx.java, in a code package called pythex
  • All methods and variables in your code must be public and static.
  • You may not use a separate PythTriple class.
  • Represent the Pythagorean triples with a 3 x 3 array of doubles called triples (public static double[][] triples = new double[3][3]). Each row should contain the values for the side A, side B, and hypotenuse of a Pythagorean Triple.
  • Write a static method similar to the inputTwoSides method in the OOP version. This method should take values for side A, side B, and an array row index, calculate the hypotenuse, and add the values to the 2D array at the specified row. For example, if this method is called with parameters 3, 4, and 0, it will set triples[0][0] to 3, triples[0][1] to 4, and triples[0][0] to 5.
  • Write a static method similar to the inputOneSideAndHyp method in the OOP version. The bullet point above should give you an idea how it should work.
  • Write a main() method that uses the *same examples* used in my driver class and generates output formatted similarly to the output in the example.

After you write the code, consider this question: how would you do this if the different data variables were not all the same type, so that you could not use a 2D array? For example, if the data consisted of payroll records, each with a String for the employee's name, a long for an employee id, a double for the employee's hourly wage, and another double for the number of hours the employee worked this week?

Turn in your .java code and the output from running your driver (screen shot or command line output pasted to a .txt file).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!