Assignment 1
Assignment 1
Rationale
This assignment is designed to reinforce the subject material covered in the learning modules and encourage students to work consistently throughout the semester. This assignment has been designed to give students the opportunity to demonstrate their skill in: writing code to apply basic Java statements; passing arguments to methods and capturing the return value; designing applications that require user interaction; using good programming style. Question 1 [20 marks] (loops, random generator, selection) Write a program that could be used to help children practise their subtraction problems. The problems involve only integers between 10 and 99, inclusive. The program should start by asking the user how many problems they would like to try, and read this value from the keyboard. Then the program presents to the user that many problems and allows the user to enter an answer for each. After user has input each answer, the program then display a message indicating that the answer is correct, or if it is not correct, display the correct answer.
When all the problems are done, the program should print the percentage of problems answered correctly. Below is a sample run of such a program. The user input is shown in boldface.
How many problems would you like? Question 1: What is 10 - 8? 2 Correct! Question 2: What is 12 - 3? No, 12 - 3 is 9 Question 3: What is 5 - 0? Correct! Your score is 66.66% 5 8 3
NOTE: a) The subtraction should not result in negative answer. That means if the first integer is less than the second, you should swap the two randomly generated integers so that the answer will always be non-negative.
b) Write the solution program in a single file called 'Subtraction.java'. Write the whole program inside the main method.
Question 2 [20 marks] (Math, methods, no data validation) Write a program that calculates the volume and surface area of a cone, given its radius and perpendicular height. Print an informative introduction and prompt the user for the radius and height of the cone. Use the following formulae for the volume (V) and surface area (A) of a cone. 1 V = r 2h 3
A = r 2 + rs, where s = r 2 + h 2
The program should consist of the main method, together with three methods: one is to display the information introduction message, another to calculate and return the volume of the cone, and the last is to calculate and return the surface area of the cone. The radius and height of the sphere should be passed as a parameter to the methods. Question 3 [25 marks] (loops, Math, method, string, selection) Write a program to model a simple calculator. Each data line should consist of the next operation to be performed from the list below and the right operand. Assume the left operand is the accumulator value (initial value of 0.0). You need a method doNextOp that performs the required operation. doNextOp has three input parameters (the operator, the left operand, and the right operand), and return the result of the computation. The valid operators are: + add - subtract * multiply / divide ^ power (raise left operand to power of right operand) q quit Your calculator should display the accumulator value after each operand. A sample run follows:
A simple calculator ~~~~~~~~~~~~~~~~~~~ + 5.5 Result so far is 5.5 * 2 Result so far is 11.0 ^ 2 Result so far is 121.0 / 10.0 Result so far is 12.1 - 11.5 Result so far is 0.5999999999999996 * 2 Result so far is 1.1999999999999993 / 5 Result so far is 0.23999999999999985 + 10 Result so far is 10.24 q 0 Final result is 10.24
Question 4 [15 + 5 = 20 marks] (loop, selection, string, operators % and /) a) Binary numbers is important in computers. Binary number system uses only two digits, 0 and 1. There are pre-defined methods available in Java that will allow us to convert from denary integer (base 10) to binary integer (base 2). The method is:
Integer.toBinaryString(int)
So to convert the denary integer 12, you invoke the method by passing the integer 12 as parameter:
Integer.toBinaryString(12),
and the result returned is the binary equivalent of denary 10 as string 1100. In this question, you are to write a program that prompt user to enter a denary integer, and convert and display the integer in its binary equivalent. You are to write your own logic of conversion without using the pre-defined method. If you want, you may write a method that will accept an integer parameter, and return the binary equivalent as string, similar to the pre-defined method. b) Enhanced your question in (a) by including a method toHexa that will accept a denary integer and return the hexadecimal equivalent of the integer as string. There are 16 symbols used in hexadecimal (base 16), they are digits 0 to 9, a (for 10), b (for 11), c (for 12), d (for 13), e (for 14), and f (for 15). Again you have to write your own conversion logic, and not used the pre-defined Integer.toHexaString method.
Documentation
You should include comments in your code stating what each method does and explaining any complex sections of code. You should also include your student ID (and NOT name) as comments within the code. You should of course use meaningful variable names so that your code is to some extent self-documenting.
What to Submit
You should submit the following: A cover-sheet stating your student number. NOTE: Do not put your name on the coversheet. Printouts of your source code using Courier-New 10 point size font. Make sure that your long Java statements, if any, do not have the second line printed starting from the lefthand margin. One possible solution is to change the orientation of the page to landscape instead of portrait mode so that your source code presents correctly. Printouts demonstrating real interaction between yourself and your application A disk containing the all the source files, with extension .java Write your student number (NOT name) and subject code on the disk.
Marking Scheme
Question 1 requirements completed with correct results Question 2 requirements completed with correct results Question 3 requirements completed with correct results Question 4 requirements completed with correct results Assignment presentation, quality of code and documentation 20 marks 20 marks 25 marks 20 marks 15 marks