I'm pretty sure my code should be working properly, but the tests in my learning platform fail stating that the constructors in my subclasses "should set the description to a value other than 'Unclassified'"
Parent Class
public class Rock
{
int sampleNumber;
String description;
double weight;
public Rock(int sampleNumber, double weight)
{
this.sampleNumber = sampleNumber;
this.description = "Unclassified";
this.weight = weight;
}
public void setSampleNumber( int sampleNumber ){
this.sampleNumber = sampleNumber;
}
public int getSampleNumber(){
return this.sampleNumber;
}
public void setDescription( String description ){
this.description = description;
}
public String getDescription(){
return this.description;
}
public void setWeight(double weight){
this.weight = weight;
}
public double getWeight() {
return this.weight;
}
public String toString()
{
return "The number of samples are: "+sampleNumber+
"\nThe weight of the rock is: "+ weight+
"\nThe description of rock type is: "+ description;
}
}
One of the child classes (There are 3 and they are pretty much the same)
public class SedimentaryRock extends Rock
{
public SedimentaryRock(int sampleNumber,int weight)
{
super(sampleNumber, weight);
}
public void setDescription( String description ){
super.description = description;
}
public String getDescription(){
return super.description;
}
}
This is the main class
public class DemoRocks
{
public static void main(String[] args)
{
IgneousRock rock = new IgneousRock( 2, 200);
rock.setDescription("andesite");
System.out.println(rock.toString());
SedimentaryRock rock2= new SedimentaryRock( 3, 300);
rock2.setDescription("sandstone");
System.out.println(rock2.toString());
MetamorphicRock rock3=new MetamorphicRock( 4, 400);
rock3.setDescription("quartzite");
System.out.println(rock3.toString());
}
}
The output is:
The number of samples are: 2
The weight of the rock is: 200.0
The description of rock type is: andesite
The number of samples are: 3
The weight of the rock is: 300.0
The description of rock type is: sandstone
The number of samples are: 4
The weight of the rock is: 400.0
The description of rock type is: quartzite
One way to avoid that problem is to set the description in the parent class to " " instead of "Unclassified" but it needs to be set to "unclassified".
I don't know what causes it to behave that way.
I suspect the test wants you to do below:
public SedimentaryRock(int sampleNumber,int weight) {
super(sampleNumber, weight);
super.setDescription("sedimentary");
}
I am assuming that the test wants you to override the property in constructor itself rather than setting it explicitly
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.