100% found this document useful (1 vote)
28 views10 pages

Opp Nono Assignment 1

Oop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
28 views10 pages

Opp Nono Assignment 1

Oop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ZQMS-ARC-REC-002

ASSIGNMENT COVER

REGION: _HARARE _______________________________________________________________

PROGRAMME: ____SOFTWARE ENGINEERING____________________________________________


INTAKE: _8__

FULL NAME OF STUDENT: NONHLANHLA___MTETWA______________________________ PIN: __


P1956143R__

MAILING ADDRESS:

[email protected]______________________________________________
_

CONTACT TELEPHONE/CELL: ___ +263 77 700 5446_________________ ID. 84-048367v19


__________

COURSE NAME: OBJECT ORIENTED PROGRAMMING___________________________ COURSE CODE:


____BSEH 231_

ASSIGNMENT NO. e.g. 1 or 2: __1_________________________ DUE DATE: _30/09/2024

___________

ASSIGNMENT TITLE: _OBJECT ORIENTED PROGRAMMING ASSIGNMENT 1

___________________________________________________________________________
______________________________________________________________________________
MARKER’S COMMENTS: ______________________________________________________

______________________________________________________________________________

______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________

OVERALL MARK: _____________ MARKER’S NAME: ________________________

MARKER’S SIGNATURE: _______________________________ DATE: ___________

BSEH231/BITH331 OBJECT ORIENTED PROGRAMMING

Assignment 1

Question 1

a. Errors in Declarations

i.int intA[] = new double[10];

Error Reason: Type mismatch. The array is declared as an int array but initialized with a
double array. The types must match.

ii.int intA[] = new int[1.5];

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.

iii.double doubleA = newdouble[-10];

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];

Error Reason: Incomplete multidimensional array initialization. When declaring a two-


dimensional array, both dimensions must be specified. It should be something like new
int[10][10]; to define the size of both dimensions.

b. What is a Compound Statement?

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

a) Given the following declaration

List list <Integer> = new LinkedList<>();

Show using sample code how you can add String objects into the list. [6]

import java.util.LinkedList;

import java.util.List;

public class Main {

public static void main(String[] args) {

List<Object> list = new LinkedList<>();

list.add(1);

list.add(2);

list.add("Hello");

list.add("World");

for (Object obj : list) {

System.out.println(obj);

}
}

b) When it comes to Threads, very little is guaranteed. Describe behaviors of Threads


that may be guaranteed and those that may not be guaranteed. [10]

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.

c) Exceptions can either be caught or declared. Explain using an example how to


declare an
exception. [4]

In Java, exceptions can be either caught using try-catch blocks or declared in a


method's signature using the throws keyword. Declaring an exception informs callers of
a method that it may throw a specific type of exception, allowing them to handle it
appropriately.

import java.io.FileInputStream;

import java.io.FileNotFoundException;

public class FileReader {


// Method that declares an exception

public void readFile(String filePath) throws FileNotFoundException {

FileInputStream fileInputStream = new FileInputStream(filePath);

// Code to read from the file would go here

fileInputStream.close();

public static void main(String[] args) {

FileReader fileReader = new FileReader();

try {

// Calling the method that may throw an exception

fileReader.readFile("nonexistentfile.txt");

} catch (FileNotFoundException e) {

// Handling the exception

System.out.println("File not found: " + e.getMessage());

Question 3

Explain what is meant by the following terms in object-oriented programming:

a. Polymorphism

Polymorphism is a mechanism which allows objects of different classes to be treated as


objects of a common superclass. It enables a single interface to represent different data types.
b. Inheritance

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]

// Abstract superclass Student

abstract class Student {

protected String name;

protected String id;

protected double grade;


protected int age;

protected String address;

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;

// Abstract method to be implemented by subclasses

public abstract boolean isPassed(double grade);

// Subclass Undergrad

class Undergrad extends Student {

public Undergrad(String name, String id, double grade, int age, String address) {

super(name, id, grade, age, address);

@Override

public boolean isPassed(double grade) {

return grade > 70.0;


}

// Subclass Grad

class Grad extends Student {

public Grad(String name, String id, double grade, int age, String address) {

super(name, id, grade, age, address);

@Override

public boolean isPassed(double grade) {

return grade > 80.0;

// Test class

public class StudentTest {

public static void main(String[] args) {

// Creating a Grad object

Grad gradStudent = new Grad("Alice", "G123", 85.0, 25, "123 Main St");

// Testing isPassed method for Grad

System.out.println("Grad Student: " + gradStudent.name + " - Passed: " +


gradStudent.isPassed(gradStudent.grade));
// Creating an Undergrad object

Undergrad undergradStudent = new Undergrad("Bob", "U456", 65.0, 20, "456 Oak Ave");

// Testing isPassed method for Undergrad

System.out.println("Undergrad Student: " + undergradStudent.name + " - Passed: " +


undergradStudent.isPassed(undergradStudent.grade));

You might also like