Table of Contents generated with DocToc
- LaunchcodeBeginnerExercises
- About this Repository
- Data Types (org.launchcode.java.demos.lsn1datatypes & org.launchcode.java.studios.areaofacircle)
- Control Flow & Collections (org.launchcode.java.demos.lsn2controlflowandcollections)
- Classes
- Modifiers
- Objects
- Unit testing
- Inheritance
- Interfacess & Polymorphism
- Requirements
- TempConverter.java
- Primitive vs Reference variables
- Static methods
- Updating
This repo contains code that demonstrates the owners knowledge of Java concepts like
In ArrayListGradebook.java notice that we declared grades to be of type ArrayList, using the wrapper class Double rather than the primitive type double. All values stored in Java collections must be objects, so we’ll have to use object types in those situations.
In lines 10 and 11, we also initialize each list by creating a new, empty list. Note that when we call the ArrayList constructor, as in new ArrayList<>(), we don’t need to specify type (it’s implicit in the left-hand side of the assignment).
Note You will sometimes see the ArrayList class written as ArrayList, where E represents a placeholder for the type that a programmer will declare a given list to hold. This is especially true in documentation. You can think of E as representing an arbitrary type. Classes like ArrayList that take another type or class as a parameter are referred to as generic classes or generic types.
Arrays are of fixed size. You cannot expand or contract an Array after it is created, so you must know exactly how many elements it will need to hold when you create it. This fact is reason enough to use ArrayLists in most scenarios.
In HashMapGradeBook looping through a map is slightly more complex than it is for ordered lists. Let’s look at the for-each loop from this example:
for (Map.Entry<String, Double> student : students.entrySet()) {
System.out.println(student.getKey() + " (" + student.getValue() + ")");
sum += student.getValue();
}
The iterator variable, student, is of type Map.Entry<String, Double>. The class Map.Entry is specifically constructed to be used in this fashion, to represent key/value pairs within HashMaps. Each Map.Entry object has a getKey method and a getValue method, which represent (surprisingly enough!), the key and value of the map item.
If you only need to access the key of each item in a map, you can construct a simpler loop:
for (String student : students.keySet()) {
System.out.println(student);
}
A similar structure applies if you only need the values, using students.values():
for (double grade : students.values()) {
System.out.println(grade);
}
You are allowed to create local variables (variables declared within a method) with the same name as a field of the given class. In this case, in order to refer to the field, we must use this.
Example Let’s look at how this works with our HelloWorld class:
public class HelloWorld {
public String message = "Hello World";
public void sayHello() {
String message = "Goodbye World";
// The line below prints "Goodbye World"
System.out.println(message);
// The line below prints "Hello World"
System.out.println(this.message);
}
}
| Modifier | Class | Package | World |
|---|---|---|---|
| public | ✅ | ✅ | ✅ |
| protected | ✅ | ✅ | ❌ |
| (no modifier) (default) | ✅ | ✅ | ❌ |
| private | ✅ | ❌ | ❌ |
This repo contains starter code for the in-book examples for LaunchCode's Java Web Development course.
The classes in this repo require Java11. Please refer to the book referenced above for instructions on how to download and use the examples.
To update this Project's SDK to use Java11 on your computer:
-
Under Project Settings, select Project
-
Under Project SDK, select your downloaded Java 11 JDK.
- Note: You may also download the JDK directly from your menu options here.
-
Next, under Platform Settings, select SDKs and again select Java11.

-
Once selected, hit Apply and Ok
Arrays in Java may NOT change size once created. This is limiting and not very practical. Thankfully, Java provides more flexible ways to store data, which we will explore in a later lesson. These objects will allow us to rearrange, add to, or remove data.
Aside from using arrays to build some simple loop examples, we will only use them in special cases.
int firstCatAge = 11; int secondCatAge = firstCatAge;
Since int is a primitive type, the variables firstCatAge and secondCatAge function like separate boxes, each one holding the integer value 11.
A reference variable (such as myCat above) does not actually store the object in question. Instead, it stores a reference to the object, which is literally a memory address. We visualize a reference as an arrow pointing to the location of the object in memory.
A static method is one that can be called without creating an instance of the class to which it belongs.
Define the class Cat and include the static keyword before the makeNoise method name:
public class Cat {
public static void makeNoise(String[] args) {
// some code
}
}
Since makeNoise is static, we do NOT need to create a Cat object to access it. Instead of doing this:
Cat myCat = new Cat(); // Create a new Cat object.
myCat.makeNoise("purr"); // Call the makeNoise method.
We can call the method directly:
Cat.makeNoise("roar");
Occasionally, the LaunchCode team will make changes to this repository that will affect your coursework. When you start your prep-work for each lesson of the course, be sure to fetch to stay up to date with the latest changes.

