Week 1 Power Point Slides
Download
Report
Transcript Week 1 Power Point Slides
JAVA IN INDUSTRY
Develop desktop applications
JGrasp, NetBeans, Eclipse, and many others
ACORNS, ELK, WOLF, SoundEditor
Java Server Pages (JSP): Respond to client side requests
for Web-pages and JavaScript AJAX requests
Applets: Applications that run from within a browser and can
be dragged to run as a stand alone application
Network Services: Server side programs that listen for client
side requests
Cloud Computing: Applications that reside on distributed
groups of remote computers
Check Out: http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
JAVA TERMINOLOGY REVIEW
public static void main(String[] args)
public
static
void
main
String[] args
Variable
Identifier
Command line
Class, Object
Constructor
Instance variable
public, private
Primitive variable
Reference variable
Scope and Visibility
OTHER RESERVED WORDS
PrimitiveData Types
final
import
package
return
null
new
class, this
break, continue
Integer
Fraction
byte
char
short
int
long
float
double
Boolean (boolean)
true
false
Note: Java reserved words are always lower case
BASIC CONSTRUCTS
All programs, no matter how complex, consist of:
Sequence blocks
Decision statements (if, switch, case, default)
Loops (four kinds)
do/while
– posttest
while – pretest
for – counter controlled
for/each
Expressions and operators
Arrays, initializer list
COMMON COMPILATION ERRORS
Cannot resolve symbol zzz
Variable x might not have been initialized
'Class' or 'interface' expected
Not a statement
Non-static method doIt() cannot be called from a static
context
; expected
Cannot return a value from a method of type void
Method does not return a value
Missing return statement
Unreachable statement
Return type required
Possible loss of precision
COMMON RUNTIME EXCEPTIONS/MESSAGES
NullPointerException
ArithmeticException
ArrayIndexOutOfBoundsException
StringIndexOutOfBoundsException
Could not find main class
NumberFormatException
DEVELOPMENT ENVIRONMENTS
For Beginners
Jgrasp, BlueJ, DrJava
Easy to learn and use
Limited plugin and language support
For large scale projects typical of industry
Development environment for many languages besides Java
Extensive plugin support
Fully featured: including integrated profiler and version control
NetBeans: Created by Sun/Oracle; easier to use than Eclipse
Eclipse: Created by IBM; more plugins than NetBeans
Note: We will use the Eclipse IDE in this class
DEBUGGING
When a problem is not obvious
Ensure a program really works for all cases
Glass Box debugging
Test every statement and every path
Thoroughly test the loops
Force the program through error paths
Black Box debugging: Functional tests and quality assurance
In Eclipse (Debug View)
Insert print statements
Comment out lines
Set a breakpoint: Execute one statement at a time
Over, into, out, to cursor
By condition or count
See what happened
Cursor over a variable
In debug view, type window, then expressions
DEVELOPING AN APPLET
A web-based application that runs within a browser
public class MyApplet extends javax.swing.Japplet // Inheritance
{
public void init() // Initialization
{
// Add code here to instantiate variables and configure the interface
// Set the size, and color the background
// Set an icon if desired
}
public @Override void paint(Graphics g) // Control what displays
{
super.paint(g);
// Add code here to draw onto the Applet window
} }
JAVA GRAPHICS
Measured in pixels
The (0,0) point is at the
upper left corner
x-axis values increase as
positions go to the right
The y-axis values increase
as positions descend
vertically
If part of a shape is beyond
the bounds, that part will
not display
POLYGON CLASS
Create a polygon
int[] xs = {10,30,100,120,300};
int[] ys = {200,250,270,300,350};
Polygon poly = new Polygon(xs, ys, xs.length);
Move polygon: x horizontally, y vertically
poly.translate(x, y);
Get polygon width and height:
poly.getBounds().width & poly.getBounds().height
Draw and Fill polygon (draw only paints the outline) :
g2d.draw(poly) and g2d.fill(poly);
POLYGON EXAMPLE
(xPoints[0], yPoints[0])
(xPoints[5], yPoints[5])
(xPoints[1], yPoints[1])
(xPoints[3], yPoints[3])
(xPoints[4], yPoints[4])
(xPoints[2], yPoints[2])
int[] xPoints = {100, 300, 250, 200, 120, 70};
int[] yPoints = {80, 100, 160, 130, 150, 95};
Polygon poly = new Polygon(xPoints, yPoints, xPoints.length);
poly.translate(-70, -80); // Move to the top left corner)
g.setColor(Color.RED);
g2d.fill(poly);
GRAPHICS CLASS
Draw lines, circles, rectangles,
polygons, colors, text
public @Override void paint(Graphics g)
{ Graphics2D g2d = (Graphics2D)g;
g2d.setColor(new Color(255,0,0));
Line2D.Float line = new Line2D.Float(20,10,700,250);
g2d.draw(line);
g2d.setColor(new Color(0,255,0,128));
g2d.fill(new Rectangle(300,100,50,100));
g2d.setColor(Color.LIGHT_GRAY);
g2d.fill(new Ellipse2D.Float(500,150,100,150));
int[] xPoints = {10,30,100,120,300};
int[] yPoints = {200,250,270,300,350};
g2d.setColor(Color.MAGENTA);
g2d.fill(new Polygon(xPoints, yPoints, 5));
Font font = new Font("Times New Roman",Font.BOLD,25);
g2d.setFont(font);
g2d.setColor(Color.BLACK);
g2d.drawString("Meaningless Shapes", 300,500);
}