Você está na página 1de 12

5) primitives and references

Primitives hold fundamental values: int, double, long, float, boolean,

Variables must have a type (e.g. int) and a name (e.g. x).

Instance variables vs. local variables

Dog
- size: int

+ bark()

6) What is JUnit?

JUnit is a simple, open source framework to write and run repeatable tests. JUnit features
assertions for testing excepted results

Public static void assertEquals(double expected, double


actual, double delta)

Asserts that 2 doubles or floats are equals to within a positive delta. (max delta between
expected and actual for which both numbers are still considered equal)

Example:

assertEquals(3.14, 3.17, 0.5); passed

But if the delta would be 0.02, it would fail.

7) Primitive types

Byte 8 bits -128 to 128

Short 16 bits -32768 to 32768

Int 32 bits -2147483647 to 2147483647

Long 64 bits: huge to huge (264)

Int x = 24; wont work!

Wiki page 1 of 12
Brodschneider Vinzenz, Frnschu Michael, Lackner Florian PUC | WU, PO, RI
Byte b = x; You cannot put a large value into a small cup!

Float point:
Float -32 bits double big = 3456789;

Double 64 bits float f = 32.5f; the f, because java thinks that

everything with a floating point is a double,

unless you use f

8) And, Or

And gate &&


A HIGH output (1) results only if all the inputs tot he AND gate are HIGH (1). If neither or
only one input to the AND gate is HIGH, a LOW output results.

Or gate ||
A HIGH output (1) results if one or several of the input to the gate are HIGH (1). If neither
input is HIGH, a low output (0) results).

Product of 5 numbers

Public class Product


{
Private int [] numbers;
Public int computeProduct()
{
Int product = 1;
for( int i = 0; I < 5; i++)
{
Product = numbers [i] * product;
}
Return product;
}
Public int[] getNumbers()
{
Return numbers;
}
Public void setNumbers(int[] numbers)
{
This.numbers = numbers;
}

Wiki page 2 of 12
Brodschneider Vinzenz, Frnschu Michael, Lackner Florian PUC | WU, PO, RI
9) StToInt/IntToString

String to Integer
String Text = 123;

Int x = Integer.parseInt(text);

String text2 = 123.123;

Double z = Double.parseDouble(text2);

Integer to String
Int x = 7;

Stringt text = +x;

Ex:

StringToInt

-Value:String

-output:int

+compute()

String text = 12304;

Char c[0] = text.charAt(0);

Int txl = text.length();

1*104 + 2*103 + 3*102 + 0*101 + 4*100

If(c[0] ==0) x ==1;

104 = 10*10*10*10

For loop
C = counter
c sv, ev
statement Sv = startvalue
statement
statement Ev = end value

Wiki page 3 of 12
Brodschneider Vinzenz, Frnschu Michael, Lackner Florian PUC | WU, PO, RI
Ex:

min nums array of integers

Ges. Struktogramm

10) switch statement

Ex: + CharToInt(int value: char): int

9 ifs value == 1;

Switch(value)
{
Case 1:
Help =1;
Break;

Default : help = 0;

11) Comparing strings

When you start working with strings, you will find that you can use == and != to campe
objects with one another.

Ex:
String help1 = help;
String help2 = help;
If(help1 != help2)
{
Sout(yes); String pool
}
Else sout (not);
The equal method compares 2 strings to see whether they have the same characters in them.
Wiki page 4 of 12
Brodschneider Vinzenz, Frnschu Michael, Lackner Florian PUC | WU, PO, RI
If(help1.equals(help2))
{
Sout(true);
}
Else
{
Sout(false);
}
12) References

Book b = new Book();

Book c = new Book();

If( b == c) { //false }

Book a = c;

If(a ==c) { //true}

If(a == b) { // false}

C = null;

c.setName(??) Null Pointer Exception

ex0026 BintoDec
String input = 1111;
Public inv convert(String input)
{
Int i; sum = 0, value, e, help;
for(i = 0; i< input.length(); i++)
{
help = 1;
char c = input.charAt(i);
value = 0;
if(c == 1) {value = 1;}
for(c = 0; c< input.length()-1-I; c++)
{
Help = help *2;
}
Sum = value * help + sum;
}
Return sum;
}

Ex: sum of all integers


Int [] nums = { 1,2,3,4,5};
Int sum (int [] numbers)
{
Wiki page 5 of 12
Brodschneider Vinzenz, Frnschu Michael, Lackner Florian PUC | WU, PO, RI
Numbers [0] = 0;
Int i , sum;
for( i = 0; i < numbers.length; i++)
{
Sum = sum + numbers[i];
}
Return sum;
}

For (sum)
Int i, sum =0;
for( i =0; I < nums.lenth; i++)
{
Sum = sum + nums[i];
}

While
Int i = 0, sum =0;
While(i<nums.length)
{
Sum = sum + nums[i];
i++;
}

Do-while
int i = 0; sum = 0;
do
{
If(nums.length>0)
{
Sum = sum + nums[i]
i++;
}
}
While(i<nums.length);

2 dim arrays

Int [][] nums = new Int [3] [4];

Nums[1][2] = 1;

Nums[2][0] = 3;

Wiki page 6 of 12
Brodschneider Vinzenz, Frnschu Michael, Lackner Florian PUC | WU, PO, RI
3

Tic Tac Toe

X O
O=1
X X
X = -1
O O O
1) Felder erzeugen
2) Feld mit Nullen fllen
3) -1 bzw 1
4) Gewinner berprfen
5) Ausgabe mit sout() oder Feld

While

Nur eine bestimmte Eingabe erlauben (zwischen 10 und 500);

do

String Text = JOptionPane.showInputDialog();

If(text != null)
-1 0 1
{ -1 -1 0
1 1 1
Int n = Integer.parseInt(text);

While(n>500||n<10)

16) DOS Commands

Cd wechselt Verzeichnis

Dir zeigt Orderinhalt an

Copy files kopiert Dateien

Delete files lscht Dateien

Move files verschiebt Dateien


Wiki page 7 of 12
Brodschneider Vinzenz, Frnschu Michael, Lackner Florian PUC | WU, PO, RI
Mkdir erstellt einen Ordner

Arbeitsbltter Exception

Exception

public void test()


{
FunnyArrays f = new FunnyArrays();
try
{
f.readInput();
}
Catch(Exception ex)
{
Sout(Falsche Eingabe);
Sout(ex.getMessage());
}
}
Exceptionbehandung bei JAVA ME

try
{

}
catch(Exception ex)
{
Alert a = new Alert(Fehler);
a.setType(AlertType.ERROR);
a.setString(ex.getmessage());
display.setCurrent(a, myForm);
}

Ex0031 Calculator

Public class Calculator


{
Public Calculator(int op1, int op2) throws Exception
{
If(op1 < 100 || op2 < 100)
{
Wiki page 8 of 12
Brodschneider Vinzenz, Frnschu Michael, Lackner Florian PUC | WU, PO, RI
Throw new Exception(Invalid Numbers);
}
Operant1 = op1;
Operant2 = op2;
}
Public void div()
{
Calculator calc = new Calculator(3,0);
Calc.div();
}
}

Random Numbers

Math.random(0,1)

0 = start value

1 = endvalue

Ex:
math.randoM(0,100);

18) Collections

Ist ein Array mit undefinierter Lnge

Ex: divisor of a number


n = 5 : 1,5

n = 10: 1,2,5, 10

n = 16: 1,2,4,8,16

java.util.ArrayList.*;

size() length

add(Object elem) adds the object parameter to

the list

get(int Index) returns the object currently

at the index parameter

Wiki page 9 of 12
Brodschneider Vinzenz, Frnschu Michael, Lackner Florian PUC | WU, PO, RI
Ex:
ArrayList list = New ArrayList();
for( i = 1; I < Math.sqrt(n); i++)
{
If((n%i)==0)
{
Int divisor = n/I;
List.addelement(new Integer(i));
List.add(new Integer(divisor));
}
}
For(i = 0; I< list.size(); i++)
{
Integer t = (Integer) list.get(i); Type cast
Sout(t.toSring()); Type cast
}

Collections.sort(); ArrayList sortieren


Int n = (Integer) list.get(0).intValue();
Fail(No Exception excepted); Fehler in JUnit
Ausgeben

@Test(Expected = Exception.class)
Test-class statt try-catch: throws Exception
- Long startime = System.currentTimeMillis(); //Stoppuhr

19) GUI

New->JFrame Form-> Name GUI


X JFrame-> Rechtsklick->Set Layout-> Boarder Layout

North
West Center East
South

Panel rein ziehen


List ins Panel ziehen

Wiki page 10 of 12
Brodschneider Vinzenz, Frnschu Michael, Lackner Florian PUC | WU, PO, RI
Im Panel-> setLayout -> Boarder Layout
Title Boarder auswhlen.> Titel eingeben
Button hinzufgen: Dirakor: South: TitleBoarder:Input
Set Layout-> Gridbag Layout
Label + Textfield nach Input ziehen
Text: wumber text: 12:columns:12
GridBag Layout-> rechtsklick.> raustornise-> Abstand unten 10 setzen
JBottum markieren -> Events -> Action Performed
Private void on Cumpute(java.awt.eventAction()
{DefaultListModel dlm = new DefaultListModel();
dlm.addElement(new Integer(12);
dlm.addElement(new Integer(1);
JList1.setModel

20) Low Level API

Erstellen eines Zeichen-Programms:

1. New JFrameForm -> Name: GUI


2. Borderlayout
3. Panel: plCanvas ins Feld ziehen
4. Java Class im Package erstellen (Name: Canvas)
5. im Canvas:
public class Canvas extends JPanel
{@Override
protected void paintComponent(Graphics g)
{super.paintComponent(g);

Hufigsten Zeichenmethoden

g.setColor(Color.XXXX)
g.drawLine(StartpunktX, StartpunktY, EndpunktX, EndpunktY)
g.drawRect(StartpunktX, StartpunktY, EndpunktX, EndpunktY)
g.fillRect(StartpunktX, StartpunktY, EndpunktX, EndpunktY)

6.) Jpanel markieren => Code => coustumcreation


Code => new canvas();

Wiki page 11 of 12
Brodschneider Vinzenz, Frnschu Michael, Lackner Florian PUC | WU, PO, RI
21) Zahlensysteme

1) Array

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) 4321 : 5 = 864 Rest = 1 * 50
2) 864 : 5 = 172 Rest = 4 * 51
3) 172 : 5 = 34 Rest = 2 * 52
4) 34 : 5 = 6 Rest = 4 * 53
5) 6:5 = 1 Rest = 1 * 55
6) 1:5 = 0 Rest = 1 * 56

Wiki page 12 of 12
Brodschneider Vinzenz, Frnschu Michael, Lackner Florian PUC | WU, PO, RI

Você também pode gostar