Sei sulla pagina 1di 7

Year 1 (Level 4) CSY1020 – Problem Solving & Programming

CSY1020 – Problem Solving & Programming


Assignment 2: Programming (Java) (50%)

Starting Date:Wednesday 12th June 2019 Finish Date: Friday 12th July 2019 (23h59).

Brief:
Using the course book (Douglas Bell, Mike Parr 2010, Java for Students + CD, 6th Edition)
0131735799) provide solutions to the following exercises:

Chapter 3 (p33) 3.3, 3.4 4 (p57) 4.1, 4.3 5(p84) 5.3, 5.4 6(p112) 6.2
Chapter 7 (p147) 7.3 8 (p167) 8.1, 8.2 9 (p191) 9.3, 9.4 11 (p224) 11.1, 11.2
Chapter 12 (p241) 12.4, 12.5 13 (p259) 13.1, 13.4 14 (p274) 14.4 15 (p298) 15.1, 15.2

Note: The course notes for the above 15 chapters can be found at:
http://194.81.104.27/~gary/csy1020/

Assignment Essential Requirements:


1. Clearly write out the 21 Exercise details (for every exercise) in the header of the code as
follows:
/**
Exercise: 4.3 Write a program that inputs three integer exam marks,
which displays the mean (average) mark as a double value.
Check your answer with a calculator.
Filename: Exercise4_3.java
@author: © Gary Hill (200WXYZ)
Course: BSc Computing
Module: CSY1020 Problem Solving & Programming
Tutor: Gary Hill
@version: 1.0
Date: 11/06/19
*/
2. Ensure your code is clearly commented.
3. Ensure that you show a screen shot in the report after the code to show that your program
works correctly.
4. If the program does not work clearly explain the problem/s and your thoughts on why the
program is not working.
5. The Exercises section of the report should include a commented file listing of all source
code.
6. It is expected that your report should make appropriate use of screen shots, particularly
during the Exercises section (which includes Implementation and Testing).
7. The source code files containing the main() methods and the compiled byte code class files
should be named by the exercise number as follows:
Excercise3_3.java & Excercise3_3.class.
Excercise3_4.java & Excercise3_4.class.

Technical Report
The report should be 2500 words (minimum) and should be structured as follows:
• Std. Front Sheet (Attached to this assignment, DO NOT include the whole assignment brief)
• Title Page
• Table of contents
• Exercises (all code for each exercise, screen shots, any problems).
• References
• Bibliography

1
E-Submission through NILE as a single WORD doc/docx.
To do this go to the NILE site for this module and under ‘Submit your work’ use the link
labelled ‘CSY1020-AS2-1819-Resit Assessment 2’. This is an individual assignment
Year 1 (Level 4) CSY1020 – Problem Solving & Programming
Notes on achieving the most from this assignment and where marks can be lost:
1 Carefully read and read again the assignment brief. Following the instructions given
carefully and check the front sheet to see where marks are allocated.
2 Always write in the third person i.e. no I thought this, I did this etc. If you need to refer
to yourself use “The author felt” etc.
3 If an assignment is not submitted a mark of G is given.
4 The brief stated that “The source code file containing the main() method and the
compiled byte code class files should be named in a particular way, failure to comply will
lead to lost marks.
5 A “Std. Front Sheet (Attached to this assignment)” should be attached to the assignment
with a Title Page.
6 Meaningful variable names should be used in code e.g. jBrotate (for the rotate JButton).
7 Your code should have meaningful comments e.g. //button to rotate 900 clockwise.

Exercises:

Exercise: 3.3: Draw an empty Tic Tac Toe (noughts and crosses) board.

Exercise: 3.4: Design a simple house and draw it.

Exercise: 4.1: Write a program to compute the volume of a box, given its three dimensions.

Exercise: 4.3: Write a program that inputs three integer exam marks, which displays the mean
(average) mark as a double value. Check your answer with a calculator.

Exercise: 5.3: write a method named displayEarnings with two integer parameters representing an
employee’s salary and the number of years they have worked. The method should display their total
earnings in a message dialog, assuming that they
earned the same amount every year. The program should obtain values via input dialogs prior to
calling
displayEarnings.

Exercise: 5.4: code a method which draws a circle, given the coordinates of the centre and the
radius its header should be:

private void drawCircle(graphics drawingArea, int xCenter, int yCenter, int radius)

Exercise 6. 2. Write a program which produces a random number between 200 and 400 each time
a button is clicked. The program should display this number, and the sum and average of all the
numbers so far. As you click again and again, the average should converge on 300. If it doesn't, we
would suspect the random number generator - just as we would be suspicious of a coin that came
out heads 100 times in a row!

Exercise 7.4 Betting A group of people are betting on the outcome of three throws of the dice. A
person bets $1 on predicting the outcome of the three throws. Write a program that uses the
random number method to simulate three throws of a die and displays the winnings according to the
following rules:

• all three throws are sixes: win $20;


• all three throws are the same (but not sixes): win $10;
• any two of the three throws are the same: win $5.

Exercise: 8.1: Write a program that uses a loop to display the integer numbers 1 to 10 together with
the cubes of each of their values.

2
E-Submission through NILE as a single WORD doc/docx.
To do this go to the NILE site for this module and under ‘Submit your work’ use the link
labelled ‘CSY1020-AS2-1819-Resit Assessment 2’. This is an individual assignment
Year 1 (Level 4) CSY1020 – Problem Solving & Programming
Exercise: 8.2: Random Numbers. Write a program to display 10 random numbers using a loop. Use
the library class Random to obtain integer random numbers in the range 0 to 9. Display the
numbers in a text field.

Exercise 9.3:
Bank account Write a program that simulates a bank account. A button allows a deposit to be
made into the account. The amount is entered into a text field. A second button allows a withdrawal
to be made. The amount (the balance) and the state of the account is continually displayed - it is
either OK or overdrawn. Create a class named Account to represent bank accounts. It has methods
deposit, withdraw, getCurrentBalance and setCurrentbalance.

Exercise 9.4:
Scorekeeper Design and write a class that acts as a scorekeeper for a computer game. It maintains
a single integer, the score. It provides a method to initialize the score to zero, a method to increase
the score, a method to decrease the score, and a method to return the score. Write a program to
create a single object and use it. The current score is always on display in a text field. Buttons are
provided to increase, decrease and initialize the score by an amount entered into a text field.

Exercise 11.1: Cost of phone call. A phone call costs 10 cents per minute.
Write a program that inputs via text fields the duration of a phone call, expressed in hours, minutes
and seconds, and displays the cost of the phone call in cents, accurate to the nearest cent.

Exercise 11.2: Measurement Conversion. Write a program to input a measurement expressed in feet
and inches via two text fields. When a button is clicked, convert the measurement to centimetres
and display it in a text field, correct to two decimal places. There are 12 inches in a foot; 1 inch is
2.54 centimetres.

Exercise 12.4: Write a program that allows items to be inserted into or removed from any position
within an array list, using suitable buttons.

Exercise 12.5: Improve the search method so that it displays a message whether or not the required
item is found in the array list.

Exercise 13.1: Nim.


A human plays against the computer. At the start of the game there are three piles of matches. In
each pile there is a random number of matches in the range 1 to 20. The three
piles are displayed throughout the game. A random choice determines who goes first. Players take it
in turns to remove as many matches as they like from any one pile, but only
from one pile. A player must remove at least one match. The winner is the player who makes the
other player take the last match. Make the computer play randomly: that is, it chooses a pile
randomly and then a number of matches randomly from those available.

Exercise 13.4: Rain data


Complete the program to handle rainfall data by including the following operations:
Add up the values and display the total.
Find the smallest value and display it.
Find the index of the largest value.

Exercise 14.4: Write a program to input the elements of an array in the same manner as the rainfall
program. It transposes the

array when a button is clicked and displays it.

3
E-Submission through NILE as a single WORD doc/docx.
To do this go to the NILE site for this module and under ‘Submit your work’ use the link
labelled ‘CSY1020-AS2-1819-Resit Assessment 2’. This is an individual assignment
Year 1 (Level 4) CSY1020 – Problem Solving & Programming
Exercise: 15.1: Write a program which inputs two strings from text fields, and which joins them
together. Show the resulting string in a text field.

Exercise: 15.2: Write a program which inputs one string and determines whether or not it is a
palindrome. A palindrome reads the same backwards and forwards, so "abba" is a palindrome.

Exercise: 9. Balloons Add to the class Balloon some additional data: a String that holds the name of
the balloon and a Color variable that describes its color. Add code to initialize these values using a
constructor method. Add the code to display the colored balloon and its name.
Enhance the balloon program with buttons that move the balloon left, right, up and down.

Exercise: 9.2 Thermometer. Some thermometers record the maximum and minimum temperatures
that have been reached.
Write a program that simulates a thermometer using a slider. It displays in text fields the maximum
and minimum values that the slider has been set to. Write a class that remembers the largest and
smallest values and compares new values. This class has methods setNewValue, getLowestValue
and getHighestValue.

Exercise 11.1: Cost of phone call. A phone call costs 10 cents per minute.
Write a program that inputs via text fields the duration of a phone call, expressed in hours, minutes
and seconds, and displays the cost of the phone call in cents, accurate to the nearest cent.

Exercise 11.2: Measurement Conversion. Write a program to input a measurement expressed in


feet and inches via two text fields. When a button is clicked, convert the measurement to
centimetres and display it in a text field, correct to two decimal places. There are 12 inches in a
foot; 1 inch is 2.54 centimetres.

Exercise 12.1: Write a program in which an item in an array list is deleted. Provide a 'delete' button
to delete the item that is specified as an index in a text field. Add a button to the program that
causes the array list to be emptied, using the method clear.

Exercise 12.2: Exercise 12.2: Add a button to the program in Exercise 12.1 that causes the array list
to be emptied, using the method clear.

Exercise 3.1: Nim.


A human plays against the computer. At the start of the game there are three piles of matches. In
each pile there is a random number of matches in the range 1 to 20. The three piles are displayed
throughout the game. A random choice determines who goes first. Players take it in turns to remove
as many matches as they like from any one pile, but only
from one pile. A player must remove at least one match. The winner is the player who makes the
other player take the last match. Make the computer play randomly: that is, it chooses a pile
randomly and then a number of matches randomly from those available.

Exercise 13.4: Rain data


Complete the program to handle rainfall data by including the following operations:
Add up the values and display the total.
Find the smallest value and display it.
Find the index of the largest value.

Exercise 14.1:Data handler


Write a program that uses a 4 x 7 array of numbers similar to the rainfall program (with output as
shown in Figure 14.2). Extend the program to carry out the following operations:

When a button marked ‘sums’ is pressed, add up the values for each of the seven columns and add
up all the values of each of the four rows and display them.

4
E-Submission through NILE as a single WORD doc/docx.
To do this go to the NILE site for this module and under ‘Submit your work’ use the link
labelled ‘CSY1020-AS2-1819-Resit Assessment 2’. This is an individual assignment
Year 1 (Level 4) CSY1020 – Problem Solving & Programming
When a button marked ‘largest’ is pressed, find the largest value in each row, the largest in each
column and the largest value in the complete array.

When a button marked ‘scale’ is pressed, multiply every number in the array by a number entered
into a text field. (This could be used to convert from centimetres to inches. (p274 – 6th Edition)

Exercise: 15.1: Write a program which inputs two strings from text fields, and which joins them
together. Show the resulting string in a text field.

Exercise: 15.2: Write a program which inputs one string and determines whether or not it is a
palindrome. A palindrome reads the same backwards and forwards, so "abba" is a palindrome.

Useful resources:
Guide To Writing Technical Reports at :
http://www.eng.nene.ac.uk/~gary/BScC/reportwriting.html

One of the past assignment reports at:


http://194.81.104.27/~gary/csy1020/20042005/csy1020report0405.pdf.

Rough Guide to Harvard Referencing:


http://library.northampton.ac.uk/pages/hrg

Screen shot software:


http://www.mirekw.com/winfreeware/mwsnap.html

Remember to Reference your Software Code


Author (Year) Title of Program (Version Number) [format type] (computer program, software or
code, Place of publication: publisher (if available). Available from: URL (if online).

E.g.
In Text or Code: (Eclipse, 2014)

In Reference list:
Eclipse (2014) Eclipse Lunar (Version 4.4.1) [Software] Eclipse.org. Available from:
http://www.eclipse.org/downloads/ [Accessed 12 Feb 2014]

1 Coding Conventions
A set of coding conventions should be followed throughout, which help in the creation of
code which is more efficient, maintainable, robust and most importantly – readable. The
following guidelines should be used:

Naming Rules:
Variables were prefixed with lowercase mnemonic indicating nature of object. E.g.
jBVariable(JButton), iconBall(ImageIcon), fVariable(float), dVariable(double),
boxVariable(Box), bVariable (boolean), jTHello (JTextfield), jLDirection (JLabel), nSquare
(int) bgVariable (BranchGroup) etc.

Align brackets:
By default eclipse places starting bracket for a code construct at end of the starting line,
and aligns the closing bracket with the first column of the construct. This was changed to
align the starting and ending brackets on the same columns, so that a reader can clearly
identify the borders for a code construct. E.g.:
for (int i=0; i<20; i++)

5
E-Submission through NILE as a single WORD doc/docx.
To do this go to the NILE site for this module and under ‘Submit your work’ use the link
labelled ‘CSY1020-AS2-1819-Resit Assessment 2’. This is an individual assignment
Year 1 (Level 4) CSY1020 – Problem Solving & Programming
{
// … Place code here
}

Code Indentation:
A practice often ignored by most programmers, indenting is a good practice that helps
immensely with code readability and debugging.

Commenting:
All important areas of the code should be commented, clearly explaining the functionality.
As with some of the techniques above, this helps with code readability, maintenance and
debugging.

2 References
Eclipse Foundation (2014). Eclipse SDK 4.4.1 [online]. Available from:
http://www.eclipse.org [Accessed 12 Feb 2014]

Sun Microsystems (2014a) Lesson: Exceptions. Available from:


http://java.sun.com/docs/books/tutorial/essential/exceptions [Accessed 20 may 2014].

Wikipedia (2014) Code Refactoring [online]. Available from:


http://en.wikipedia.org/wiki/Refactoring [Accessed 12 May 2014].

6
E-Submission through NILE as a single WORD doc/docx.
To do this go to the NILE site for this module and under ‘Submit your work’ use the link
labelled ‘CSY1020-AS2-1819-Resit Assessment 2’. This is an individual assignment
Year 1 (Level 4) CSY1020 – Problem Solving & Programming

BSc/BEng/HND COMPUTING
(Year 1)
Problem Solving & Programming (Assignment 2: Java)
Due for Issue Wednesday 12th Date for Friday 12th July
(week commencing): June 2019 Submission: 2019 (23h59).
Agreed Date for late Module Tutor: Gary Hill
submission: Signed:
Student Name:
Student ID:
Assessment Feedback
Aspect (& weighting)

Needs much more


Needs some more
Satisfactory
Very Good
Excellent

work

work
Exercises (Quality of Coding, Code
Listing Screen Shots) (90%):
Report Presentation (Format, Layout,
Grammar, Syntax, Spelling) (10%):
Specific aspects of the assignment that the marker Specific aspects of the assignment that
likes: need more work:

Tutor’s Signature: Date: Grade:

Note: Copy this page to the front of your assignment submission

7
E-Submission through NILE as a single WORD doc/docx.
To do this go to the NILE site for this module and under ‘Submit your work’ use the link
labelled ‘CSY1020-AS2-1819-Resit Assessment 2’. This is an individual assignment

Potrebbero piacerti anche