PUC – Wiki part 2

Dear Mr. Wurzinger! Hello and welcome again to my scriptum! It shows you how fast a school year passes by, but above all it is the evidence of half a year’s work.

 Arbeitsblatt „You can send more than one thing to a method“  Arbeitsblatt „You can things to a method“
Example: Book b = new Book(); int x = 7; foo.go(x, b); sout(x);  0 sout(b);  0; public go(int z1, Book z2) { z1 = 0; z2 = null; } Example: Sum of all Integers int[] nums = {1,2,3,4,5}; foo.sum(nums); public void sum(int[] nums) { numbers[0] = 0; int i, sum = 0; for (i = 0; i < numbers.length; i++) { sum += numbers[i]; } return sum; }

Summary „For“
int i, sum = 0;

for (i = 0; i < nums.length; i++) { sum = sum + nums[i]; } int i = 0, sum = 0; while (i < nums.length) { sum = sum + nums[i]; i++; }

int i = 0, sum = 0; do { if (nums.length >= 0) { sum = sum + nums[i]; i++; } } while(i < nums.length)

Example: Solange einlesen bis die Zahl kleiner 10 und größer 500 ist. Verwende da zu das JOptionPane!

ungültig

10

500

ungültig

boolean help = false; while (help == false) { String text = JOptionPane.showInputDialog("Bitte Zahl größer 10 und kleiner 500 eingeben!"); if (Integer.parseInt(text) < 10 && Integer.parseInt(text) > 500) { help = true; } }

XIII) 2 dimensionale Arrays
int [] [] nums = new int [3] [3];

columns  0 rows  0 1 2 zuerst Reihe, dann Spalte! nums[1][2] = 3 3 1 2

Summenbildung for (col = 0; col < 3; col++) { sum += nums[1][col]; }

XIV) TicTacToe
X X … -1 O…1 O

1) 2) 3) 4) 5)

Feld erzeugen Feld mit 0 füllen -1 bzw. 1 einsetzen überprüfen, ob jemand gewonnen hat Ausgabe des Feldes

GUI für TicTacToe
1) 2) 3) 4) 5) New JFrame Form JFrame  Set Layout  Grid Layout Grid Layout  Properties  Columns 3, Rows 3 Buttons bennenen Jeden Button markieren  Properties  Events  Action Performed evtl. getActionCommand()

WICHTIG
1) Beziehungen 2) Teile und Herrsche
public Class GUI extends … { private TicTacToe ttt = new TicTacToe(); public void onClick() { … }

XV) DOS – Commands
cmd cd Command Line Change Directory cd .. cd <Name> Directory lists all the folders and files Kopieren copy <File> <Directory> (from actual directory!) Löschen delete <File> (from the actual directory!) Bewegen move <File> <Directory> (from the actual directory!) runs specific java command Creates directory (in the actual directory!) Deletes Direcotry (in the actual directory!)

dir copy

delete

move

java mkdir rmdir

XVI) Exceptions
Example: try { f.readLine();  Source Code } catch(NumberFormatException ex) { sout(„Falsche Eingabe!“); }

Arbeitsblatt „Handling Exceptions“

Alert on Handy try { … } catch(Exception ex) { Alert e = new Alert(„Fehler!“); e.setType(AlertType.ERROR); e.setString(ex.getMessage()); display.setCurrent(e, myForm); }

ex0031 Calculator Calculator - operand1: int - operand2: int - result: double + Calculator + div, add, sub, mul + getResult(): double

Random Numbers Math.random(); 0…2 start value sv sv + (ev – sv) * Math.random(); end value ev

Mandaterechner

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

1017 508,3 339 254 203,4 169,5 145,28 127,13 113 101,7 92,45 84,7 78,23 72,64 67,8

386 193 128,6 96,5 77,2 64,3 55,14

126 63 42

XVII) Konstruktor
public class Calculator { public calculator (int op1, int operand2) throws Exception { if ((op1 <= 100) || (op2 <= 100)) { throw new Exception(„invalid number!“); } else { operand1 = op1; this.operand2 = operand2; } …

Calculator c = new Calculator(3, 0); c.div();

XVIII) Collections
Example: divisor of a number n = 5 // 1, 5 n = 16 // 1, 2, 4, 8, 16 n = 10 // 1, 2, 5, 10 Collections.sort(list); ArrayList  „java.util.ArrayList“ - add(Object obj): adds the object parameter to the list - get (int index): returns the object at the index parameter

ArrayList list = new ArrayList(); for (int i = 1; i < Math.sqrt(n); i++) { if ((n%i) == 0) { int divisor = n / i; list.add(new Integer(i)); list.add(new Integer(divisor));

} for (int i < 0; i < list.size(); i++) { Integer d = (Integer) list.get(i); sout(d.toString()); }

Test Cases: package ex0033test; import org.junit.Test; import static org.junit.Assert.*; import ex0033.*; import java.util.ArrayList; /** * * @author johann */ public class DivisorOfNumberTester { @Test public void test1() { try { DivisorOfNumber dn = new DivisorOfNumber(1); dn.compute(); ArrayList list = dn.getList(); assertEquals(1,list.size()); int n = ((Integer)list.get(0)).intValue(); assertEquals(1,n); } catch (Exception ex) { fail("no exception expected"); } } @Test(expected=Exception.class) public void test2() throws Exception { new DivisorOfNumber(0); } @Test public void test3() throws Exception {

DivisorOfNumber dn = new DivisorOfNumber(2); dn.compute(); ArrayList list = dn.getList(); assertEquals(2,list.size()); int n = ((Integer)list.get(0)).intValue(); assertEquals(1,n); n = ((Integer)list.get(1)).intValue(); assertEquals(2,n); } @Test public void test4() throws Exception { DivisorOfNumber dn = new DivisorOfNumber(64); dn.compute(); ArrayList list = dn.getList(); assertEquals(7,list.size()); assertEquals(1,((Integer)list.get(0)).intValue()); assertEquals(2,((Integer)list.get(1)).intValue()); assertEquals(4,((Integer)list.get(2)).intValue()); assertEquals(8,((Integer)list.get(3)).intValue()); assertEquals(16,((Integer)list.get(4)).intValue()); assertEquals(32,((Integer)list.get(5)).intValue()); assertEquals(64,((Integer)list.get(6)).intValue()); } @Test public void test5() throws Exception { Stopwatch watch = new Stopwatch(); DivisorOfNumber dn = new DivisorOfNumber(2147483646); dn.compute(); System.out.println(dn.getList().size()); System.out.println("time = " + watch.getTime()); } @Test public void test6() throws Exception { Stopwatch watch = new Stopwatch(); DivisorOfNumber dn = new DivisorOfNumber(2147483646); dn.compute2(); System.out.println(dn.getList().size()); System.out.println("time = " + watch.getTime()); } }

Vector Vector list = new Vector(); - list.addElement(Object obj) - list.removeElementAt(int index); for (int i = 0; i < list.size(); i++) { if (list.getElement(i) < 5) { list.removeElementAt(i); } }

package ex0033_Package; import java.util.Vector; /** * * @author Horst */ public class CollectionTester { private Vector list; public CollectionTester() { list = new Vector(); } public void test() { int i; for (i = 0; i < 10; i++) { Integer obj = new Integer(i + 1); list.add(obj); } System.out.println(list.size()); System.out.println(list.toString()); list.removeElementAt(5); list.removeElement(new Integer(8)); for (i = 0; i < list.size(); i++) { Integer obj = (Integer) list.elementAt(i + 1); if (obj.intValue() > 4)

{ list.removeElementAt(i); // Falsche Methode!!! } } } public void test2() { int i; int helper = 0; for (i = 0; i < 10; i++) { Integer obj = new Integer(i + 1); if (obj.intValue() > 4) { helper++; list.add(obj); } else { list.add(obj); } } int e = list.size() - 1; while (i > 0) { Integer obj = (Integer) list.elementAt(i); if (obj.intValue() > 4) { list.removeElementAt(i); } i--; } } }

XIX) Average Tester Version 1 / 2
1) 2) 3) 4) 5) 6) Given are numbers between 10 and 100 Manage the numbers with the class Vector, OR an Integer Array Calculate the average Remove all numbers greater than „1.2 * average“ Remove all numbers less than „0.8 * average“ Test (JUnit)! Plausibility test (from < 10 to > 100)!

XX) Desktop Application
1) New Project  New Package  New JFrame Form 2) versch. Layouts z.B. Grid Layout 3) JFrame  SetLayout BorderLayout North West

Center

South 4) Panel reinziehen 5) Properties  Layout  Direction  Center 6) Panel  Set Layout  Border Layout 7) Panel  Properties  Border  Title Boarder 8) Button reinziehen  in Liste rausziehen 9) noch ein Panel reinziehen  Grid Bag Layout 10) Label reinziehen, TextField reinziehen

East

11) Label  Customize 12) Button  Events  Action Performed DefaultListModel dlm = new DefaultListModel(); dlm.addElement(new Integer(…)); list1.setModel(dlm);

XXI) GUI zum Zeichnen
1) 2) 3) 4) JFrame Form Border Layout JPanel in Center mit Namen plCanvas leere Klasse mit „Canvas“

… extends JPanel @ Override protected void paintComponent(Graphics g) super.paintComponent(g); g.drawLine(20,20,getWidth() – 20, get Height() – 20); g.drawRect(200,30,100,80); g.setColor(Color.RED); g.fillRect(30,200,100,80); 5) JPanel  Properties  Code: new Canvas(); 0/0

x

getWidth()

y

fillRect(0,0,3,2) // in Pixeln!

getHeight()

drawRect(0,0,3,2) // In Pixeln!

g.setColor(Color.RED); g.fillRect(0,0,getWidth(), getHeigh() / 3); g.setColor(Color.WHITE); g.fillRect(0,getHeight()/3*2, getWidth(), getHeight() / 3); g.setColor(Color.RED); g.fillRect(0,getHeight() / 3, getWidth(), getHeight() /3);

Canvas

Version 1   Version 2 mit 6 Linien Version 3 mit n Linien

XXII) Ping Pong

ballY

radius ballX

getHeight)

paddleHeight

paddleLength getWidth()

Game Loop:

update

render

new Midlet(PingPong_GUI) extends Canvas implements Runnable Source  Insert Code  Override Method - showNotify() - hideNotify - private void render (Graphics g) - private void update(int keystrokes) - g.drawArc(ballX, ballY, r, r, 0, 360) - g.drawRect(paddleX, paddleY, paddleLength, paddleHeight) - Konstruktor(MIDlet midlet)

XXIII) Zahlensysteme
Example: 0 1 1 100 2 2 3 8 4 0 5 11 6 5

nums

1) Array 1.1) int [] newNums = new int[nums.length] int i, counter = -1; for (i = 0; i < nums.length; i++) { if (nums[i] < 4) { counter++; newNums[counter] = nums; } } geg: 432110 ges: ?5 4321 = 4 * 53 + 3 * 52 + 2 * 51 + 1 * 50 1) 2) 3) 4) 5) 6) 4321 : 5 = 864 864 : 5 = 172 172 : 5 = 34 34 : 5 = 6 6:5 = 1 1:5 = 0 Rest = 1 * 50 Rest = 4 * 51 Rest = 2 * 52 Rest = 4 * 53 Rest = 1 * 55 Rest = 1 * 56

Now you are at the end of the scriptum, but I’m sure there are new ideas for the - next work, - next challenge, - next scriptum! So the end is the beginning of a new start – I’m looking forward tot he next school year.

Yours, Horst Petschenig

Sign up to vote on this title
UsefulNot useful