1 / 35

Algoritmiek

Algoritmiek. Variables and Methods Graphics Hoorcollege 10 - Ma. 6 nov. 2006 L.M. Bosveld-de Smet. Variables: associatie (1). Variables: associatie (2). Objects: instance variables instance methods. Voorbeeld Object: letter ‘a’. Classes en Objects. Variables: kenmerken. Naam Data Type

teresa
Télécharger la présentation

Algoritmiek

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. Algoritmiek Variables and Methods Graphics Hoorcollege 10 - Ma. 6 nov. 2006 L.M. Bosveld-de Smet

  2. Variables: associatie (1)

  3. Variables: associatie (2)

  4. Objects: instance variablesinstance methods

  5. Voorbeeld Object: letter ‘a’

  6. Classes en Objects

  7. Variables: kenmerken • Naam • Data Type • 8 primitive types • onbeperkt aantal object of reference types • Bereik (Scope) • Levensduur (Lifetime) • Initialisatie • Declaratie

  8. Variables: verschillende soorten • Local variables • Parameters • Instance variables (non-static fields) • Class variables (static fields)

  9. Variables: overzicht

  10. public class variable: voorbeeld • Constante van de Math class (java API) public static final double PI = 3.1415926535…; public class PrintPI { public static void main (String[] args){ System.out.println(Math.PI); } }

  11. Instance variable: voorbeeld public class Bicycle { … … … private int speed; … … … public Bicycle (int startSpeed) { speed = startSpeed; } … … … public int getSpeed (){ return speed; } … … … } instance variable constructor: Bicycle myBicycle = new Bicycle (10); getter method: myBicycle.getSpeed()

  12. public class method: voorbeelden • Methods van de Math class (java API) public static int round (float a) public static double sqrt (double a) public static double random () Math.round(5.5)

  13. private class variable / public class method: voorbeelden public class Bicycle { … … … private int speed; private int id; private static int numberOfBicycles = 0; … … … public Bicycle (int startSpeed) { speed = startSpeed; id = ++numberOfBicycles; } … … … public static int getNumberOFBicycles(){ return numberOfBicycles; } … … … } class variable class method: Bicycle.getNumberOfBicycles

  14. local variables: voorbeeld (1) public class Adding { public static void main (String[] args) { int next=1; int total = 0; while (next > 0) { next = Integer.parseInt(SimpleIO.readLine()); total = total + next; } System. out.println("Total is : " + total); } }

  15. local variables: voorbeeld (2) while (workToDo (table)) { for (int r = 0; r < table.length; r++) { for (int c = 0; c < table [0].length; c++) { if (table [r] [c] > 3) { stamps += 4; table [r] [c] -= 4;

  16. parameter: voorbeeld public void setFee (double newFee) { fee = newFee; } parameter doc.setFee (150.00);

  17. Array parameters public class WordLists { public static void main(String[] args) String[] wordList1 = {"aap", "hond", "varken", "luipaard", "bever"}; String[] wordList2 = {"hortensia", "roos", "lelie", "geranium", "magnolia"}; System.out.println ("Longest word list 1: " + wordList1[longestWord(wordList1)]); System.out.println ("Longest word list 2: " + wordList2[longestWord(wordList2)]); } private static int longestWord(String[] list) { int indexLongestWord = 0; for (int i = 1; i < list.length; i++) { if (list[i].length() > list[indexLongestWord].length()) { indexLongestWord = i; } } return indexLongestWord; } }

  18. Instance methods public class Person { private String name; public Person () { name = "no name yet."; } public Person (String initialName) { name = initialName; } public void setName (String newName) { name = newName; } public String getName () { return name; } public void writeOutput () { System.out.println ("Name: " + name); } public boolean sameName (Person otherPerson) { return (this.name.equalsIgnoreCase (otherPerson.name)) } }

  19. Class methods: examples public static void callByName (String name) { system.out.print (name); } private static int updateScore (int currentScore, int points) { return currentScore + points; }

  20. Class var./method: gebruik in class public class <class_name> <declaratie class variable(s)> public static void main (String [] args) { <main body> } <declaratie class method(s)> }

  21. “helpers” • Class methods als “helpers” • “cohesion” • “stepwise refinement” • “top-down decomposition”

  22. Verdeling van taken • Quiz-programma: • Stel een vraag aan de gebruiker • Lees het antwoord van de gebruiker • Controleer of het antwoord correct is • Geef adequate feedback • Hou de score bij

  23. Class “helper” methods private static String readAnswer (String question) { SimpleIO.prompt (question); String userInput = SimpleIO.readLine(); return userInput.trim().toLowerCase(); } private static boolean match (String userAnswer, String correctAnswer) { return (userAnswer.equals(correctAnswer)); } private static void giveFeedback (boolean ok, String hurrah, String sorry) { System.out.println (ok ? hurrah : sorry); } private static void updateScore (boolean ok) { if (ok) { score += 10; } }

  24. Gebruik van “helpers” in main import jpb.*; public class Quiz { private static int score; public static void main(String[] args) { score = 0; String userAnswer = ""; boolean isCorrect = false; userAnswer = readAnswer ("Waarvan is bit de afkorting?\n") ; isCorrect = match (userAnswer, "binary digit") ; giveFeedback (isCorrect, "Goed geantwoord!", "Sorry. Verkeerd antwoord."); updateScore (isCorrect) ; userAnswer = readAnswer ("Hoeveel bytes komen overeen met 32 bits?\n") ; isCorrect = match (userAnswer, "4") ; giveFeedback (isCorrect, "Het gaat prima zo!", "Wat jammer nu!") ; updateScore (isCorrect) ; System.out.println ("Your total score is " + score + " points."); } <declaration helpers> }

  25. Graphics

  26. Graphics class of AWT package

  27. DrawableFrame object en methoden import java.awt.*; import jpb.*; public class <class name> { public static void main (String[] args) { // create drawable frame DrawableFrame df = new DrawableFrame(<frame title>); df.show() / df.setVisible(true); df.setSize(<width>,<height>); // obtain graphics context Graphics g = df.getGraphicsContext(); //make drawing <Graphics methods applied to Graphics object>; // repaint frame df.repaint(); } }

  28. DrawableFrame- en Graphics-objecten graphics context frame / window line

  29. DrawLine class // draws a line inside a frame import java.awt.*; import jpb.*; public class DrawLine { public static void main (String[] args) { // create drawable frame DrawableFrame df = new DrawableFrame("Line"); df.setVisible(true); df.setSize(200,200); // obtain graphics context Graphics g = df.getGraphicsContext(); // draw line g.drawLine(50,50,150,150); // repaint frame df.repaint(); } }

  30. Vormen tekenen

  31. Gele achtergrondvorm stoplicht kleur van de vorm // Draw background of sign g.setColor(Color.yellow) int[] xBackground = {0, 100, 200, 100}; int[] yBackground = {100, 0, 100, 200}; g.fillPolygon(xBackground, yBackground, xBackground.length); X coordinaten van vorm Y coordinaten van vorm

  32. 5 geneste vierkanten // Draw five nested polygons to form the stripe on the // outer border of the sign g.setColor(Color.black); int[] xPolygon1 = {5, 100, 195, 100}; int[] yPolygon1 = {100, 5, 100, 195}; g.drawPolygon(xPolygon1, yPolygon1, xPolygon1.length); int[] xPolygon2 = {6, 100, 194, 100}; int[] yPolygon2 = {100, 6, 100, 194}; g.drawPolygon(xPolygon2, yPolygon2, xPolygon2.length); int[] xPolygon3 = {7, 100, 193, 100}; int[] yPolygon3 = {100, 7, 100, 193}; g.drawPolygon(xPolygon3, yPolygon3, xPolygon3.length); int[] xPolygon4 = {8, 100, 192, 100}; int[] yPolygon4 = {100, 8, 100, 192}; g.drawPolygon(xPolygon4, yPolygon4, xPolygon4.length); int[] xPolygon5 = {9, 100, 191, 100}; int[] yPolygon5 = {100, 9, 100, 191}; g.drawPolygon(xPolygon5, yPolygon5, xPolygon5.length);

  33. Tekenen van stoplicht // Draw a black rectangle (the traffic signal) g.fillRect(77, 35, 46, 130); // Draw red,yellow,and green circles (the traffic // lights) g.setColor(Color.red); g.fillOval(83, 43, 34, 34); g.setColor(Color.yellow); g.fillOval(83, 84, 34, 34); g.setColor(Color.green); g.fillOval(83, 125, 34, 34);

  34. Tonen van tekst Monospaced, Bold, 30 SansSerif, Italic, 30 Serif, Plain, 40

  35. Toon tekst: programma import java.awt.*; import jpb.*; public class DisplaySum { public static void main (String[] args) { // create drawable frame DrawableFrame df = new DrawableFrame("Wirth's RULE"); df.setVisible(true); df.setSize(350,200); // obtain graphics context Graphics g = df.getGraphicsContext(); // display text in red monospaced bold font g.setColor(Color.red); g.setFont(new Font("Monospaced", Font.BOLD, 30)); g.drawString("DATA STRUCTURES + ", 15, 50); // display text in green sansserif italic font font g.setColor(Color.green); g.setFont(new Font("SansSerif", Font.ITALIC, 30)); g.drawString("ALGORITHMS = ", 15, 100); // display text in blueserif plain font g.setColor(Color.blue); g.setFont(new Font("Serif", Font.PLAIN, 40)); g.drawString("PROGRAMS", 15, 150); // repaint frame df.repaint(); } }

More Related