联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codehelp

您当前位置:首页 >> 数据库数据库

日期:2022-08-27 08:11

COMP2015 & COMP2016 Programming Techniques
Week 4 Practical Exercises
During the Week 4 practical complete the following exercises by producing an IPO chart, algorithm and working Java code using the Eclipse IDE.
Coding Standards: When writing code in this unit you are required to adhere to the Coding Standards and General Principles as defined in the FAQ in vUWS. Your code for the following
exercises should adopt these standards and general principles.
1.
a. As mentioned in the week 3 lecture Java defines a static method in the Math class named pow. Its purpose is to calculate the result of raising a number (the base) to the
power of a second number (the exponent), for example 194. The pow method can be called in a Java program by using a statement like Math.pow(base, exponent).

Write your own Java method named power that performs the same task as Math.pow. Your power method will need two integer parameters that represent the base and
the exponent. Your power method should return the result of raising the base to the exponent (ie, baseexponent). Your power method must not call the Math.pow method.
Note: your power method will only be accurate for relatively small values of the base and exponent.

Recall from basic mathematics that to calculate ab means multiplying a by itself b times, and that a0 = 1. For example:
53 = 5 * 5 * 5
194 = 19 * 19 * 19 * 19
270 = 1
50 = 1

The program fragment below obtains two positive whole numbers from the user, displaying the result of passing them to your power method and the Math.pow method.
Secondly, the program displays a SUCCESS message if the result of your power method is equivalent to the result of the Math.pow method (Note: your power method will
only be accurate for relatively small values of the base and exponent), or a FAILURE message otherwise. The Scanner class must be used for keyboard input.


static Scanner kb = new Scanner(System.in);

public static void main(String[] args) {
int base;
int exponent;

System.out.print("Enter a positive base: ");
base = kb.nextInt();
System.out.print("Enter a positive exponent: ");
exponent = kb.nextInt();
System.out.println("\nMy power method result: " + base + " raised to the power of " + exponent + " = " + call_your_power_method_here);
System.out.println("Math.pow method result: " + base + " raised to the power of " + exponent + " = " + Math.pow(base, exponent));

if (call_your_power_method_here == Math.pow(base, exponent)) {
System.out.println("SUCCESS – it looks like you wrote the power method correctly.");
} else {
System.out.println("Try again with smaller values of the base and exponent or something appears to be wrong with your power method.");
}
When writing the method ask yourself the following questions to help determine how the power method will be called/used:
§ What is the purpose of the method (all methods should perform only one task)? Remember that ‘one task’ does not necessarily mean ‘one executable statement’.
§ What are the main outputs of the power method – are there any?
§ What are the inputs to the power method – are there any?
§ What are the processing steps that the method will need to perform?
b. Write another method that will validate an integer number to ensure that it is not negative. Incorporate this method into your program to validate the base and exponent.
Test your code.

Class file name: The name of the class file should be PowerTest

2. Moose Population.
Copy your code solution for question 2 from week 3 (Moose Population) into your project for week 4. Modify your solution to incorporate two methods, validateInt and
validateFloat. Both methods should have two parameters, the number to be validated, and the minimum value allowed. Call the appropriate method(s) to validate the user input
before performing the calculations. All other aspects of the question are unchanged from week 3. The Scanner class must be used for keyboard input.

Class file name: The name of the class file should be Moose

3. Dimensions
Write a menu-driven Java program to enable a user to calculate the area and perimeter of right-angled triangles, squares, and regular pentagons. The menu needs to contain the
following options

1. Right Triangle
2. Square
3. Regular Pentagon
4. Exit Program

The program should not exit until option 4 is chosen. For each of the shapes in the menu the user should be prompted for the lengths of the appropriate dimensions and then the
program needs to calculate and display the area and perimeter of the chosen shape. Convert the following algorithm to Java code including appropriate methods with appropriate
parameter passing. As shown by the algorithm the program must validate user input and should not end until the user chooses to do so. The Scanner class must be used for
keyboard input. Output of area and perimeter of each shape should be displayed correct to two decimal places. Figure 1 shows example output from a sample run of the program.

Class file name: The name of the class file should be Dimensions

repeat
display Menu
validate menu Choice
switch menu Choice
1:
prompt “Enter the base length of the triangle”
get length
validate length
prompt “Enter the height of the triangle”
get height
validate height
display "Area = " calculate area
display "Perimeter = " calculate perimeter
2:
prompt “Enter the side length of the square”
get length
validate length
display "Area = " calculate area
display “Perimeter = " calculate perimeter
3:
prompt “Enter the side length of the pentagon”
get length
validate length
display "Area = " calculate area
display “Perimeter = " calculate perimeter
4:
display "Program terminated."
until menu Choice == 4

相关文章

版权所有:留学生编程辅导网 2021,All Rights Reserved 联系方式:QQ:99515681 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。