Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 

Repository files navigation

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:

image
  1. Create a new repository from this template by clicking on the green Use this template button and selecting Create a new respository.

    image image
  2. On the Create a new repository page, select your GitHub username for the Owner and add in a name for your repository.

    image
  3. If you want to include the solution files as well, you will want to select Include all branches.

    image
  4. Then click the green Create repository button to create a new repository using the template repository.

  5. Once the new respository is create, copy the URL to use in Visual Studio Code.

    image
  6. In Visual Studio Code, click on the Source Control tab, then click Clone Repository.

    image
  7. 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.

    image
  8. Under Run Configurations add --enable-preview --source 21 to VM Options.

  9. Click Run main above the main method to test the code. You should see Let's simulate a vet office.


Modification #1

  1. Add a Dog record with meaningful names for the Dog's name, breed, and weight.
  2. Add a Dog object in the main method and print the value to the screen.
  3. The solution to this modification can be found in 2-SimpleDogRecord files.

Modification #2

  1. Add a LicenseName record that consists of an show name and nickname for a dog.
  2. Modify the Dog record to include a LicenseName, breed, and weight.
  3. Update your Dog object to include a new LicenseName with the show name and nickname of your dog.
  4. The solution to this modification can be found in 3-LicenseRecord files.

Modification #3

  1. Add a Cat record that consists of the cat's name, breed, and weight.
  2. Add a Cat object in the main method and print the value to the screen.
  3. Now we want to add our animals to an ArrayList. In order to do this, we need an Animal interface. You can create a sealed interface that identifies what classes are allowed to extend it, by using sealed interface Animal permits Dog, Cat {}
  4. Modify both the Dog and Cat records to implement the Animal interface by adding implements Animal after the parameter list.
  5. Create an ArrayList of Animal and add your Dog and Cat objects to the list.
  6. Print out your list.
  7. The solution to this modification can be found in 4-AnimalInterface files.

Modification #4

  1. For each Animal in the list print out their name. If the animal is a Dog print the dog's show name. If the animal is a Cat print the cat's name. Your code may look similar to this:

    image
  2. Update your code to use pattern matching with instanceof combine the test, declaration, and typecasting into one statement. Here is the code modification to update the check for the Dog objects:

    image

    Update your code to check for Cat using pattern matching.

  3. The solution to this modification can be found in 5-PatternMatchingIfStatement files.


Modification #5

  1. Consider a list with many different types of animals beyond just Dog and Cat. We could continue our if branching, but a better way would be to use a switch statement instead. Because we created Animal as a sealed interface we do not need to include a default branch to our switch statement. Your code might look similar to the following:

    image
  2. The solution to this modification can be found in 6-PatternMatchingSwitchStatement files.


Modification #6

  1. We can use unnamed variables and pattern matching and to store the name instance variables for both the Dog and Cat objects. You can do this by changing the case statement to case Dog(LicenseName name, _, _): Since we aren't using the values of breed and weight we do not need to store them.
  2. 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

  1. Update the switch case to use lambda notation. The solution to this extension can be found in 8-PatternMatchingSwitchStatementUnnamedVarLambda files.
  2. Update the Animal interface to permit additional animals of your choice.
  3. Add a record for your new animal.
  4. Create additional objects of your new Animal type record and add them to your ArrayList
  5. Update your switch statement to deal with your new Animal object, 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");

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages