Top Banner

Click here to load reader

of 26

Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.

Dec 25, 2015

Download

Documents

Theodore Byrd
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • Slide 1
  • Week #2 Java Programming
  • Slide 2
  • Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check Show Line Numbers
  • Slide 3
  • Simple Output For printing words (or Strings) onto the screen, we use this command: System.out.println(Anything you want printed here); If we want to print something that contains a variable, we would use this command: String mySentence = Hey there.; System.out.println(mySentence);
  • Slide 4
  • Simple Output String commands: \This represents a quotation mark \This represents an apostrophe \nThis represents a new line \\This represents a backslash \tThis represents a tab \sThis represents a space
  • Slide 5
  • Simple Output Another way to print out text onto your screen: This is used when you have a lot of variables to be included in the print statement, example: String name = Vincent Tsang; int age = 60; String school = Binghamton University; System.out.format(My name is %s, I am %s years old, I go to %s., name, age, school);
  • Slide 6
  • Simple Output Printing out variables using the System.out.println(); format: String name = Vincent Tsang; int age = 60; String school = Binghamton University; System.out.println(My name is + name + . I am + age + years old, I go to + school + .);
  • Slide 7
  • If/Else Statements What is an if/else statement? Basically, in programming, we will often have different ways to approach things depending on the values given to it. We tell the program that If something occurs, do this. If something else occurs, do something else. Else do something else T his will be clearer when we write out the code
  • Slide 8
  • If/Else Statements The command for the if/else statement is in this format: if(comparison) { Something happens here; } else { Something else happens here; }
  • Slide 9
  • If/Else Statements You are allowed to have more than one if statement: if(comparison) { Something happens here; } if(comparison2) { Something else happens here; } else { Something else happens here; }
  • Slide 10
  • If/Else Statements Nested If/Else Statements: if(comparison) { if(comparison2) {// Basically the same as && symbol Something happens here; } } else { Something else happens here; }
  • Slide 11
  • If/Else Statements Logical Operators || represents or This can be found above your enter key (its located with the backslash) && represents and This can be found with the 7 key Switch Statements This is used when you have a lot of things to compare and do different type of commands This is to save time so that you dont have to type out a billion if statements
  • Slide 12
  • If/Else Statements In a switch statement, we need a variable to compare its value with: Example: int month;// This is our integer variable switch(month) { case 1: System.out.println(January); break; // We will always need to break out of the statement case 2: System.out.println(February); break; } Do Exercise 1 (ExerciseOne.java)
  • Slide 13
  • Scanners What is a scanner? Scanner will ask user for an input and store that input somewhere in the program to be used later Scanners are not imported into java by default, you need to import this yourself by using this command at the top of your code: import java.util.Scanner;
  • Slide 14
  • Method Functions What is a method? A method is a collection of statements that performs an operation Steps to do something, like washing clothes you would first take your clothes out of the hamper, then put it into the washing machine, then turn on the washing machine, put in detergent etc. Function is another name for Methods Remember that function/method names are always capitalized except the first word. (Refer to Code Convention Format) Method contains two parts Method header Method body
  • Slide 15
  • Method Functions Method Header (Signature for the method) The heading that describes the method, example: public static void sayHi() {// Method Header Code goes here;// Method Body } Another Example (With return value): public static int getAge() { return 15; }
  • Slide 16
  • Method Functions with Parameters Same as before but this time we will be using parameters, we use parameters when we need to give the method outside information: Example: public static void main(String[] args) { int age = 20; print(age); // This is a method I created myself and not predefined by java } public static void print(int age) { System.out.println(age); }
  • Slide 17
  • Operators What are operators? Operators are mathematical symbols used to calculate numbers +, -, /, *, % +Addition -Subtraction /Division *Multiplication %Modulus
  • Slide 18
  • Increment / Decrement operators There are commands for you to increment or decrement numbers in a variable, for that we use ++ to increment and we use to decrement, example: int counter = 0; counter++;// This will add 1 to counter counter--;// This will subtract 1 from the counter counter += 5;// This is the same as counter = counter + 5; counter -= 5;// This is the same as counter = counter 5; Things to remember: ++, --, +=, -=
  • Slide 19
  • Math Library Functions Like the scanner, the math isnt included by default, we need to import it: import java.lang.Math; Commands we can use from this library: AbsAbsolute Value PowPower CeilCeiling FloorFloor MaxMaximum MinMinimum SqrtSquare Root
  • Slide 20
  • Math Library Functions How to use these functions: Absolute value: int myInt = -5; int otherInt = Math.abs(myInt); System.out.println(otherInt);// Will print out 5 Power: System.out.println(Math.pow(otherInt, 2));
  • Slide 21
  • Math Library Functions Ceiling System.out.println(Math.ceil(25.4)); Floor System.out.println(Math.floor(37.9)); Maximum System.out.println(Math.max(otherInt, myInt)); Minimum System.out.println(Math.min(otherInt, 2));
  • Slide 22
  • Declaration Integers (Whole numbers) int myVariable = 5; Doubles (Numbers with decimal) double myVariable = 24.7; Characters (Letters) char myVariable = a; Boolean (True/false) boolean myVariable = false; boolean myVariable = true;
  • Slide 23
  • Declaration String (Words/Sentences) String myVariable = Hello World!; There are functions that are pre-made for strings which are very useful: substring(int n) Returns everything after the n th position including the one on n th position on a string indexOf(String n) Returns the index of the string n on a string isEmpty() Returns true/false depending if the string is empty toLowerCase() Returns a string that is all in lower case toUpperCase() Returns a string that is all in upper case equals(String n) Compares one string to another
  • Slide 24
  • Declaration String functions examples: String myVariable = Hello; Substring myVariable.substring(3);// Will return lo; myVariable.substring(3,4);// Will return l; myVariable.substring(0,2);// Will return ? indexOf myVariable.indexOf(llo);// Return 2 isEmpty myVariable.isEmpty();// Returns false
  • Slide 25
  • Declaration toLowerCase myVariable.toLowerCase();// Returns hello toUpperCase myVariable.toUpperCase();// Returns HELLO Equals myVariable.equals(Hello);// Returns true myVariable.equals(hello);// Returns false
  • Slide 26
  • Program 1 Create a simple calculator