Coder Social home page Coder Social logo

3zbumban / 04-generics Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ll9/04-generics

0.0 1.0 0.0 197 KB

Home Page: https://www.th-rosenheim.de/die-hochschule/fakultaeten-institute/fakultaet-fuer-informatik/

License: MIT License

Java 100.00%

04-generics's Introduction

This is an assignment to the class Programmieren 3 at the University of Applied Sciences Rosenheim.

Assignment 4: Generics

Travis CI

In this assignment we want to improve the previously implemented SimpleListImpl of Assignment 2. Back then, SimpleListImpl was implemented to store references of type Object, which in turn required a type cast on retrieval:

SimpleList sl = new SimpleListImpl();
sl.add(new MyClass());
MyClass k = (MyClass) sl.get(0);

Inside SimpleListImpl, the knowledge about the actual class was lost, and worse: the following code would compile but produce a runtime exception:

SimpleList sl = new SimpleListImpl();
sl.add(new MyClass());
sl.add(new MyOtherClass());
MyClass k1 = (MyClass) sl.get(0);  // all ok
MyClass k2 = (MyOtherClass) sl.get(1);  // ClassCastException!

Generics help us to avoid both the type cast and the risk of runtime exceptions by checking the type at compile time.

For this assignment, start with the reference solution of assignment 2 and the abstract model class Plant.

Setup

  1. Create a fork of this repository (button in the right upper corner)
  2. Clone the project (get the link by clicking the green Clone or download button)
  3. Import the project to your IDE (remember the guide in assignment 1)
  4. Validate your environment by running the tests from your IntelliJ and by running gradle test on the command line.

Generic Lists

class spec 1

To make a class generic, introduce a generic type (typically named T) in the class or interface signature, and replace all affected actual types with the generic type.

  1. Make the following interfaces and classes generic
    • SimpleList
    • SimpleFilter
    • SimpleListImpl
    • SimpleIteratorImpl
    • Element
  2. Adopt the changes in the test class SimpleListTests.java
  3. Remove the now unnecessary type casts
  4. Add a new method addDefault to the SimpleList interface; the purpose is to add a default instance (using the default constructor) to the list
    Hint: this method aims at the instantiation problem of generics.

Generic Methods

class spec 2

In the second part we want to focus on generic and default methods. For this purpose we'll add an additional method map(...) and move the method filter(...) to the interface SimpleList.

  1. Implement the filter(...) method as default method in the SimpleList interface
    (remember to run the tests when you completed the refactoring to ensure that the result is still the same)
  2. Add the map(...) method to the SimpleList interface according to the given UML (default method)
    The map(...) method transforms every element of your list with the given Function<T,R> to another element of type R and collects all elements in a new SimpleList.
  3. Optionally: Implement the the sort(...) method as static utility method in the abstract class CollectionsUtility.
    You may choose any sort algorithm: Bubblesort, Mergesort,...depending on your choice you may need to add some methods to SimpleList and SimpleListImpl
    (can you imagine why this class should be abstract and optimally has a private constructor?)

Remember, an untested implementation is worthless! Expand the given test suite to ensure that your algorithms are correct.

04-generics's People

Contributors

prskr avatar sikoried avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.