Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
Software Construction - Lab 2
From loops and arrays to OOP, references, and the Collections Framework.
for, while) and iteration patternsstatic and final keywordsList, Set, MapfirstName, not a1)Ctrl+Shift+F to auto-formatelse is enough once prior conditions rule out the rest// while loop
int i = 0;
while (i < 3) {
System.out.println("Count: " + i);
i = i + 1; // must change i or it loops forever!
}
// for loop
for (int i = 0; i < 3; i++) {
System.out.println("Count: " + i);
}break exits the loop immediatelycontinue skips to the next iteration0 is the first element; values[i] is a value, i is just a number - don't confuse index with valueint[] numbers = {12, 24, -23, 47, 100};
int size = 10;
double[] data = new double[size];Worked Example
public static int getMinIndex(int[] values) {
int minValue = Integer.MAX_VALUE;
int minIndex = -1;
for (int i = 0; i < values.length; i++) {
if (values[i] < minValue) {
minValue = values[i];
minIndex = i;
}
}
return minIndex;
}Ctrl+Shift+F in Eclipse) - messy code hides bugspublic class Student {
String name;
int age;
double gpa;
Student(String studentName, int studentAge) {
name = studentName;
age = studentAge;
gpa = 0.0;
}
void displayInfo() {
System.out.println(name + " (" + age + "): GPA " + gpa);
}
}Student ahmed = new Student("Ahmed Ali", 20);
Student fatima = new Student("Fatima Hassan", 19);
System.out.println(ahmed.name); // "Ahmed Ali"
ahmed.displayInfo();new allocates the object; fields are accessed with .; methods are called with .y = x gives y its own copy.s2 = s1 makes s2 point to the same object.Student s1 = new Student("Ahmed", 20, "CS");
Student s2 = s1; // s2 points to SAME object
s1.gpa = 3.8;
System.out.println(s2.gpa); // 3.8 (same object!)
Student s3 = new Student("Ahmed", 20, "CS");
System.out.println(s1 == s3); // false , different objectsstatic Keywordpublic class Student {
static int totalStudents = 0; // SHARED by all instances
String name; // UNIQUE to each instance
Student(String studentName) {
name = studentName;
totalStudents++;
}
}MathHelper.add(5, 3))main is static because Java must call it before any objects existfinalx = 10; changes what the variable points toahmed.gpa = 3.8; changes the object's internal state; ahmed still points to the same objectfinal int[] scores = {85, 90, 78};
scores[0] = 95; // OK: mutating contents
// scores = new int[]{1,2,3}; // ERROR: reassignment not allowedfinal locks a reference, not the object's contentsList<String> courses = new ArrayList<>();
courses.add("Math");
courses.add("Physics");
Set<Integer> ids = new HashSet<>();
ids.add(101);
ids.add(101); // duplicate , ignored
System.out.println(ids.size()); // 2, not 3
Map<String, Double> grades = new HashMap<>();
grades.put("Ahmed", 3.8);
System.out.println(grades.get("Ahmed")); // 3.8for/while) with break/continue drive iteration; arrays hold fixed-length, same-type sequences== compares references, not contentsstatic members belong to the class and are shared across all instances; final locks a reference, not the value it points toList for ordered/duplicate data, Set for uniqueness, Map for key-to-value lookupsMahmoud Abas|Software Construction - Lab 2: Java Collections, Arrays, Loops & Classes