1. Consider the following partially complete class: public class TestClass imple
ID: 3843062 • Letter: 1
Question
1. 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)
2. 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)
Explanation / Answer
1)
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 )
{
TestClass other = (TestClass)obj;
if(city.compareTo(other.city) < 0)
return -1;
else if(city.compareTo(other.city) > 0)
return 1;
else
return 0;
}
}
You have not explained Q2 in details, like, what does convertHours() method do, so i can not answer
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.