OOF Assignment Part B – due: 10:00 am Tues 12th May this is the first and final hand in date p. 1 of 12
Department of Computer Science and Information Technology
La Trobe University
CSE1OOF Session 1, 2020
Assignment Part B
Due Date: Wednesday, 13th May, at 10.00 a.m.
First and Final date for SUBMISSION Wednesday 13th May at 10.00 am
Delays caused by computer downtime cannot be accepted as a valid reason for a late
submission without penalty. Students must plan their work to allow for both scheduled and
unscheduled downtime. There are no days late or extensions on this assignment as
execution test marking will begin on Thursday 14th May– in your assigned lab (Week 7)
This is an individual Assignment. You are not permitted to work as a Pair
Programming partnership or any other group when writing this assignment.
Copying, Plagiarism: Plagiarism is the submission of somebody else’s work in a manner
that gives the impression that the work is your own. The Department of Computer Science
and Information Technology treats academic misconduct seriously. When it is detected,
penalties are strictly imposed. Refer to the subject guide for further information and strategies
you can use to avoid a charge of academic misconduct.
Assessment Objectives:
To practise using the Math class and the String class. (Lecture/Workshop 2)
To practise reading from the console and writing to the screen.
To practice using switch and/or multiway selection branches
To practise using loops (repetition)
Submission Details: Full instructions on how to submit electronic copies of your source
code files from your latcs8 account are given at the end. If you have not been able to
complete a program that compiles and executes containing all functionality, then you should
submit a program that compiles and executes with as much functionality as you have
completed. (You may comment out code that does not compile.)
Note you must submit electronic copies of your source code files using the submit
command on latcs8. Ensure you submit the required file. For example, the file
Landing.java would be submitted with the command:
> submit SOF Landing.java
PLEASE NOTE: While you are free to develop the code for this progress check on any operating
system, your solution must run on the latcs8 system.
Marking Scheme: This assignment is worth 4% of your final mark in this subject.
Implementation (Execution of code) 90%, explanation of code 10%
You may be required to attend a viva voce (verbal) assessment
(to be used as a weighting factor on the final mark).
Do NOT use the LMS to submit your files, use latcs8 only
OOF Assignment Part B – due: 10:00 am Tues 12th May this is the first and final hand in date p. 2 of 12
Return of Mark sheets:
The face to face execution test marking in the lab (Week 7) constitutes a return of the
assignment mark, subject to the conditions on pages 14 – 15.
Please note carefully:
The submit server will close at 10:00 am on Wednesday May 13th
After the submit server has closed, NO assignments can be accepted.
Please make sure that you have submitted all your assignment files
before the submit server closes. (see pages 14 – 15)
There can be NO extensions or exceptions.
Everyone has been given a 1 week extension on the normal hand in
date for this assignment.
Your assignment will be marked in your normal lab, starting week 7,
Thursday May 14th.
If you cannot attend the lab you have signed up for on the LMS page,
please email me (m.ahmadi2@latrobe.edu.au) to arrange another
time.
Marking scheme:
90% for the code executing correctly
10%, you will be asked to explain (and / or alter) parts of your code.
You must attend the lab you have signed up for on the LMS OOF
page to have your assignment marked. If you have not signed up
for a lab on the LMS page, your assignment will not be marked
and it will be awarded 0. Non-attendance at the week 7 lab you
have signed up for on the LMS page will also result in your
assignment being awarded 0, except as detailed below.
OOF Assignment Part B – due: 10:00 am Tues 12th May this is the first and final hand in date p. 3 of 12
Please also note carefully that whilst we encourage innovation and exploring java beyond
what has been presented in the subject to date, above all, we encourage understanding.
All of the Tasks that follow can be solved using techniques that have been presented in
lectures, lecture / workshops and labs so far.
This assignment is based on Question 7 of Lab 4.
These are the techniques and knowledge that we will later be examining in the Real Time
Test (20 marks) and the exam (50 marks).
Code and techniques that are outside the material presented will not be examined, of course.
You are free to solve the Tasks below in any way, with one condition.
Any assignment that uses code that is outside what has been presented to this point must
be fully explained at the marking execution test. Not being able to fully explain code
outside what has been presented in the subject so far will result in the assignment being
awarded a mark of 0, regardless of the correctness of the program.
Submitting an assignment with code outside what has been presented so far and not
attending the marking execution test will result in an automatic mark of 0, regardless of the
correctness of the submission.
All assignments in OOF are marked, face to face, in the lab, in an
execution test. This means that we mark running code. Your code
must compile and display a result to the screen. Regrettably, we don’t
have the time or resources to look at code. The smallest amount of
code that produces and displays a correct result will gain more marks
than lots of code that doesn’t compile, run or display something to the
screen.
Using code not taught in OOF – READ THIS
How this assignment is marked
OOF Assignment Part B – due: 10:00 am Tues 12th May this is the first and final hand in date p. 4 of 12
Shapes and Images
In this assignment you will be making some tools to be used by graphic designers to automate placing
of graphical overlays on images.
You will need to make the following classes.
1. Rectangle.java
2. Oval.java
3. Triangle.java
4. ShapeDriver.java
The first three classes MUST ALL have:
1) A constructor that takes 5 arguments (int x, int y, int sizex, int sizey, Colour colour)
2) A method public void draw(BufferedImage img);
Only the ShapeDriver class should have a void main()
The driver program will take user input from the command line arguments.
You must not use the Scanner class!
Please read the “how to use command line arguments” section below.
The shape classes will all operate in a similar manor as described below.
The user will provide the following data via command line arguments:
1) The name of an input image. (optional)
2) The name of the shape (one of “rectangle”, “oval”, “triangle”)
3) The X (horizontal from the left edge) position of where the shape will sit on the image (in pixels)
4) The Y (vertical from the top edge) position of where the shape will sit on the image (in pixels)
5) The width of the shape
6) The height of the shape.
7) The name of a colour.
8) The name of the output image
If the image file exists, try to load it.
If not, show an error.
If the user did not provide an input file name then make a new image the size of the new image will be
the same size as the shape.
The shape is then drawn onto the image.
The image is saved as the same file name.
Your program is only expected to support png or jpg output files.
If the output file name provided is not one of these file types, then show an error.
If the user enters a shape name other than “rectangle”, “oval”, “triangle” show and error and list what
the valid shapes are.
OOF Assignment Part B – due: 10:00 am Tues 12th May this is the first and final hand in date p. 5 of 12
How to use command line arguments.
Consider the following Java program
public class MyProgram
public static void main(String[] args)
System.out.println(“Number of args: “+args.length);
System.out.println(“First Arg: “+args[0]);
System.out.println(“Second Arg: “+args[1]);
When you run this program from the command line you can pass it command line arguments like so
> java MyProgram hello world
Number of args: 2
First Arg: hello
Second Arg: world
As you can see you can access the command line arguments “hello” and “world” using the args
variable. In this assignment, you must take all user input via the args.
Make sure to check that the user has provided the correct number of arguments, if the user provides the
wrong number of arguments you must show an error that says what the program does, and what the
correct argument format is.
The input file name is optional, the user does not need to provide it. You need to check the number of
arguments to see if they provided it or not
The program will have the argument pattern like so:
> java ShapeDriver [infile] [shape] [x pos] [y pos] [width] [height] [color] [outfile]
> java ShapeDriver [shape] [x pos] [y pos] [width] [height] [color] [outfile]
For example
> java ShapeDriver Rectangle 10 15 30 40 red outFile.png
> java ShapeDriver inFile.png Rectangle 10 15 30 40 red outFile.png
The x pos, y pos, width, height are integers. The others are strings.
Note that args are always strings, you will need to convert some of them to integers.
This can be done using Integer.parseInt(String) like so:
String myStr = “123”;
int myInt = Integer.parseInt(myStr);
OOF Assignment Part B – due: 10:00 am Tues 12th May this is the first and final hand in date p. 6 of 12
How to use BufferedImage
The buffered image class provides an easy way to manipulate images in Java.
Remember that on a computer all images are represented as a series of pixels.
Each pixel has three colour components, red, green, and blue.
Each of these components ranges from 0 to 255. (8 bit)
With just these three colours, you can make over 16 million possible colours.
Red = 255, 0,0
Green = 0,255,0
White = 255,255,255
Black = 0,0,0
Yellow = 255,255.0
Grey = 128,128,128
Magenta = 255,0,255
Cyan = 0, 255, 255
You can read all about it here
https://docs.oracle.com/javase/8/docs/api/java/awt/image/BufferedImage.html
But the main methods we care about are:
1) int getWidth()
2) int getHeight()
3) int getRGB(int x, int y)
4) void setRGB(int x, int y, int rgb)
X and Y refers to the location of the pixel on the image, position 0,0 is the top left corner.
To create a blank BufferedImage use the following code:
BufferedImage myImage = new BufferedImage(width, height, TYPE_INT_ARGB);
The last argument is a built in Java constant, you don’t need to define it.
To load a Buffered image from a file, use the following code
BufferedImage myImage = ImageIO.read(new File(“image.png”));
To save a Buffered image to a file:
ImageIO.write(myImage, “png”, new File(“image.png”));
Note that you need to provide the file type, your program only needs to support “png” and
“jpg” you must look at the output filename to decide what name to use.
To use these classes, make sure to import the following:
import java.awt.image.BufferedImage;
import java.awt.Color;
import javax.imageio.ImageIO;
import java.io.*;
These also throw IOException, so you will need to add thows IOException to your main
method.
OOF Assignment Part B – due: 10:00 am Tues 12th May this is the first and final hand in date p. 7 of 12
Working with Colours.
You may notice that the get and set rgb takes a single integer, not three.
This is because the colours are bitpacked meaning that the three 8 bit values are packed into
one 32bit number.
For a red pixel, the binary the value is:
11111111 11111111 00000000 00000000
Alpha Red Green Blue
If you try to print it out in decimal is -65536 or in hexadecimal 0xFFFF0000
The alpha is how opaque (not transparent) the pixel, is an alpha of 0 means it is transparent.
For this assignment we will not use transparency all pixels will have alpha set to 255.
You can use bit shifting to access these values; however, Java provides a convenient class
that does this for you called Color.
https://docs.oracle.com/javase/8/docs/api/java/awt/Color.html
The parts we are interested in:
1) int getRed()
2) int getGreen()
3) int getBlue()
4) int getRGB()
5) Color(int rgb) – This constructor will take the rgb int value from BufferedImage.
6) Color(int red, int green, int blue)
Here is an example of how to it.
File file = new File(“img.png”);
BufferedImage myImage = ImageIO.read(file);
// get the pixel at the top left
int rgb = myImage.getRGB(0,0);
Color c1 = new Color(rgb);
System.out.println(c1.getRed());
System.out.println(c1.getGreen());
System.out.println(c1.getBlue());
// set the pixel to red.
Color c2 = new Color(255, 0, 0);
myImage.setRGB(0,0, c2.getRGB());
ImageIO.write(myImage, “png”, file);
Your program must support ALL the colours mentioned on page 6 and be case insensitive.
You must also add 3 additional distinct colours of your choosing.
OOF Assignment Part B – due: 10:00 am Tues 12th May this is the first and final hand in date p. 8 of 12
Images in PuTTY
SSH is unable to show images, so how will we view the images?
You could use WinSCP, however it is easier to take advantage of the webserver that is installed on
latcs8.
Open latcs8 and run the commands:
> mkdir ~/public_webprog
> chmod 701 ~
> chmod 705 ~/public_webprog
> cd ~/public_webprog
This will create a directory named public_webprog in your home directory and set the permissions to
allow public access. The ~ character is shorthand for your home directory.
Copy the sample image from CSILIB and set its permissions.
> cp /home/student/csilib/cse1oof/bu1-2020/b/* .
> chmod 705 *
You must do the chmod command for any files you put in here that you want to be accessible.
Open your web browser and go to the link below
BUT CHANGE THE NUMBER TO YOUR STUDENT ID!
http://webprog.cs.latrobe.edu.au/~12345678
You should be able to view the image.
If you get a message “403 Forbidden” make sure you did all the chmod commands above.
Sometimes it can take a minuet for the change to apply to the web server.
Press the “reload” button in your web browser to try again.
When you run your program, you can specify the output file to be in the public_webprog directory like
so.
> java ShapeDriver Rectangle 10 15 30 40 red ~/public_webprog/outFile.png
This will run your code and put the output into the webprog directory.
Do not put your Java code in the webprog directory because anybody will be able to download
it!
I have provided you a basic image
viewer that will not blur the image
when zoomed.
Open imgview.php in your web
browser to use the viewer.
Microsoft Paint also works well.
OOF Assignment Part B – due: 10:00 am Tues 12th May this is the first and final hand in date p. 9 of 12
Shape positioning
Here is an example of how the shapes are position on a small canvas the pixel grid is added for clarity.
The common notation for working with images is width (x axis) first then height (y axis)
We always work from the top left. The red pixel in the top left is the origin (0,0)
The yellow background shows the full width and height of each shape, it is only shown here for clarity
your final program must not include the yellow area. But it is helpful for when you are debugging.
The black pixel shows the x,y position for that shape, again only show here for clarity.
.
| Shape | Type | X | Y | Width | Height |
| A | Rectangle | 5 | 3 | 25 | 15 |
| B | Oval | 5 | 20 | 30 | 10 |
| C | Oval | 10 | 35 | 20 | 20 |
| D | Triangle | 40 | 2 | 20 | 35 |
| E | Triangle | 40 | 38 | 20 | 10 |
| F | Triangle | 40 | 50 | 20 | 5 |
OOF Assignment Part B – due: 10:00 am Tues 12th May this is the first and final hand in date p. 10 of 12
Note that your program MUST support drawing shapes outside of the canvas.
It is valid to draw a shape at a negative x and y position.
However, you are not expected to support negative widths or heights.
Note that the setRGB() method will throw and exception if you try to draw outside the canvas.
You must check that the pixel you are setting is not less than zero and not greater than the height or
width of the image. Do not use try & catch for this.
Rectangle.java
Do this one first.
The draw() method must take a BufferedImage as its argument, and draw the rectangle on the image.
This one is the easiest, can be done with two for loops.
Triangle.java
Do this one second.
The draw() method must draw an isosceles triangle with its base at the top and the point facing
downwards.
The way to think about this is to do the exact same as Rectangle except when you move 1 pixel down
the page, you need to “move in” some number of pixels.
The amount you “move in” each iteration will depend on the ratio of the height to the width.
If you consider a triangle with a width of 20 and a height of
10, then the sides would be 45 degrees.
The top row of pixels would be 20 pixels wide, but the
second row would be 18 pixels wide.
You have “moved in” 1px on the left and 1px on the right.
So, it is a 1:1 ratio, 1px in height has a “move in” of 1px
A triangle that is 35px tall, and 20px wide would have a “move in” of 0.29px
Notice that this is a float, you will need to do as much of the maths as you can as floats, then cast to an
integer at the last possible moment to reduce rounding errors.
OOF Assignment Part B – due: 10:00 am Tues 12th May this is the first and final hand in date p. 11 of 12
Oval.java
Do this one last.
Break the problem down by first considering an oval whose width is the same as its height.
You may recognise this as a circle.
Hint: Consider every pixel in the image above (both green and yellow) and for each of the pixels, ask
yourself “is this pixel inside the circle?” if so, then output a green pixel, else a yellow pixel.
Once you can draw circles, an oval is the same as a circle but “squashed” by the ratio of the
height/width.
Sample Output
Starting with a 320×320 base image of an apple, some shapes were added:
Notice that the circle could go outside the edge of the image.
OOF Assignment Part B – due: 10:00 am Tues 12th May this is the first and final hand in date p. 12 of 12
Electronic Submission of the Source Code
| |
Submit all the Java files that you have developed in the tasks above. The code has to run under Unix on the latcs8 machine. You submit your file from your latcs8 account. Make sure you are in the same directory as the file you are submitting. Submit the file using the submit command. |
submit SOF Landing.java
After submitting the files, you can run the following command that lists the files submitted
from your account:
verify
You can submit the same filename as many times as you like before the assignment
deadline; the previously submitted copy will be replaced by the latest one.
Please make sure that you have read page 2 about the submission close off date and
time and the compulsory requirement to attend the execution test in Week 7
Failure to do both things will result in your assignment be awarded a mark of 0,
regardless of the correctness of the program.
Execution test marks are provisional and subject to final plagiarism checks and
checks on the compliance of your code to this assignment document.
As such final assignment marks may be lower or withdrawn completely.
Final notes
Don’t let anyone look at your code and don’t give anyone a copy of your code. The plagiarism
checker will pick up that the assignments are the same and you will both get 0 (it doesn’t
matter if you can explain all your code).
There will be consultation sessions for the assignment, the times will be posted on LMS, if
you have problems come to consultation.
And a final, final, final note, “eat the dragon in little bits”. Do a little bit every night; before you
know it you will be finished. The assignment is marked with running code, so you are better
to have 2 or 3 parts completed that actually compile and run, rather than a whole lot of code
that doesn’t compile.
The execution test is done on latcs8 so please make sure that your
code runs on latcs8 before you submit.
The post CSE1OOF Session 1 appeared first on My Assignment Online.