首页 > > 详细

SWEN20003辅导、Software讲解、java讲解、java程序语言调试辅导Database|解析Haskell程序

SWEN20003
Object Oriented Software Development
Workshop 4
Rohyl Joshi
Semester 1, 2020
Discussion
Input/Output
1. What is the difference between Scanner.next() and Scanner.nextLine()?
2. Do you need to remember the code for setting up file input and output in the test and exam?
Maven
Maven is a project management tool. Similar to Makefiles in C, it allows you to specify structure, compiler
settings, and dependencies for Java projects. Maven projects are specified in a file called pom.xml.
1. What are some of the important considerations when sharing software projects with other developers?
2. What is a dependency in software engineering?
3. Rather than requiring users to manually download dependencies like Makefiles, Maven has an ecosystem of
online packages that are downloaded as required. What are the benefits and drawbacks of this approach?
1
Problems
Use IntelliJ to write solutions to these exercises.
Bagel
We will learn how to use Bagel today. Bagel (Basic Academic Graphical Engine Library) is a simple graphics
library that you will use throughout your projects this semester. It is designed specially for use in SWEN20003.
Full documentation of all the classes and methods is available at https://people.eng.unimelb.edu.au/
mcmurtrye/bagel-doc/.
Note: Bagel is a beta version. If you encounter bugs or errors that are not due to your code, please email
Rohyl at rohyl.joshi@unimelb.edu.au.
Your tutor will walk you through the steps below. Pay close attention.
Note: The following steps deviate slightly from Lecture 7, you can follow either way - as long as you can get
the same end result (a working Bagel game).
1. First, we’ll import the Maven project.
(a) Download the zipped folder for this workshop from the LMS and extract it.
(b) Open IntelliJ and close any open projects you have (File -> Close Project).
(c) Click Import Project and navigate to the extracted bagel-starter-pack folder, selecting it and
pressing OK.
(d) Make sure the setting Import project from external model is set, and Maven is selected.
(e) If you are asked to choose what LWJGL natives to install, tick the one appropriate for your system.
Otherwise, leave the default settings and finish creating the project by pressing Next several times,
then Finish.
2. Next, we’ll import the Bagel library. Since Bagel is only used within the University, Maven can’t import
it for us. There are two main ways to do this:
If bagel.jar is in the project directory
(a) In IntelliJ, right click on bagel.jar and click “Add as Library”
Otherwise
(a) In IntelliJ, click File -> Project Structure....
(b) Click Libraries and press the + button at the top left of the window.
(c) Select Java and navigate to bagel.jar.
(d) Press OK to close all windows.
We will begin by looking at the most basic Bagel program.
1. Open src/Game.java
2. Run the program by doing one of the following:
• Clicking the green Run button on the gutter next to the main method.
• OR Right clicking Game.java and clicking ”Run”
• OR Manually creating a run configuration
3. You should see a window appear with a blue background.
2
Next, we will write a simple game using Bagel. Copy the contents of the assets folder that came with the
tutorial package into the res folder of your project. You should then have a directory structure similar to this:
bagel-starter-pack
lib
bagel.jar
res
fonts
conformable.otf
images
house.png
player.png
map
map.tmx
simple.png
tileset.tsx
src
Game.java
If there are extra files and folders (such as .idea folder, .iml file, target folder), they are there to support
your IntelliJ project, do not worry about them and leave them as is.
Here are the specifications for the game:
i. The game should load and render the map specified by the map file res/map/map.tmx
ii. When the game starts, our player should be rendered to the screen at the point: (50, 350), and our
house should be rendered at point: (850, 180).
iii. The player should be able to move left, right, up, and down, using the respective arrow keys at a constant
speed. Try different values for the speed (a constant), starting at 1.
iv. The player should not be able to move to a coordinate if its “blocked” map property is set to true.
v. If the player comes within 55 pixels of the center of the house door, print to the console “Welcome home!”.
The center of the door is located at point: (854, 268),
Hint: a simple approach would be to try Pythagoras to figure out the distance between objects.
vi. Instead of printing the greeting to the console, draw the text to the screen using the font provided.
vii. The game should exit when ESC is pressed.
The Map
When the game is run, the map should be rendered to the screen. The map files for this project will be supplied
in the TMX format. Bagel has rudimentary functionality to parse and render a tiled map, so you do not need
to worry about the specifics regarding the map format. The supplied map contains one main piece of metadata:
• Blocked tiles
This metadata comes in the form of a property value, search the Bagel docs to find out how to access this
property value.
It is highly suggested that you spend some time exploring Bagel’s documentation.
3
Your game should look like this when you are done:
Change your program so that the player is in a separate class called Player, in a file called Player.java.
The player should have the following attributes:
1. an x and y position
2. an Image
The player class should have the following methods:
1. getX() that returns the x position
2. getY() that returns the y position
3. move(double dx, double dy) that moves the player to a new position (x + dx, y + dy).
4. render() that draws the player to the screen.
Make sure this new class is used correctly by your main class!
Bonus #1: Investigate the Rectangle class to see if you can more accurately tell when the player is near
the door.
Bonus #2: Modify the controls so that forward and backward accelerate/decelerate the plane, rather
than turning on/off its motion.
Bonus #3: See how else you can improve the object-oriented design of your solution; add a class for the
house. There are plenty of other potential extensions to challenge yourself. Have fun!
4
Input/Output
1. Copy input.txt and dict.txt to the root directory of your project. input.txt contains a modified
excerpt from a famous book. Each line of dict.txt contains a word that is a part of our custom ‘dictionary’.
Our dictionary contains most words in the book, with a few exceptions.
Your task is to find all words that are in the book but not in the dictionary and write them to
output.txt. You may find the following skeleton code structure useful, but are not required to use it.
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class DictionaryCheck {
private static final String DICTIONARY_FILE = "dict.txt";
private static final String INPUT_FILE = "input.txt";
private static final String OUTPUT_FILE = "output.txt";
private static final int MAX_DICTIONARY_WORDS = 11_000;
public static void main(String[] args) {
try (Scanner dictionaryReader = new Scanner(new FileReader(DICTIONARY_FILE));
Scanner inputReader = new Scanner(new FileReader(INPUT_FILE));
PrintWriter outputWriter = new PrintWriter(new FileWriter(OUTPUT_FILE))) {
// Read in the words from the dictionary file using the
// provided dictionaryReader object
String[] dictionaryWords = readDictionary(dictionaryReader);
// Read in all the words from the input file
String[] inputWords = readInputFile(inputReader);
/*
For each word, check if it is in the dictionary array.
Make sure to 'clean' the input words to remove characters
that are not letters before checking if it exists in the
dictionary array. You can either use Regex (short way)
or manually remove non-letter characters (long way).
Make sure to use the outputWriter to write your results
to output.txt
*/
} catch (IOException e) {
e.printStackTrace();
}
}
public static String[] readInputFile(Scanner inputReader){
// Use the scanner that is provided to read the input file
}
public static String[] readDictionary(Scanner dictionaryReader){
// Use the scanner that is provided to read the dictionary
}
// Feel free to define more helper methods down here
}
5
Assessable Questions
Timetabling
Frustrated with the lack of adequate timetable planning tools for your school, you are determined to provide
a planning system to your students. Equipped with the knowledge of classes and the power of object-oriented
programming, we want to build a system that helps students plan their timetable1
. One core component of
the system is, of course, a Timetable. However, there can be many, many timetables possible so we want to
be able to derive some insights into a particular timetable such as: “how many hours a day am I spending in
class?”, “let’s be real, i’m skipping lectures - does that number go down?”, and “how many hours of my day
will I spend on campus?”.
Overview
A Timetable is comprised of a number of Subjects and a number of Activities from these subjects. An
example of an activity is “Workshop 1” at 5pm on a Tuesday or “Lecture 2” at 11am on a Friday. Each subject
has a number of activities available.
Project Overview
A number of classes come with the timetabling project. There is Timetable which is an interface - you will
learn about these soon. There is also TimetableTest which checks the logical correctness of components of
your solution (unit tests). You are also given a skeleton of a class: UnimelbTimetable, this is the file you
will build your solution in.
Data Sources
You are provided with:
• Four files (one for each subject) that contain the list of activities a particular subject has to offer. (e.g.
SWEN20003.csv).
• Two CSV files that contain timetable data for a ‘simple’ timetable and a ‘regular’ timetable.
You are not required to interact with these files at all in your code. They are there to support the
testing class.
Your Task
In the class UnimelbTimetable there are unfinished methods:
• void loadSubject(String subjectCode, String[] activityInformation)
Loads a subject into the timetable.
The parameter String[] activityInformation is an array of activity information for a subject. Each
String in the array follows the format: activity code, activity name, day, start, finish.
day is a zero-based index representing the days of a typical university week Monday (0) to Friday (4).
start and end represent the starting time and ending time of an activity. It is represented by the
number of hours since midnight (0). For example, a value of 17.25 represents 5:15pm.
You can see these lines in the CSV files for each subject.
• void loadActivityFromCode(String activityCode)
Loads an activity into the timetable.
Activity code will always be valid input, for example: SWEN20003/U/1/SM1/L02/1.
• double[] getDailyHourTotalsExcludingLectures()
Returns an array of 5 doubles. Each element at index i is the sum of the length of all activities on day
index i of the week excluding lectures. An activity is a lecture if its activity name starts with “Lecture”.
1Shameless plug: http://lookahead.rohyl.io/
6
For example, if there were only three activities in the timetable, a tutorial on Monday that went for
3 hours, a lecture on Thursday that went for one hour, and a tutorial on Thursday that went for 2 hours,
the method would return: [3, 0, 0, 2, 0]
• double[] getDailyHourTotals()
Returns an array of 5 doubles. Each element at index i is the sum of the length of all activities on day
index i of the week.
• double[] getDailyTimeAtUni()
Returns an array of 5 doubles. Each element at index i is the total time that would be spent at uni for
day index i. The time spent at uni for a day is the difference between the end time of the last activity of
the day and the start time of the first activity of the day .
Your task is to implement these methods, such that the methods work as intended and pass all the tests
in the TimetableTest class. You are free (and highly encouraged) to create more classes and methods 2
. You
are not required to do any I/O operations, this is handled for you in the TimetableTest class. You are to
work with the data passed into the methods as parameters. You can assume all data passed in is well-formed
and complies with data format in the CSV files (located in the res/ folder). You can assume that timetables
will not have clashes.
Deadline
The due date for this assessable problem is Friday the 1st of May at 11:59pm.
Running Tests
To test your solution, the TimetableTest class is provided for you. This does basic checks on your statisticoriented
methods with a simple timetable and a regular timetable. To run the test, right click on the class and
click “Run”. Note that this doesn’t guarantee logical accuracy of your code, but you can be fairly confident
if the tests pass. These tests will be similar to the ones run for marking, and you are free to create your own
driver code, or additional tests.
Marking Criteria
You will get full marks if all marking testcases pass, half marks if some fail, and no marks if all fail.
Submission
Once again, you are expected to push your code to your GitLab workshops repository. Just like the Tutorial
3, you will maintain the following file/folder structure:
username -workshops
workshop-3
workshop-4
src
Timetable.java
TimetableTest.java
UnimelbTimetable.java
Having more files and folders than shown is expected and acceptable. You just need to ensure that the core
structure is followed. You can find a reference repository here.
Getting Started
Copy the “workshop-4” folder from workshop-4.zip into your local repository. Open IntelliJ and close any
open projects you have (File -> Close Project). Click Import Project and navigate to the workshop-4
folder you just copied, selecting it and pressing OK.
There is also a video guide on the LMS going through this entire process, including submission.
2You may be tempted to create a class called “Class”, but you won’t be allowed to do that - call it “Activity” instead
7

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

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