1. The class Shapes includes two void methods: calcRectangleArea( ) and calcRect
ID: 3843038 • Letter: 1
Question
1. The class Shapes includes two void methods: calcRectangleArea( ) and calcRectanglePerimeter( ). The calcRectangleArea( ) method takes two int parameters (length and width), calculates the area of a rectangle, and assigns the value to a private instance variable (area). The calcRectanglePerimeter( ) method takes two int parameters (length and width), calculates the perimeter of a rectangle, and assigns the value to a private instance variable (perimeter). The Shapes class also includes two getter methods, which return the calculated values. The Shapes class implements the Calculatable interface.
Write the Shapes class and the Calculatable interface. (12 points)
2. Write an abstract method convertInches( ) that takes inches as an int parameter and returns a double value. (2 points)
3. Write an abstract method convertFeet( ) that takes feet as an int parameter and returns a double value. (2 points)
4. Write a Traveler interface that includes methods to calculate the following. (Do not over analyze these methods!)
The distance between two points on a line. Each point is represented by a pair of (x, y) coordinates.
The distance between two cities. Each city is represented by a longitude and latitude. (4 points)
5. Consider the following partially completed class:
public class SomeClass extends SomeOtherClass
{
public void convertHours(double hours)
{
//some code here
}
public void convertMonths(double months)
{
//some code here }
}
}
Write the corresponding abstract class. (4 points)
6. Consider the following partially complete class:
public class TestClass implements Comparable
{
private String city;
private String state
public TestClass( String city, String state )
{
this.city = city;
this.state = state;
}
public String getCity()
{
return this.city;
}
public String getState()
{
return this.state;
}
public int compareTo( Object obj )
{
// complete the code here
}
}
Using the Comparable interface, write a compareTo( ) method that returns -1 if the private instance variable city occurs lexicographically before obj, 0 if the private instance variable city and obj are lexicographically the same, or 1 if the private instance variable city occurs lexicographically after obj. Use a getter method any time you need to use a private instance variable. (6 points)
Explanation / Answer
1. Write the Shapes class and the Calculatable interface.
interface Calculatable{ //declaring interface
public:
calcRectangleArea(); //abstract class
calcRectanglePerimeter(); //abstract class
convertInches(); //abstract class
};
class Shapes implements Calculatable{ //implementing an interface
public: //defining variables as public
int length,width;
private:
int area,perimeter;
calcRectangleArea(int length,int width){ //coding the abstract classes
area = length*width;
return area;
}
calcRectanglePerimeter(){ //coding the abstract classes
perimeter = 2*(length+width);
return perimeter;
}
public:
int getArea(){ //getter methods
return area;
}
int getPerimeter(){ //getter methods
return perimeter;
}
}
2. Write an abstract method convertInches( ) that takes inches as an int parameter and returns a double value.
double convertInches(int inches){
double dinches;
dinches = (double)inches;
return dinches;
}
3. Write an abstract method convertFeet( ) that takes feet as an int parameter and returns a double value.
double convertFeet(int feet){
double dfeet;
dfeet = (double)feet;
return dfeet;
}
4. Write a Traveler interface that includes methods to calculate the following.
The distance between two points on a line and the distance between two cities.
class Traveler{
double distanceBwPoints(); //abstract methods
double distanceBwCities(); //abstract methods
}
double distanceBwPoints(double x1, double y1, double x2, double y2){
return math.sqrt((x1-x2)(x1-x2) + (y1-y2)(y1-y2));
}
double distanceBwCities(double x1, double y1, double x2, double y2){
return math.sqrt((x1-x2)(x1-x2) + (y1-y2)(y1-y2));
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.