320 likes | 432 Views
Java Programming. Mike DiLalo mike@mikedilalo.com Room 113 June 26 th – July 11 th 8:00 – 11:15 a.m. No Class July 4 th & 5 th Open-ended course. Course Website. mikedilalo.com/java
E N D
Java Programming • Mike DiLalomike@mikedilalo.com • Room 113June 26th – July 11th8:00 – 11:15 a.m. • No Class July 4th & 5th • Open-ended course
Course Website • mikedilalo.com/java • Any announcements as well as all lecture notes, class exercises, assignments, and solutions will be posted on this site.
Textbook Think Java: How to Think Like a Computer Scientist By: Allen B. Downey www.greenteapress.com/thinkapjava/
What is a program? • A program is a sequence of instructions that specifies how to perform a computation. • The computation might be something mathematical, but it can also be a symbolic computation.
High Level vs Low Level High Level Languages – Java, Python, Javascript, C++ Low Level Languages – Machine code, Assembly Language What is the trade off? Which is better?
Java bridges the gap Java source code is compiled and generates byte code. This byte code can then be executed on any device running a special interpreter called a Java Virtual Machine, or JVM for short.
Statement Types There are five basic types of programming instructions, also known as statements. • Input • Output • Math • Testing • Repetition
Debugging • Syntax Errors • Run-time Errors • Logic Errors &Semantics
EXERCISE 1-1 Create a new Java project named “Hello” and copy the code below. Once this works, edit your code to make your program print out your name on the next line after “Hello, world!” class Hello { public static void main(String[] args) { System.out.println("Hello, world."); } }
Explanation of Hello World class Hello { public static void main(String[] args) { System.out.println("Hello, world."); } // end of main method } // end of Hello class
Comments • Comments are used to explain what something in a program does. • Comments make working on someone else’s code much easier to understand. • When the compiler sees //, it ignores everything from there until the end of the line.
Variables • A variable is a named location that stores a value. Values are things that can be printed, stored, and operated on. (such as the string “Hello, world”) • To store a value, you must first create a variable. This is called a declaration. • int year; // declaration of an int named year • String city; // declaration of a String named city
Assignment • After a variable has been declared, it can be assigned a value. This is known as assignment. • A variable is assigned a value by using the = sign. • int year; // declaration of an int named year • year = 2013; // assignment of the value 2013 • System.out.println(year); // what will this print?
Shortcuts • Multiple variable of the same data type can be declared at the same time by separating variable names with commas. • int day, month, year; • String firstName, lastName; • A variable can be declared and assigned a value in a single line by combining the two steps. • int year = 2013; • String city = “Metuchen”;
int • int is a primitive data type used to store a signed 32 bit signed integer. (± 2,000,000,000) • int is the default data type used for integral values unless there is a reason to choose something else. • Mathematical calculations can be made using int variable names in place of numbers. • int year = 2013; • intnextYear; • nextYear = year + 1; • System.out.println(year); • System.out.println(nextYear); • // What will the output look like?
int • int values can be added, subtracted, multiplied and divided. The +, -, *, and / symbols are known as operators. • Int num = 50; • num = num + 5; • num = num – 3; • num = 2 * 9; • num = num / 6; • // What is the value of num?
int division problem • int half = 7/2; • // What is the value of half? • int only stores integers so the variable half will only be assigned the value 3. • int also supports the modulus operator % which will find the remainder of a division operation. • int half = 7/2; // 3 • inthalfRemainder = 7%2; // 1
int Shortcuts • An int can be incremented or decremented by 1by using this special notation. • intnum = 5; • num++; // same as: num = num + 1; • num--; // same as: num = num -1; • Addition or subtraction of int variables can be done using the extended assignment notation. • intnum = 5; • num += 5; // same as: num = num + 5; • num -= 5; // same as: num = num – 5;
EXERCISE 1-2 Create a new Java project named “Midnight” and copy the code below. Edit your program to make it calculate the number of seconds that have passed since midnight. Store your answer in the int result. Hint: Start by assigning the current time values to the variables hour, minute, and second. class Midnight{ public static void main(String[] args) { int hour, minute, second, result; // your code goes here System.out.println(result); } }
EXERCISE 1-2 Solution class Midnight{ public static void main(String[] args) { int hour, minute, second, result; hour = 10; minute = 02; second = 37; result = hour*60*60 + minute*60 + second; System.out.println(result); } }
double • The double data type is a double precision 64-bit IEEE 754 floating point. In other words, it can be used for decimal numbers. • Like int, double is the default data type for decimal numbers. • double pi = 3.14159; • double year = 2013; // 2013.0
char • The char data type is a 16-bit Unicode character. • char literals are surrounded by single quotation marks. • char grade = ‘A’; • char question = ‘?’; • char zero = ‘0’;
Strings • A String is actually an object, not a data type. The components of a String object are characters. For now, we can declare and assign values to Strings as if they were one of the other data types. • String literals are surrounded by double quotation marks. • String str = “Hello, world!”; • System.out.println(“Hello, world!”); • System.out.println(str); • There are many useful object methods for the String.
String + String • The mathematical operators used on types int and double are not used for Strings except for + • + can be used to append a String with another String. • String name = “Ray”; • name = name + “ Rice”; • System.out.println(name); • // What will the output be?
String.charAt • The charAt method can tell us what character is at a specific location within the String. • charAt takes a single int as an argument and returns the char which is at that location within the String. • String fruit = “banana”; • char letter = fruit.charAt(1); • System.out.println(letter); • // What is the output?
String.length • The length method can tell us how many characters are in a String. • length takes no arguments and returns an int which is the length of the String. • String fruit = “banana”; • Int len = fruit.length(); • System.out.println(len); • // What is the output?
EXERCISE 1-3 Create a new Java project in named “Last” and copy the code below. Edit your program so it saves the last character of the String to the result char. Hint: In Java, the index starts at 0, not 1. class Last{ public static void main(String[] args) { String hello = “Hello, world!”; char result; // your code goes here System.out.println(result); } }
EXERCISE 1-3 Solution class Last{ public static void main(String[] args) { String hello = “Hello, world!”; char result; int len = hello.length(); result = hello.charAt(len – 1); System.out.println(result); } }
String.indexOf • The indexOf method is the inverse of charAt. • charAt takes an index and returns the character at that index. • indexOf takes a character and finds the index where that character appears. • indexOf takes a single character as an argument and returns a single int which is the location of the first occurrence of that character within the String. • String fruit = “banana”; • Int index = fruit.indexOf(‘a’); • System.out.println(index); • // What will the output be?
String.indexOf • indexOf can also take an int as a second argument. If this argument is included, the method will return the first occurance of the char starting at that index. • String fruit = “banana”; • Int index = fruit.indexOf(‘a’,2); • System.out.println(index); • // What will the output be?
Strings are immutable • The String.toUpperCase and String.toLowerCase methods take no arguments and return an identical string with all upper or lower case letters. • Strings are immutable meaning that no method can change (or mutate) the value of the String. Instead, they return a new String with the effects applied. • String name = Mike; • String newName = name.toUpperCase(); • System.out.println(name); • System.out.println(newName); • What will the output be?