1.
2.
Initial state of the class must be that location is Gainesville.
We always return to Gainesville
Pre-conditions:
travelToNewYork() – location is Gainesville
travelToOrlando() – location is Gainesville
returnToGainesville – you are outside of Gainesville (say Orlando or New York)
Post-conditions:
travelToNewYork() – location is Gainesville
travelToOrlando() – location is Gainesville
returnToGainesville – location is Gainesville
3.
{location = Gainesville}
travelToNewYork()
{location = Gainesille}
{location = Gainesville}
travelToOrlando()
{location = Gainesille}
{location = outside Gainesville}
returnToGainesville()
{location = Gainesille}
4.
No, this method does not return us to Gainesville.
{location = Gainesville}
travelToCancun()
{location = Cancun}
5.
location possible states are: Gainesville, Cancun, Miami, Backyard Burger, Miami International Airport, New York, Charlotte, Orlando
6. and 7. Answers may vary
Sample solutions will be provided upon request during office hours or in discussion.
8 – 11.
abstract class Calculation {
/*
* properties
*/
protected int data;
/*
* default constructor
*/
Calculation(){
data = 0;
}
/*
* constructor initializing all properties
* @param data the data of the calulation
*/
Calculation(int data){
this.data = data;
}
/*
* get the data property
* @return the data property of this class
*/
int getData(){
return data;
}
/*
* set the data property
* @param data the data of the calulation
*/
void setData(int data){
this.data = data;
}
/*
* abstract method implemented by all subclasses. Calculates a value
* @return an int result
*/
abstract int calculate();
/*
* String representation of this object
* @return a String representing this object
*/
public String toString(){
return "data: " + data;
}
}
class SquareRoot extends Calculation{
/*
* properties
*/
private int value;
/*
* default constructor
*/
SquareRoot(){
super();
value = 0;
}
/*
* constructor initializing all properties
* @param data the data of the calulation
* @param value the value from which to find the square root
*/
SquareRoot(int data, int value){
super(data);
this.value = value;
}
/*
* get the value property
* @return the value property of this class
*/
int getValue(){
return value;
}
/*
* set the value property
* @param value the value of the calulation
*/
void setValue(int value){
this.value = value;
}
/*
* calculates the square root of the given value
* @return the square root or closest integer that when squared is greater
* than value. If the square root can't be calculated 0 is returned.
*/
int calculate(){
for(int i = 0; i < value; i++){
if((i*i) >= value){
data = i;
return data;
}
}
return data;
}
/*
* String representation of this object
* @return a String representing this object
*/
public String toString(){
return super.toString() + ", value: " + value;
}
}
class Power extends Calculation{
/*
* properties
*/
private int number;
private int raise;
/*
* default constructor
*/
Power(){
super();
number = 0;
raise = 0;
}
/*
* constructor initializing all properties
* @param data the data of the calulation
* @param number the number
* @param raise the value we are raising number to
*/
Power(int data, int number, int raise){
super(data);
this.number = number;
this.raise = raise;
}
/*
* get the number property
* @return the number property of this class
*/
int getNumber(){
return number;
}
/*
* set the number property
* @param number the value of the calulation
*/
void setNumber(int number){
this.number = number;
}
/*
* get the raise property
* @return the number property of this class
*/
int getRaise(){
return raise;
}
/*
* set the number property
* @param number the value of the calulation
*/
void setRaise(int raise){
this.raise = raise;
}
/*
* calculates the power, given a number and the value to raise it to
* @return an int which is the value of number raised to raise
*/
int calculate(){
data = calculate(number, raise);
return data;
}
/*
* calculates the power, given a number and the value to raise it to
* @param base the base
* @param exponent the exponent
* @return an int which is the value of base raised to exponent
*/
int calculate(int base, int exponent){
if(exponent == 0){
return 1;
}
if(exponent == 1){
return base;
}
else{
return base * calculate(base, (exponent - 1));
}
}
/*
* String representation of this object
* @return a String representing this object
*/
public String toString(){
return super.toString() + ", number: " + number + ", raise: " + raise;
}
}
class Driver{
public static Calculation[] calculations = new Calculation[3];
public static void main(String args[]){
calculations[0] = new SquareRoot(0, 25);
calculations[1] = new Power(0, 6, 3);
calculations[2] = new SquareRoot(0, 14);
for(int i = 0; i < calculations.length; i++){
System.out.println(calculations[i].toString() + "\n" + "calculation yields: " + calculations[i].calculate());
}
}
}
12.