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:

  1. public static void method1() _______method1();__
  2. public static void method2(int a, String b) ___method2(3,"v");_________
  3. public static double method3() _______double x = method3()_____________________
  4. public static String method4(int a) ____String x = method4(1);__________

Exercise #2: Method header creation

What is a possible method header for these method calls

  1. method8(); ____public static void method8()__
  2. int y = method9(); _____public static int method9()____
  3. int y = method10("x"); ____public static int method10(String z)_______
  4. String x = method11(3.65,4.3) __public static String method11(double x, double y)______

Exercise #3: method returns and variables

  1. If I had a method that ended with return "x"; what would I know about the method header __it has the word String before the method header__
  2. If I had a method that ended with return 3; what would I know about the method header __it has the word int before the method header__
  3. If my method header is public static double method7(), can it have the statement return "x"? _no, it must return a double
  4. If I create x with double x in my method, can x be an input parameter? _no, because I would not be allowed to recreate x then.
  5. If I use x without creating it in my method, can x be an input paramter? _yes, and it must be true
  6. If I have public static void method1(int x), can I have the statement int x inside my method? _no
  7. What is a possible return statement for each of these methods?:

    1. public static void method1() _____return;________________
    2. public static void method2(int a, String b) ______return;_____________
    3. public static double method3() _________return 3.5;____________
    4. public static String method4(int a) ______return "abc";_____________

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;

  1. a > b && c < 11 _____ F _____
  2. a > b || c < 11 _____T _____
  3. (a > b || c < 11) && (b==5 || a < 6) _____T _____

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:

  &nbspfor (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?

  &nbspint 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?

&nbspint 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?

  &nbspint 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:

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");

}

}

}