Download presentation
Presentation is loading. Please wait.
Published byJessica Clara Newman Modified over 9 years ago
1
Copyright © 2004-2013 Curt Hill Java Looking at our first console application in Eclipse
2
Copyright © 2004-2013 Curt Hill 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 may do both
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 –www.eclipse.orgwww.eclipse.org Copyright © 2004-2013 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 © 2004-2013 Curt Hill
5
Eclipse Construction steps Create new project Create new class Customize as needed Debug and run
6
Copyright © 2004-2013 Curt Hill 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
7
Create New Project Copyright © 2004-2013 Curt Hill
8
Enter Project Name Copyright © 2004-2013 Curt Hill
9
A usually skipped screen Copyright © 2004-2013 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 © 2004-2013 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 © 2004-2013 Curt Hill
12
Add an initial class Copyright © 2004-2013 Curt Hill
13
Name the class Copyright © 2004-2013 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 © 2004-2013 Curt Hill
15
Generated Code Copyright © 2004-2013 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
17
Customized Copyright © 2004-2013 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
19
Start Run Copyright © 2004-2013 Curt Hill
20
Run Copyright © 2004-2013 Curt Hill
21
package firstcon; public class FirstCon { /** * @param args */ public static void main (String[] args) { // TODO Auto-generated method stub System.out.println("Hello World"); } } Code
22
Copyright © 2004-2013 Curt Hill Comments A comment explains things for people –Ignored by Java /* and */ –Enclose multiline comments // –Makes the rest of the line a comment Documentation uses special comments –Program: Javadoc
23
Copyright © 2004-2013 Curt Hill 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
24
Copyright © 2004-2013 Curt Hill 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
25
Copyright © 2004-2013 Curt Hill 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
26
Copyright © 2004-2013 Curt Hill 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
27
Copyright © 2004-2013 Curt Hill 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
28
Copyright © 2004-2013 Curt Hill Main function It is static –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
29
Copyright © 2004-2013 Curt Hill 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
30
Copyright © 2004-2013 Curt Hill print and println Overloaded method names Accept one parameter There is a version for almost every parameter type
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 © 2004-2013 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 © 2004-2013 Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.