1 / 33

Java Programming Essentials Review

Dive into Java programming basics with this comprehensive review including class methods, instance methods, conditional statements, recursions, and more. Learn to develop applications and work with strings and lists effectively.

maldonadoj
Download Presentation

Java Programming Essentials Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class 19 - Review • This lecture contains a selection of slides from previous lectures, giving the “high points” of the material we have covered. • This selection is intended merely as an “outline” of the topics. All material covered in lecture may be included in the exam. (Exception: you will not be asked to draw a Hilbert curve.)

  2. Applications vs. applets • Applications: stand-alone programs, usually invoked from command line.

  3. Strings Strings have many instance methods provided in the Java API, e.g. • int length () • int indexOf (String) • String substring(int, int) • String toUpperCase() • ... lots more...

  4. Review • Class methods: call using class name and method name, e.g. Math.sin(...)In documentation, begins with word static. • Instance methods: call using value and method name, e.g. String s; …s.substring(0,4)In documentation, does not begin with word static.

  5. Summary • Classes introduce types. Values with these types are called objects. • Create objects using new (exception: String’s) • Classes have • Class methods: classname.methodname (arg’s) • Class variables: classname.variablename

  6. Summary (cont.) • Objects have: • Instance methods: expression .methodname (arg’s) • Instance variables: expression.variablename • Variables declared inside main (or other methods) are called local variables. These are different from class variables and instance variables, and are referred to simply by their names.

  7. Methods • Method = named collection of statements • Can be class method (static) or instance method • Has return type • Method call is statement if return type is void • Method call is expression if return type is not void

  8. Conditional statements A conditional statement has the formif ( condition)statement1else statement2 It executes statement1 if the condition is true, statement2 if the condition is false.

  9. Conditional expressions • A conditional expression has the form: condition ? expression1 : expression2 • It evaluates the condition and then either evaluates expression1 or expression2, depending whether condition is true or false • Note: the type of expression1 and expression2 must be the same; that type is the type of the conditional expression

  10. Recursive methods As of now, we lack the ability to do repetitive actions. Can solve them using recursive methods: A recursive method is one that calls itself.

  11. Recursive methods in Java Recursive method f with argument x has the form static typename f (typename x) { if (x is simple enough) solve directly else use f(value “smaller than” x)to calculate f(x) }

  12. Thinking recursively Key ideas: • Know exactly what the method is supposed to do. • Assume method works for smaller values. Then just figure out how to use it to compute method for larger values. • Don’t forget base cases - small values that can be calculated directly.

  13. Lists Lists are a simple data structure in which data are stored in a row. We will talk first about lists of integers: x0, x1, ..., xn-1 (n >= 0) Elements can be added and removed only at the beginning (x0).

  14. Recursion on lists Writing recursive methods on lists follows same principle as for integers: • To compute f(L), assume f(L’) can be calculated for lists L’ smaller than L, and use f(L’) to calculate f(L). • Some lists are small enough for f to be calculated directly

  15. Lines We will provide a class called Line, with several operations: • Construct a line from (x0,y0) to (x1,y1) by: new Line(x0,y0,x1,y1) • Give line L, find coordinates of starting and ending points using instance methods x0(), y0(), x1(), y1(). • Lines can be printed by System.out.println.

  16. Line lists The class LL has the same operations as IL, but contains operations on lists of Line objects: • LineList cons (Line ln, LineList L) - construct list containing i at front • nil

  17. Drawing lines at an angle To draw a line of length m at an angle theta from the x-axis: m y = m sin   x = m cos  (But remember, this is upside-down...)

  18. Drawing “stars” (cont.) static LineList star1 (int order, int radius, double theta, double angleIncr) { if (order == 0) return LL.nil; else return LL.cons( new Line(0, 0, (int)Math.round(radius*Math.cos(theta)), (int)Math.round(radius*Math.sin(theta))), star1(order-1, radius, theta+angleIncr, angleIncr)); } (Detail: Math.cos and Math.sin take arguments in radians, instead of degrees.)

  19. Rotation Rotation is more complicated. Consider rotating one point around the origin by angle : (x’,y’) 1. Calculate m and  m = x2+y2  = tan-1(y/x) 2.  =  +  3. (x’,y’) = point of length m, at angle     (x,y)

  20. Rotation (cont.) We’ll rotate shapes (i.e. LineList’s) about the origin by rotating each line: static LineList rotateShapeAboutOrigin (LineList L, double theta) { if (LL.empty(L)) return LL.nil; else return LL.cons(rotateLine(LL.hd(L), theta), rotateShapeAboutOrigin(LL.tl(L), theta)); }

  21. HTML • Web pages are text documents with special formatting commands, called tags. • The formatting language is called HTML. • Can view raw HTML by selecting “view source” in browser.

  22. The APPLET tag • Place all class files in a subdirectory, say appletcode. <APPLET CODE=“appletname.class” CODEBASE=“appletcode” HEIGHT = 400 WIDTH = 300> </APPLET> stuff in here will be displayed if browser is not Java-equipped mandatory

  23. Writing applets The general form of an applet is: import java.applet.*; import java.awt.*; public class appletname extends Applet { public void init () { ... } public void paint (Graphics g) { ... } ... other methods ... } Usually present

  24. init and paint methods Most applets have these two methods (at least): • init initializes instance variables (see below). • paint performs graphics operations to draw shapes in the applet’s window • Called when applet is first displayed, and whenever it is re-displayed. • The Graphics objects is used to do the drawing

  25. Drawing operations The Graphics class defines numerous instance methods: • drawLine(int x0, int y0, int x1, int y1) - draw a line from (x0,y0) to (x1,y1) • drawRect(int x0, int y0, int width, int height) - draw a width x height box, with upper left corner at (x0,y0) • fillRect(int x0, int y0, int width, int height) - same as drawRect, but filled

  26. Example: drawing a LineList • When the paint method is called with a Graphics object, it can call other methods. The Graphics object can be used to draw in the applet window. • Draw a LineList in a Graphics object: static void drawLineList (LineList L, Graphics g) { if (!LL.empty(L)) { Line ln = LL.hd(L); g.drawLine(ln.x0(), ln.y0(), ln.x1(), ln.y1()); drawLineList(LL.tl(L), g); } }

  27. Color • The Color class (in the java.awt package) has values that denote different colors. • Symbolic constants Color.black, Color.red, etc., are predefined. • Arbitrary colors can be created by giving RGB values in the range 0-255 when constructing a Color value: • Color col1 = new Color(100,220,155); • Color darkred = new Color(100,50,50);

  28. Instance variables • Variables declared inside a method (the kind we’ve used so far) are either parameters or local variables. • Variables declared inside a class but outside of any method are either class variables or instance variables. (They are class variables if they are declared with the static keyword; we will consider only instance variables for now.)

  29. Components Some sample components: • Label: Place text in applet (alternative to drawString): new Label(“A label”); • Button: Place button, possibly with label: new Button(); new Button(“Press me”); • TextField: Create field of given width to input text: new TextField(10);

  30. Placing GUI components in an applet • Normally, declare an instance variable of the component type (Button, Checkbox, TextField, etc.) • In init • Initialize variable. • Use add method to place it in applet. • paint does not have to draw components - they are automatically drawn

  31. Example: A component-filled applet • public class BusyApplet extends Applet { • Label L = new Label(“A label”); • Button b = new Button(); • Button bl = new Button(“Press me”); • TextField t = new TextField(10); • CheckBox cb = new Checkbox(); • public void init () { • add(L); add(b); add(bl); • add(t); add(cb); • } • }

  32. Input in applets • Users enter text in TextField components • Reading the value in a TextField involves four changes to what we’ve seen: import java.awt.*; import java.applet.*; import java.awt.event.*; public class appletname extends Applet implements ActionListener { 1 2

  33. Input in applets (cont.) TextField t = new TextField(4); public void init () { ... add(t); t.addActionListener(this); } public void actionPerformed (ActionEvent e) { ... t.getText() ... repaint(); } 3 4

More Related