Variables, Assignment,and Data Types

Slide Note
Embed
Share

Explore key Java programming concepts such as variables, assignment, data types, Java keywords, printing strings, pseudocode, escape sequences, and more. These fundamental topics are essential for building a strong foundation in Java programming. Dive into code examples and explanations to enhance your understanding.


Uploaded on Mar 22, 2024 | 7 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Variables, Assignment, and Data Types

  2. Java Keywords (bad for variable names) abstract assert boolean break byte case catch char class const continue default do double else enum extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while

  3. Printing strings in Java (review) System.out.println ( Whatever you are, be a good one. ); 1. System represents the Operating System 2. out is the console/screen data stream 3. println is a function to push data to the console

  4. Pseudocode CLASS CountDown BEGIN METHOD Main() BEGIN s1 = "Three... " s2 = "Two... " s3 = "One... " s4 = "Zero... " PRINT(s1 + s2 + s3 + s4 + "Liftoff!") PRINTLINE() PRINT("Houston, we have a problem.") END Main END CountDown Output: Ps Three... Two... One... Zero... Liftoff! Houston, we have a problem.

  5. Java // Program CountDown.java public class CountDown { public static void main (String[] args) { String s1 = "Three... "; String s2 = "Two... "; String s3 = "One... "; String s4 = "Zero... "; System.out.println (s1 + s2 + s3 + s4 + "Liftoff!"); System.out.println ("Houston, we have a problem."); } } Output: Three... Two... One... Zero... Liftoff! Houston, we have a problem.

  6. Escape Sequences Printing and escape sequence prints a special character in an output string. Common escape sequences: \b backspace. (e.g B\bsecz\bret prints what?) \t tab \n newline \r carriage return \ double quote \ single quote \\ backslash

  7. Java public class Roses { public static void main (String[] args) { System.out.println ("Roses are red,\n\tViolets are blue,\n" + "Sugar is sweet,\n\tBut I have \"commitment issues\",\n\t" + "So I'd rather just be friends\n\tAt this point in our " + "relationship."); } } Output: Roses are red, Violets are blue, Sugar is sweet, But I have "commitment issues", So I'd rather just be friends At this point in our relationship.

  8. Pseudocode // Prints the number of keys on a piano. CLASS PianoKeys BEGIN METHOD Main() BEGIN keys = 88 PRINT("A piano has " + keys + " keys.") END Main END PianoKeys Ps Output: A piano has 88 keys.

  9. Java // Prints the number of keys on a piano. public class PianoKeys { public static void main (String[] args) { int keys = 88; //declare and initialize System.out.println ("A piano has " + keys + " keys."); } } Output: A piano has 88 keys.

  10. Pseudocode // Print the number of sides of several geometric shapes. CLASS Geometry BEGIN METHOD Main() BEGIN sides = 7 PRINT("A heptagon has " + sides + " sides.") sides = 10 PRINT("A decagon has " + sides + " sides.") sides = 12 PRINT("A dodecagon has " + sides + " sides.") END Main END Geometry Output: A heptagon has 7 sides. A decagon has 10 sides. A dodecagon has 12 sides. Ps

  11. Java // Print the number of sides of several geometric shapes. public class Geometry { public static void main (String[] args) { int sides = 7; // declare and initialize System.out.println ("A heptagon has " + sides + " sides."); sides = 10; // assignment statement System.out.println ("A decagon has " + sides + " sides."); sides = 12; // assignment statement System.out.println ("A dodecagon has " + sides + " sides."); } }

  12. Java Use all CAPITAL LETTERS for constants and separate words with an underscore: Pseudocode: CREATE TAX_RATE = .05 Java: final double TAX_RATE = .05; Declare constants at the top of the program

  13. Example in Java Example in Java import java.util.*; class Main { public static void main(String[] args) { String myStr = "Whats up, Doc?"; System.out.println (myStr.length()); // 14 System.out.println (myStr.equals("Hello")); // false System.out.println (myStr.indexOf('s')); // 4 System.out.println (myStr.substring(2, 7)); // ats u } }

More Related Content