teaching machines

CS 1: Lecture 13 – Casting and Relational Operators

October 4, 2017 by . Filed under cs1, cs145, cs148, fall 2017, lectures.

Dear students,

Today we close out our focused discussion on methods. They will never go away; we will continue to write them all throughout the semester. But I want to tie up a few loose ends having to do with types.

We will begin with a little science experiment to illustrate the need (or lack of need) for casting.

Let’s order the types from greatest (most general) to least (least general):

  1. double
    
  2. float
    
  3. long
    
  4. int
    
  5. short
    
  6. byte
    

When we go from a less general type to a more general type, we are upcasting. Upcasting is automatic; we don’t need to explicitly cast to drop the value of the lesser type into the greater type. The value of the lesser type is automatically promoted. We’ve seen promotion in another context already: when an operator combines two operands of a lesser type and greater type, the result is always a value of the greater type. For example, to avoid integer division in 10 / 3, we can simply widen the type of just one of the operands: 10 / 3.0. Promotion will automatically turn this into 10.0 / 3.0.

When we go from a more general type to a less general type, we are downcasting. There’s a potential for information loss, so an explicit cast is required.

We have seen the Computer as Calculator, a lover of number crunching. We have seen the Computer as Chef, organizing its tasks into self-contained recipes. Next week we see the Computer as Philosopher, seeking truth from our data.

To begin, let’s meet some new operators: <, <=, >, >=, ==, and !=. These are called the relational operators. (== and != are sometimes distinguished as equality operators.) We’ll introduce these by writing some methods that serve as “magic 2 balls”, answering yes/no questions like these:

See you next class!

Sincerely,

P.S. It’s time for a haiku!

It’s below zero
But that means nothing really
Not in Fahrenheit

P.P.S. Here’s the code we wrote together in class…

Casting.java

package lecture1004;

public class Casting {
  public static void main(String[] args) {
//    byte b = 128;
//    String s = b + " <- result";
  }
  
  
}

Certainly.java

package lecture1004;

public class Certainly {
  public static void samesies(int expected, int actual, String message) {
    System.out.printf("%d == %d | %s%n", expected, actual, message);
  }
  
  public static void samesies(boolean expected, boolean actual, String message) {
    System.out.printf("%b == %b | %s%n", expected, actual, message);
  }
   
  
  public static void main(String[] args) {
    System.out.println("Foo");
  }
}

Relational.java

package lecture1004;

public class Relational {
  public static final double PI = 3.14159;
  
  public static void main(String[] args) {
    Certainly.samesies(false, isEligible(57), "ssn for 57");
    Certainly.samesies(true, isEligible(73), "ssn for 73");
    Certainly.samesies(true, isEligible(62), "ssn for 62");
    Certainly.samesies(false, isEligible(61), "ssn for 61");
    Certainly.samesies(false, isEligible(0), "ssn for 0");
    Certainly.samesies(false, isWalk(0), "walk for 0");
    Certainly.samesies(false, isWalk(1), "walk for 1");
    Certainly.samesies(false, isWalk(2), "walk for 2");
    Certainly.samesies(false, isWalk(3), "walk for 3");
    Certainly.samesies(true, isWalk(4), "walk for 4");
    Certainly.samesies(true, isTattooable(10,  "Chris"), "is taa...");
    Certainly.samesies(false, isTattooable(10,  "Bartholomew"), "is taa...");

  }
  
  public static boolean isTattooable(int nDigits, String name) {
    return name.length() <= nDigits;
  }
  
  public static boolean isDriverable(double bac) {
    return bac < 0.08;
  }
  
  public static boolean isPolygamous(int nSpouses) {
    return nSpouses > 1;
  }
  
  public static boolean isEligible(int age) {
    return age >= 62;
  }
  
  public static boolean isWalk(int nBalls) {
    return nBalls == 4;
  }
}

Inputter.java

package lecture1004;

import java.util.Scanner;

public class Inputter {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter a number: ");
    int i = in.nextInt();
    in.nextLine();
    System.out.print("Enter a line: ");
    String s = in.nextLine();
    System.out.println(i + s);
  }
  
  public static void foo() {
    byte b = 127; // [-128, 127]
    String s = b + " <- result";
  }
    
}

Booleandrews.java

package lecture1004;

public class Booleandrews {
  public static void main(String[] args) {
    Certainly.samesies(false, isEligible(55), "ssn for 55");
    Certainly.samesies(false, isEligible(61), "ssn for 61");
    Certainly.samesies(true, isEligible(62), "ssn for 62");
    Certainly.samesies(true, isTattooable(10, "Chris"), "tattoo Chris?");
    Certainly.samesies(false, isTattooable(10, "Demosthenes"), "tattoo Demosthenes?");

  }
  
  public static boolean isEligible(int age) {
    return age >= 62;
  }
  
  public static boolean isWalk(int nBalls) {
    return nBalls == 4;
  }
  
  public static boolean isPolygamous(int nSpouses) {
    return nSpouses > 1;
  }
  
  public static boolean isLegalToDrive(double bac) {
    return bac < 0.08;
  }
  
  public static boolean isTattooable(int nFingers, String name) {
    return name.length() <= nFingers;
  }
}