CS 1: Lecture 13 – Casting and Relational Operators
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):
double
float
long
int
short
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.
- Cast or Not? #1
double x = 45;
- Cast or Not? #2
int x = 45.0;
- Cast or Not? #3
int i = 101; double x = i;
- Cast or Not? #4
int i = 0; short s = i;
- Cast or Not? #5
int i = 899; long j = 900; int k = i + j;
- Cast or Not? #6
byte b = 127; String s = b + " <- result";
- Cast or Not? #7
public static void main(String[] args) { short s = randomEven100(); } public static short randomEven100() { Random g = new Random(); return g.nextInt(51) * 2; }
- Cast or Not? #8
public static void main(String[] args) { int i = 54; labeledPrint("i", i); } public static void labeledPrint(String label, long l) { System.out.printf("%s: %d%n", label, l); }
- Cast or Not? #9
public static void main(String[] args) { int j = getRandomInt(); } public static int getRandomInt() { return new Random(); }
- Cast or Not? #10
String s = 93; int i = "37";
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:
- Social Security?Is person X eligible for Social Security?
- Walk?Should the batter walk to first base?
- Polygamous?Is person X polygamous?
- Drive legally?Is person X (who has been drinking) able to drive legally?
- Wrong number?Has person X guessed the wrong number?
- Is tattooable?Is your loved one’s name tattooable on your knuckles, one letter per?
- Is blank?Is the given string empty?
- Is logout?Is the given string
"logout"
?
See you next class!
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;
}
}