In: Computer Science
For java code: Create a library to do it all. The attached file has all the directions and methods that need to be included in your library (.jar). You will create a .jar file, and a tester app that will include your .jar file (Add External JAR).
The attached file below:
Create a java project called MyLibrary, a package called net.dtcc.lib, and a class called AllInOne. The class should have the following methods, each method should return the answer: Area: Area of Rectangle Area of Square Area of Triangle Area of Circle Area of Trapezoid Area of Ellipse Area of Pentagon Area of Parallelogram Area of Rhombus Area of Hexagon Area of Polygon ** Pass in appropriate variables Fractions: Adding 2 fractions Subtracting 2 fractions Multiplying 2 fractions Dividing 2 fractions ** Pass in numerator/denominator of first number; numerator/denominator of second number Binary: Binary to decimal conversion ** Pass in binary number Temperature: Celcius to Fahrenheit Celcius to Kelvin Fahrenheit to Celcius Fahrenheit to Kelvin ** Pass in temperature to be converted Volume: Volume of Cube Volume of Box Volume of Cylinder Volume of Cone Volume of Sphere ** Pass in appropriate variables Perimeter: Perimter of Square Perimeter of Rectangle ** Pass in appropriate variables Circumference: Circumference of Circle ** Pass in appropriate variables Pythagorus Theorem: ** Pass in 2 values (a,b) Once you have finished the class, create a library (.jar) file with the name Taz2015.jar Next create another java project called Tester, a package called .net.dtcc.tester, and a class called LibTester. You will need to import your Taz2015.jar to the project (hint: remember derby.jar?). In this class, you should test each method in the library.
Steps to do the assignment.
Source code
1. AllInOne .java
public class AllInOne {
public double areaOfRectangle(double l, double b) {
return l * b;
}
public double areaOfSquare(double a) {
return a * a;
}
public double areaOfTriangle(double base, double height) {
return (base * height) / 2;
}
public double areaOfCircle(double r) {
return 3.14 * r * r;
}
public double areaOfTrapezoid(int b1,
int b2,
int h) {
return ((b1 + b2) / 2) * h;
}
public double areaOfEllipse(float a, float b) {
return (float) 3.142 * a * b;
}
public double areaOfPentagon(double a) {
return (double)(Math.sqrt(5 * (5 + 2 *
(Math.sqrt(5)))) * a * a) / 4;
}
public double areaOfParallelogram(double base, double height)
{
return base * height;
}
public double areaOfRhombus(double l1, double l2) {
return (double)(l1 * l2) / 2;
}
public double areaOfHexagon(double s) {
return ((3 * Math.sqrt(3) *
(s * s)) / 2);
}
public double areaOfPolygon(double X[], double Y[],
int n) {
double area = 0.0;
int j = n - 1;
for (int i = 0; i < n; i++) {
area += (X[j] + X[i]) * (Y[j] - Y[i]);
j = i;
}
return Math.abs(area / 2.0);
}
private int gcd(int a, int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
// Function to convert the obtained fraction
// into it's simplest form
private void lowest(int den3, int num3) {
// Finding gcd of both terms
int common_factor = gcd(num3, den3);
// Converting both terms into simpler
// terms by dividing them by common factor
den3 = den3 / common_factor;
num3 = num3 / common_factor;
System.out.println(num3 + "/" + den3);
}
//Function to add two fractions
public void addFraction(int num1, int den1,
int num2, int den2) {
// Finding gcd of den1 and den2
int den3 = gcd(den1, den2);
// Denominator of final fraction obtained
// finding LCM of den1 and den2
// LCM * GCD = a * b
den3 = (den1 * den2) / den3;
// Changing the fractions to have same denominator
// Numerator of the final fraction obtained
int num3 = (num1) * (den3 / den1) + (num2) * (den3 / den2);
// Calling function to convert final fraction
// into it's simplest form
lowest(den3, num3);
}
public void substractFraction(int num1, int den1,
int num2, int den2) {
addFraction(num1, den1, -1 * num2, den2);
}
public void multiplyFraction(int num1, int den1,
int num2, int den2) {
int num = num1 * num2;
int den = den1 * den2;
System.out.println(num + "/" + den);
}
public void divideFraction(int num1, int den1,
int num2, int den2) {
int num = num1 * den2;
int den = den1 * num2;
System.out.println(num + "/" + den);
}
public int binaryToDecimal(int n) {
int num = n;
int dec_value = 0;
// Initializing base
// value to 1, i.e 2^0
int base = 1;
int temp = num;
while (temp > 0) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
public double celciusToFahrenheit(double c) {
return (double) c * (9 d / 5) + 32;
}
public double fahrenheitToCelcius(double fah) {
return (double)((fah - 32) * 5) / 9;
}
public double fahrenheitToKelvindouble(double fah) {
return 273.5 d + ((fah - 32.0 d) * (5.0 f / 9.0 d));
}
public double celciusToKelvin(double c) {
return (float)(c + 273.15);
}
public double volumeOfSquare(double a) {
return a * a * a;
}
public double volumeOfBox(double l, double b, double h) {
return l * b * h;
}
public double volumeOfCylinder(double height, double radius)
{
return 3.14 * (radius * radius) * height;
}
public double volumeOfCone(double height, double radius) {
return 3.14 * (radius * radius) * height / 3;
}
public double volumeOfSphere(double r) {
return (4 * 22 * r * r * r) / (3 * 7);
}
public double perimeterOfSquare(double a) {
return 4 * a;
}
public double perimeterOfSquare(double a, double b) {
return 2 * (a + b);
}
public double circumferenceOfCircle(double r) {
return 2 * 3.14 * r;
}
public double pythagorusTheorem(double a, double b) {
double c = (a * a) + (b * b);
return Math.sqrt(c);
}
}
2. LibTester.java
public class LibTester {
public static void main(String[] args) {
AllInOne allInOne = new AllInOne();
double triangleArea = allInOne.areaOfTriangle(3.4, 9.5);
System.out.println(triangleArea);
}
}
Sample output: