Exercises that will help you study for the Midterm:
Exercise A. Strings:
Consider String temp = "remainder" and String another = "main"
temp.length() = _9__
temp.indexOf("main") = __2__
temp.indexOf(another) = ____2_____
temp.substring(3,7) = __aind_____
temp.equals(another) = ___false______
Exercise B. Mod operator : %
Consider int i = 11324
How to determine if i is even: _______i %2== 0__________________________
How to determine if i is odd: _________i%2 == 1________________________
How to determine if divisible by 10: _____i%10 == 0_________________________
How to get the last digit of i: _______i%10_________________________________
Print 1 then 0 repeatedly inside a loop that looks like this:
(for int x = 0; x < 10, x++)
{ System.out.println (___(x+1)%2______);}
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? __2___
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;
}
}120
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);
}}16
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.
public static void swapIt(int varA, int varB)
{
int varC = varB;
varB = varA;
varA = varC;
System.out.println("VarA now contains " + varA + " and varB now
contains " + varB);
}
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? _20_ (once for 10, and then continuing
through to 29)
b. What would happen if the test were z > 30? __It would not run
because 10 is not > 30
c. How would you change this to count by 2's? __ z++ change to z = z +
2;
d. How would you change this to count by 5's? __ z++ change to z = z +
5;
e. How would you add up all these numbers and just print the total?
 int sum = 0;
for (int z =10; z<30;z++)
{
System.out.println("The number is " + z);
sum = sum + z;
}
System.out.println("The total is " + sum);
f. How would you print the running total every time you counted?
 int sum = 0;
for (int z =10; z<30;z++)
{
System.out.println("The number is " + z);
sum = sum + z;
System.out.println("The total is " + sum);
}
g. How would you get the average of all the numbers printed?
 int sum = 0;
for (int z =10; z<30;z++)
{
System.out.println("The number is " + z);
sum = sum + z;
}
double average = sum/20;
System.out.println("The total is " + sum);
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
Extra: Once that main routine works, change it to give the total of all the sums
import java.util.Scanner;
public class Test1{
public static void main(){
Scanner myscan = new Scanner (System.in);
System.out.println("What is your first name? ");
String name = myscan.nextLine();
double totalOfAllRanges = 0;
for (int c = 1; c<=5;c++)
{
System.out.println("What is the low number?");
int low = myscan.nextInt();
System.out.println("What is the high number?");
int high = myscan.nextInt();
double tot = printRange(start, end, name);
totalOfAllRanges = totalOfAllRanges + tot;
}
System.out.println(totalOfAllRanges);
}
public static double printRange(int start, int end, String name)
{System.out.print(name.toUpperCase() + ", ");
double total = 0;
for (int count = start; count <= end; count++)
{System.out.print(count + "+"); // note how + in quotes will print plus
total = total + count;}
total = total + end;
System.out.print(end);// if I put this inside the loop, I would have an extra + printed
System.out.print( "=" + total);
double average = total / (end-start+1);
System.out.println(" and the average is " + average); // ln gives the new line
return total; // do you see why I return total and not average?}
}
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
import java.util.Scanner;
public class Test1{public static void main(){
Scanner myscan = new Scanner (System.in);
System.out.println("enter a number");
int numb = myscan.nextInt();
if (numb == 20 || (numb > 30 && numb < 40) || numb > 100)
{System.out.println("You won");
}
}
}