Getting Set-Up
Before we start, Visual Studio Code will often try to be helpful and pop up additional extensions to load. Some extensions interfere with others, so use caution when installing more extensions. Here is an example of a suggested extension that I ignored:
-
Create a new repository from this template by clicking on the green Use this template button and selecting Create a new respository.
-
On the Create a new repository page, select your GitHub username for the Owner and add in a name for your repository.
-
If you want to include the solution files as well, you will want to select Include all branches.
-
Then click the green Create repository button to create a new repository using the template repository.
-
Once the new respository is create, copy the URL to use in Visual Studio Code.
-
In Visual Studio Code, click on the Source Control tab, then click Clone Repository.
-
Paste the respository URL into the Visual Studio search window. You'll be prompted to choose the location to store the files and whether or not you trust the authors of the folder.
-
Under Run Configurations add
--enable-preview --source 21to VM Options. -
Click
Run mainabove the main method to test the code. You should seeLet's simulate a vet office.
Modification #1
- Add a
Dogrecord with meaningful names for the Dog's name, breed, and weight. - Add a
Dogobject in the main method and print the value to the screen. - The solution to this modification can be found in 2-SimpleDogRecord files.
Modification #2
- Add a
LicenseNamerecord that consists of an show name and nickname for a dog. - Modify the
Dogrecord to include aLicenseName, breed, and weight. - Update your
Dogobject to include anew LicenseNamewith the show name and nickname of your dog. - The solution to this modification can be found in 3-LicenseRecord files.
Modification #3
- Add a
Catrecord that consists of the cat's name, breed, and weight. - Add a
Catobject in themainmethod and print the value to the screen. - Now we want to add our animals to an
ArrayList. In order to do this, we need anAnimalinterface. You can create asealedinterface that identifies what classes are allowed to extend it, by usingsealed interface Animal permits Dog, Cat {} - Modify both the
DogandCatrecords to implement theAnimalinterface by addingimplements Animalafter the parameter list. - Create an
ArrayListofAnimaland add yourDogandCatobjects to the list. - Print out your list.
- The solution to this modification can be found in 4-AnimalInterface files.
Modification #4
-
For each
Animalin the list print out their name. If the animal is aDogprint the dog's show name. If the animal is aCatprint the cat's name. Your code may look similar to this:
-
Update your code to use pattern matching with
instanceofcombine the test, declaration, and typecasting into one statement. Here is the code modification to update the check for theDogobjects:
Update your code to check for
Catusing pattern matching. -
The solution to this modification can be found in 5-PatternMatchingIfStatement files.
Modification #5
-
Consider a list with many different types of animals beyond just
DogandCat. We could continue ourifbranching, but a better way would be to use aswitchstatement instead. Because we createdAnimalas asealed interfacewe do not need to include adefaultbranch to ourswitchstatement. Your code might look similar to the following:
-
The solution to this modification can be found in 6-PatternMatchingSwitchStatement files.
Modification #6
- We can use unnamed variables and pattern matching and to store the name instance variables for both the
DogandCatobjects. You can do this by changing the case statement tocase Dog(LicenseName name, _, _):Since we aren't using the values ofbreedandweightwe do not need to store them. - The solution to this modification can be found in 7-PatternMatchingSwitchStatementUnnamedVar files.
To see how all of this would have been written differently using Java 8 features, please go here: https://github.com/clfurman/PatternMatchingJava8
Extension
- Update the switch case to use lambda notation. The solution to this extension can be found in 8-PatternMatchingSwitchStatementUnnamedVarLambda files.
- Update the
Animalinterface to permit additional animals of your choice. - Add a record for your new animal.
- Create additional objects of your new
Animaltype record and add them to yourArrayList - Update your switch statement to deal with your new
Animalobject, making the switch statement exhaustive.
If you are struggling with VS code, you can add all of your code to the Java Playground here:
System.out.println("Let\'s simulate a vet office."); // Thread.sleep(1); // throw new Exception("test");