Opp Nono Assignment 1
Opp Nono Assignment 1
ASSIGNMENT COVER
MAILING ADDRESS:
[email protected]______________________________________________
_
___________
___________________________________________________________________________
______________________________________________________________________________
MARKER’S COMMENTS: ______________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________
Assignment 1
Question 1
a. Errors in Declarations
Error Reason: Type mismatch. The array is declared as an int array but initialized with a
double array. The types must match.
Error Reason: Invalid array size. The size of an array must be a positive integer. Using a
floating-point number (1.5) is not allowed.
Error Reason: Syntax and invalid size. There is a typo (newdouble should be new double),
and the array size cannot be negative. Array sizes must be non-negative integers.
iv.int intMatrik[][] = new int[10];
Is the grouping of one or more statements enclosed within curly braces {}. It is treated as a
single statement in programming languages like C, C++, and Java.
Question 2
Show using sample code how you can add String objects into the list. [6]
import java.util.LinkedList;
import java.util.List;
list.add(1);
list.add(2);
list.add("Hello");
list.add("World");
System.out.println(obj);
}
}
Guaranteed Behaviors
Thread Creation:
You can reliably create threads using the Thread class or by implementing the Runnable
interface. The process of instantiating and starting a thread is guaranteed to work,
allowing concurrent execution.
Thread Termination:
A thread will terminate when its run() method completes or if it is interrupted. Once a
thread finishes executing, it will enter the terminated state, which is guaranteed.
Synchronization Mechanisms:
Using synchronized blocks or methods ensures that only one thread can execute that
block at a time. This guarantees data integrity for shared resources when properly
synchronized, preventing race conditions.
Thread Scheduling:
The Java Virtual Machine (JVM) will schedule threads for execution. While the exact
order of execution is not guaranteed, the JVM ensures that the threads will be allocated
CPU time.
Visibility of Changes:
Changes made by one thread to shared variables will be visible to other threads if proper
synchronization (like using volatile variables) is applied. This ensures that data
consistency is maintained across threads when using synchronization.
Non-Guaranteed Behaviors
Execution Order:
The order in which threads are executed is not guaranteed. Different executions of the
same program can yield different execution orders, leading to non-deterministic behavior.
Thread Interleaving:
The JVM may interleave the execution of multiple threads in unpredictable ways. This
can lead to race conditions if threads access shared resources without proper
synchronization.
Thread Lifetime:
The lifetime of a thread is not guaranteed. A thread may terminate unexpectedly due to
exceptions, and there is no guarantee on how long a thread will remain active.
Memory Consistency:
Without proper synchronization, changes made by one thread may not be visible to others
immediately. This can result in stale data being read from shared variables, leading to
inconsistent states.
Resource Availability:
Threads may compete for resources, and there is no guarantee that a thread will have
immediate access to the resources it needs. Contention for locks or other resources can
lead to delays and increased complexity in thread management.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
fileInputStream.close();
try {
fileReader.readFile("nonexistentfile.txt");
} catch (FileNotFoundException e) {
Question 3
a. Polymorphism
Inheritance is a mechanism where one class (subclass) acquires the properties and behaviors
(methods) of another class (superclass). It promotes code reuse and establishes a hierarchical
relationship.
c. Encapsulation
Encapsulation is the bundling of data and methods that operate on that data into a single unit
(class). It restricts direct access to some components, protecting the integrity of the data.
d. Abstraction
Abstraction is the process of hiding complex implementation details and exposing only the
necessary features of an object. It allows users to interact with an object at a higher level
without needing to understand its inner workings.
Question 4
Create a superclass, Student, and two subclasses, Undergrad and Grad. The superclass
Student should have the following data members: name, ID, grade, age, and address. The
superclass, Student should have at least one method: boolean isPassed (double grade).
The purpose of the isPassed method is to take one parameter, grade (value between 0 and
100) and check whether the grade has passed the requirement for passing a course. In the
Student class this method should be empty as an abstract method. The two subclasses,
Grad and Undergrad, will inherit all data members of the Student class and override the
method isPassed. For the UnderGrad class, if the grade is above 70.0, then isPassed
returns true, otherwise it returns false. For the Grad class, if the grade is above 80.0, then
isPassed returns true, otherwise returns false. Create a test class for your three classes. In
the test class, create one Grad object and one Undergrad object and test your methods.
For each object, provide a grade and display the results of the isPassed method. [20
marks]
public Student(String name, String id, double grade, int age, String address) {
this.name = name;
this.id = id;
this.grade = grade;
this.age = age;
this.address = address;
// Subclass Undergrad
public Undergrad(String name, String id, double grade, int age, String address) {
@Override
// Subclass Grad
public Grad(String name, String id, double grade, int age, String address) {
@Override
// Test class
Grad gradStudent = new Grad("Alice", "G123", 85.0, 25, "123 Main St");
Undergrad undergradStudent = new Undergrad("Bob", "U456", 65.0, 20, "456 Oak Ave");