76.8k views
4 votes
Write a Java program that meets the following requirements:

Create a class named Rock that acts as a superclass for rock samples collected and catalogued by a natural history museum. The Rock class contains fields for a number of samples, a description of the type of rock, and the weight of the rock in grams. Include a constructor that accepts parameters for the sample number and weight. The Rock constructor sets the description value to Unclassified. Include get methods for each field. Since you do not create any "Rock" objects make the Rock class abstract. Create three child classes named IgneousRock, SedimentaryRock, and MetamorphicRock. The constructors for these classes require parameters for the sample number and weight. Search the Web for a brief description of each rock type and assign it to the description field. Create an application that instantiates an object of each type and demonstrate that the methods work appropriately. Save the files as Rock.java, IgneousRock.java, SedimentaryRock.java, MetamorphicRock.java, and DemoRocks.java.

User Amyunimus
by
5.4k points

1 Answer

5 votes

Answer: provided in the explanation segment

Step-by-step explanation:

Below is the code for the different classes of rock.

Rock.java

public class Rock

{

int noOfsamples;

String typeOfrock;

int weightOfrock;

public Rock(int noOfsamples,int weightOfrock)

{

this.noOfsamples=noOfsamples;

this.typeOfrock="Unclassified";

this.weightOfrock=weightOfrock;

}

public void setnoOfsamples( int noOfsamples ){

this.noOfsamples = noOfsamples;

}

public int getnoOfsamples(){

return this.noOfsamples;

}

public void setweightOfrock( int weightOfrock ){

this.weightOfrock = weightOfrock;

}

public int getweightOfrock(){

return this.weightOfrock;

}

public String toString()

{

return "The number of samples are:"+noOfsamples+

"\\The weight of the rock is:"+weightOfrock+

"\\The description of rock type is:"+typeOfrock;

}

}

IgneousRock.java

public class IgneousRock extends Rock

{

public IgneousRock( int noOfsamples,int weightOfrock)

{

super(noOfsamples, weightOfrock);

}

public void settypeOfrock( String typeOfrock ){

super.typeOfrock = typeOfrock;

}

public String gettypeOfrock(){

return super.typeOfrock;

}

}

SedimentaryRock.java

public class SedimentaryRock extends Rock

{

public SedimentaryRock(int noOfsamples,int weightOfrock)

{

super(noOfsamples, weightOfrock);

}

public void settypeOfrock( String typeOfrock ){

super.typeOfrock = typeOfrock;

}

public String gettypeOfrock(){

return super.typeOfrock;

}

}

MetamorphicRock.java

public class MetamorphicRock extends Rock

{

MetamorphicRock(int noOfsamples,int weightOfrock)

{

super(noOfsamples, weightOfrock );

}

public void settypeOfrock( String typeOfrock ){

super.typeOfrock = typeOfrock ;

}

public String gettypeOfrock () {

return super.typeOfrock ;

}

}

DemoRocks.java

public class DemoRocks

{

public static void main(String[] args)

{

IgneousRock obj1=new IgneousRock(2,200) ;

obj1.settypeOfrock("andesite") ;

System.out.println(obj1.toString()) ;

SedimentaryRock obj2= new SedimentaryRock(3,300) ;

obj2.settypeOfrock("sandstone") ;

System.out.println(obj2.toString()) ;

MetamorphicRock obj3=new MetamorphicRock(4,400);

obj3.settypeOfrock("quartzite");

System.out.println(obj3.toString()) ;

}

}

cheers i hope this helps!!!

User Volodymyr Kozubal
by
5.9k points