Unit 7: Practice
Table of Contents
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
}
}
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.
- 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. - Remove the Readme file.
- Create a file called
hello.txt
. - Add the following line to
hello.txt
(without quotes): “world” - Print the contents of
hello.txt
. - Change directories into
src/com/omegarobotics
. - List all of the files in this directory.
- Go back up one directory.
- Print the directory you’re in.
- Create a new directory called
practice
. - Change into the
practice
directory. - Create a file called
name.txt
. - Add the following line to
name.txt
(without quotes): “Jane Doe” - Go back up one directory.
- Print the directory you’re in.
- Remove the
com
directory.
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.
- 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. - Create a new file called
quote.txt
. - Add the following to
quote.txt
(without quotes): “Smart people are stupid.” - Display the status of the repository.
- Add the
quote.txt
file to the staging area. - Commit the changes with the message “Add quote.txt”.
- Change directories into
src/com/omegarobotics
. - Append your favorite song to
songs.txt
. - Append your favorite book to
books.txt
. - Append your favorite TV show to
tvshows.txt
. - Display the status of the repository.
- Add all of the modified files to the staging area.
- Commit the changes with the message “Add my favorite song, book, and TV show”.
- Display the log of commits.
- Create a new branch called
feature
and check it out. - Add another song you like to
songs.txt
and commit that change. - Check out the
master
branch. - Remove
songs.txt
and commit that change. - Merge
feature
intomaster
. Resolve the conflict by completing the deletion ofsongs.txt
. - Delete the
feature
branch.
GitHub
git
github
command line
forking
pull requests
Go to the Learn Code Unit 7 repository on GitHub.
- Fork the repository to your account.
- 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):
- Make a new directory called
practice
in the root directory and change into it. - Create a new file called
hello.txt
and add the following: “Hello world, I’m a programmer”. - Add and commit the changes with the message
"Add hello.txt"
. - Push the changes to your remote
master
branch. - Checkout the
feature
branch. - Add the line “Java is an object-oriented programming language” to
hello.txt
. - Add and commit the changes with the message
"Add Java description"
. - Checkout the
master
branch. - 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
- Add and commit the changes with the message
"Add poem lines"
. - Merge
feature
intomaster
. Resolve the conflict so thathello.txt
looks like this:Hello world, I'm a programmer This is the way the world ends Not with a bang, but a whimper
- Push the changes to your remote
master
branch.
Go to the official Learn Code Unit 7 repository.
- Create a new pull request by comparing
omega9656:master
withyour_username:master
(whereyour_username
is replaced by your GitHub username). - Be sure to include a concise title and description of what changed.
- Label your pull request with the green
practice
label.
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 toint
) - 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
}
}