Exercises that will help you study for the Midterm:
Exercise A. Strings:
Consider String temp = "remainder" and String another = "main"
temp.length() = ___
temp.indexOf("main") = ____
temp.indexOf(another) = _________
temp.substring(3,7) = ___________
temp.equals(another) = _________
Exercise B. Mod operator : %
Consider int i = 11324
How to determine if i is even: _________________________________
How to determine if i is odd: _________________________________
How to determine if divisible by 10: ______________________________
How to get the last digit of i: ________________________________________
Print 1 then 0 repeatedly inside a loop that looks like this:
(for int x = 0; x < 10, x++)
{ System.out.println (_________);}
Is it x/2; (x%2)+1; or (x+1)%2?
Exercise C. Calling a method and passing in a variable:
In your main method:
method(3,5)Another method:
public int method(int x, int y){
Return (y - x);}Remember the 3 goes into x and the 5 goes into y.
Is 2 or -2 returned? _____
Exercise #1: Method calls
How would you call methods with these headers:
Exercise #2: Method header creation
What is a possible method header for these method calls
Exercise #3: method returns and variables
What is a possible return statement for each of these methods?:
Exercise #4:
What does this print:
public class Tracer
{
public static void main(){
System.out.println("NEW ONE");
double b= 0;
double holder = 0;
for (int count = 1; count <=3; count++)
{
b = b + 10;
double addAnswer = addition(b, b);
holder = holder + addAnswer;
}
System.out.println("the total of all answers together is " + holder);
}
public static double subtraction(double num1, double num2)
{
double difference = num2 - num1;
return difference;
}
public static double addition(double x, double y)
{
double addition= x + y;
return addition;
}
}
Exercise #4B:
What does this print:
public class Tracer2
{
public static void main(){
System.out.println("NEW ONE");
double b= 0;
for (int count = 1; count <=3; count++)
{
if (count == 2){
b = b+ 10;
}if (b> 2 && count < 3){
b = b-1;
}
else {
b=b+2;
}
b=b+1;
}
System.out.println("the mystery number is " +b);
}}
Exercise #4c: Boolean Logic:
Evaluate true or false:
int a = 5; int b = 6; int c = 10;
Exercise #5: Swap A and B
Create a method that takes in two integers, varA and varB. Swap their values. Print varA's value and varB's value.
Exercise #6
Consider this loop:
   for (int z =10; z<30;z++)
   {
      System.out.println("The number is " + z);
    }
a. How many times will this run?
b. What would happen if the test were z > 30?
c. How would you change this to count by 2's?
d. How would you change this to count by 5's?
e. How would you add up all these numbers and just print the total?
f. How would you print the running total every time you counted?
g. How would you get the average of all the numbers printed?
h. How would you print as many stars as the number inside the loop? (So when
z was 10, print 10 stars and when z was 11, print 11 stars, making a figure
that grew as it went down.)
Exercise #7
Write a program to ask the user their name and then ask for a low number and then a high number. It should print the name in uppercase and then the numbers from low to high separated by a plus sign, and then print the total of the numbers and the average of the numbers. (Creating and using one method in your program is required.) (Extra: Also print the total of all the sums
A samples session would be:
What is your first name? Kris
What is the low number? 5
What is the high number? 10
KRIS, 5+6+7+8+9+10 = 45 and the average is 7.5
What is the low number? -10
What is the high number? -5
KRIS, -10+-9+-8+-7+-6+-5=-45 and the average is -7.5
. . . (repeat 3 more times)
---------------------------------
Questions to ask yourself:
What building blocks do I need? Make a block of anything complex. Make a block
of anything repeated.
What will each block print? What will each block return? What does each block
need to take in to do its job?
In building each block, and the main program, ask:
What do I need next? What variables and literals are available to me right now.
Some Help for Exercise #7:
Write a method named printRange that takes two integers as parameters, a low
and a high, and the name. Make your method print out the name in uppercase and
all integers in the range from low to high separated by a plus sign. Then, print
= and the total of the numbers. Return that total.
For example, the call:
printRange(5, 10, "kris");
should produce the following output:
Kris, 5+6+7+8+9+10 = 45
Your method should produce a complete line of output. If low and high are equal,
the output should be a list with just one number. You can assume the low number
is smaller than the high number. Your method should also work properly for negative
integers. For example, the following series of calls:
printRange(3, 3,"Sally");
printRange(-10, -5,"James");
should produce the following lines of output:
Sally 3 = 3
James -10+-9+-8+-7+-6+-5=-45
Once this method works, create a main routine to ask for the two numbers and give the result 5 times.
Extra: Once that main routine works, change it to give the total of all the sums,
Exercise #8 IF Statement and Scanner
Ask the user for a number. Then tell the user "YOU WON" if they entered any of the following:
- 20
- between (not including) 30 - 40
- any number over 100