Packages
Learning Objective
* Package Concepts in JavaOrientation Video
Introduction
Packages:
Multiple classes of larger programs are usually grouped together into a package. Packages correspond to directories in the file system, and may be nested just as directories are nested. Package = directory.
Reasons to use packages
• Grouping related classes.
• To allow classes to be combined in a single ".jar" file.
• To control the namespace.
• To limit the scope of names.
Grouping related classes. If you are writing a normal one-programmer application, it will typically be in one package. The purpose of a package is to group all related classes together. If you are working on a program that is divided into separate sections that are worked on by others, each section might be in its own package. This prevents class name conflicts and reduces coupling by allowing package scope to be used effectively.
Package declarations:
Each file may have a package declaration which precedes all non-comment code. The package name must be the same as the enclosing directory. Default package. If a package declaration is omitted, all classes in that directory are said to belong to the "default" package. Here are two files in the packagetest directory. package packagetest;
class ClassA { public static void main(String[] args) { ClassB.greet(); } } and package packagetest; // Same as in previous file. class ClassB { static void greet() { System.out.println("Hi"); } }
Resources
http://en.wikipedia.org/wiki/Polynomialhttp://java.sun.com/docs/books/tutorial/java/package/packages.html
Task Description :
Polynomial Arithmetic Package (Java OOP basics)In this mini task you would be developing a small package for Arithmetic on Polynomials. To make life a little easier, we would only be dealing with polynomials in one variable. You should be packaging the whole code in a package called "java-pack". Learn how to use Packages in Java. Eg: Ax^2 + Bx + C or Ax+B or Ax^7 + Bx^5 +Cx^2 etc
You would be developing a Polynomial library to do the following :-
· Add two polynomials of any degree
polynomial 1: Ax^3 + Bx + C
polynomial 2: Px^3 + Qx^2
Result: (A+P)x^3 + Qx^2 + Bx + C
· Subtract two polynomials
polynomial 1: Ax^3 + Bx + C
polynomial 2: Px^3 + Qx^2
Result: (A-P)x^3 - Qx^2 + Bx + C
· Multiply two polynomials
polynomial 1: Ax^3 + Bx + C
polynomial 2: Px^3 + Qx^2
Result: A*P x^6 + P*Bx^4 + P*Cx^3 + A*Q x^5 + B*Qx^3 + C *Qx^2
Hint: There are many ways of doing it, but since you would have already done addition by now, you may see this as doing the following
Polynomial.AddPolynomial( Ax^3 + Bx + C , Px^3 ),
Polynomial.MultiplyPolynomial(Ax^3 + Bx + C , Qx^2))
Comments
Post a Comment