Question

In: Computer Science

Apply The knowledge of “Mapping Models to code” Concept to write Chunks of java code which...

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

Solutions

Expert Solution

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.


Related Solutions

Write this code in java and don't forget to comment every step. Write a method which...
Write this code in java and don't forget to comment every step. Write a method which asks a baker how hot their water is, and prints out whether it is OK to make bread with the water. If the water is at or above 110F, your method should print "Too Warm." If the water is below 90.5F, print "Too Cold." If it is in between, print "Just right to bake!" For example, if the user inputs the number 100.5, the...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
write down the java code for garden sprinkler system.
write down the java code for garden sprinkler system.
Effective auscultation of the lungs relies on knowledge of which which anatomical concept ?
Effective auscultation of the lungs relies on knowledge of which which anatomical concept ?
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
Write Java code which will create a GUI window on screen. The window has one button:...
Write Java code which will create a GUI window on screen. The window has one button: Doggy. When the user selects Doggy, “Bow Bow” should be printed on screen. Justify your syntax.
Write Java code which will create a GUI window on screen. The window has one button:...
Write Java code which will create a GUI window on screen. The window has one button: Doggy. When the user selects Doggy, “Bow Bow” should be printed on screen. Justify your syntax.
Write Java code which will create a GUI window on screen. The window has one button:...
Write Java code which will create a GUI window on screen. The window has one button: Doggy. When the user selects Doggy, “Bow Bow” should be printed on screen. Justify your syntax.
Write Java code which will create a GUI window on screen. The window has one button:...
Write Java code which will create a GUI window on screen. The window has one button: Doggy. When the user selects Doggy, “Bow Bow” should be printed on screen. Justify your syntax.
Write Java code which will create a GUI window on screen. The window has one button:...
Write Java code which will create a GUI window on screen. The window has one button: Doggy. When the user selects Doggy, “Bow Bow” should be printed on screen. Justify your syntax.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT