Skip to content

Commit

Permalink
adap-hw07: Pre- und Postconditions sowie assertClassInvariants inklusive
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
MaikeMagdalena committed Dec 3, 2018
1 parent 2d43020 commit 0fa7a75
Show file tree
Hide file tree
Showing 7 changed files with 481 additions and 168 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ dependencies {
testCompile 'com.google.appengine:appengine-api-stubs:+'
testCompile 'com.google.appengine:appengine-api-labs:+'
testCompile 'com.google.appengine:appengine-tools-sdk:+'
testCompile 'junit:junit:4.+'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.22.0'
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/wahlzeit/main/ModelMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public void saveAll() throws IOException{
*
*/
protected void createUser(String userId, String nickName, String emailAddress, String photoDir) {

UserManager userManager = UserManager.getInstance();
User user = new User(userId, nickName, emailAddress);

Expand Down
137 changes: 137 additions & 0 deletions src/main/java/org/wahlzeit/model/AbstractCoordinate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* adap-hw 06
*
* This file is part of the Wahlzeit photo rating application.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/

package org.wahlzeit.model;

public abstract class AbstractCoordinate implements Coordinate {

double DELTA = 0.0000000000000002;

/**
* This method is an assertClassInvatiants-method for all Coordinates
* They should not be null
*/
protected static void assertNotNull(Coordinate coord) throws NullPointerException{
if (coord == null){
throw new NullPointerException("input coordinate can not be null");
}
}

/**
* two doubles are compared and if they are (nearly) equal this method becomes true.
* @param oneDouble
* @param secondDouble
* @return
*/
public boolean doublesAreEqual(double oneDouble, double secondDouble){
if(oneDouble + DELTA >= secondDouble &&
oneDouble - DELTA <= secondDouble){
return true;
}
else
{
return false;
}
}

@Override
public abstract CartesianCoordinate asCartesianCoordinate();

@Override
public abstract SphericCoordinate asSphericCoordinate();

/**
* This method gives back a cartesian distance. This is implemented already as getDistance(CartesianCoordinate)
*/
@Override
public double getCartesianDistance(Coordinate coord) {

//Precondition: Input coordinate should not be null
assertNotNull(coord);

CartesianCoordinate cartCoord = coord.asCartesianCoordinate();
CartesianCoordinate thisCoord = this.asCartesianCoordinate();

//Precondition: Cartesian coordinate should have valid arguments
cartCoord.assertValidCartesianCoord();
thisCoord.assertValidCartesianCoord();

return thisCoord.getDistance(cartCoord);
}

/**
* Checks if a given coordinate is equal to this coordinate.
*/
@Override
public boolean isEqual(Coordinate coordinate) {

//Precondition: Input coordinate should not be null
assertNotNull(coordinate);

CartesianCoordinate thisCoord = this.asCartesianCoordinate();
CartesianCoordinate coord = coordinate.asCartesianCoordinate();

//Precondition: Cartesian coordinate should have valid arguments
thisCoord.assertValidCartesianCoord();
coord.assertValidCartesianCoord();

if (doublesAreEqual(coord.getX(), thisCoord.getX()) &&
doublesAreEqual(coord.getY(), thisCoord.getY()) &&
doublesAreEqual(coord.getZ(), thisCoord.getZ()))
{
return true;
}
else
{
return false;
}
}

/**
* This method gives back the CentralAngle
*/
@Override
public double getCentralAngle(Coordinate coordinate) {

//Precondition: Input coordinate should not be null
assertNotNull(coordinate);

SphericCoordinate thisCoord = this.asSphericCoordinate();
SphericCoordinate coord = coordinate.asSphericCoordinate();

//Precondition: Spheric coordinates should have valid arguments
thisCoord.assertValidSphericCoord();
coord.assertValidSphericCoord();

double psi1 = 90 - thisCoord.getPhi();
double lambda1 = thisCoord.getTheta();
double psi2 = 90 - coord.asSphericCoordinate().getPhi();
double lambda2 = coord.asSphericCoordinate().getTheta();

double deltaLambda = Math.abs(lambda1 - lambda2);
double sinPsi12 = Math.sin(psi1) * Math.sin(psi2);
double cosPsi12DeltaLambda = Math.cos(psi1) * Math.cos(psi2) * Math.cos(deltaLambda);
double centralAngle = Math.acos(sinPsi12 + cosPsi12DeltaLambda);

return centralAngle;
}


}
99 changes: 47 additions & 52 deletions src/main/java/org/wahlzeit/model/CartesianCoordinate.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

package org.wahlzeit.model;

public class CartesianCoordinate implements Coordinate{
public class CartesianCoordinate extends AbstractCoordinate{
private double x;
private double y;
private double z;
Expand All @@ -38,15 +38,31 @@ public CartesianCoordinate(double x, double y, double z){
this.z = z;
}

/**
* This method is an assertClassInvatiants-method ONLY for the Cartesian Coordinate
* They should have valid arguments
*/
protected void assertValidCartesianCoord() throws IllegalArgumentException{
if((new Double(this.getX()).isNaN()) ||
new Double(this.getY()).isNaN() ||
new Double(this.getZ()).isNaN()){
throw new IllegalArgumentException("Cartesian coordinate has illegal arguments");
}
}

/**
* This method gives the distance between a given coordinate and the current object.
* @param coord is a given coordinate to calculate the distance to.
* @return distance
*/
public double getDistance(CartesianCoordinate coord){
if(coord == null){
throw new IllegalArgumentException("coord can not be null");
}

//Precondition: Input coordinate should not be null
assertNotNull(coord);

//Precondition: Cartesian coordinates should have valid arguments
coord.assertValidCartesianCoord();
this.assertValidCartesianCoord();

double distance;

Expand All @@ -58,58 +74,35 @@ public double getDistance(CartesianCoordinate coord){
return distance;
}

/**
* This methods checks if a given coordinate is equal with the current object's coordinate.
* @param coord is a given coordinate to check if it is equal with current object's coordinate.
* @return
*/
public boolean isEqual(CartesianCoordinate coord){

double DELTA = 0.00001;

if(coord == null){
throw new IllegalArgumentException("coord can not be null");
}

if (coord.x + DELTA < this.x && coord.x - DELTA > this.x
&& coord.y + DELTA < this.y && coord.y - DELTA > this.y
&& coord.z + DELTA < this.z && coord.z - DELTA > this.z) {
return true;
}

if (coord.x == this.x && coord.y == this.y && coord.z == this.z){
return true;
} else {
return false;
}
}

/**
* This method gives back a cartesian coordinate. It is already a cartesian coord, so it gives back itself.
*/
@Override
public CartesianCoordinate asCartesianCoordinate() {

//Precondition: Cartesian coordinate should have valid arguments
assertValidCartesianCoord();

return this;
}

/**
* This method gives back a cartesian distance. This is implemented already as getDistance(CartesianCoordinate)
*/
@Override
public double getCartesianDistance(Coordinate coord) {
CartesianCoordinate cartCoord = coord.asCartesianCoordinate();
return this.getDistance(cartCoord);
}

/**
* This method calculates spheric Coordinates out of the Cartesian ones.
*/
@Override
public SphericCoordinate asSphericCoordinate() {

//Precondition: Cartesian coordinate should have valid arguments
assertValidCartesianCoord();

double radius = Math.sqrt(x*x + y*y + z*z);
double phi = Math.atan2(y, x);
double theta = Math.acos(z/radius);
SphericCoordinate spherCoord = new SphericCoordinate(radius, theta, phi);

//Postcondition: Spheric coordinate should have valid arguments
spherCoord.assertValidSphericCoord();

return spherCoord;
}

Expand All @@ -118,14 +111,24 @@ public SphericCoordinate asSphericCoordinate() {
*/
@Override
public double getCentralAngle(Coordinate coord) {

//Precondition: Input coordinate should not be null
assertNotNull(coord);

CartesianCoordinate cartCoord = coord.asCartesianCoordinate();

//Precondition: Cartesian coordinates should have valid arguments
cartCoord.assertValidCartesianCoord();
this.assertValidCartesianCoord();

double x1 = this.x;
double y1 = this.y;
double z1 = this.z;
double x2 = coord.asCartesianCoordinate().getX();
double y2 = coord.asCartesianCoordinate().getY();
double z2 = coord.asCartesianCoordinate().getZ();
double x2 = cartCoord.getX();
double y2 = cartCoord.getY();
double z2 = cartCoord.getZ();

double DELTA = 0.0000000000000002;
//double DELTA = 0.0000000000000002;

//calculate with Scalar-Product
double centralAngle =
Expand All @@ -135,7 +138,7 @@ public double getCentralAngle(Coordinate coord) {
* Math.sqrt(x2 * x2 + y2 * y2 + z2 * z2)
);

//correct minimal Math-Errors (cos can be only beteween 1 and -1):
//correct minimal Math-Errors (cos can be only between 1 and -1):
if(centralAngle > 1){
centralAngle = centralAngle - DELTA;
}
Expand All @@ -147,14 +150,6 @@ public double getCentralAngle(Coordinate coord) {
return centralAngle;
}

/**
* This method checks if a Coordinate is equal with this one. It is already implemented as "equals"
*/
@Override
public boolean isEqual(Coordinate coord) {
return this.equals(coord);
}


public double getX() {
return x;
Expand Down
Loading

0 comments on commit 0fa7a75

Please sign in to comment.