{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

IntroToJava - Getting Started with Java CS 180 Sunil...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Getting Started with Java CS 180 Sunil Prabhakar Department of Computer Science Purdue University Thursday, January 12, 2012
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Objectives This week we will study simple Java programs the difference between object declaration and creation some useful classes the incremental development approach Thursday, January 12, 2012
Image of page 2
3 Our First Java Program The fundamental OOP concept illustrated by the program: An object-oriented program uses objects. This program displays a window on the screen. The size of the window is set to 300 pixels wide and 200 pixels high. Its title is set to Hello World . Thursday, January 12, 2012
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 Program Hello World import javax.swing.*; class HelloWorld { public static void main(String[ ] args) { JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle( "Hello World" ); myWindow.setVisible( true ); } } Declare a name Create an object Use an object Thursday, January 12, 2012
Image of page 4
5 Program Diagram for HelloWorld myWindow : JFrame HelloWorld setSize(300, 200) setTitle(“Hello World”) setVisible(true) Thursday, January 12, 2012
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 Java basics Java is Case-Sensitive ¡ਟ myWindow Mywindow are different A program is made up of statements ¡ਟ end with ; Statements are composed of ¡ਟ “words” import new myFrame } ; { Some words have a special meaning in Java: reserved or keywords. Shown in bold orange in the slides ¡ਟ Spaces -- also called white spaces Space, tabs, returns (show up as blanks) Multiple, contiguous whitespaces are ignored Thursday, January 12, 2012
Image of page 6
7 More Examples Object Declaration JFrame myWindow; Account customer; Student jan, jim, jon; Vehicle car1, car2; Object Name One object is declared here. Class Name This class must be defined before this declaration can be stated. Thursday, January 12, 2012
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
8 Identifiers In order to manipulate an object, we have to give it a name and also create the object. Names are also called identifiers An identifier ¡ਟ Cannot be a reserved word ¡ਟ Can consist only of letters( A..Z , a..z ), digits( 0..9 ), $ and _ ¡ਟ Cannot begin with a digit Examples in recitation These are required rules. We also have naming conventions that make programs easier to read ¡ਟ Identifiers begin with a lowercase letter ¡ਟ Class names begin with an uppercase letter Thursday, January 12, 2012
Image of page 8
9 Object Creation myWindow = new JFrame ( ) ; More Examples customer = new Customer ( ) ; jon = new Student ( "John Java" ) ; car1 = new Vehicle ( ) ; Object Name Name of the object we are creating here. Class Name An instance of this class is created. Argument No arguments are used here. Thursday, January 12, 2012
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
10 Declaration vs. Creation Customer customer; customer = new Customer( ); Customer customer; new Customer ( ) ; 1. The identifier customer is declared and space is allocated in memory. 2. A Customer object is created and the identifier customer is set to refer to it.
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}