1 / 105

Introduction to Java Programming

Learn how to write and execute a Java program. Understand String concatenation, escape sequences, compiling, and debugging. Overview of creating, declaring, and running Java classes. Step-by-step guidance provided.

aimeenoel
Download Presentation

Introduction to 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. Introduction to Java Programming

  2. Writing a Java program. How to send output to the command line console. Learn how String concatenation works. Learn about escape sequences. Learn how to compile, debug, and execute a Java program. Objectives:

  3. Top Down Design

  4. Top Down Design • Start JCreator. • Create a new file called “Lab01.java”. • Save the new file in your Lab01 folder.

  5. Creating A Java Class File

  6. Creating A Java Class File

  7. Creating A Java Class File

  8. Creating A Java Class File (cont…)

  9. Creating A Java Class File (cont…)

  10. Creating A Java Class File (cont…)

  11. Creating A Java Class File (cont…)

  12. Creating A Java Class File (cont…) Class names should always begin with an uppercase letter.

  13. Creating A Java Class File (cont…) Click this button to set the location.

  14. Creating A Java Class File (cont…)

  15. Creating A Java Class File (cont…)

  16. public class Lab01 { } Declaring A Java Class This class can be used by other classes!

  17. public class Lab01 { } Declaring A Java Class The keyword class defines this file as a Java class.

  18. public class Lab01 { } Declaring A Java Class User defined name. Class names should always begin with an uppercase letter.

  19. public class Lab01 { } Declaring A Java Class The body of every class begins and ends with a set of curly brackets.

  20. Compiling A Java Class File

  21. Compiling A Java Class File

  22. Compiling A Java Class File

  23. Running A Java Program

  24. Running A Java Program

  25. Running A Java Program

  26. The Main Method

  27. In Java, you need to have a method named main in at least one class. This method must appear within a class, but it can be any class. A class containing a main method is a program. Every program has a main method but not every class is a program. The main Method

  28. public class Lab01 { public static void main(String[ ] args) { } } The main Method (cont…) Curly brackets are used to define block statements. Methods, like classes, are block statements and must begin and end with a set of curly brackets.

  29. public class Lab01 { public static void main(String[ ] args) { } } The main Method (cont…) The main method is static.

  30. Running A Java Program

  31. Running A Java Program

  32. Console Output

  33. Console Output In Java, console output is achieved by calling System.out.printor System.out.println. • The data to be output is given as an argument in parentheses. System.out.println(“Blackjack”); • Every invocation of println ends a line of output. Blackjack _

  34. println Versus print The print method is like println, except that it does not end a line • With println, the next output goes on a new line • With print, the next output goes on the same line

  35. public class Lab01 { public static void main(String[ ] args) { System.out.println(“Main Method!”); } } The main Method (cont…)

  36. Running A Java Program

  37. Calling Methods

  38. public class Lab01 { public static void main(String[ ] args) { System.out.println(“Main Method!”); } public void output() { System.out.println(“Hello World”); } } Calling Methods output is not static

  39. public class Lab01 { public static void main(String[] args) { System.out.println(“Main Method!”); } public void output() { System.out.println(“Hello World”); } } Calling methods (cont…) Run the program

  40. Calling methods (cont…)

  41. public class Lab01 { public static void main(String[ ] args) { System.out.println(“Main Method!”); } public void output() { System.out.println(“Hello World”); } } Calling methods (cont…) output did not execute

  42. public class Lab01 { public static void main(String[ ] args) { System.out.println(“Main Method!”); } public void output() { System.out.println(“Hello World”); } } Calling methods (cont…)

  43. public class Lab01 { public static void main(String[ ] args) { } public void output() { System.out.println(“Hello World”); } } Calling methods (cont…) How do we get from here

  44. public class Lab01 { public static void main(String[ ] args) { } public void output() { System.out.println(“Hello World”); } } Calling methods (cont…) How do we get from here to there

  45. public class Lab01 { public static void main(String[ ] args) { output(); } public void output() { System.out.println(“Hello World”); } } Calling methods (cont…) We need to call the method output()

  46. public class Lab01 { public static void main(String[ ] args) { output(); } public void output() { System.out.println(“Hello World”); } } Calling methods (cont…) Build the program

  47. error: non-static method output() cannot be referenced from a static context

  48. Calling methods (cont…) Double click on the first line of the error.

  49. Calling methods (cont…) The editor will send the cursor to the line that contains the error.

More Related