CS 145 Lecture 9 – Methods that Take and Give

Agenda

  • what ?s
  • what does this do?
  • methods that take
  • methods that give
  • visualizing method execution

TODO

  • Talent is skill developed while no one is looking. While no one is looking, complete some problems from the String-1 section of CodingBat. On Friday’s 1/4 sheet, write down the names of the problems you’ve solved and any questions that arose.
  • Start homework 2, due before October 6.

Note

You are learning a new language. Think about how you learned language the first time around. What did you do? You immersed yourself in it day after day. To become fluent, you must spend time hearing and speaking. There is no magic.

Let’s play a round of What Does This Do? I’ll reveal the following problems. Look at them in silence for at least 15 seconds and see if you can figure out what they’re doing… Then, what task are they performing and what are better names for the variables?

  1. public static void main(String[] args) {
      Random generator = new Random();
      int a = generator.nextInt(6) + 1;
      int b = generator.nextInt(6) + 1;
      System.out.println(a + b);
    }

  2. public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
    
      String a = in.next();
      String b = in.next();
      String c = in.next();
    
      String foo = "" + a.charAt(0) + b.charAt(0) + c.charAt(0);
      foo = foo.toLowerCase();
    
      System.out.println(foo);
    }

  3. public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
    
      int a = in.nextInt();
      String s = "" + a;
    
      System.out.println(s.startsWith("-"));
    }

  4. public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      String line = in.nextLine();
      
      Scanner colonizer = new Scanner(line);
      colonizer.useDelimiter(":");
    
      int a = colonizer.nextInt();
      int b = colonizer.nextInt();
      int c = colonizer.nextInt();
    
      double foo = a + b / 60.0 + c / 3600.0;
      System.out.println(foo);
    }

  5. public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      String a = in.nextLine();
      String b = a.replace("?", "");
      int foo = a.length() - b.length();
      System.out.println(foo);
    }

We’ll next have a look at the methods that don’t just achieve some effect, but actually give us back values that we can use elsewhere in our code.

Code

RandomLetterer.java

package lecture0923;

import java.util.Random;

public class RandomLetterer {
  public static void main(String[] args) {
    Random generator = new Random();

    char c1 = getRandomLetter(generator);
    char c2 = getRandomLetter(generator);
    char c3 = getRandomLetter(generator);
    
    System.out.println("" + c1 + c2 + c3);
    
    foo(76);
    int j = 67;
    foo(j);
    foo(5 * 6);
  }
  
  private static void foo(int i) {
    System.out.println(i + " " + i);
  }
  
  private static char getRandomLetter(Random generator) {
    String alphabet = "abcdefghijklmnñopqrstuvwxyz";
    int index = generator.nextInt(alphabet.length());
    char c = alphabet.charAt(index);
//    System.out.println(c);
    
    return c;
  }
}

Line.java

package lecture0923;

public class Line {

  public static void main(String[] args) {
    System.out.println(slope(4, 20, 2, 10));
  }
  
  private static double slope(double x1,
                              double y1,
                              double x2,
                              double y2) {
    return (y2 - y1) / (x2 - x1);
  }
}

Haiku

f, on his breakup:
I can’t function anymore.
I want my x back.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *