Unit 7: Practice

Table of Contents

  1. A note on all problems
  2. Practice
    1. Input Mismatch
    2. Command Line
    3. Git
    4. GitHub
  3. Challenges
    1. Retrieve Element

A note on all problems

Problems tagged with the red Unit 7 Repository label utilize the Learn Code Unit 7 repository. Before starting the practice exercises, you need to download or clone a fresh version of this repository.

Also, rather than having “solution code”, the solution text file will outline the commands you should have done. (There may be slight variation in things like commit hashes, but everything else should be exactly the same.)

If you want to restart the problem for any reason, simply delete the repo directory and then download/clone it again.

Practice

Input Mismatch

exception handling

Scanner

while loop

Create a class called Blah which prompts the user to enter an integer continuously. If an InputMismatchException occurs, print an error message and keep asking them for an int. If an int has been successfully entered, stop asking the user for input and print the int.

Note that you will need to import java.util.InputMismatchException.

public class InputMismatch {
    public static void main(String[] args) {
        // write your code here
    }
}

Solution

Command Line

command line

bash

Unit 7 repository

Note: We are assuming you have not modified the Learn Code Unit 7 repository after downloading/cloning it.

Everything done in this exercise is via the terminal.

  1. Change directories into the learn-code-unit-7 repo you downloaded/cloned. From now on, we will refer to this as the root directory, or simply root.
  2. Remove the Readme file.
  3. Create a file called hello.txt.
  4. Add the following line to hello.txt (without quotes): “world”
  5. Print the contents of hello.txt.
  6. Change directories into src/com/omegarobotics.
  7. List all of the files in this directory.
  8. Go back up one directory.
  9. Print the directory you’re in.
  10. Create a new directory called practice.
  11. Change into the practice directory.
  12. Create a file called name.txt.
  13. Add the following line to name.txt (without quotes): “Jane Doe”
  14. Go back up one directory.
  15. Print the directory you’re in.
  16. Remove the com directory.

Solution

Git

git

command line

Unit 7 repository

Note: We are assuming you have not modified the Learn Code Unit 7 repository after downloading/cloning it.

Everything done in this exercise is via the terminal.

  1. Change directories into the learn-code-unit-7 repo you downloaded/cloned. From now on, we will refer to this as the root directory, or simply root.
  2. Create a new file called quote.txt.
  3. Add the following to quote.txt (without quotes): “Smart people are stupid.”
  4. Display the status of the repository.
  5. Add the quote.txt file to the staging area.
  6. Commit the changes with the message “Add quote.txt”.
  7. Change directories into src/com/omegarobotics.
  8. Append your favorite song to songs.txt.
  9. Append your favorite book to books.txt.
  10. Append your favorite TV show to tvshows.txt.
  11. Display the status of the repository.
  12. Add all of the modified files to the staging area.
  13. Commit the changes with the message “Add my favorite song, book, and TV show”.
  14. Display the log of commits.
  15. Create a new branch called feature and check it out.
  16. Add another song you like to songs.txt and commit that change.
  17. Check out the master branch.
  18. Remove songs.txt and commit that change.
  19. Merge feature into master. Resolve the conflict by completing the deletion of songs.txt.
  20. Delete the feature branch.

Solution

GitHub

git

github

command line

forking

pull requests

Go to the Learn Code Unit 7 repository on GitHub.

  1. Fork the repository to your account.
  2. Clone your forked repository to local. (Use the terminal.)

Open an IDE or text editor application and do the following (from now on you can choose to complete the instructions using the application’s GUI or using the command line):

  1. Make a new directory called practice in the root directory and change into it.
  2. Create a new file called hello.txt and add the following: “Hello world, I’m a programmer”.
  3. Add and commit the changes with the message "Add hello.txt".
  4. Push the changes to your remote master branch.
  5. Checkout the feature branch.
  6. Add the line “Java is an object-oriented programming language” to hello.txt.
  7. Add and commit the changes with the message "Add Java description".
  8. Checkout the master branch.
  9. Replace the line “Hello world, I’m a programmer” in hello.txt with 2 lines:
    This is the way the world ends
    Not with a bang, but a whimper
    
  10. Add and commit the changes with the message "Add poem lines".
  11. Merge feature into master. Resolve the conflict so that hello.txt looks like this:
    Hello world, I'm a programmer
    This is the way the world ends
    Not with a bang, but a whimper
    
  12. Push the changes to your remote master branch.

Go to the official Learn Code Unit 7 repository.

  1. Create a new pull request by comparing omega9656:master with your_username:master (where your_username is replaced by your GitHub username).
  2. Be sure to include a concise title and description of what changed.
  3. Label your pull request with the green practice label.

Solution

Challenges

Retrieve Element

exception handling

arrays

Scanner

Adapted from Exercise 12.3 from Introduction to Java Programming (Comprehensive), 10th ed. by Y. Daniel Liang

Create a class called RetrieveElement which meets the following requirements:

  • Creates an array with 100 randomly chosen integers (use Math.random() * 100 and cast the result to int)
  • Continuously prompts the user to enter the index of the array, then displays the corresponding element value. If the specified index is out of bounds, display the message "Out of Bounds".
  • The program should end when the user enters "quit" (case insensitive*)
  • If the user enters anything other than an index or "quit", display the message "Invalid index"

*To check if two strings are equal without taking into account case, use the equalsIgnoreCase method.

Note that we have not told you what exception classes you need to catch. Use your brain - how can you figure out which classes to use?

Hint: When using Scanner, you will need to use the next method to give the user the flexibility to enter any type. The return type of this method is String, so you will need to use the Integer.parseInt method to convert the String into an int.

public class RetrieveElement {
    public static void main(String[] args) {
        // write your code here
    }
}

Solution