Aliyah
Aliyah
Aliyah
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
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.
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.
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 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.
l cookie-cutter
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.
/*
*/
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");
/*
Creating objects
*/
/*
*/
thor.name = "Thor";
thor.age = 3;
thor.color = "Brown";
thor.sleep();
/*
*/
rambo.name = "Rambo";
rambo.age = 4;
rambo.breed = "Maine Coon";
rambo.color = "Brown";
rambo.play();
Abstraction
I. Abstract class
Abstract classes can’t be instantiated (can’t create objects of abstract classes). They can have
constructors, static methods, and final methods.
An abstract method doesn’t have implementation (no method body and ends up with a semi
colon). It shouldn’t be marked as private.
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.
System.out.println("Circle!");
circle.draw();
}
When do we want to mark a class as abstract?
l Interface
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 {
interface Drawable {
void draw();
System.out.println("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 private instance variables (private instance variables can be accessed only through
public getter and setter methods).
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
a. Generalization
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 {
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 {
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:
numberOfCopies--;
else{
numberOfCopies += num;
}
StockKeeper class:
this.name = name;
public void manageAlbum(Album album, String name, String artist, double price, int
numberOfCopies){
/*
*/
album.name = name;
album.artist = artist;
album.price = price;
album.numberOfCopies = numberOfCopies;
/*
*/
System.out.println("Album details::::::::::");
Main class:
/*
Stock keeper creates album and assigns negative values for price and number of copies
available
*/
Output:
Album details::::::::::
numberOfCopies--;
else{
numberOfCopies += num;
this.name = name;
return artist;
this.artist = artist;
return price;
if(price > 0) {
this.price = price;
else {
this.price = 0.0;
return numberOfCopies;
}
if(numberOfCopies > 0) {
this.numberOfCopies = numberOfCopies;
else {
this.numberOfCopies = 0;
StockKeeper class:
StockKeeper(String name){
setName(name);
public void manageAlbum(Album album, String name, String artist, double price, int
numberOfCopies){
/*
*/
album.setName(name);
album.setArtist(artist);
album.setPrice(price);
album.setNumberOfCopies(numberOfCopies);
/*
*/
System.out.println("Album details::::::::::");
return name;
this.name = name;
Main class:
/*
Stock keeper creates album and assigns negative values for price and number of copies
available
*/
Output:
Album details::::::::::
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.
class JavaProgrammer{
System.out.println("Coding in C++");
System.out.println("Coding in "+language);
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.
System.out.println("main()");
System.out.println("String args");
System.out.println("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 All Java objects can be considered polymorphic (at the minimum, they are of their
own type and instances of the Object class)