CS 145 Lecture 3 – Hello Again, Java
Dear students,We start today with a little segment I like to call What Does This Do?. I show you a short snippet of code, you slurp it into your brain, swish it around a few times, and figure out what it does. The task would be much easier if I picked good meaningful names for the variables, but I haven’t. Here we go.
import java.util.Scanner; public class Foo1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); double flarg = in.nextDouble(); double blop = in.nextDouble(); double nerb = flarg * blop / 2.0; System.out.println(nerb); } }
import java.util.Scanner; public class Foo2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); double a = in.nextDouble(); double b = in.nextDouble(); double c = a + (b - a) * 0.5; System.out.println(c); } }
import java.util.Scanner; public class Foo3 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); n = n / 10 * 10; System.out.println(n); } }
In case you’re wondering, I like little games like this and we will play them often. I use them because some of the most enjoyable instruction I’ve ever experienced was from Mrs. Lee, my high school Spanish teacher. She knew that learning a language required a lot of practice, and she got us practicing by having us play games in her class—all the time.
This weekend I was trying to get my one-year-old to talk more. With my first child, I went overboard. One of his first words, when he was just nine months old, was Nicaragua. That kid will resent me soon. So, I’m going slower with my fourth. When I think about what words he should know first, I think body parts. Why? Because kids are always getting hurt or feeling sick, and I need them to be able to communicate about what ails them. So it is with you. You’ve got to know the body parts of our program, so that you can tell me about what ails you.
Let’s just jot down some notes about the grammar of Java that we’ve seen so far:
program: 1 or more classes class: package name; 0 or more import statements public class name { 0 or more methods } method: public void name(...) { 0 or more statements } statement: type name; <- declaration name = expression; <- assignment type name = expression; <- declassignment object.method(expression); <- call expression: 324 16.7 name expression + expression expression - expression expression * expression expression / expression
Last time we ended after seeing how to get user input. However, the user experience was a little sad. The computer was sitting there waiting for input, but the user had no indication of that. We must add a prompt? But where and how?
Someone asked on a quarter sheet if there were other ways to study the material for this course. There are many. The one I recommend the most, because it accompanies the textbook, is .
These “texty” programs probably don’t resemble what you know as programs. As we have time this semester, we may introduce graphical user interfaces with windows and buttons. These things build off of the elements that we are discussing now.
Now, let’s solve some problems!
- How many electrons are present in an electrically neutral atom of some element?
- What percentage of presidents have been Democrats? Republicans?
- How much space in square feet does each person get in the country of _______?
- A farmer sells as many full eggs as possible, and eats the rest. How many does she eat?
- 183 days from now, something big will happen. What day of the week is that?
Here’s your TODO list of things to complete before next class:
- Start homework 1. Two of the problems (
LemonAid
andUshSure
) use only the ideas we have discussed so far. The other one uses ideas we’ll hit upon Wednesday. - Read chapter 2 through section 2.2 in your book. On a 1/4 sheet, answer these questions:
- What happens when you try to store a
double
value in anint
? - Which operation has higher precedence: casting or division?
- What happens when you declare a variable twice?
- What happens when you try to print a
String
who contents are Java code?
- What happens when you try to store a
See you next class!
P.S.
Here’s the code we wrote together in class…
Electronizer.java
package lecture0912; import java.util.Scanner; public class Electronizer { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter atomic number: "); int atomicNumber = in.nextInt(); System.out.print("Number of electrons: "); System.out.println(atomicNumber); } }
EggCounter.java
package lecture0912; import java.util.Scanner; public class EggCounter { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Hey, Farmer S*****. How many eggs did you collect? "); int nEggs = in.nextInt(); int nCartons = nEggs / 12; int nLeftover = nEggs % 12; System.out.println("Leftover: " + nLeftover); } }
SomethingBig.java
package lecture0912; import java.util.Scanner; public class SomethingBig { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("What day is it? 0-6? "); int dayOfWeek = in.nextInt(); System.out.print("How many days away? "); int offset = in.nextInt(); int target = (dayOfWeek + offset) % 7; System.out.println("Target: " + target); } }
Presidents.java
package lecture0912; public class Presidents { public static void main(String[] args) { int nPresidents = 42; int nRepublicans = 18; int nDemocrats = 15; System.out.println(nRepublicans / (double) nPresidents); } }