PROGRAMMING
Thabang Magaele
Kabelo Makamane
240241177
230905544
FUNDAMENTALS 151
Katleho Mofokeng
Samukelo Cassius Ndlovu
240919416
240894022
ASSIGNMENT 1
Samuel Adino Mutessa
Chautelle Kamogelo Ngwenya
230823270
230274846
Masehla Mahlatse 240815467
Zintle Mngqinya 240340885
Sphephile Zwane 230101518
Senamile Mkhonza 240407504
Question 1
(A.)
The three core concepts of object-oriented programming, or OOP, are
polymorphism, inheritance, and encapsulation. These ideas form the basis
for creating and putting into practice scalable, effective software systems:
a) Encapsulation: Encapsulation is the process of combining techniques
(functions or procedures) that manipulate data with data (attributes or
properties) to form a single unit known as an object. The object
protects the data and procedures from outside intervention and misuse
by acting as a capsule or container around them. This implies that an
object's internal operations are concealed from the outside world and
that only the methods specified within the object may access the data.
Information hiding is encouraged via encapsulation, which improves
the codebase's flexibility, maintainability, and security. It permits
improved modularization and by dividing the system into more
manageable, smaller components, it facilitates greater modularization
and lowers total system complexity.
b) Inheritance: A new class, referred to as the derived class or subclass,
can inherit characteristics and actions from an existing class, referred
to as the base class or superclass, through the method of inheritance.
This encourages the development of a hierarchical structure among
classes and permits the reuse of code. The subclass can define its own
extra properties and methods, or override the inherited ones, in
addition to inheriting the superclass's methods and attributes. Code
organization is made easier via inheritance, which also encourages the
development of more specialized classes from more broad ones and
makes the "is-a" link between classes possible. It encourages software
design extensibility and flexibility by enabling modifications at various
class hierarchy levels without impacting the overall codebase.
c) Polymorphism: The term polymorphism, which translates to "many
forms," describes the ability to treat objects of multiple classes as
belonging to the same superclass. It makes it possible to use a single
interface for entities of various kinds, allowing for more generic and
adaptable code to be written. The usual ways to accomplish
polymorphism are by overloading and overriding methods. While
1
method overloading allows a class to create numerous methods with
the same name but distinct argument lists, method overriding allows a
subclass to give a specific implementation of a method that is already
specified in its superclass. Because polymorphism enables developers
to construct code that works on objects of a superclass without
needing to know the precise subclass types at compile time, it
improves code reusability, encourages modularity, and eases code
maintenance. It enables dynamic binding, in which the runtime
chooses the right method implementation according to the real type of
the object being referred. This makes it possible to create systems that
are more resilient and adaptable to shifting conditions and demands.
(B.)
Variables that are labeled as public can be accessed and modified by any
class in the program.
Variables labeled as private can only be accessed and modified within the
class where they are declared, preventing other classes from directly
interacting with them. This upholds the principle of encapsulation and
ensures the integrity of the data.
Variables marked as protected can be accessed within the same package as
well as by subclasses, even if they are in different packages. This provides
more flexibility than private but still imposes restrictions compared to public.
Accessor types in Java are used to control the visibility and accessibility of
variables within a class. There are three main types of accessors:
a) Public Accessor (public): When a variable is declared as public, it can
be accessed from anywhere in the program. This means that any other
class can access and modify the variable directly. The implication of
using public accessors is that it can lead to a lack of encapsulation,
potentially allowing unintended changes to the variable's state from
external classes.
b) Private Accessor (private): When a variable is declared as private, it
can only be accessed and modified within the same class where it is
declared. This promotes encapsulation, as the internal state of the
2
class is hidden from external classes. However, it also means that the
variable cannot be directly accessed or modified from outside the
class, potentially requiring the use of getter and setter methods to
interact with it.
c) Protected Accessor (protected): Variables declared as protected can be
accessed within the same package or by subclasses of the class in
which they are declared. This provides a middle ground between public
and private access, allowing restricted access to certain classes while
still maintaining some level of encapsulation. However, it also
introduces the risk of subclasses modifying the variable's state in ways
that might not be intended by the superclass.
(C.)
1. Code Reusability: Modularization and methods allow you to
encapsulate specific functionalities into separate modules or methods
that can be reused throughout the program or even in different
projects. This reduces code duplication and promotes a more efficient
and maintainable codebase.
2. Modularity and Maintainability: By organizing code into smaller, self-
contained modules or methods, it becomes easier to understand,
maintain, and debug. Each module or method can focus on a specific
task or functionality, making the overall program structure more
modular and easier to manage.
3. Abstraction and Encapsulation: Modularization and methods allow you
to abstract away implementation details and expose only necessary
interfaces to the rest of the program. This promotes encapsulation by
hiding internal workings and reducing dependencies between different
parts of the program, leading to more robust and scalable software.
4. Improved Readability and Understandability: Breaking down a program
into smaller modules with well-defined interfaces and methods makes
the codebase more readable and understandable. Developers can
easily grasp the purpose and functionality of each module or method,
leading to better collaboration and faster development cycles.
3
(D.)
public class Main {
public static void main(String[] args) {
System.out.println("\n \n \n *\n|||||||||");
EXAMPLE OF RUNNING CODE:
4
5
Question 2
Source code:
package Assignment1;
import java.util.Scanner;
public class Assignment1 {
public static void Student_mark() {
//Creating a scanner object for user input
Scanner Enter_mark = new Scanner(System.in);
System.out.println("Enter your mark below");
//Reading user input
double Mark_entered = Enter_mark.nextDouble();
//Conditions that determine a student's outcome
//based on the mark entered
if(Mark_entered < 50) {
System.out.println("Fail");
}
else if(49 < Mark_entered && Mark_entered <75) {
System.out.println("Pass");
}
else if(Mark_entered > 74) {
System.out.println("Pass with distinction");
}
public static void main(String[] args) {
//Calling the Student_mark method
Student_mark();
}
}
6
Examples of running code: