首页 > > 详细

C++辅导ASSIGNMENT -Strings, ArrayLists, and more OOP辅导C++编程


Introduction

1. ,,path
2. ,path
3.
Requirement
ASSIGNMENT 4: Strings, ArrayLists, and more OOP
COMP 1020 Winter 2018
Page 1 of 4
D UE D ATE : M ARCH 23, 2018 AT 11:59 PM
Instructions:
 Only submit the java files. Submit them individually. Do not submit any other files. Do not submit a ZIP file!
REALLY REALLY do not submit a RAR file!
 To submit the assignment, upload the specified files to the Assignment 4 folder on the course website.
 Assignments must follow the programming standards document published on UMLearn.
 After the due date and time, assignments may be submitted but will lose 2% of the marks per hour late. The
last file submitted determines the late penalty for the entire assignment.
 If you submit a question multiple times, only the most recent version will be marked.
 These assignments are your chance to learn the material for the exams. Code your assignments
independently. We use software to compare all submitted assignments to each other, and pursue academic
dishonesty vigorously.
 To be eligible to earn full marks, your Java programs must compile and run upon download, without requiring
any modifications. Make sure your files DO NOT CONTAIN A PACKAGE LINE.
Assignment overview
In this assignment, you will implement graphics windows in which one or more paths will be drawn. A path is a list of
(x,y) coordinates that specify locations in the window, and a path is drawn using small circles at those locations, and
lines joining consecutive locations, as shown below.
Phase 1: Initial Setup
A. Define a public class Coord which will hold one (x,y) coordinate, as a pair of
int values. It’s OK to make the instance variables for x and y public in a
small class like this. Provide a constructor, which accepts the two int values
(x and y in that order). Provide a toString method which returns a
String in the format (143,319), mainly for testing and debugging.
B. Define a public class Path which will hold a list of Coords. Do this by
making Path a subclass of ArrayList.
a. Provide a constructor with one parameter of type PathWindow. In
order to draw the path in a window, you will need to know which window it is, so save this reference
to a window in an instance variable. The PathWindow class is described below.
b. This class should have only one instance method (for now): void plotPath() which will draw the
path as shown on the right. To do this, you will need to draw circles and lines. Methods will be provided
to do these things in the PathWindow class, which is described below. Don’t forget that Path will also
have a very large number of methods inherited from the ArrayList class.
C. Define a public class PathApp which will act as the “main program” for one particular window. This class will
behave a lot like a Processing program, which will be familiar to you if you took COMP 1010. It should contain
three methods:
a. A constructor with one parameter of type PathWindow. (This will be like setup() in Processing.) The
PathApp will need to know which window it is controlling. Save this reference in an instance variable.
In this phase, each window will contain only one Path, so you should create that new Path.
b. public void draw() will be called whenever the image in the window needs to be drawn. It should
use plotPath() to draw your Path.
c. public void mouseClicked(int x, int y) will be called whenever the mouse is clicked in
the window at location (x,y). In this phase, it should add that coordinate to the end of your Path.
d. Note that each of these two instance methods should contain only one line of code (for now).
D. A class PathWindow will be supplied to you. It will open three Java windows, and will test your classes. Just
click in the windows to create paths like the one shown above. If you have written your classes correctly, all
three windows should function independently. This class provides two methods that you can call to draw circles
ASSIGNMENT 4: Strings, ArrayLists, and more OOP
COMP 1020 Winter 2018
Page 2 of 4
and lines (below). These are instance methods that must be applied to a PathWindow object, since the circles
and lines will be drawn in one particular window. That’s why your classes need to keep track of which window
they’re using.
a. circle(int x, int y, int diameter)
b. line(int x1, int y1, int x2, int y2)
Note that the file supplied to you will be named PathWindowPhase1 but you will have to rename it
PathWindow before compiling it and running it. There will be separate PathWindow files for Phases 1, 2, and
3.
Phase 2: Operations on Paths
Add four more operations to your application. There will be a set of five
buttons drawn along the bottom of the window, as shown. The supplied
PathWindow class will manage the buttons for you. Exactly one button
will be selected at any time, and that button will be drawn in a darker
gray, as shown. This will control the mode of the program.
 The public instance method int getMode() in the
PathWindow class will tell you which mode is currently
selected.
 There are five public class (static) constants in the PathWindow
class: EXTEND, DELETE, INSERT, SELECT, and MOVE. The
getMode() function will return one of these 5 values.
 The operation of the program will be different in each of the
five modes, as specified below.
EXTEND mode: This is exactly as it was in Phase 1. When the mouse is clicked, a new Coord should be added to the
end of the path.
DELETE mode: When the mouse is clicked very close to a Coord in the path, that Coord should be deleted from the
path. (A line will now join the two Coords on either side of it.) Any click that is not close to a Coord in the path should
be ignored. Add a boolean near(Coord) instance method to your Coord class which will return true when two
Coords are very close together. Define your own constant to control how close is “close enough”.
INSERT mode: When the mouse is clicked anywhere close to a line in the path, a new Coord should be inserted at
that point in the path (between the two Coords that are the endpoints of that line). Determining if a point is close
to a line can be rather tricky, so a public static boolean pointNearLine(x,y,x1,y1,x2,y2,max)
function is supplied in the PathWindow class. It will return true if the point (x,y) is within a distance of max pixels
of the line joining (x1,y1) and (x2,y2). All parameters are type int, except for max which is double. Define your
own constant to control how close is “close enough”.
SELECT mode: Clicking on a Coord in the path should make that Coord the selected one. There can only be one
selected Coord, and at times there may be no selected Coord at all. Add an instance variable to your Path class to
keep track of which Coord is selected, if any. When the mouse is clicked on a non-selected Coord, then it should
become the selected one. When the mouse is clicked on the selected Coord, then it should be un-selected (and now
there will be no selected Coord.) In the Phase 2 version of the PathWindow class, the circle method has been given
an extra parameter: circle(int x, int y, int size, boolean selected). If the last parameter is true,
the circle will be drawn in red. Modify your Path class to draw the path with the selected Coord in red.
MOVE mode: When the mouse is clicked, the selected Coord should be moved to the location of the mouse click. It
should remain selected. If there is no selected Coord, then nothing should happen. (The SELECT and MOVE modes
work together.)
Modify your Coord, Path, and PathApp classes so that the program performs as described above. Add appropriate
methods of your own choosing to these classes. A file PathWindowPhase2 is supplied but you will have to rename
it PathWindow before compiling it and running it. There will be separate PathWindow files for Phases 1, 2, and 3.
ASSIGNMENT 4: Strings, ArrayLists, and more OOP
COMP 1020 Winter 2018
Page 3 of 4
Phase 3: Open and Save
In this phase, Open and Save buttons will be added that will allow a Path to be saved to a file, or read in from a file.
There is a new supplied PathWindowPhase3.java file. You must rename it PathWindow.java before using it. It will
add two new buttons labelled Open and Save. These buttons will not change the “mode”. Instead, they will immediately
call the following two methods, which must be added as instance methods to your PathApp class.
 public void doSave(): This method must use the code below to allow the user to create or choose a file.
JFileChooser chooser = new JFileChooser(“.”); //Use current directory
chooser.showSaveDialog(null);
File chosenFile = chooser.getSelectedFile();
It should write one line into this file for each Coord in the Path in this window, in the format previously
specified for the toString method. The contents of the file should have this format:
(87,178)
(61,326)
(272,408)
(380,331)
(512,121)
Make appropriate use of try-catch so that if anything goes wrong, a suitable message is displayed to the user
using a JOptionPane dialog box, as shown below. The program should continue running no matter what.
There should be no way to cause the program to crash. (Warning: the variable chosenFile could be null.)
 public void doOpen(): This method must use the code below to allow the user to select an existing file.
JFileChooser chooser = new JFileChooser(“.”); //Use current directory
chooser.showOpenDialog(null);
File chosenFile = chooser.getSelectedFile();
It should read in the selected file, which should contain one line for each Coord in a Path, like the files created
by the Save button. It should use a new type of constructor in the Coord class, described below, to turn each
line from the file into a Coord.
Once again, make appropriate use of try-catch so that if anything goes wrong, a suitable message is displayed
to the user using a JOptionPane dialog box, as shown below. The program should continue running no matter
what. If anything goes wrong, then the Path in the window should not be changed. It should only change if
the complete file was read with no errors.
 Add a second constructor to your Coord class that will convert a String in the format (123,456) into a Coord.
This is like a toString method in reverse. To be valid, the String must contain a pair of parentheses ( ),
inside of which is a comma, with two valid integers on either side of the comma. It may contain blanks anywhere
except inside a number. So the String “(12,34)” is valid, and “ ( 12 , 34 ) “ is valid,
but “( 1 2 , 3 4 )” is not valid. If the String is invalid for any reason, the constructor should
throw a NumberFormatException (yes, constructors can throw Exceptions, like any other method). Once
again, it should be impossible for this constructor to crash. You can use the int
Integer.parseInt(String) method to convert a String containing only digits into an int value. (That
method will also throw a NumberFormatException if something goes wrong, which is convenient. But it
does not use very good messages. It’s better to catch it and substitute a better message of your own, as shown
below. This is optional.)
ASSIGNMENT 4: Strings, ArrayLists, and more OOP
COMP 1020 Winter 2018
Page 4 of 4
Examples of some errors you could produce.
Hand in
Submit your .java files. Do not submit .class or .java~ files! Do not submit a .zip file or a rar file! You do not need to
submit the PathWindow.java files that were given to you. If you did not complete all phases of the assignment, use
the Comments field when you hand in the assignment to tell the marker which phases were completed, so that only
the appropriate tests can be run. For example, if you say that you completed Phases 1-2, then the marker will compile
your files and run the main method in version 2 of PathWindow. If it fails to compile and run, you will lose all of the
marks for the test runs. The marker will not try to run anything else, and will not edit your files in any way. (Make sure
none of your files specify a package at the top!)

 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!