Deeper Class Design - the Square, Circle, Picture Class he Bigger Picture – The ObjectList (or ArrayList (v1.0)) Class A picture is simply a composition of shapes, and in this last section, we’ll build a class used to manage such a picture. We’ll create this new class by reusing code from an existing piece of software. We’ll create an Picture class that will contain, amongst other state items (data), an array (or list) of Objects that are the Squares and Circles in the Picture to be drawn. Picture.java will be a simple abstraction here, and will just “draw” shapes to the console in the order that they appear in the list –ignoring the coordinate pairs stored in each Shape for now. Again, note the static storage restriction of only 100 shapes per picture; in future sections, we’ll learn how to dynamically resize our arrays. Aside: when we get to working with any of the Java graphics framework classes and/or swing, then a simple use of this Picture class (called a PicturePanel extending JPanel) would accommodate for much more interesting shape behaviors and interactions. Copy-and-paste the IntList.java code into a new file called Picture.java Note this is the same process we used to quickly create the Circle.java class Change the class name from IntList to Picture. Change the instance variable that is your int[] array to an Object[] array called myShapes. Change your “public void add(int nx)” method to “public void add(Object nx)”. Remove functions sum(), indexOf(), and save() and recompile using the provided ShapesPictureDriver.java driver. Run your main and notice it still processes a list of integers correctly Now run the driver code and uncomment the Picture segment and execute it. Data Members private Object[] myShapes; //used to be “data[]” This is an array of type Object, which can thus store any object of any class What about primitives? private int numElements; This integer tracks the number of live shapes in our array Method Members void add ( Object shape ); This function adds the Circle or Square to the array of circles @Override public String toString(); { //iterates through the array, calling toString() on each shape and appending this //to one large string to be returned Sample Output [] [] O O O Alternate Sample Output [] [] O O O Square class public class Square { privateintx; privateinty; privatedoublesideLength; private String shape = "[]"; public Square() { } public Square (int nx, int ny) { x = nx; y = ny; } public Square (int nx, int ny, double len) { x = nx; y = ny; sideLength = len; } publicvoid draw() { System.out.println(shape); } publicint getX() { returnx; } publicvoid setX(int nX) { x = nX; } publicint getY() { returny; } publicvoid setY(int nY) { y = nY; } publicdouble getSideLength() { returnsideLength; } publicvoid setSideLength(double sl) { sideLength = sl; } publicdouble getArea() { returnsideLength * sideLength; } public String toString() { returnshape; } publicboolean equals(Square that) { if (this.x == that.x & this.y == that.y && this.sideLength == that.sideLength) { return true; } return false; } }

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

In java language

Deeper Class Design - the Square, Circle, Picture Class

he Bigger Picture – The ObjectList (or ArrayList (v1.0)) Class

A picture is simply a composition of shapes, and in this last section, we’ll build a class used to manage such a picture. We’ll create this new class by reusing code from an existing piece of software. We’ll create an Picture class that will contain, amongst other state items (data), an array (or list) of Objects that are the Squares and Circles in the Picture to be drawn. Picture.java will be a simple abstraction here, and will just “draw” shapes to the console in the order that they appear in the list –ignoring the coordinate pairs stored in each Shape for now. Again, note the static storage restriction of only 100 shapes per picture; in future sections, we’ll learn how to dynamically resize our arrays. Aside: when we get to working with any of the Java graphics framework classes and/or swing, then a simple use of this Picture class (called a PicturePanel extending JPanel) would accommodate for much more interesting shape behaviors and interactions.

  1. Copy-and-paste the IntList.java code into a new file called Picture.java
    • Note this is the same process we used to quickly create the Circle.java class
  2. Change the class name from IntList to Picture.
  3. Change the instance variable that is your int[] array to an Object[] array called myShapes.
  4. Change your “public void add(int nx)” method to “public void add(Object nx)”.
  5. Remove functions sum(), indexOf(), and save() and recompile using the provided ShapesPictureDriver.java driver.
  6. Run your main and notice it still processes a list of integers correctly
  7. Now run the driver code and uncomment the Picture segment and execute it.

Data Members

  • private Object[] myShapes; //used to be “data[]”
    • This is an array of type Object, which can thus store any object of any class
      • What about primitives?
  • private int numElements;
    • This integer tracks the number of live shapes in our array

Method Members

  • void add ( Object shape );
    • This function adds the Circle or Square to the array of circles
  • @Override
    public String toString(); {
    //iterates through the array, calling toString() on each shape and appending this
    //to one large string to be returned

Sample Output

[] [] O O O

Alternate Sample Output

[]
[]
O
O
O

Square class

public class Square {

privateintx;

privateinty;

privatedoublesideLength;

private String shape = "[]";

 

public Square() {

}

 

public Square (int nx, int ny) {

x = nx;

y = ny;

}

 

public Square (int nx, int ny, double len) {

x = nx;

y = ny;

sideLength = len;

}

 

publicvoid draw() {

System.out.println(shape);

}

 

publicint getX() {

returnx;

}

 

publicvoid setX(int nX) {

x = nX;

}

 

publicint getY() {

returny;

}

 

publicvoid setY(int nY) {

y = nY;

}

 

publicdouble getSideLength() {

returnsideLength;

}

 

publicvoid setSideLength(double sl) {

sideLength = sl;

}

 

publicdouble getArea() {

returnsideLength * sideLength;

}

 

public String toString() {

returnshape;

}

 

publicboolean equals(Square that) {

if (this.x == that.x & this.y == that.y && this.sideLength == that.sideLength) {

return true;

}

return false;

}

}

 

public class ShapesPictureDriver {
//precondition: assumes {Square, Circle, Picture} all exist in the same working directory
//postcondition: 2 Squares, 2 Circles, and 1 Picture are constructed and manipulated, then reclaimed once mair
public static void main(String0 args) {
Square firstSquare = new Square();
Square secondSquare = new Square(10,20);
firstSquare.setX(3);
firstSquare.setY(4);
System.out.println( "Drawing the first square :
+ firstSquare.toString());
firstSquare.draw();
//secondSquare.setWidth(30);
//secondSquare.setHeight(30);
System.out.println( "Drawing the next square with area :
+ secondSquare.getArea());
secondSquare.draw();
//now for some circles
Circle firstCircle - new Circle();
Circle secondCircle - new Circle(5,5);
firstCircle.setX(1);
firstCircle.setY(5);
firstCircle.setRadius(3);
System.out.println( "Drawing the first circle :
+ firstCircle.toString());
firstCircle.draw();
secondCircle.setX(2);
secondCircle.setY(10);
secondCircle.setRadius(6);
System.out.println( "Drawing the second circle with area
+ secondCircle.getArea());
secondCircle.draw();
Picture picture
new Picture();
%3D
picture.gddSquare( firstSquare );
picture.gddSquare( secondSquare );
picture.gddCirsle( firstCircle );
Transcribed Image Text:public class ShapesPictureDriver { //precondition: assumes {Square, Circle, Picture} all exist in the same working directory //postcondition: 2 Squares, 2 Circles, and 1 Picture are constructed and manipulated, then reclaimed once mair public static void main(String0 args) { Square firstSquare = new Square(); Square secondSquare = new Square(10,20); firstSquare.setX(3); firstSquare.setY(4); System.out.println( "Drawing the first square : + firstSquare.toString()); firstSquare.draw(); //secondSquare.setWidth(30); //secondSquare.setHeight(30); System.out.println( "Drawing the next square with area : + secondSquare.getArea()); secondSquare.draw(); //now for some circles Circle firstCircle - new Circle(); Circle secondCircle - new Circle(5,5); firstCircle.setX(1); firstCircle.setY(5); firstCircle.setRadius(3); System.out.println( "Drawing the first circle : + firstCircle.toString()); firstCircle.draw(); secondCircle.setX(2); secondCircle.setY(10); secondCircle.setRadius(6); System.out.println( "Drawing the second circle with area + secondCircle.getArea()); secondCircle.draw(); Picture picture new Picture(); %3D picture.gddSquare( firstSquare ); picture.gddSquare( secondSquare ); picture.gddCirsle( firstCircle );
public class Picture { //or Intlist, caps at 100 elements with no bullet-proofing or bounds checks, etc.
private int numElements
private Object0 myShapes;
0; //This integer tracks the number of live shapes in our array
public void addCObject nx) {
//add the value n to array data
myShapes [numElements] = nx;
//add one to num_elements
numElements++;
//In a loop, append all the elements in our data array to this string, separated by a comma
public String toString() {
String retVal - "";
for(int i = 0; i < numElements; i++) {
if (i==0) {
retVal += myShapes[i];
else {
retVal = retVal + ",'
}
}
return retVal;
+ myShapes [i];
Transcribed Image Text:public class Picture { //or Intlist, caps at 100 elements with no bullet-proofing or bounds checks, etc. private int numElements private Object0 myShapes; 0; //This integer tracks the number of live shapes in our array public void addCObject nx) { //add the value n to array data myShapes [numElements] = nx; //add one to num_elements numElements++; //In a loop, append all the elements in our data array to this string, separated by a comma public String toString() { String retVal - ""; for(int i = 0; i < numElements; i++) { if (i==0) { retVal += myShapes[i]; else { retVal = retVal + ",' } } return retVal; + myShapes [i];
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Introduction to Interface
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
  • SEE MORE 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