The main goals for this lab are for you to get more comfortable with methods. We will also be covering String methods.
This lab is a paired programming assignment. What exactly does that mean? You will be working in pairs on the CS lab computers. Each pair will be working on one computer. One person will be the driver and the other person will be the navigator. Here is the rule: the driver controlls the lab computer, but the driver can only type what the navigator tells them to type. For this to work well, each pair should be constantly talking among themselves. After each problem, you will switch roles, the navigator will become the driver and the driver will become the navigator.
Before leaving the lab, make sure you fill out the attendance sheet and go through your answers with a TA or instructor.
For each of the follow programs, draw a stack diagram to trace out what happens as the program executes. We removed the class
just for simplicity
public static int add(int a, int b) {
int result = a + b;
return result;
}
public static void main(String[] args) {
int a = 4;
int b = 8;
int c = add(b, a);
System.out.printf("%d + %d = %d\n", a, b, c);
}
public static int add(int a, int b) {
a = 2;
int result = a + b;
return result;
}
public static void main(String[] args) {
int a = 4;
int b = 8;
int c = add(a, b);
System.out.printf("%d + %d = %d\n", a, b, c);
}
Checkpoint: Before continuing, have your TA review your stack diagrams with you.
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.
In 2.1 - 2.5, you will be creating simple methods based on the documentation we provide below.
Make sure to test each method individually in your main method before moving onto the next method.
Write a method called getUserNumber()
that follows the specification.
/**
Asks user for a number
@return the number that a user put in
*/
public static double getUserNumber()
In this method, create a new Scanner
object
and then use it to read in data from a user.
/**
sums two numbers
@param a the first number
@param b the second number
@return the sum of two numbers
*/
public static double sum(double a, double b)
Before implementing this function, answer the question below.
/**
Sums three numbers
@param a the first number
@param b the second number
@param b the third number
@return the sum of the three numbers
*/
public static double sum(double a, double b, double c)
Question: What is different between the methods in 2.2 and 2.3?
When implementing this new function
sum(double a, double b, double c)
, you should call thesum(double a, double b)
method you created in 2.2
/**
Computes an average based on
a sum and total number of items
@param total the sum
@param numItem the number of items that made the sum
@return the average value
*/
public static double avg(double total, int NumItem)
Now in your main method, use these methods to ask a user for 5 numbers and print out the average.
*
represents user input.
$ javac Average.java
$ java Average
Please give me a number: *2.5*
Please give me a number: *3.6*
Please give me a number: *9.2*
Please give me a number: *2.2*
Please give me a number: *0*
The average of your numbers is 3.5
Now we will look at a few String
methods.
If we have a String
, we can determine the following information:
Look at section 7.4 and 7.5 of the textbook for some more String methods. You will probably want to use the method described in 7.4
Note: there is another data type in java called a
character
. This is just one single character and we denote it by putting the character between single quotes, e.g.'a'
."a"
would be aString
, not acharacter
.
Write a program called WordInfo.java
where you will ask a user for a word and then a character.
The program will then tell the user:
$ javac WordInfo.java
$ java WordInfo
Please give me a word: *character*
Please give me a character: *a*
character has 9 letters, a is the #3 letter in the character
In todays lab we covered methods and you had more practice working
with the Scanner
class.
Before leaving, make sure your TA/instructor have signed you out of the lab. If you finish the lab early, you are free to go.