1 / 85

Introduction to Computer Programming

Introduction to Computer Programming. CSC171 – 001 Getting Started – Chapters 1 & 2 Professor Pepper ( presentation adapted from Dr. Siegfried ). What Is Programming?. Computers cannot do all the wonderful things that we expect without instructions telling them what to do.

vanmatre
Download Presentation

Introduction to Computer 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. Introduction to Computer Programming CSC171 – 001 Getting Started – Chapters 1 & 2 Professor Pepper (presentation adapted from Dr. Siegfried)

  2. What Is Programming? • Computers cannot do all the wonderful things that we expect without instructions telling them what to do. • Program – a set detailed of instructions telling a computer what to do • Programming – designing and writing computer programs • Programming language – a language used to express computer programs. (Syntax & Vocab)

  3. A First Program Open braces mark the beginning Class header Method header public class MyFirstjava { public static void main(String[] args) { System.out.println ("This is my first Java program."); } } statements Close braces mark the end

  4. A First Program – What Does It Do? System.out.println ("This is my first Java program."); Prints the message This is my first Java program. Ends the line

  5. Source x.java Compile (javac x.java) Object x.class Execute (java x) Make the Program Run • Compile (translate to java byte code) • Run (interpreter for that OS reads java byte code and translates for machine)

  6. Steps • Start / program / accessories / notepad • Type in program and save as MyFirstjava.java right on the C drive. • Start / program / accessories / command prompt • Type: Path=“c:\program files\java\jdk1.5.0_09\bin”  so windows can find javac • Type: javac MyFirstjava.java  creates MyFirstjava.class • Look in Windows (right click start/ explore)  see MyFirstjava.class • Type java MyFirstjava

  7. Exercise • Add the line “Welcome to Java” • When you run the program, you will see: This is my first Java Program. Welcome to Java. • Extra: • Info: print() does not go to the next line • Try splitting Welcome to Java into two statements: “Welcome” and then “ to Java.”

  8. Development Environment Blue Jay Download at: http://www.bluej.org/download/download.html Instructions on syllabus

  9. Average • On Paper: Average 2 + 4 + 6 • Pay attention to your steps

  10. A second program Problem – write a program which can find the average of three numbers. Let’s list the steps that our program must perform to do this: • Add up these values • Divide the sum by the number of values • Print the result Each of these steps will be a different statement.

  11. Writing Our Second Program • Add up these values • Divide the sum by the number of values • Print the result sum = 2 + 4 + 6; an assignment statement sum = 2 + 4 + 6;

  12. Assignment Statements • Assignment statements take the form: variable=expression Memory location where the value is stored Combination of constants and variables

  13. Expressions • Expressions combine values using one of several operations. • The operations being used is indicated by the operator: + Addition - Subtraction * Multiplication / Division

  14. Expressions – Some Examples 2 + 5 4 * value x / y

  15. Writing Our Second Program • sum = 2 + 4 + 6; • Divide the sum by the number of values • Print the result average = sum / 3; Names that describe what the values represent

  16. Writing Our Second Program • sum = 2 + 4 + 6 • average = sum / 3; • Print the result System.out.println(″The average is ″ + average); The output method variable name

  17. Writing Our Second Program public static void main(String[] args) { -------------------- sum = 2 + 4 + 6; average = sum / 3; System.out.println("The average is " + average); } We still need to add a declare our variables. This tells the computer what they are.

  18. Writing Our Second Program public class Average3 { public static void main(String[] args) { int sum, average; sum = 2 + 4 + 6; average = sum / 3; System.out.println("The average is " + average); } } Tells the computer that sum and average are integers

  19. Writing Our Second Program public class Average3a { public static void main(String[] args) { int sum; int average; sum = 2 + 4 + 6; average = sum / 3; System.out.println("The average is " + average); } } We could also write this as two separate declarations.

  20. Variables and Identifiers • Variables have names – we call these names identifiers. • Identifiers identify various elements of a program (so far the only such element are the variables. • Some identifiers are standard (such as System)

  21. Identifier Rules • An identifier must begin with a letter or an underscore _ • Java is case sensitive upper case (capital) or lower case letters are considered different characters. Average, average and AVERAGE are three different identifiers. • Numbers can also appear after the first character. • Identifiers can be as long as you want but names that are too long usually are too cumbersome. • Identifiers cannot be reserved words (special words like int, main, etc.)

  22. IllegalIdentifier Reason Suggested Identifier my age Blanks are not allowed myAge 2times Cannot begin with a number times2 or twoTimes four*five * is not allowed fourTimesFive time&ahalf & is not allowed timeAndAHalf Some Illegal Identifiers

  23. Types

  24. What type to use? • Repeat a value often  worry about the size  • Float and Double imprecise  not for big money!

  25. Assignment int number1 = 33; double number2; number2 = number1; byteshortintlongfloatdouble char

  26. Dividing • int / int  int (even if you assign it to a double) • float / int  float • int / float  float Solution: Cast it ans = n / (double) m

  27. Math Operators & PEMDAS • + add • - subtract • * multiply • - division • % remainder Example: base + (rate * hours)

  28. Fancy Math variable = variable op (expression) count = count + 1 count = count + (6 / 2a + 3) variable op = expression count += 1 count += (6 / 2a + 3) Example: int count = 1; count += 2; The value of count is now 3

  29. More Fancy Math • Increment ++ • Decrment – • ++n adds 1 before executing • n++ adds 1 after executing Example:

  30. Characters Let’s talk in words, not numbers! • char = single character • Note it with single quotes (ex: ‘a’, ‘1’) • Can’t move to byte or short • We can store single characters by writing: char x, y; • x and y can hold one and only one character

  31. Character Strings • We are usually interested in manipulating more than one character at a time. • We can store more than one character by writing: String s; • If we want s can hold to have some initial value, we can write: String s = “Initial value"; • For now, we use character data for input and output only.

  32. STRINGS • Type : String Holds text • Enter with double quotes “abc” • Really a class, so capitalize String • Just a list of chars. Example byeString: • Start at 0 • byeString.charAt(3) is D • byeString.length() is 13 • byeString.equals(somethingOtherString) is either true or false • byeString.toUpperCase() is GOODBYE WORLD • byeString.toLowerCase() is goodbye world

  33. Average Pgm with String • Change the AVG program to put “the average is” into a string first, and convert the string to Uppercase using toUpperCase()

  34. Updated Avg Pgm public class Average3 { public static void main(String[] args) { int sum, average; String averageLabel = “The average is “; averageLabel = averageLabel.toLowerCase(); sum = 2 + 4 + 6; average = sum / 3; System.out.println( averageLabel + average); } }

  35. Escape Characters 1 • BUT, I really want a quote inside my string! \” is “ - “abc\”def” - abc”def \’ is ‘ - “abc\’def” - abc’def \\ is \ - “abc\def” - abc\def

  36. Escape Characters 2 How do I get new lines and tabs? \n= new line (go to beginning of next line) \r =carriage return (go to beginning of this line) \t = tab (go to next tab stop)

  37. Constants Constant doesn’t change Why use a variable if  massive changes later  show meaning  avoid Hard coding public static final int MAX_PEOPLE = 20; Capitalize by convention only -> just do it.

  38. Spelling Conventions • Name constants • Variables start lower case • Classes uppercase • Word boundaries upper case (numberOfPods)

  39. Comments • // -> comment line ex: // this is a comment • /* xxx */  comment between marks ex: /* these are a bunch of comments x=y; that line above is meaningless */ • Space liberally

  40. Another Version of Average • Let’s rewrite the average program so it can find the average any 3 numbers we try: • First, make up examples • We now need to: • Find our three values • Add the values • Divide the sum by 3 • Print the result

  41. Examples for Average • 0 + 0 + 0 = 0/3 = 0 (Try zeroes) • 100 -50 -29 = 21/3 = 7 (Try + and -) • 2 + 4 + 6 = 12/3 = 4 (Try normal)

  42. Writing Average3b This first step becomes: 1.1Find the first value 1.2 Find the second value 1.3 Find the third value 2. Add the values 3. Divide the sum by 3 4. Print the result

  43. Writing Avg3 (continued) Since we want the computer to print out some kind of prompt, the first step becomes: 1.1.1 Prompt the user for the first value 1.1.2Read in the first value 1.2.1 Prompt the user for the second value 1.2.2 Read in the second value 1.3.1 Prompt the user for the third value 1.3.2 Read in the third value 2. Add the values 3. Divide the sum by 3 4. Print the result

  44. Writing Avg3 (continued) We can prompt the user with: 1.1.1 System.out.println ("Enter the first value ?"); 1.1.2Read in the first value 1.2.1 System.out.println ("Enter the second value ?"); 1.2.2 Read in the second value 1.3.1 System.out.println ("Enter the third value ?"); 1.3.2 Read in the third value 2.Addthe values 3. Divide the sum by 3 4. Print the result

  45. The Scanner Class • Most programs will need some form of input. • At the beginning, all of our input will come from the keyboard. • To read in a value, we need to use an object belonging to a class called Scanner: Scanner keyb = new Scanner(System.in);

  46. Reading from the keyboard • Once we declare keyb as Scanner, we can read integer values by writing: variable= keyb.nextInt();

  47. Writing the input statements in Average3b We can read in a value by writing: System.out.println ("What is the first value\t?"); int value1 = keyb.nextInt(); System.out.println ("What is the second value\t?"); int value2 = keyb.nextInt(); System.out.println ("What is the third value\t?"); int value3 = keyb.nextInt(); 2. Add the values 3. Divide the sum by 3 4. Print the result

  48. Writing the assignments statements in Average3b System.out.println ("What is the first value\t?"); int value1 = keyb.nextInt(); System.out.println ("What is the second value\t?"); int value2 = keyb.nextInt(); System.out.println ("What is the third value\t?"); int value3 = keyb.nextInt(); sum = value1 + value2 + value3; 3. Divide the sum by 3 4. Print the result Adding up the three values

  49. Writing the assignments statements in Average3b System.out.println ("What is the first value\t?"); int value1 = keyb.nextInt(); System.out.println ("What is the second value\t?"); int value2 = keyb.nextInt(); System.out.println ("What is the third value\t?"); int value3 = keyb.nextInt(); sum = value1 + value2 + value3; average = sum / 3; 4. Print the result Calculating the average

  50. Writing the output statement in Average3b System.out.println ("What is the first value\t?"); int value1 = keyb.nextInt(); System.out.println ("What is the second value\t?"); int value2 = keyb.nextInt(); System.out.println ("What is the third value\t?"); int value3 = keyb.nextInt(); sum = value1 + value2 + value3; average = sum / 3; System.out.println("The average is " + average);

More Related