for-eachthis nur bei Überschattung notwendigpublic class Circle {
public void setRadius(double radius) {
// Method body
}
}
public class Circle2 {
public void setRadius2(double radius) {
// Method body
}
}
Circle2 bildet neuen „Namensraum“, die Methode kann und sollte also wieder setRadius heißenpublic Circle(double radius) {
this.radius = radius;
diameter = 2 * radius;
circumference = diameter * Math.PI;
this.area = Math.pow(radius, 2) * Math.PI;}
public void setRadius(double radius) {
this.radius = radius;
diameter = 2 * radius;
circumference = diameter * Math.PI;
this.area = Math.pow(radius, 2) * Math.PI;}
public void setDiameter(double diameter) {
this.diameter = diameter;
radius = diameter / 2;
circumference = diameter * Math.PI;
this.area = Math.pow(radius, 2) * Math.PI;}
public class Circle {
public Circle(double radius) {
setRadius(radius);
}
public void setRadius(double radius) {
this.radius = radius;
diameter = 2 * radius;
circumference = diameter * Math.PI;
this.area = Math.pow(radius, 2) * Math.PI;
}
public void setDiameter(double diameter) {
setRadius(diamter / 2);
}
}
Ein Array, auch Feld genannt, ist ein spezieller Datentyp, der mehrere Werte des gleichen Typs zu einer Einheit zusammenfasst.
// declaration, preferred
int[] numbers;
// initialize empty array
numbers = new int[42];
// all in one
String[] animals = new String[3];
String[] animals = {"Hund", "Katze", "Maus"};
int[] lostNumbers = {4, 8, 15, 16, 23, 42};
lostNumbers[3] = 27; // write in array
for (int i = 0; i < lostNumbers.length; ++i) {
System.out.println(lostNumbers[i]);
}
int-Index angesprochenlength beinhaltet Anzahl der Einträge
final, also schreibgeschütztlength - 1m.socrative.com
Room number:prog12
// Example 1:
int lostNumbers[];
// Example 2:
int[] primes;
primes = {2, 3, 5, 7, 11, 13};
// Example 3:
lostNumbers = new int[] {4, 8, 15, 16, 23, 42};
Implementiert die Methoden entsprechend der Kommentare
Aufgabe / Lösung
Eure Ergebnisse
prog12.raphinesse.de/collab
for-each// classic for
int[] lostNumbers = {4, 8, 15, 16, 23, 42};
for (int i = 0; i < lostNumbers.length; ++i) {
System.out.println(lostNumbers[i]);
}
// syntax
for (Laufvariable : Array) {
Rumpf
}
// for-each
int[] lostNumbers = {4, 8, 15, 16, 23, 42};
for (int x : lostNumbers) {
System.out.println(x);
}
for-each läuft immer das ganze Array ab// declaration
short[][] monoChromePixels;
// initialize empty array
monoChromePixels = new short[640][480];
// access
monoChromePixels[42][300] = 255;
// length
int width = monoChromePixels.length;
int height = monoChromePixels[0].length;
int[][] months = new int[12][];
months[0] = new int[31];
months[1] = new int[29];
months[2] = new int[31];
months[3] = new int[30];
// and so on...
