1 / 26

Java Programming

Java Programming. Week #2. Enable Line Numbering in Eclipse. Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check “Show Line Numbers”. Simple Output. For printing words (or Strings) onto the screen, we use this command:

urbano
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 Week #2

  2. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check “Show Line Numbers”

  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);

  4. Simple Output • String commands: • \” This represents a quotation mark • \’ This represents an apostrophe • \n This represents a new line • \\ This represents a backslash • \t This represents a tab • \s This represents a space

  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);

  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 + “.”);

  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” • This will be clearer when we write out the code

  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; }

  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; }

  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; }

  11. If/Else Statements • Logical Operators • || represents “or” • This can be found above your enter key (it’s 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 don’t have to type out a billion if statements

  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)

  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;

  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

  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 intgetAge() { return 15; }

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

  17. Operators • What are operators? • Operators are mathematical symbols used to calculate numbers • +, -, /, *, % • + Addition • - Subtraction • / Division • * Multiplication • % Modulus

  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: ++, --, +=, -=

  19. Math Library Functions • Like the scanner, the math isn’t included by default, we need to import it: import java.lang.Math; • Commands we can use from this library: • Abs Absolute Value • Pow Power • Ceil Ceiling • Floor Floor • Max Maximum • Min Minimum • Sqrt Square Root

  20. Math Library Functions • How to use these functions: • Absolute value: intmyInt = -5; intotherInt = Math.abs(myInt); System.out.println(otherInt); // Will print out 5 • Power: System.out.println(Math.pow(otherInt, 2));

  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));

  22. Declaration • Integers (Whole numbers) intmyVariable = 5; • Doubles (Numbers with decimal) double myVariable = 24.7; • Characters (Letters) char myVariable = ‘a’; • Boolean (True/false) booleanmyVariable = false; booleanmyVariable = true;

  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 nth position including the one on nth 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

  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

  25. Declaration • toLowerCase myVariable.toLowerCase(); // Returns “hello” • toUpperCase myVariable.toUpperCase(); // Returns “HELLO” • Equals myVariable.equals(“Hello”); // Returns true myVariable.equals(“hello”); // Returns false

  26. Program 1 • Create a simple calculator

More Related