Aliyah

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

Part A: Report

Task1.Provide design solution (UML diagrams) for the above mention Scenario.
Provide clear explanation for all the diagrams mention below. (Provide assumption if
necessary) (30 marks) (LO2)
a) Use case Diagram
b) Class Diagram
c) Sequence Diagram

Use case Diagram

In addition to being the most well-known diagram type among the behavioral UML kinds,
use case diagrams provide a visual representation of the players involved in a system, the
various functions required by those actors, and the way these various functions interact.

It's an excellent beginning point for any project conversation since it allows you to quickly
identify the primary actors engaged as well as the primary processes of the system under
consideration. You can use our tool to build use case diagrams, or you can start right away by
downloading one of our use case templates.
Class Diagram

In any object-oriented solution, class diagrams serve as the fundamental building element.
System classes are represented by the properties and activities of each class, as well as the
relationships between the classes themselves.

In most modeling programs, a class is divided into three sections. The top of the page has the
name, the middle contains the characteristics, and the bottom contains the operations or
methods. Creating class diagrams is useful in large systems with numerous related classes
since it allows for easier grouping of classes. Different types of arrows are used to depict
different types of links between classes.

An example of a class diagram is shown below.


Sequence Diagram

In UML, sequence diagrams depict how items interact with one another and in what order
they interact. It's worth noting that they only reveal interactions for a specific scenario. The
processes are depicted vertically, with arrows indicating interactions.
Task 2: Develop suitable system for the above scenario based on the design. Required to
use Object Oriented concepts (Object, Class, Abstraction, Inheritance, Encapsulation
and Polymorphism) for the development. Document the main functionalities and Object
Oriented concepts applied with proper explanation and source code.

What Are Objects?

l Dogs and cats are two examples of this. An example of this would be a dog, which
has characteristics such as its age, coat color, and name, in addition to routines like
eating, sleeping, and running.

l The object's state tells us what it appears like or what its properties are.

l The object's behavior shows us what it is capable of.

l By specifying its states and behaviors, we may simulate a real-world dog in a


computer program.

l Real-world objects are represented in software as virtual objects. When constructing


a logical object, RAM is allocated.

l A class instance can alternatively be referred to as an object. An object is created


when a class is instantiated.

l When creating an object, keep in mind that the reference type must be of the same
type as the object type, or a supertype. Later on in this tutorial, we'll look at what a
reference type is.

What Are Classes?


l A class is a template or blueprint from which objects are created.

l Imagine a class as a cookie-cutter and objects as cookies.

l cookie-cutter

l Classes define states as instance variables and behaviors as instance methods.

l Instance variables are also known as member variables.

l Classes don't consume any space.

l To give you an idea about classes and objects, let's create a Cat class that represents
states and behaviors of real world Cat.

public class Cat {

/*

Instance variables: states of Cat

*/

String name;

int age;

String color;

String breed;

/*
Instance methods: behaviors of Cat

*/

void sleep(){

System.out.println("Sleeping");

void play(){

System.out.println("Playing");

void feed(){

System.out.println("Eating");

How can we define them in our program?

First, we need to create two objects of the Cat class.

public class Main {

public static void main(String[] args) {

Cat thor = new Cat();

Cat rambo = new Cat();

Next, we’ll define their states and behaviors.


public class Main {

public static void main(String[] args) {

/*

Creating objects

*/

Cat thor = new Cat();

Cat rambo = new Cat();

/*

Defining Thor cat

*/

thor.name = "Thor";

thor.age = 3;

thor.breed = "Russian Blue";

thor.color = "Brown";

thor.sleep();

/*

Defining Rambo cat

*/

rambo.name = "Rambo";

rambo.age = 4;
rambo.breed = "Maine Coon";

rambo.color = "Brown";

rambo.play();

Abstraction

Pressing the accelerator increases the vehicle's speed as an illustration of abstraction.


However, it is unnecessary for the driver to be aware of how pressing the accelerator
increases the vehicle's speed. The term "abstract" is used to describe something that is either
incomplete or has yet to be finished. There are two techniques to abstract in Java: abstract
class (0% to 100%) and interface (0% to 100%). (100 percent ). Classes and methods can use
the abstract keyword. When it comes to abstract and final or static, the two cannot exist at
once.

I. Abstract class

An abstract class is one that contains the keyword abstract.

Abstract classes can’t be instantiated (can’t create objects of abstract classes). They can have
constructors, static methods, and final methods.

II. Abstract methods

An abstract method is one that contains the keyword abstract.

An abstract method doesn’t have implementation (no method body and ends up with a semi
colon). It shouldn’t be marked as private.

III. Abstract class and Abstract methods


l If at least one abstract method exists inside a class then the whole class should be
abstract.

l We can have an abstract class with no abstract methods.

l We can have any number of abstract as well as non-abstract methods inside an


abstract class at the same time.

l The first concrete sub class of an abstract class must provide implementation to all
abstract methods.

l If this doesn't happen, then the sub class also should be marked as abstract.

l In a real world scenario, the implementation will be provided by someone who is


unknown to end users. Users don’t know the implementation class and the actual
implementation.

abstract class Shape {

public abstract void draw();

class Circle extends Shape{

public void draw() {

System.out.println("Circle!");

public class Test {

public static void main(String[] args) {

Shape circle = new Circle();

circle.draw();

}
When do we want to mark a class as abstract?

l To force sub classes to implement abstract methods.

l To stop having actual objects of that class.

l To keep having a class reference.

l To retain common class code.

l Interface

l An interface is a blueprint of a class.

An interface can have only abstract methods, not concrete methods. By default, interface
methods are public and abstract. So inside the interface, we don’t need to specify public and
abstract.

So when a class implements an interface’s method without specifying the access level of that
method, the compiler will throw an error stating “Cannot reduce the visibility of the inherited
method from interface”. So that implemented method’s access level must be set to public.

interface Runnable {

int a = 10; //similar to: public static final int a = 10;

void run(); //similar to: public abstract void run();

public class InterfaceChecker implements Runnable{

public static void main(String[] args) {

Runnable.a = 5;//The final field Runnable.a cannot be assigned.

interface Drawable {
void draw();

class Circle implements Drawable{

public void draw() {

System.out.println("Circle!");

public class InterfaceChecker {

public static void main(String[] args) {

Drawable circle = new Circle();

circle.draw();

Inheritance

xIn the image above, you can see that Album and Movie share many common states and
activities (shared code).

Are you going to write (or copy & paste) the complete code for Movie when you implement
this class diagram into code? If you do, you're just saying the same thing over and over again.
What can you do to prevent rewriting the same code?
Inherited?

l Visibility/access modifiers impact what gets inherited from one class to another.

l In Java, as a rule of thumb we make instance variables private and instance methods
public .

l In this case, we can safely say that the following are inherited:

l public instance methods.

l private instance variables (private instance variables can be accessed only through
public getter and setter methods).

Types of Inheritance in Java

There are five types of inheritance in Java. They are single, multilevel, hierarchical, multiple,
and hybrid.

Class allows single, multilevel and hierarchical inheritances. Interface allows multiple and
hybrid inheritances.
A class can implement as many interfaces as it wants, but it can only extend one other class.
It is possible for a single interface to extend to multiple others.
Relationships

I. IS-A relationship

An IS-A relationship refers to inheritance or implementation.

a. Generalization

Generalization uses an IS-A relationship from a specialization class to generalization class.

II. HAS-A relationship

An instance of one class HAS-A reference to an instance of another class.


a. Aggregation

In this relationship, the existence of class A and B are not dependent on each other.

For this aggregation part, we going to see an example of the Student class and the
ContactInfo class.

class ContactInfo {

private String homeAddress;

private String emailAddress;

private int telephoneNumber; //12025550156

public class Student {

private String name;

private int age;

private int grade;

private ContactInfo contactInfo;//Student HAS-A ContactInfo

public void study() {

System.out.println("Study");

The student possesses a method of getting in touch with him or her. The ContactInfo class
can be used in different contexts, such as a company's Employee class. So both Student and
ContactInfo can exist independently of each other. Aggregation describes this kind of
connection.

b. Composition

In this relationship, class B can not exist without class A – but class A can exist without class
B.

To give you an idea about composition, let's see an example of the Student class and the
StudentId class.

class StudentId {

private String idNumber;//A-123456789

private String bloodGroup;

private String accountNumber;

public class Student {

private String name;

private int age;

private int grade;

private StudentId studentId;//Student HAS-A StudentId

public void study() {

System.out.println("Study");

}
Encapsulation

Encapsulation is a process of wrapping code and data together into a single unit.

It's just like a capsule that contains a mix of several medicines, and is a technique that helps
keep instance variables protected.

This can be achieved by using private access modifiers that can’t be accessed by anything
outside the class. In order to access private states safely, we have to provide public getter and
setter methods. (In Java, these methods should follow JavaBeans naming standards.)

Let’s say there is a record shop that sells music albums of different artists and a stock keeper
who manages them.
Album class:

public class Album {

public String name;

public String artist;

public double price;

public int numberOfCopies;

public void sellCopies(){

if(numberOfCopies > 0){

numberOfCopies--;

System.out.println("One album has sold!");

else{

System.out.println("No more albums available!");

public void orderCopies(int num){

numberOfCopies += num;

}
StockKeeper class:

public class StockKeeper {

public String name;

public StockKeeper(String name){

this.name = name;

public void manageAlbum(Album album, String name, String artist, double price, int
numberOfCopies){

/*

Defining states and behaviors for album

*/

album.name = name;

album.artist = artist;

album.price = price;

album.numberOfCopies = numberOfCopies;

/*

Printing album details

*/

System.out.println("Album managed by :"+ this.name);

System.out.println("Album details::::::::::");

System.out.println("Album name : " + album.name);

System.out.println("Album artist : " + album.artist);

System.out.println("Album price : " + album.price);


System.out.println("Album number of copies : " + album.numberOfCopies);

Main class:

public class Main {

public static void main(String[] args) {

StockKeeper johnDoe = new StockKeeper("John Doe");

/*

Stock keeper creates album and assigns negative values for price and number of copies
available

*/

johnDoe.manageAlbum(new Album(), "Slippery When Wet", "Bon Jovi", -1000.00, -


50);

Output:

Album managed by :John Doe

Album details::::::::::

Album name : Slippery When Wet

Album artist : Bon Jovi

Album price : -1000.0

Album number of copies : -50


Album class:

public class Album {

private String name;

private String artist;

private double price;

private int numberOfCopies;

public void sellCopies(){

if(numberOfCopies > 0){

numberOfCopies--;

System.out.println("One album has sold!");

else{

System.out.println("No more albums available!");

public void orderCopies(int num){

numberOfCopies += num;

public String getName() {


return name;

public void setName(String name) {

this.name = name;

public String getArtist() {

return artist;

public void setArtist(String artist) {

this.artist = artist;

public double getPrice() {

return price;

public void setPrice(double price) {

if(price > 0) {

this.price = price;

else {

this.price = 0.0;

public int getNumberOfCopies() {

return numberOfCopies;
}

public void setNumberOfCopies(int numberOfCopies) {

if(numberOfCopies > 0) {

this.numberOfCopies = numberOfCopies;

else {

this.numberOfCopies = 0;

StockKeeper class:

public class StockKeeper {

private String name;

StockKeeper(String name){

setName(name);

public void manageAlbum(Album album, String name, String artist, double price, int
numberOfCopies){

/*

Defining states and behaviors for album

*/
album.setName(name);

album.setArtist(artist);

album.setPrice(price);

album.setNumberOfCopies(numberOfCopies);

/*

Printing album details

*/

System.out.println("Album managed by :"+ getName());

System.out.println("Album details::::::::::");

System.out.println("Album name : " + album.getName());

System.out.println("Album artist : " + album.getArtist());

System.out.println("Album price : " + album.getPrice());

System.out.println("Album number of copies : " + album.getNumberOfCopies());

public String getName() {

return name;

public void setName(String name) {

this.name = name;

Main class:

public class Main {

public static void main(String[] args) {


StockKeeper johnDoe = new StockKeeper("John Doe");

/*

Stock keeper creates album and assigns negative values for price and number of copies
available

*/

johnDoe.manageAlbum(new Album(), "Slippery When Wet", "Bon Jovi", -1000.00, -


50);

Output:

Album managed by :John Doe

Album details::::::::::

Album name : Slippery When Wet

Album artist : Bon Jovi

Album price : 0.0

Album number of copies : 0

Polymorphism

When something has polymorphism, it has the capacity to take on a variety of different
appearances. In OOP, polymorphism occurs when a super class refers to a subclass object.
All Java objects are polymorphic because they have at least one IS-A relationship (at least all
objects will pass the IS-A test for their own type and for the class Object). A reference
variable gives us direct access to an object. Only one type of reference variable is allowed.
The type of a reference variable cannot be modified once it has been declared. As a class or
interface type, a reference variable can be defined Reference variables of various types can
all refer to the same object, as long as they are of the same type or a super type.
Method overloading

If a class has multiple methods that have same name but different parameters, this is known
as method overloading.

Method overloading rules:

Must have a different parameter list.

May have different return types.

May have different access modifiers.

May throw different exceptions.

class JavaProgrammer{

public void code() {

System.out.println("Coding in C++");

public void code(String language) {

System.out.println("Coding in "+language);

public class MethodOverloader {

public static void main(String[] args) {

JavaProgrammer gosling = new JavaProgrammer();

gosling.code();

gosling.code("Java");

/*
Output:

Coding in C++

Coding in Java

*/

We can overload the main() method but the Java Virtual Machine (JVM) calls the main()
method that receives String arrays as arguments.

public class PolyTest {

public static void main() {

System.out.println("main()");

public static void main(String args) {

System.out.println("String args");

public static void main(String[] args) {

System.out.println("String[] args");

//Output: String[] args

Polymorphism refers to the ability to perform a certain action in different ways. In Java,
polymorphism can take two forms: method overloading and method overriding.

Method overloading happens when various methods with the same name are present in a
class. When they are called, they are differentiated by the number, order, or types of their
parameters. Method overriding occurs when a child class overrides a method of its parent.

Polymorphism in Java:

l The same method name is used several times


l Different methods of the same name can be called from an object

l All Java objects can be considered polymorphic (at the minimum, they are of their
own type and instances of the Object class)

l Static polymorphism in Java is implemented by method overloading

l Dynamic polymorphism in Java is implemented by method overriding

Task 3: Provide a user manual for the developed solution


Part B: Demonstration
Task 4: System demonstration. System should work according to the expected
functionalities. Should be able to demonstrate Object Oriented concepts (Object, Class,
Abstraction, Inheritance, Encapsulation, and Polymorphism) applied to the given
scenario.
OOP ideas in Java make it easier for you to structure your code. Aside from making it easier
to reuse and secure your Java code, the seven object-oriented principles we've discussed here
can also help you make your applications run faster and more reliably.
You can install Raygun Application Performance Monitoring and Crash Reporting within
minutes and monitor your Java code as you're working to improve its performance.
This programming paradigm is based on objects, and it is known as object-oriented
programming (OOP).
Messages are sent and received between objects. When an object receives a message, it
determines what to do with it on its own. In OOP, each object's state and behavior are of
primary importance.
The purpose of Java's OOP ideas is to reduce development time without compromising the
safety and usability of the code. It's clear that these best practices are all focused on achieving
that end aim.
Do Not Repeat Yourself (DRY) is an acronym for this dictum. DRY is a fundamental Java
principle that states that you should never have two blocks of identical code in two different
places. If you like, you can use the same procedure for all of your needs.
Make all variables and methods private from the beginning if you expect your Java code to
alter. As the code evolves, you may need to expand "restricted" access, but be careful not to
make it too public.
Having exclusive responsibility. One of the most important principles of Java's OOP concepts
is that a class should always have just one purpose. Coupling between distinct features will
not be a problem because the class can be called and/or extended on its own.
Design with an Open-Closed Concept. All methods and classes should be closed to alteration,
but open to extension. While the code remains static, it is able to be adapted to do new jobs as
required.
An excellent resource for learning about OOP ideas in Java is this blog post. C# OOP
principles can also be found in our C# OOP article.
If these concepts and best practices are not implemented properly, they are of no use. Java
productivity tools are essential if you want to raise the quality of your code.

You might also like