OOP_Assignment 1.docx
-
Upload
tn-zahiruddin -
Category
Documents
-
view
22 -
download
5
Transcript of OOP_Assignment 1.docx
Assignment Chapter 2: Elementary Programming
There is an error in the codes above which is in line number 2. The main method of the codes is
not declared as static. Main method should be declared as public, static and void in Java.
Output when the main method declared as public void main (string [ ] args) is below;
Figure 1 : Error when main method is not declared as static
Otherwise when the main method is declared as public static void main (string [ ] args), the
output will be as below;
Assignment Chapter 2: Elementary Programming
In order to let the user enter a double value from the keyboard, we need to import
“java.util.Scanner” followed by the statement below in the main ;
Scanner myinput = new Scanner(System.in);
double value = myinput.nextDouble( );
There would be an error telling that we have entered a mismatch input. This is because ‘5a’
contains of a character ‘a’. If we just enter ‘5’ then the program will run without error.
Figure 2 : Error when user entered input with different data type
Assignment Chapter 2: Elementary Programming
The “import java.util.Scanner;” is called as specific import and the “import java.util.*;” is called as wildcard import declaration.
miles = valid
Test = valid
a++ = invalid because it is an operator that show post-increment
--a = invalid because it is an operator that show pre-decrement
4#R = invalid because it consists an illegal character which is ‘#’
$4 = valid
#44 = invalid because it consists an illegal character which is ‘#’
apps = valid
class = invalid because it is a java keyword
public = invalid because it is a java keyword
int = invalid because it is a java keyword
x = valid
y = valid
radius = valid
Assignment Chapter 2: Elementary Programming
The error is in line 3. The variable ‘k’ is not declared in the code. To fix the error, the variable
‘k’ must be declared and initialized to a value before line 3.
The error is in line 3. The variable ‘j’ and ‘k’ are not declared. To fix the error, the variable ‘j’
and ‘k’ must be declared before the line number 3.
Assignment Chapter 2: Elementary Programming
i. You don’t have to repeatedly type the same value if it is used multiple times.
ii. If you have to change the constant value, you need to change it only in a single
location in the source code.
iii. A descriptive name for a constant makes the program easy to read.
int SIZE = 20 ;
Naming Conventions
Class Capitalize the first letter of each word in a class name.
E.g. ComputeArea and System.
Method Use lowercase for methods. If a name consists of several words, concatenate
them into one, making the first word lowercase and capitalizing the first letter
of each subsequent word.
E.g. print.
Constants Capitalize every letter in a constant, and use underscores between words.
E.g. PI and MAX_VALUE.
Variables Use lowercase for variables. If a name consists of several words, concatenate
them into one, making the first word lowercase and capitalizing the first letter
of each subsequent word.
E.g. radius and area.
MAX_VALUE = Constant
Test = Class
read = Method
readDouble = Variable
Assignment Chapter 2: Elementary Programming
Assignment Chapter 2: Elementary Programming
The data types that requires the least amount of memory is byte 8-bit storage size.
Assignment Chapter 2: Elementary Programming
Since the remainder of 100/7 is 2, therefore we just need to find the 2 days after Tuesday. So the
answer will be Thursday.
Assignment Chapter 2: Elementary Programming
To get the result as a floating-point number, we can rewrite the expression as below:
Assignment Chapter 2: Elementary Programming
A float value has 7 to 8 number of significant digits and a double value has 15 to 17 number of
significant digits.
Assignment Chapter 2: Elementary Programming
12.3 = Not a floating-point number (double)
12.3e+2 = Floating-point number
23.4e-2 = Floating-point number
-334.4 = Not a floating-point number (double)
20.5 = Not a floating-point number (double)
39F = Floating-point number
40D = Not a floating-point number (double)
5.2534e+1 = 52.534 (Same)
0.52534e+2 = 52.534 (Same)
525.34e-1 = 52.534 (Same)
5.2534e+0 = 5.2534 (Not the same)
5_2534e+1 = Correct
_2534 = Incorrect
5_2 = Correct
5_ = Incorrect
Assignment Chapter 2: Elementary Programming
a. (4 / (3*(r+34))) - 9(a+b*c) + (3+d*(2+a))/(a+b*d)
b. 5.5*Math.pow(r+2.5,2.5+t)
Assignment Chapter 2: Elementary Programming
a. True
b. True
c. True
d. False
Assignment Chapter 2: Elementary Programming
Assignment Chapter 2: Elementary Programming
When the arithmetic expression in line 11 changed to (int)(tax*100)/100, the result will be
returned in integer instead of double/float number.
Assignment Chapter 2: Elementary Programming
Assignment Chapter 2: Elementary Programming