Create a class Square derived from DrawableShape, as described in the previous exercise. A Square object should know the length of its sides The class should have an accessor method and a mutator method for this length. It should also have methods for computing the area and perimeter of the square. Although characters are taller than they are wide—so the number of characters in the vertical sides will differ from the number in the horizontal sides—you need not worry about this detail when drawing the square.
Want to see the full answer?
Check out a sample textbook solutionChapter 8 Solutions
Java: Introduction to Problem Solving and Programming
Additional Engineering Textbook Solutions
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Starting Out with Java: Early Objects (6th Edition)
Software Engineering (10th Edition)
Programming in C
Modern Database Management
Computer Science: An Overview (12th Edition)
- 1: create a class Pentagon, which implements the RegularPolygon interface. It should have a double field called length, as well as a constructor that initializes this length field to a given value. 2: In the main() method of Pentagon class, add the following code: RegularPolygon sq = new Square(10); RegularPolygon penta = new Pentagon(20); Write code to get the perimeter for sq and penta. 3: In the main() method defined in Q2, add this line of code: sq.getArea(); Will this code pass through compiler? What is the reason? The Java feature in Q1 which create a new version of predefined method getPerimeter() is called Polymorphism Override Overload Inheritancearrow_forwardWrite a compareTo method that compares two Song objects based on the artist, and title within artist. For example, given the following Songobjects: "Glass Animals""Heat Waves" "Elton John, Dua Lipa""Cold Heart" "Adele""Easy on Me" "Doja Cat""Kiss Me More" "Adele""My Little Love" They will be put in the following order: "Adele""Easy on Me" "Adele""My Little Love" "Doja Cat""Kiss Me More" "Elton John, Dua Lipa""Cold Heart" "Glass Animals""Heat Waves" In which class does the compareTo method belong?arrow_forwardIn this exercise, you are going to build on your Circleclass from the previous exercise. You are going to add 2 method, areaDifference and perimeterDifference. Both methods take a doubleradius of a second circle and return the difference from the current circle. For example, if you create a Circle object with a radius of 4 and call areaDifference(3), you will return the diffence between the area of a circle with radius 4 and the area of a circle with a radius of 3. perimeterDifferencewould be the same. Make sure you create at least one Circle and test and print the results of your methods. in javaarrow_forward
- In this exercise, you are going to build on your Circleclass from the previous exercise. You are going to add 2 method, areaDifference and perimeterDifference. Both methods take a doubleradius of a second circle and return the difference from the current circle. For example, if you create a Circle object with a radius of 4 and call areaDifference(3), you will return the diffence between the area of a circle with radius 4 and the area of a circle with a radius of 3. perimeterDifferencewould be the same. Make sure you create at least one Circle and test and print the results of your methods. given: public class Circle{private double radius;public Circle(double theRadius){radius = theRadius;}// Add a method called area that returns the area of a circle// using Math.PIpublic double area(){return Math.PI*radius*radius;}// Add a method called perimeter that returns the perimeter of a// circle using Math.PIpublic double perimeter(){return Math.PI*2*radius;}}arrow_forwardGiven below is a Rectangle class consisting of an upper left coordinate and a lower right coordinate. class Rectangle { int upperLeftX; int upperLeftY; int lowerRightX; int lowerRightY; } Since the upper left coordinate and lower right coordinate comprise of points, how would you modify the definition of the Rectangle class to include these points in your class?arrow_forwardWrite a class 'Cylinder' that has two fields: height and radius. The class should have only one parameterized constructor function to initialize these fields. The class should also have a method 'getVolume()' to calculate the volume of this cylinder. Now, create any 2 objects of this class with arbitrary values and show their volumes.arrow_forward
- Write a program to print the area of rectangle by creating a class named "Rectangle" with constructor having the two sides(width and height) as its parametersarrow_forwardAdd the following constructor to your Rectangle class: public Rectangle(Point p, int width, int height) Construct a new Rectangle whose top-left corner is specified by the given Point and with the given width and height.arrow_forwardFor this project you will have to write two classes.Class Rectangle is a class that represents an axis-aligned rectangle. ClassRectangleTester is the driver class, used to test the Rectangle class.Class Rectangle:An instance of the class Rectangle represents a valid axis-aligned rectangle. It has severalinstance variables that one needs to use to describe a rectangle, as well as methods thatoperate on these variables.Rectangle instance variables:Class Rectangle will have the following instance variables:• x – a double that represents the x coordinate of the lower left vertex of therectangle• y – a double that represents the y coordinate of the lower left vertex of therectangle• width – a double that represents the width of the rectangle• height – a double that represents the height of the rectangle• color – a String that represents the color of the rectangleFor the rectangle to be valid, the coordinates of all vertices must be between 0 and 1inclusive. The color can be one of the…arrow_forward
- Write a Rectangle class to represent rectangles, with two data fields (width, height) and four methods (input data, display data, calculate and return area, calculate and return perimeter). Write another class to test all methods of the Rectangle class.arrow_forwardCreate a class called “Rectangle” with properties of length and width, andincludes method to calculate the area and perimeter of the rectangle. Create aDriver class and instantiate 2 Rectangle objects that return the area andperimeter for each. Please feel free to set the length and width to values ofyour choice. for JAVAarrow_forwardWhy do we need encapsulation? What happens if data is not encapsulated within a class? Is there a way to access data which is encapsulated in a class? Give examples to encapsulate data within class and package. There is an online banking system where a user can view his contact info, account summary, add few beneficiaries account details. The bank officer can also view user's contact info, account summary, ATM card charges, bank charges, and other deductions. Create a class Customer and define its attributes and method names with proper access modifiers i.e. public, private or default.arrow_forward
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage