In: Computer Science
Apply The knowledge of “Mapping Models to code” Concept to write
Chunks of java
code which maps the following relationships:
a. Aggregation
b. Composition
c. Generalization
d. Realization
e. Dependency
Aggregation Example HAS A Relationship
class Author
{
String authorName;
int age;
String place;
// Author class constructor
Author(String name, int age, String place)
{
this.authorName = name;
this.age = age;
this.place = place;
}
}
class Book
{
String name;
int price;
// author details
Author auther;
Book(String n, int p, Author auther)
{
this.name = n;
this.price = p;
this.auther = auther;
}
public static void main(String[] args) {
Author auther = new Author("John", 42, "USA");
Book b = new Book("Java for Begginer", 800, auther);
System.out.println("Book Name: "+b.name);
System.out.println("Book Price: "+b.price);
System.out.println("------------Auther Details----------");
System.out.println("Auther Name: "+b.auther.authorName);
System.out.println("Auther Age: "+b.auther.age);
System.out.println("Auther place: "+b.auther.place);
}
}
Composition Example Part Of Relationship
// Java program to illustrate
// the concept of Composition
import java.io.*;
import java.util.*;
// class book
class Book
{
public String title;
public String author;
Book(String title, String author)
{
this.title = title;
this.author = author;
}
}
// Libary class contains
// list of books.
class Library
{
// reference to refer to list of books.
private final List<Book> books;
Library (List<Book> books)
{
this.books = books;
}
public List<Book>
getTotalBooksInLibrary(){
return books;
}
}
// main method
class GFG
{
public static void main (String[] args)
{
// Creating the Objects of Book
class.
Book b1 = new Book("EffectiveJ
Java", "Joshua Bloch");
Book b2 = new Book("Thinking in
Java", "Bruce Eckel");
Book b3 = new Book("Java: The
Complete Reference", "Herbert Schildt");
// Creating the list which contains
the
// no. of books.
List<Book> books = new
ArrayList<Book>();
books.add(b1);
books.add(b2);
books.add(b3);
Library library = new
Library(books);
List<Book> bks =
library.getTotalBooksInLibrary();
for(Book bk : bks){
System.out.println("Title : " + bk.title + " and "
+" Author : " +
bk.author);
}
}
}
Generalization Example
class Father {
public void work()
{
System.out.println("Earning
Father");
}
}
class Son extends Father {
public void play()
{
System.out.println("Enjoying
son");
}
}
class Main {
public static void main(String[] args)
{
// father is a superclass
reference
Father father;
// new operator returns a
subclass reference
father = (Father) new Son();
// which is widened using
casting
// and stored in father
variable
// Though casting is done but it is
not needed
father.work();
// Uncomment next lone to see
the error
// father.play();
}
}
Realization Example Implementing the interface
public interface Figure {
public double area();
}
public class Rectangle implements Figure {
public double length = 10.0;
public double width = 10.0;
public double area() {
return length * width;
}
}
public class Circle implements Figure {
public double pie = 3.27;
public double radius = 5.0;
public double area() {
return pie * radius *
radius;
}
}
Dependency Example dependent upon something (use of this keyword)
import java.util.logging.Logger;
public class MyClass
{
private Logger logger;
public MyClass(Logger logger)
{
this.logger = logger;
logger.info("This is a log message.")
}
}
You can also do integration of all part of things to make a whole java project. and these examples given here are independent of each other.
Thanks.