1 / 32

Java Programming

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

star
Download Presentation

Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming • Mike DiLalomike@mikedilalo.com • Room 113June 26th – July 11th8:00 – 11:15 a.m. • No Class July 4th & 5th • Open-ended course

  2. Course Website • mikedilalo.com/java • Any announcements as well as all lecture notes, class exercises, assignments, and solutions will be posted on this site.

  3. Textbook Think Java: How to Think Like a Computer Scientist By: Allen B. Downey www.greenteapress.com/thinkapjava/

  4. 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.

  5. 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?

  6. Interpreted vs. Compiled

  7. 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.

  8. Statement Types There are five basic types of programming instructions, also known as statements. • Input • Output • Math • Testing • Repetition

  9. Debugging • Syntax Errors • Run-time Errors • Logic Errors &Semantics

  10. 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."); } }

  11. 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

  12. 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.

  13. 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

  14. 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?

  15. 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”;

  16. 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?

  17. 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?

  18. 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

  19. 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;

  20. 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); } }

  21. 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); } }

  22. 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

  23. 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’;

  24. 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.

  25. 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?

  26. 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?

  27. 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?

  28. 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); } }

  29. 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); } }

  30. 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?

  31. 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?

  32. 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?

More Related