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 the following fields: sampleNumber - of type int description - A description of the type of rock (of type String) weight - The weight of the rock in grams (of type double) 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. Create three child classes named IgneousRock, SedimentaryRock, and MetamorphicRock. The constructors for these classes require parameters for the sample number and weight. Search the Internet for a brief description of each rock type and assign it to the description field using a method named setDescription inside of the constructor.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

This is the question - 

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 the following fields:

  • sampleNumber - of type int
  • description - A description of the type of rock (of type String)
  • weight - The weight of the rock in grams (of type double)

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.

Create three child classes named IgneousRock, SedimentaryRock, and MetamorphicRock. The constructors for these classes require parameters for the sample number and weight. Search the Internet for a brief description of each rock type and assign it to the description field using a method named setDescription inside of the constructor.

I am only missing two checks and they seem to be with the rock, here is my code then I will copy one check - 

import java.util.*;
public class DemoRocks
{
    public static void main(String[] args)
    {
        Rock rock = new Rock(2, 5.0);
        System.out.println("Number of samples: " + rock.getSampleNumber());
        System.out.println("Weight: " + rock.getWeight());
        System.out.println("Description: " + rock.getDescription());

        IgneousRock rock2 = new IgneousRock(3, 7.0);
        System.out.println("Number of samples: " + rock2.getSampleNumber());
        System.out.println("Weight: " + rock2.getWeight());
        System.out.println("Description: " + rock2.getDescription());

        SedimentaryRock rock3 = new SedimentaryRock(3, 7.0);
        System.out.println("Number of samples: " + rock3.getSampleNumber());
        System.out.println("Weight: " + rock3.getWeight());
        System.out.println("Description: " + rock3.getDescription());

        MetamorphicRock rock4 = new MetamorphicRock(3, 7.0);
        System.out.println("Number of samples: " + rock4.getSampleNumber());
        System.out.println("Weight: " + rock4.getWeight());
        System.out.println("Description: " + rock4.getDescription());
    }
}
-----------------
public class IgneousRock extends Rock {
    public IgneousRock(int number, double weight) {
        super(number, weight);
        setDescription();
    }

    public void setDescription() {
        description = "Fire rocks formed from cooled lava that often have large crystals in them";
    }

}
-------------
public class MetamorphicRock extends Rock {
    public MetamorphicRock(int number, double weight) {
        super(number, weight);
        setDescription();
    }

    public void setDescription() {
        description = "Flat or squished rocks that are formed by changes to Igneous or Sedimantary rocks from extreme heat or pressure";
    }

}
-------------------
public class Rock
{
    private int sampleNumber;
    protected String description;
    private double weight;
    public Rock(int number, double weight)
    {
        sampleNumber = number;
        this.weight = weight;
        description = "something different";
    }
    public int getSampleNumber()
    {
        return sampleNumber;
    }
    public String getDescription()
    {
        return description;
    }
    public double getWeight()
    {
        return weight;
    }
}
-------------
public class SedimentaryRock extends Rock {
    public SedimentaryRock(int number, double weight) {
        super(number, weight);
        setDescription();
    }

    public void setDescription() {
        description = "Rocks formed from sediment or fossils that are fused together under water over time";
    }

}
The checks -
Define Rock constructor
Build Status
Build Succeeded
[FAILED]: unitTest(NtTest2d407a3c): expected:<[Unclassified]> but was:<[something different]> false
@Test public void unitTest() { j
ava.lang.reflect.Field sampleNumberField;
java.lang.reflect.Field descriptionField;
java.lang.reflect.Field weightField;
Rock rock = new Rock(0, 5);
Rock rock2 = new Rock(92, 154);
try { sampleNumberField = Tester.accessPrivateField("sampleNumber", Rock.class); descriptionField = Tester.accessPrivateField("description", Rock.class); weightField = Tester.accessPrivateField("weight", Rock.class);
assertEquals(0, sampleNumberField.get(rock));
assertEquals("Unclassified", descriptionField.get(rock));
assertEquals(5.0, weightField.get(rock));
assertEquals(92, sampleNumberField.get(rock2));
assertEquals("Unclassified", descriptionField.get(rock2));
assertEquals(154.0, weightField.get(rock2));
// Display errors
} catch (Exception e)
{ Tester.displayError(e);
}
}
public static class Tester
{ public static java.lang.reflect.Field accessPrivateField(String fieldName, Class className) {
java.lang.reflect.Field field = null;
{ field = className.getDeclaredField(fieldName);
field.setAccessible(true);
return field;
} catch
(Exception e)
{ Tester.displayError(e);
} return field; }
 
Only part of error had to cut some out due to word length
 
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education