Git Product home page Git Product logo

cs56-rational-example's Introduction

cs56-rational-example

This is a set of tutorial examples of a class for Rational Numbers. Illustrates various concepts/techinques of Java Coding (a list of these appears below.)

These examples are intended as support for a course in which there are also reading assignments and lectures; this repo is not intended to be a stand-alone curriculum for learning Java.

Having said that, if you follow the code examples in order, and look up the concepts that are new to you at each step, that will probably go a long way towards learning some important basic concepts of Java programming.

Link to javadocs: http://ucsb-cs56-m16.github.io/cs56-rational-example

Guide to this set of Examples

  • Top level directory contains

    • this README.md file
    • subdirectories ex01, ex02, etc. one per example
    • support files for continuous integration (.travis.yml)
  • To follow these examples, consult each README.md in each subdirectory (ex01/README.md) etc. in order, for further information.

Basic description of the Rational class

The Rational class provides an abstraction for a rational number, that is one that has an integer numerator, and a non-zero integer denominator. For a more formal definition, consult the Wikipedia article on Rational Numbers.

This tutorial will not try to motivate why we need a class for rational numbers. We will assume that there is some need to represent these in a program. We'll assume that once some Java code has gotten input from somewhere for the numerator and denominator of two rational numbers, it needs to:

  • Constructor objects that represent these two rational numbers
  • Compute new rational numbers representing their sum, difference, and product.
  • Output those rational numbers in some way to the user.

We'll assume that instances of the class are immutable, i.e. we provide a way to construct objects, and getters for numerator and denominator, but no setters for numerator and denominator.

We'll assume that when instances of Rational are constructed, that if there are any common factors between the numerator and denominator, they are factored out. That is, the rational is represented internally in reduced form, and the getters for numerator and denominator reflect this. Example:

  • Suppose we create a Rational with Rational r=new Rational(2,4);
  • Then, r.toString() will return "1/2"
  • The getters for numerator and denominator will return 1 and 2, respectively.

We'll assume the following rules for negative numbers:

  • The negative sign is attached to the numerator, and the denominator is always positive.
  • Thus getNumerator() can return either a positive, zero, or negative integer, while getDenominator() always returns a strictly positive integer.

We'll also assume that, as a straightforward way of "outputting" rational numbers, a toString() method suffices, with these formatting rules:

  • The representation is simply numerator/demoninator, e.g. 3/4, -6/7.
  • We'll also assume that when the denominator is 1, that it should be omitted, e.g. 2, and 0, not 2/1 and `0/1'
  • We'll assume that the negative sign, when present, always appears in the numerator

We'll leave the "quotient" of two rational numbers as a very straightforward programming exercise.

In the first few examples, not all of these rules may yet be in place. We'll add those incrementally. Also, as we proceed through the examples, we may make additional assumptions, set additional goals, and/or place additional restrictions. Those will be noted in the README.md for each example.

Java Language Concepts

This is a work in progress; not all may be covered yet. Will update this list with reference to the first example where each concept is introduced as work on this repo progresses.

  1. private data members (ex01)
  2. public constructors (ex01)
  3. toString method (ex02)
  4. public getters (ex02)
  5. JUnit testing (ex03)
  6. immutable objects (throughout, but discussed in ex04)
  7. static methods (class level) (ex05)
  8. exceptions (IllegalArgumentException) (ex07)
  9. proper way to override equals and hashcode (ex09 and beyond)
  10. equals vs. == (ex09 and beyond)
  11. comparable interface (ex09 and beyond)
  12. creating an arraylist of rationals (ex09 and beyond)
  13. sorting an arraylist of rationals (ex09 and beyond)

Java Language Toolset Skills

  1. Compiling/Running by hand with javac and java (ex01)
  2. Ant (ex02)
  3. Running from a jar file (ex03)
  4. JUnit jars (ex03)
  5. Javadoc (ex04)
  6. Adding line numbers to stack traces (ex05)
  7. Adding links to Java Standard Libraries in our javdoc (ex06)
  8. Packages (ex09 and beyond)
  9. Maven (ex09 and beyond)

cs56-rational-example's People

Contributors

dvanmali avatar hiranya911 avatar pconrad avatar

Watchers

 avatar  avatar  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.