Question

In: Computer Science

Java Programming 5th edition Chapter 10 Inheritance Programs Part 1 Number 3 on page 719 which...

Java Programming 5th edition Chapter 10 Inheritance Programs

Part 1

Number 3 on page 719 which creates a Point class with 2 instance variables; the xCoordinate and yCoordinate. It should have a default constructor and a values constructor. Also include a set method that sets both attributes, a get method for each attribute, and a method that redefines toString() to print the attributes as follows.

point: (x, y)

Part 2

Do number 4 on page 719 which creates a Circle class that extends the Point class above. It adds the radius, circumference, and area instance variables. It should have a default constructor and a values constructor (circumference and area will be 0.0). Include a set method that sets the coordinates and radius attributes, get methods that get each of the 3 new attributes, and a method that redefines toString() to print all attributes. Also, include methods to calculate the circumference and area of the circle. Use pie = 3.141593. Make sure your methods don’t repeat the code already written in the Point class.

Circumference = 2pier

Area = pier2

Part 3

Do number 5 on page 719 which creates a Cylinder class that extends the Circle class above. It adds the height, surfaceArea, and volume instance variables. It should have a default constructor and a values constructor (circumference and circleArea will be calculated from the Circle class, surfaceArea and volume will be 0.0). Include a set method for the center point coordinates, radius, height, circumference, and circleArea attributes (circumference and circleArea will be calculated from the Circle class), get methods that get each of the 3 new attributes, and a method that redefines toString() to print all attributes. Also, include methods to calculate the surfaceArea and volume of the cylinder. Make sure your methods don’t repeat the code already written in the Circle class.

surfaceArea = 2 * circleArea + circleCircumference * cylinderHeight

volume = circleArea * cylinderHeight

Hints:

1.In the values constructor and the set method you’ll need to call the methods to calculate circumference and area so that they have values to use in calculating the surface area and volume.

2.The methods in the Circle class to calculate circumference and area will need to return those values for use in the Cylinder class.

Part 4

Create a client program that uses all 3 of the classes created above. Make it do the following in this order:

1.Instantiate point1 with the default constructor.

2. Instantiate point2 with the values constructor.

3. Use the Point class print method to print point1 and point2.

4. Call the set method to set the x and y coordinates for point1.

5. Use the get methods to get the attributes for point1 and print them in the client (not with the print method).

6. Instantiate circle1 with the default constructor.

7. Instantiate circle2 with the values constructor.

8. Call the methods to calculate the circumference and area for circle2.

9. Use the Circle class print method to print attributes for circle1 and circle2.

10. Use the set method to set the coordinates and radius for circle1.

11. Call the methods to calculate the circumference and area for circle1.

12. Use the get methods to get the attributes for circle1 and print them in the client (not with the print method).

13. Instantiate cylinder1 with the default constructor.

14. Instantiate cylinder2 with the values constructor.

15. Call the methods to calculate the surfaceArea and volume for cylinder2.

16. Use the Cylinder class print method to print the attributes for cylinder1 and cylinder2

17. Call the set method to set the attributes for cylinder1.

18. Call the methods to calculate the surfaceArea and volume for cylinder1.

19.Use the get methods to get the attributes for cylinder1 and print them in the client (not with the print method).

Solutions

Expert Solution

Point.java

public class Point{

   protected double xCoordinate;
   protected double yCoordinate;
  
   protected Point(){   //Default Constructor
       xCoordinate = 0;
           yCoordinate = 0;
   }
   protected Point(double x, double y){   //Parameterized Constructor
       xCoordinate = x;  
       yCoordinate = y;
   }
   public void setCoordinates(double x, double y){
       xCoordinate = x;
       yCoordinate = y;
   }
   public double getXCoordinate(){
       return xCoordinate;
   }
   public double getYCoordinate(){
       return yCoordinate;
   }
  
        public String toString() {   //returns a string representation of this point
           return "point: (" + xCoordinate + ", " + yCoordinate + ")";
       }   
}   

Circle.java

public class Circle extends Point{

    protected double radius;
    protected double area;
    protected double circumference;
    private final double pi = 3.141593;

    //default constructor
    protected Circle() {
        super();   //calling super class constructor
   radius = 0.0;
   area = 0.0;
   circumference = 0.0;
    }
    //parameterized constructor
    protected Circle(double x, double y, double r) {
   super(x,y);    //calling super class constructor
        radius = r;
   area = pi * radius * radius;
   circumference = 2 * pi * radius;
    }
    //set circle attributes
    public void setCircleAttributes(double r) {
        double x = getXCoordinate();   //calling superclass method getXCoordinate() of Point class
   double y = getYCoordinate();   //calling superclass method getYCoordinate() of Point class
   radius = r;
   area = pi * radius * radius;
   circumference = 2 * pi * radius;
    }
    //get radius of circle
    public double getRadius() {
        return radius;
    }
    //get area of circle
    public double getArea() {
        return area;
    }
    //get circumference of circle
    public double getCircumference() {
        return circumference;
    }
    //calculate area of circle
    public double calculateArea() {
   return (pi * radius * radius);
    }
    //calculate circumference of circle       
    public double calculateCircumference() {
   return (2 * pi * radius);
    }
    //Overridden method of Point class
    //returns a string representation of the radius, circumference and area of the circle
    @Override
    public String toString() {  
           return "circle: (" + xCoordinate + "," + yCoordinate + "," + radius + ", " + area + ", " + circumference +")";  

       }
}

Cylinder.java

public class Cylinder extends Circle {
   protected double height;
   protected double surfaceArea;
   protected double volume;

   //default constructor
   protected Cylinder()
   {
      super();   //calling super class constructor
      height = 0.0;
      surfaceArea = 0.0;
      volume = 0.0;
   }
   //parameterized constructor
   protected Cylinder( double x, double y, double r, double h)
   {
      super( x, y, r );   //calling super class constructor
      height = h;
      surfaceArea = 2 * (calculateArea()) + (calculateCircumference()) * h;   //calling superclass methods getArea and getCircumference of Circle class
      volume = calculateArea() * h;   //calling superclass method getArea of Circle class
    }
   //get height of Cylinder
   public double getHeight()
   {
   return height;
   }
   //set cylinder attributes
   public void setCylinderAttributes(double h)
   {     
   double r = getRadius();
   height = h;    
        surfaceArea = 2 * (calculateArea()) + (calculateCircumference()) * h;   //calling superclass methods getArea and getCircumference of Circle class
         volume = (calculateArea()) * h;   //calling superclass method getArea of Circle class
   }
   //get area of Cylinder
   public double getSurfaceArea()
   {
   return surfaceArea;
   }
   //get volume of Cylinder
   public double getVolume()
   {
   return volume;
   }
   //calculate surface area of cylinder
   public double calculateSurfaceArea()
   {
   double r = getRadius();
   return 2 * (calculateArea()) + (calculateCircumference()) * height;   //calling superclass methods getArea and getCircumference of Circle class
    }
   //calculate volume of cylinder
   public double calculateVolume()
   {
   double r = getRadius();
   return ((calculateArea()) * height);   //calling superclass method getArea of Circle class
   }
   //convert the Cylinder to a String
   @Override
   public String toString()
   {  
        return "cylinder: (" + xCoordinate + "," + yCoordinate + "," + radius + "," + height + "," + surfaceArea + ", " + volume +")";  

   }
}

ClientProgram.java

public class ClientProgram{

   public static void main(String args[]){
       Point point1 = new Point();   //creating object of Point class using default constructor
       Point point2 = new Point(2,3);   //creating object of Point class using values constructor
      
       System.out.println(point1.toString());   //calling toString method of Point class
       System.out.println(point2.toString());

       point1.setCoordinates(4.5,5.5);
       System.out.println("point: (" +point1.getXCoordinate()+ ","+point1.getYCoordinate()+")") ;

       Circle circle1 = new Circle();   //creating object of Circle class using default constructor
       Circle circle2 = new Circle(1,1.6,4);   //creating object of Circle class using values constructor

       double area = circle2.calculateArea();   //calling calculateArea method for circle2
       double circumference = circle2.calculateCircumference();   //calling calculateCircumference method for circle2

       System.out.println(circle1.toString());   //calling toString method of Circle class
       System.out.println(circle2.toString());   //calling toString method of Circle class

       circle1.setCircleAttributes(9.5);
       circle1.calculateArea();
       circle1.calculateCircumference();  
      
       System.out.println("circle: (" +circle1.getXCoordinate()+ ","+circle1.getYCoordinate()+","+circle1.getRadius()+","+circle1.getArea()+","+circle1.getCircumference()+")") ;

       Cylinder cylinder1 = new Cylinder();   //creating object of Cylinder class using default constructor
       Cylinder cylinder2 = new Cylinder(7,10,2,8);   //creating object of Cylinder class using values constructor

       double surfaceArea = cylinder2.calculateSurfaceArea();   //calling calculateSurfaceArea method for cylinder2
       double volume = cylinder2.calculateVolume();   //calling calculateVolume method for cylinder2

       System.out.println(cylinder1.toString());   //calling toString method of Cylinder class
       System.out.println(cylinder2.toString());   //calling toString method of Cylinder class

       cylinder1.setCylinderAttributes(5);
       cylinder1.calculateSurfaceArea();
       cylinder1.calculateVolume();  

       System.out.println("cylinder: (" +cylinder1.getXCoordinate()+ ","+cylinder1.getYCoordinate()+","+cylinder1.getRadius()+","+cylinder1.getHeight()+","+cylinder1.getSurfaceArea()+","+cylinder1.getVolume()+")") ;
      
   }

}


Related Solutions

Question 17- Chapter 10 (Internet and World Wide Web 5th Edition) You will use random number...
Question 17- Chapter 10 (Internet and World Wide Web 5th Edition) You will use random number generation to develop a simulation for the classic race of the tortoise and the hare. The contenders begin the race at square 1 of 70 squares. Each square represents a possible position along the race course. The finish line is at square 70. The first contender to reach or pass square 70 is rewarded with a pail of fresh carrots and lettuce. The course...
JAVA CODE --- from the book, java programming (7th edition) chapter 7 carly's question I am...
JAVA CODE --- from the book, java programming (7th edition) chapter 7 carly's question I am getting a illegal expression error and don't know how to fix it, also may be a few other error please help CODE BELOW import java.util.Scanner; public class Event { public static double pricePerGuestHigh = 35.00; public static double pricePerGuestLow = 32.00; public static final int LARGE_EVENT_MAX = 50; public String phnum=""; public String eventNumber=""; private int guests; private double pricePerEvent; public void setPhoneNumber() {...
Chapter 6, Starting out with Programming and Logic, 4th Edition, Page 265 #6 Kinetic Energy In...
Chapter 6, Starting out with Programming and Logic, 4th Edition, Page 265 #6 Kinetic Energy In physics, an object that is in motion is said to have kinetic energy. The following formula can be used to determine a moving object’s kinetic energy: KE=12mv2 The variables in the formula are as follows: KE is the kinetic energy, m is the object’s mass in kilograms, and v is the object’s velocity, in meters per second. Design a function named kineticEnergy that accepts...
java from control structures through objects 6th edition, programming challenge 5 on chapter 16 Write a...
java from control structures through objects 6th edition, programming challenge 5 on chapter 16 Write a boolean method that uses recursion to determine whether a string argument is a palindrome. the method should return true if the argument reads the same forward amd backword. Demonstrate the method in a program, use comments in the code for better understanding. there must be a demo class and method class also .. generate javadocs through eclipse compiler. And make a UML diagram with...
research methods for the behavioral sciences 5th edition pdf Chapter 1 1. Name one non-scientific way...
research methods for the behavioral sciences 5th edition pdf Chapter 1 1. Name one non-scientific way of knowing and a problem associated with it and then describe the empirical method of gaining knowledge and why it is better. 2. Describe the conditions under which a scientist would typically use the inductive method and when they would be more likely to use the deductive method of reasoning.
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create a java class InventoryItem which has a String description a double price an int howMany Provide a copy constructor in addition to other constructors. The copy constructor should copy description and price but not howMany, which defaults to 1 instead. In all inheriting classes, also provide copy constructors which chain to this one. Write a clone method that uses the copy constructor to create...
Review and discussion question Number 4 page 21 on Supervision: Concepts and Skill Building Edition 10...
Review and discussion question Number 4 page 21 on Supervision: Concepts and Skill Building Edition 10 .....Chapter 1 What are some advantages of greater diversity?
CHEM 1414-1 Name: _______________________________ Chapter 13 Page 1 of 3 1) Circle which member in each...
CHEM 1414-1 Name: _______________________________ Chapter 13 Page 1 of 3 1) Circle which member in each pair has the larger dispersion forces? (a) CCl4 or CH4 (b) SO or SO2 (c) Br2 or HBr 2) Circle which member in each pair has the stronger intermolecular dispersion forces? (a) CO2 or CO (b) CH3Br or CH3Cl (c) CH3CH2CH2Cl or CH3CH2Cl 3) Identify the type or types of intermolecular forces present in each substance. (a) propane, C3H8 dispersion dipole-dipole hydrogen bonding ionic...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for loop, display each element of this array: String[] names = {"alice", "bob", "carla", "dennis", "earl", "felicia"}; Part 2 (30%) In a new class, implement two methods that will each calculate and return the average of an array of numeric values passed into it. Constraints: your two methods must have the same name one method should accept an array of ints; the other should accept...
afe Rational Fractions In week 4 we completed Chapter 13 Programming Exercise #10 Page 974. Make...
afe Rational Fractions In week 4 we completed Chapter 13 Programming Exercise #10 Page 974. Make sure you have a working fractionType class before starting this assignment. The template requirement from week 4 is not required for this assignment. Your assignment this week is to make your fractionType class safe by using exception handling. Use exceptions so that your program handles the exceptions division by zero and invalid input. An example of invalid input would be entering a fraction such...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT