Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Looking at our first console application in Eclipse

Similar presentations


Presentation on theme: "Java Looking at our first console application in Eclipse"— Presentation transcript:

1 Java Looking at our first console application in Eclipse
Copyright © Curt Hill

2 A moment on paradigms Java is well suited for either the console or GUI paradigm A console program views the keyboard/screen as nearly a single device We both read and write to it using strings A GUI program has a WIMP interface: Windows Input Menus Pointer The console paradigm is easier for starters, but we will do both Copyright © Curt Hill

3 Eclipse Eclipse is the Interactive Development Environment that we will use Originally developed by IBM Donated to public domain Now maintained by the Eclipse Foundation Copyright © Curt Hill

4 Terminology for Eclipse
Resource Things such as projects, directories and files Editor A window pane that allows changes to a resource, often Java code View A window pane that shows a resource Perspective A group of editors and views Copyright © Curt Hill

5 Eclipse Construction steps
Create new project Create new class Customize as needed Debug and run Copyright © Curt Hill

6 Create new project Click File|New|Java Project
Use the New Project dialog to set some initial parameters Specify the project name This will become a directory tree The project is an XML file of name: .project in the directory The screen shots follow Copyright © Curt Hill

7 Create New Project Copyright © Curt Hill

8 Enter Project Name Copyright © Curt Hill

9 A usually skipped screen
Copyright © Curt Hill

10 Projects and Classes A project is just a tool to manage pieces
We now have to add the pieces Each Java program is a class Any class might use other classes as well What we now do is add the main or root class to our project Copyright © Curt Hill

11 Create new class Click File|New|Class This generates a new class
This new class will be your program Use java.lang.Object as the ancestor class Specify a main method Click on the box Copyright © Curt Hill

12 Add an initial class Copyright © Curt Hill

13 Name the class Copyright © Curt Hill

14 Specified names In the prior screen there were three things that were specified: Package Class name Inclusion of the main method via checkbox The package name is usually all lower case The class name usually starts with a capital letter and each subsequent word is capitalized: FirstCon Copyright © Curt Hill

15 Generated Code Copyright © Curt Hill

16 Customize as needed At this point we have a Java console program with proper form, which does nothing Now add the desired functionality Insert into main: System.out.println(“Hello world”); Java is case sensitive so System must start with a capital That doesn’t sound like much work It will get worse Copyright © Curt Hill

17 Customized Copyright © Curt Hill

18 Debug and run Click the run button or use menu Run|Run
Run time properties are displayed Specify the new class name Search may be easiest Console output will appear in the bottom pane First the command line Next the program output Copyright © Curt Hill

19 Start Run Copyright © Curt Hill

20 Run Copyright © Curt Hill

21 Code package firstcon; public class FirstCon { /** args */ public static void main (String[] args) { // TODO Auto-generated method stub System.out.println("Hello World"); } } Copyright © Curt Hill

22 Comments A comment explains things for people /* and */ //
Ignored by Java /* and */ Enclose multiline comments // Makes the rest of the line a comment Documentation uses special comments Program: Javadoc Copyright © Curt Hill

23 Class Declaration public class FirstCon {
Objects in Java are called Classes A class binds together variables and methods This class is named FirstCon Its visibility is public A private class in this case could not be executed Copyright © Curt Hill

24 Classes and files A file is the compilation unit
It must have an extension .java Each file must have one and only one public class The file name must match the public class name Even case, which may be a problem on MS Operating Systems Most of these issues are handled by Eclipse without our intervention Copyright © Curt Hill

25 Compilation Units Public class names must match file name
Other private or friendly classes may be present in the file Each class will have its own object (.CLASS) file after it is compiled Copyright © Curt Hill

26 Main Function Most classes have both variables and methods
Every application must have a method called main, which is where execution starts Main’s parameter is a string array Command line parameters Often, in a console program most of the work occurs in the main method Copyright © Curt Hill

27 Main function public static void main(String args[ ]) { System.out.println("Hello World!"); } // end of main void indicates main returns nothing It is public Accessible to system Copyright © Curt Hill

28 Main function It is static This main function has just one statement
A static function has limited access to class data Can be called without having a class instance This main function has just one statement We will cover static more later Copyright © Curt Hill

29 Writing to the console System.out.println("Hello world");
System is an instance of a class that contains numerous predefined classes and methods out is an output stream class with methods print and println Copyright © Curt Hill

30 print and println Overloaded method names Accept one parameter
There is a version for almost every parameter type Copyright © Curt Hill

31 Classes and objects Sometimes these two terms are used interchangably
A class is a type Defines possible values and actions An object is an instance of a class Example A person name would be a class “Curt Hill” would be an object, an instance of a class Copyright © Curt Hill

32 Finally There are some other details that need to be considered:
Handing in programs These will be covered in a subsequent presentation Copyright © Curt Hill


Download ppt "Java Looking at our first console application in Eclipse"

Similar presentations


Ads by Google