Java代写:CS3100 Shapes, Shapes, so many Shapes



The purpose of this assignment is to get a better understanding of inheritance in Java by writing and using derived classes.

Brief Description

Your program will read in a plain text file full of commands to create pictures, with each picture full of shapes, draw the pictures, and make the pictures “dance”. The program will follow the commands; in the order the commands are given.


You will write six classes. Five of them will be Circle, Rectangle, Colored Rectangle, a driver program called, and a geometric shape of your choice that must be a descendant of Shape.
The sixth class will be Picture, a generic class that stores Shapes. Picture will have:

  • draw(Graphics g, Color c, int x, int y… other parameters…): a method that, when called, draws all the non-colored shapes in the Picture collection in the color specified as a parameter to draw(). (Any ColoredRectangles in the collection will be drawn in their own colors. All other Shapes will be drawn in the color specified in the call to Picture.draw().) draw() will also have two integer locations, specifying the location for the whole picture. draw() may have parameters, in addition to the color, x and y, if you find them useful.
  • dance(Graphics g, int x, int y, int maxX, int maxY… other parameters…): a method that, when called, makes all the individual shapes within the Picture dance. dance() will have at least four input parameters: x and y, for the location where the dances will start/be centered around, and maxX and maxY, for the largest possible x and y at which shapes will be visible (the size of your DrawingPanel window). (When implementing dance(), students choose whether in the x and y input specify the starting location for a dance, or specify the center of a dance.)
  • Important Note: Picture dance() and draw() may involve parameters not specified by me. However, the Shape objects the Picture draws must be stored as private data by each Picture object and must not be passed in as parameters to dance() or draw().
  • whatever private data fields or supporting methods you find necessary and appropriate

Hint: Consider using one of the members of the Java Collections Framework as either the parent class for Picture or as a data member within Picture.
Given: the base class for the shape classes. Read the comments in! The comments in describe the input parameters for the methods. The “dancing” in the dance() method should happen at a slow enough speed that a human being can see what is happening.
Circle will be a non-abstract subclass of Shape with added data that controls size. You may choose to make the size be the radius of the circle, the diameter, or something else. However, a circle of size 10 should be about twice the size of a circle of size 5. Your Circle class must work
Rectangle will be a non-abstract subclass of Shape with added data “length” and “width”.
ColoredRectangle is a derived class of Rectangle with added data “color” (which will be stored as a Java Standard Library Color object).
YourClass is a subclass of Shape or of one of its descendants with at least one added data item and appropriate methods. In the input files provided, “Sshape” specifies that a “student shape” should be created. Five numbers are given as sample input – two are to specify the x, y location of the shape, but the other three, plus the color and the word, may or may not be used by you as inputs to your shape creation.
Your shape classes must work with test code similar to that in (In other words, the grader has,, etc.)


Your code must properly adhere to The Rules of object-oriented inheritance, particularly the rule that “Subclass IS-A ParentClass” must always be a true sentence. (Examples: “Car IS-A Vehicle”, “CircularLinkedList IS-A LinkedList”, “Dog IS-A Mammal”.) “Picture IS-A Shape” is NOT a true sentence. Consider a child drawing a picture of a pig – a big, pink circle for the body, four rectangles for the legs, smaller circle for the head, perhaps another rectangle for the snout. The picture of a pig is a grouping of shapes.
Write a driver (client program), called, that reads commands to create pictures and fill them with shapes. When all shapes have been added to a particular picture, the input files may specify that the picture be drawn in a particular color and location, or made to dance. The input files may specify the creation of as many pictures as the user wants, each picture containing as many shapes as the user wants, and shapes may be drawn as many times as the user wants.

  • The driver program must run successfully from the command line / command prompt of a computer (Terminal program on Mac computers, Command Prompt on Windows, etc) using commands like “java Homework1 java Homework1 hwk1input1.txt”, “java Homework1 hwk1input2.txt”, and, in general, “java Homework1 validInputFileName.txt”.
  • When main() starts, in your, args[0] will store a String equal to “validInputFileName.txt”, if the grader were to use the above command. (You can see sample code for reading in an integer this way in the sample driver you were given for MiniProgram 2.) Some sample input test files are provided below, in the list of useful files.
  • Here is sample code to grab the name of the file the user wants your program to open, and open it:
if (args.length <= 0) {
System.out.println("Please use: java Homework1 validInputFileName.txt");
File f = new File (args[0]);
Scanner input = new Scanner(f);

You might consider embedding your “Scanner input” creation inside a try-catch statement (see the appendices of your textbook for instructions on how to do this), since the Scanner constructor will throw a “FileNotFoundException” if the user types the name of a file that does not exist and your program tries to open the non-existant file for reading.

Useful files

  • Hw1pics.pdf, trying to explain the relationships between Shapes and Pictures
  •, the base class for the shape classes
  •, some “starter code” for your Picture class – this code is intended to give you ideas; it will not compile or work as-is
  • You will also need (look under “Code files that appear…”, part of the way down the page).
  • hwk1input1.txt (circle)
  • hwk1input1a.txt (rectangle)
  • hwk1input2.txt
  • hwk1input3.txt

The grader will have a number of additional plain text files full of commands to create pictures, draw them, and make them dance, to use when testing your programs.
Commands may be found in input files:

  • start picture N: begins the creation of a new picture, and provides a single character identifier for that picture. The identifier may be used in later commands to specify which picture to draw or dance.
  • end picture: ends the creation of a particular picture. Pictures may only be created one at a time. Between start and end of picture creation, there should be only commands to create new shape objects.
  • erase: erase the entire window. (Try drawing a rectangle the size of the DrawingPanel window, the same color as the background.)
  • circle x y size: create a new Circle object, with the given x and y values, and add it to the current Picture. Size will control the size of the circle when it is drawn.
  • rectangle x y length width
  • coloredrect x y length width color
  • Sshape x y int int int color word: create a shape of your choice. The first two integers should specify the x and y coordinates of the shape within the picture. The other integers and words may be used, or not, as you need.
  • draw picture N color int int : draw picture with identifier N, in the color named, at the x y location specified by the two integers.
  • dance picture N x y: make picture with identifier N dance, starting from or centering around the x y location specified by the two integers. The dancing should happen slowly enough that a human being can see what is happening.

Hint: Take a look at the Java Graphics class.
You may add additional data and functionality to your classes as desired. You may not modify Shape source code.
(If you think there is a compelling reason to change Shape, contact the professor. Any change made will apply to the code used by all cs3100 students.)
Insights and Lessons Learned. In readme.txt include a paragraph or two describing what you learned from writing and testing this program. Do your very best to make this well-written. This will be an aspect of the assignment that will count heavily.

What To Turn In

Here is the list of things you have to turn in:

  1. Upload to the homework submission system before midnight on the due date:
    • Your final source code. I will compile and test your programs on the Macs in the lab.
    • Your insights and lessons learned ‘readme.txt’ file.
    • Also upload a plain text file containing commands you created for your program, to show off the best parts of what you implemented.