View Full Version : Explaining libraries, help please?
wa2blah
10-19-2005, 02:47 PM
Can someone explain to me how to use an internal library for example the rectangle library? I have no idea how these libraries work.
TheShaner
10-19-2005, 04:08 PM
From Wikipedia - Library (computing) (http://en.wikipedia.org/wiki/Library_(computing))
In computer science, a library is a collection of subprograms used to develop software. Libraries are distinguished from executables in that they are not independent programs; rather, they are "helper" code that provides services to some other independent program. Today the vast majority of the code that executes in a typical application is located in the libraries it uses.
In your previous program, you imported two libraries:
import java.awt.*;
import hsa.*;
Many of the tasks in your program were able to be executed because of those libraries. Let's look at the rectangle class you mentioned. If you added this (for fun I guess) in your previous program:
Rectangle r1 = new Rectangle(5, 10, 20, 30);
You wouldn't run into any error. Why? Because the Rectangle class, which includes its constructor and methods, is contained in the java.awt library that you imported. If you removed that library, you would then receive an error on the line instantiating the Rectangle (and errors on many other lines that use objects from the java.awt library).
Now, if you did this:
double answer = sin(0);
You would receive an error on that line. Why? Because your Java program has no idea about the sin function. It doesn't know what sin does until you import the java.lang.Math library. The Math library tells the program what the sin function does. It finds the sine of 0, which is 1.
So although you say you don't know how they work, you've actually been using them in your programs (without really realizing that you've been doing so). Hope that helps!
-Shane
wa2blah
10-20-2005, 02:47 PM
I somehwat understand what a library does now. Now I have to apply it to my rectangle program. I need to draw a rectangle but don't know what I have to do in order to draw one. Here is my code for the rectangle class:
/**
* This will calculate items for a rectangle
*
* @author Murray McClafferty
* @version Oct 2005
*/
public class Rectangle
{
// instance variables - replace the example below with your own
private double x; //This is the x of one point of the rectangle
private double y; //This is the y of one point of the rectangle
private double l; //This is the length of the rectangle
private double w; //This is the width of the rectangle
/**
* Constructor for objects of class Rectangle
*/
public Rectangle(double myX, double myY, double myL, double myW)
{
// initialise instance variables
x = myX;
y = myY;
l = myL;
w = myW;
} //constructor
/**
* Calculate the perimeter of the rectangle
*
* @return The perimeter of rectangle
*/
public double Perimeter()
{
double perimeter = (2*l) + (2*w);//Getting the perimeter
return perimeter;
}
/**
* Calculate the area of the rectangle
*
*@return The area of the rectangle
*/
public double Area()
{
double area = l * w;
return area;
}
/**
* Accessor for the x value
*
* @return the x value
*/
public double getX()
{
return x;
}
/**
* Accessor for the y value
*
* @return the y value
*/
public double getY()
{
return y;
}
/**
* Accessor for the length
*
* @return the length
*/
public double getL()
{
return l;
}
/**
* Accessor for the width
*
* @return the width
*/
public double getW()
{
return w;
}
}
and here is my code for the RectanglePanel
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
/**
* The RectanglePanel class draws a rectangle on a graph.
*
* @author Murray McClafferty
* @version 20 October 2004
*/
public class RectanglePanel extends JPanel
{
// instance variables
private Rectangle rectangle;
/**
* Constructor for objects of class QuadraticPanel
*
* @param quad the quadratic to be graphed
*/
public RectanglePanel(Rectangle rect)
{
setPreferredSize(new Dimension(200,200));
rectangle = rect;
}
/**
* Graphs the Rectangle.
*
* @param g Graphics object to draw in
*/
public void paintComponent(Graphics g)
{
int[] x = new int[51];
int[] y = new int[51];
super.paintComponent (g); // This must be done first or weird things might happen
Graphics2D g2 = (Graphics2D) g; // More modern graphics object
}
}
Any help would be appreciated. I know I have to use the rectangle class by using a point with a width and length but i don't know what I have to do in order to draw the rectangle
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.