GWU

CS 1111

Introduction to Software Development

GWU Computer Science


Lecture Notes 10: Advanced Methods and Introduction to Conditionals


Objectives

By the end of this module, for simple programs with real numbers, you will be able to:




Before Starting

If you do not have your Codio course ready, use any text editor or simple IDE. Some possibilities are:




Catching Up

Before we move forward, let's catch up (complete any remaining work from the previous module)
In this case, make sure we've got:

  1. String API: Searching the API and implementing combinations of loops and API calls to achieve complex string manipulation.




Writing and using Methods that use Input and/or return Output

In the previous module, we practiced using methods that resolve (or return) something and that might need input parameters to run.
In this segment we'll practice writing and invoking such methods.

First, two definitions:

Look at this program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18


  public class IOMethods1
  {
    public static void main(String args[])     
    {
      int num1 = 5;
      int num2 = 13;
      // Invoke the myOperation method and save result in the num3 variable
      int num3 = myOperation(num1, num2);  // myOperation(num1, num2) resolves into the resulting value
      System.out.println ("myOperation on " + num1 + " and " + num2 +  " is: " + num3);
    }

    public static int myOperation(int localNum1, int localNum2)  
    {
      int localNum3 = (localNum1 * localNum2) - (localNum1 - localNum2);
      // this causes the method invocation to resolve into localNum3
      return localNum3;
    }
  }  
  

Note the following:

Activity 1: In IOMethods1.java write the above program and see what gets printed.
Now, let us run it inside the Java Visualizer to understand the way these parameters are passed around.

Activity 2: In IOMethods1.java inside the method myOperation, modify the values of localNum1 and localNum2 before returning localNum3 by using these two lines:


localNum1+=99;
localNum2+=99;


then, inside the main, add the following statement:


System.out.println ("num1 after modifying localNum1 is: " + num1 );
System.out.println ("num2 after modifying localNum2 is: " + num2 );


What do you print? why?



Now Look at this program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30


  public class IOMethods2
  {
    public static void main(String args[])     
    {
      String base = "Pizza should not have pineapple";
      String part = "pine";

      // Invoke the extract method and save result in out variable
      String out = extract(base, part);  // extract(base, part) resolves into the resulting string
      System.out.println ("The new String is: " + out);
    }

    public static String extract(String localBase, String localPart)  
    {
      // The return variable
      String localOut = "";

      // Get the index of localPart inside localBase
      int idx = localBase.indexOf ( localPart );
      // Get the size of localPart
      int partSize = localPart.length();

      // copy localBase up to the start of localPart
      localOut = localBase.substring (0,idx);
      // copy localBase after localPart until the end of localBase
      localOut = localOut + localBase.substring (idx+partSize);

      return localOut;
    }
  }  
  

Activity 3: In IOMethods2.java write the above program and see what gets printed.
Now, let us run it inside the Java Visualizer to understand the way these parameters are passed around.

Note the following:



Activity 4: In IOMethods2.java inside the method extract, modify the values of localBase and localPart before returning localOut by using these two lines:

localBase = "New Base";
localPart = "New Part";


then, inside the main, add the following statement:


System.out.println ("base after modifying localBase is: " + base );
System.out.println ("part after modifying localPart is: " + part );


What do you print? why?



Activity 5: In IOMethods3.java Write a method that takes no parameters but returns a value. Then invoke that method from main and print the return value.

Activity 6: In IOMethods4.java Write a method that takes one String parameter (called text), one integer parameter (called idx), and one char parameter (called ch).
The method should construct a new String which is a version of the String text with the character at index idx replaced with the character ch. Then invoke that method from main and print the return value.




Conditionals!! ... A simple example

Consider this program:

Activity 7: In MyIfExample.java, add an additional println right below the "Hey, ..." println. Compile and execute the program. Then, change the value of y to 6 and compile/execute. What is the output?

About the if-statement:

Activity 8: Consider this (part of a) program. Try to mentally execute and identify the output before confirming in MyIfExample2.java,
        int s = 0;
        for (int i=1; i<=5; i++) {
            s = s + i;
        }
        if (s < 15) {
            System.out.println ("Less than 15");    
        }
        System.out.println ("End");
     




If-else

Consider this program:

We can combine an else clause with an added if-clause:

Activity 9: Type up the above example in IfElseExample.java, and change the values of x one at a time so that you see each block execute. That is, try one value of x to make the if-block execute, then another value of x to make the else-if block execute etc.




Comparison operators

: Activity 10: Are there any circumstances under which the last block executes? Activity 11: Suppose we want to identify whether x has the largest value among x, y and z. Why doesn't the following program work?

Can you alter the program (in MyThreeVariableExample.java) to make it work?