From
this Lab1, I have learnt how to identify and fix the common error.
In
Test1, the word ‘static’ is missing after ‘public’ in main method. The
static
keyword in Java means that the
variable or function is shared between all instances of that class as it
belongs to the type, not the actual objects
themselves.
In Test2, an error occurred as the declaration of the
variable ‘k’ is missing before we assign ‘k’+2 is equal to I. We need to
declare the value of variable by typing ‘double’ or ‘int’ if the value is
within the range for ‘double’ and ‘int’.
For
Test3, the statement int i=j=k=2 only declare i , j and k is not declared. In
order to fix the error, we need to declare the value of each term, that is
int i=2;
int j=2;
int k=2;
In
Test4, the answer of 5/2 should be 2.5 but the answer I get was 2 in the java
program. This is because the computer assumed that there is no decimal places
if we insert the amount is 5 instead of 5.0. In order to insert the amount as
5.0, we need to use ‘double’ instead of ‘int’ as ‘int’ is use for integral.
There
is an error occurred in Test5. The ‘R’ in line3 should be small capital
letters.
An
error occurred in test 6. Since the value range of ‘int’ is -2147483648 to
2147483647, the answer we get in test 6 is overflow and exceed the range of
‘int’. so, we need to put a letter of ‘l’ after 2147483647l to represent as the word long.
We
apply similar principle on Test7, long(sum) is used to calculate sum of long integral.
In
Test8, I applied what I have learnt in computational physics. To get the exact
value of 0.1, we need to decide the decimal format as one decimal point by
inserting ‘DecimalFormat df=new DecimalFormat(“#.#”)’ after the main method
line.
In Test9,Java
will not throw an exception if it is divided by float zero. It will detect a
run-time error only if it divided by integer zero
not double zero.
Through
this lab, I learnt how to design the solution to a problem by using Problem
Analysis Chart (PAC) and also gain the knowledge of hot to write, compile, and
run a simple computer programme based on PAC
https://mylab0programking1.blogspot.com/