Lab 3: Methods
Objectives
The main goals for this lab are:
-
To learn some more vim commands and use them while programming.
-
To get comfortable with writing and using methods.
1. Vim Survival Guide
For this part of the lab, we will go over some of my favorite and most used vim commands that I think will make programming much smoother for you. There are many vim commands, so this is just my selection.
Remember, to develop muscle memory for these commands, you just need practice. My suggestion is to pick one or two commands at a time to introduce into your workflow so you don’t get overwhelmed.
Vim has different modes. The two you will use most are:
-
Normal mode - for moving around and editing text
-
Insert mode - for actually typing text
Press esc at any time to return to normal mode.
Navigate to your lab1 / lab01 (whatever you called it) folder:
cd /home/your-username/cs113/labs/lab01
To try out these commands, open one of your java files from lab1,
for example: vim Sum1.java. Using these commands will alter your file,
this is totally fine, do not worry about preserving the old code.
1.1 Entering & Exiting Insert Mode
-
i- Start typing before the cursor -
o- Create a new line below the current line and start typing -
esc- Exit insert mode (return to normal mode)
1.2 Moving Around
You must be in normal mode to move around. Hit esc if you
are in insert mode.
You can move around with your arrow keys, but here are some other useful commands that are typically faster to use than the arrow keys.
-
0- Go to beginning of line -
shift + a- Go to the end of the line and enter insert mode -
G- Go to the bottom of the file -
gg- Go to the top of the file
1.3 Deleting Text
Make sure you are in normal mode first.
-
x- Delete the character under the cursor -
dd- Delete the entire current line -
D- Delete from the cursor to the end of the line
1.4 Undo and Redo
Made a mistake? You can undo and redo easily in vim.
-
u- Undo -
ctrl + r- Redo
Try deleting a line with dd, and then restoring it with u.
1.5 Copying and Pasting
In vim, copying is called yanking.
To copy and paste:
-
Make sure you are in normal mode (hit
esc) -
Hit
shift + y. Your current line should now be highlighted -
Use your up and down arrow keys to highlight the block of code you want to copy
-
Once you have highlighted all the code you want to copy, hit
y -
Use your arrow keys to navigate to the line where you want to paste the code
-
Press
pto paste the code there
1.6 Searching
To search for the next occurence of text in your file, use /.
For example, to search for the word "cat" in your file type:
/cat and then press enter.
-
n- go to next match -
N- go to previous match
1.7 Saving and Quitting
Press esc to enter normal mode.
-
:w- Save -
:wq- Save and quit -
:q!- Quit without saving
Saving regularly is good practice:
-
Hit
esc -
Type
:wq
2. Method Madness
Last week we asked a user for numbers and computed the sum of the numbers. This week we will compute the average of a set of numbers. Instead of implementing our entire algorithm in the main method, we will break out program down into multiple methods.
Try using a couple of the new vim commands you learned as you work on this section.
2.1 Prompt user for number
-
Navigate to your
/labsdirectory:cd .. -
Create a new lab3 directory:
mkdir lab3 -
Change to the lab3 directory:
cd lab3
Now we are ready to start. Create a new java file: vim LabThree.java.
Write a method called getUserNumber(). It should return a double. Below
is starter code you can use.
class LabThree {
public static void main() {
}
public static double getUserNumber() {
}
}
Think about how you can break this method into steps: asking the user for input, and getting that input and saving it, converting the user input to a double, and then returning that double.
TODO: Show Prof. Shiptoski your work before continuing.
2.2 Sum two numbers
Now write a method called sumTwo which takes in two doubles, and returns
their sum.
class LabThree {
public static void main() {
}
public static double getUserNumber() {
}
public static double sumTwo(double firstNum, double secondNum) {
}
}
TODO: Show Prof. Shiptoski your work before continuing.
2.3 Sum three numbers
Now write a method called sumThree() which takes in three doubles and returns
their sum.
Hint: You should called your sumTwo function from your sumThree function. There are
a few different ways to do this.
class LabThree {
public static void main() {
}
public static double getUserNumber() {
}
public static double sumTwo(double firstNum, double secondNum) {
}
public static double sumThree(double firstNum, double secondNum, double thirdNum) {
}
}
TODO: Show Prof. Shiptoski your work before continuing.
2.4 Putting it all together
Now inside your main method, use your new methods to:
-
Ask the user for three numbers.
-
Sum the three numbers.
-
Print out the sum.
$ javac Sum.java
$ java Sum
Please give me a number:
10.2
Please give me a number:
8.6
Please give me a number:
2.1
The sum of your numbers is: 20.9
TODO: Show Prof. Shiptoski your work and then you are all done with lab 3.