IntroToJava

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

Info iconThis 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Objectives This week we will study n simple Java programs n the difference between object declaration and creation n some useful classes n the incremental development approach Thursday, January 12, 2012
Background image of page 2
3 Our First Java Program n The fundamental OOP concept illustrated by the program: An object-oriented program uses objects. n This program displays a window on the screen. n 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
Background image of page 3

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

View Full DocumentRight 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
Background image of page 4
5 Program Diagram for HelloWorld myWindow : JFrame HelloWorld setSize(300, 200) setTitle(“Hello World”) setVisible(true) Thursday, January 12, 2012
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Java basics n Java is Case-Sensitive ¡ myWindow Mywindow are different n A program is made up of statements ¡ end with ; n Statements are composed of ¡ “words” n import new myFrame } ; { n Some words have a special meaning in Java: reserved or keywords. Shown in bold orange in the slides ¡ Spaces -- also called white spaces n Space, tabs, returns (show up as blanks) n Multiple, contiguous whitespaces are ignored Thursday, January 12, 2012
Background 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
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Identifiers n In order to manipulate an object, we have to give it a name and also create the object. n Names are also called identifiers n 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 n Examples in recitation n 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
Background 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
Background image of page 9

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

View Full DocumentRight Arrow Icon
Declaration vs. Creation Customer customer; customer = new Customer( ); Customer customer; new Customer ( ) ; 1. The identifier customer is declared and space is 2 Thursday, January 12, 2012
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 57

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

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online