OOP Assign1
OOP Assign1
ASSIGNMENT COVER
Instructions
Marks will be awarded for good presentation and thoroughness in your approach.
NO marks will be awarded for the entire assignment if any part of it is found to be copied directly from
printed materials or from another student.
Complete this cover and attach it to your assignment. Insert your scanned signature.
Student declaration
I declare that:
• I understand what is meant by plagiarism
• The implications of plagiarism have been explained to me by the institution
• This assignment is all my own work and I have acknowledged any use of the published or
unpublished works of other people.
String employeeFirstName ;
String employeeLastName;
int totalHoursWorked;
double overTimeRatePerHour;
double totalDeductions;
double netSalary;
Using comments in a programs makes our code more understandable. It makes the program more
readable which helps us remember why certain blocks of code were written. Comments can also be
used to ignore some code while testing other blocks of code. It makes it easy to maintain the code and
to find errors easily.
c. State whether the variables below is valid or otherwise. For the variables that are invalid, please state
the following reasons: [5]
iv. 43650BBB is an invalid variable, a variable name cannot start with a digit.
int length = 1;
System.out.println(“Area is ” +area);
e) Giving values for each of the arithmetic statements below. Statement Value
Statement Value
i. 4 + 5.0 * 6 34.0
ii. (4 + 5) * 6 54
iii. 4 + 5 / 6 4.83333333…..
iv. (4 + 5) / 6 1.5
v. 4 + 5 % 3 6
vi. (4 + 5) % 3 0
f. What is the output that will be displayed by the following program segment: [4]
r = 2;
if (r != 1) System.out.println (“AAA”);
else System.out.println(“BBB”);
Static methods provide direct access to static variables. Having static methods eliminates the
need for the caller to instantiate an object just to call the method.
Static variables are shared by each class instance. Static variables are set before a class is
loaded before any objects are generated from that class, and before any static methods are
called from that class.
h. Write java program to calculate Area and Perimeter of a rectangle and a circle. [10]
}
}
i. Which of these statements about constructors is false?
C. There must be exactly one constructor defined for a class.
D. Constructors are almost always declared as public
j. What is Compound Statement?
It is a sequence of zero or more statements enclosed in {...}.
It is considered to be a single statement. A compound statement is a context.
k. Each declaration in the table below contains error. Give reason for the error. [7]
i. int intA[ ] = new double[10]; data types are not the same.
ii. int intA[ ] = new int[1.5]; the number is declared as a float instead of an integer data type
iii. double doubleA = newdouble [-10]; there is a negative element. Array elements must
always be a positive number.
iv. int intMatrik[ ][ ] = new int[10]; different array dimensions, one is defined as single
dimensional array while the other is a two dimensional array.