Lab week 5
Objectives
• Get familiar with data type, variable, expression, method, parameter list and return type
• Distinguish local variables from parameters
• Get familiar with Class and object
• Get familiar with Sequential Execution
Exercise 1 (3%) is marked on a scale from 0-3 marks.
• Question 1: 0.5 mark
• Question 2: 0.5 mark
• Question 3: 0.5 mark
• Question 4: 0.5 mark
• Question 5: 0.5 mark
• Question 6: 0.5 mark
Question 1 (0.5 mark) :
The following program is for calculating the radius of a circle centred at the original according to the x coordinate and the y coordinate (x,y) of a point on the circumference.
The program asks the user to input the x coordinate and the y coordinate. Try the following example input, analyze the output and explain the reason of the output.
import java.util.*;
public class JavaApplication1 {
public static void main(String [] args){
double xCoordinate, yCoordinate, radius;//declare x, y, r as double
Scanner keyboard = new Scanner(System.in);//get input from keyboard System.out.print("Enter x: ");
xCoordinate = keyboard.nextDouble();//input x
System.out.print("Enter y: ");
yCoordinate = keyboard.nextDouble();//input y
radius = Math.sqrt(Math.pow(xCoordinate,2) + Math.pow(yCoordinate,2));//compute r = (x^2+y^2)^0.5
System.out.printf("Radius = %.2f\n", radius);//output radius with 2 decimal point precision System.out.print("Enter remainingDouble: ");
double remainingDouble = keyboard.nextDouble();//input remainingDouble, when did we input it?
System.out.printf("remainingDouble = %f\n", remainingDouble);//output
}
}
Example of the program output (the text in bold indicates the user input):
Enter x: 3
Enter y: 4 10
Radius = 5.00
Enter remainingDouble: remainingDouble = 10.000000
Question 2 (0.5 mark) :
Write a class to compute the property cost according to the following UML diagram and expressions. The area of a property is obtained from the command line arguments.
Expresssions required in method double calcTotalCharges(double) are as follows:
actualArea = area * RATE
propertyCharge = actualArea * UNITPRICE
propertyCost = propertyCharge + propertyCharge * GST
Input the command line argument: 155.67
Example of the program output:
Property cost: 263.70
Question 3 (0.5 mark) :
Analyze, compile and fix bugs in the following program so that the modified program will have the output in the example:
public class CircleBugs {
public static void main(String[] args) {
double t = Double.parseDouble(args[0]); int r = Integer.parseInt(args[0]);
System.out.println("r = " + r + ", t = " + t) double c = 2 * Math.PI * r;
double A = Math.PI * r * r; double x = r * cos(t); double y = r * sin(t);
System.out.println("c = " + c ); System.out.println("A = " + A ); System.out.println("x = " + x + ", " + "y = " + y );
}
}
Input the command line argument: 1.2 0.6
Example of the program output:
r = 1.2, t = 0.6
c = 7.5398223686155035
a = 4.523893421169302
x = 0.9904027378916139, y = 0.6775709680740424
Question 4 (0.5 mark) :
Draw the workflow of calcTotalCharges() method and main() method in Question
2. See diagrams of Sequential Execution in lecture notes.
Question 5 (0.5 mark) :
Implement a program that:
- Prompts the user to enter a floating point number nA
- Calculates nB = nA^2.1
- Displays nB with 1 decimal point precision if it is less than 129.0, otherwise it displays a constant value 129.0
Question 6 (0.5 mark) :
Implement a program that:
- Reads a floating-point number nA from the command line
- Calculates nB = e^nA where e is the base of natural exponential function
- Coverts nB to an integer number discarding its decimal part
- Displays nB
This assignment has been answered 5 times in private sessions.
© 2024 Codify Tutor. All rights reserved