Georgia Institute of Technology Introduction to Java part 2 Barb Ericson Georgia Institute of Technology May 2006.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Chapter 2 - Introduction to Java Applications
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Chapter 2 - Introduction to Java Applications
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
 2005 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Georgia Institute of Technology Creating and Modifying Text part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
Introduction to Programming Writing Java Beginning Java Programs.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Recitation 2 Main Method, API & Packages, Java Basics.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials.
Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005.
Chapter 2: Java Fundamentals
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2005.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
Syntax and Semantics, and the Program Development Process ROBERT REAVES.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introduction to Programming Writing Java Beginning Java Programs.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005.
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Introduction to Java Primitive Types Operators Basic input and output.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
Georgia Institute of Technology Workshop for Programming And Systems Management Teachers Chapter 3 Introduction to Media Computation, Java, and DrJava.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Georgia Institute of Technology Introduction to Java, and DrJava part 1 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology.
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Introduction to Java Applications
Introduction to Classes and Objects
Introduction to Java part 2
Workshop for Programming And Systems Management Teachers
Yanal Alahmad Java Workshop Yanal Alahmad
Declaring Variables – Mod 4
Chapter 3: Using Methods, Classes, and Objects
Creating and Modifying Text part 2
Chapter 2 - Introduction to Java Applications
Introduction to Java part 2
Introduction to Java, and DrJava part 1
Declaring Variables – Mod 4
Declaring Variables – Mod 4
Barb Ericson Georgia Institute of Technology Oct 2005
Workshop for Programming And Systems Management Teachers
Introduction to Java, and DrJava
Anatomy of a Java Program
Introduction to Java Applications
Introduction to Java part 2
Unit 3: Variables in Java
Introduction to Java, and DrJava
Introduction to Java, and DrJava part 1
Presentation transcript:

Georgia Institute of Technology Introduction to Java part 2 Barb Ericson Georgia Institute of Technology May 2006

Georgia Institute of Technology Learning Goals Understand at a conceptual level –How do you print output? –How do you represent text? –How do you invoke object and class methods? –How do you know what methods a class has?

Georgia Institute of Technology Printing Output to the Console One of the things you often want to do in a program is output the value of something In Java the way to print to the console is to use –System.out.println(); Will print out the value of the thing in the parentheses and a new line –System.out.print(); To print just the thing in the parentheses without a new line

Georgia Institute of Technology A Semicolon (;) ends a Statement Java programs are made up of statements –Like sentences in English Java statements end in a semicolon not a period –The period is used to send a message to an object System.out.println() –Or access data in the object System.out.println() DrJava’s interaction pane prints the result of statements without a semicolon –but not the result of statements with a semicolon Use System.out.println(); to force output

Georgia Institute of Technology Punctuation Exercise Fix the following: –The course was difficult it required five different texts. –that that is is that that is not is not –I was so happy, I could have jumped for joy –The clouds were slowly gathering, the leaves were shaking as the wind blew stronger.

Georgia Institute of Technology Console Output Exercise Use System.out.println() to print the results of expression to the console –System.out.println(3 * 28); –System.out.println(14 – 7); –System.out.println(10 / 2); –System.out.println( ); –System.out.println("Hi" + "There"); –System.out.println(" "); Try using System.out.print() instead –What is the difference?

Georgia Institute of Technology Strings Java has a type called: String A string is an object that has a sequence of characters in Unicode –It can have no characters (the null string "") –It can have many characters "This is one long string with spaces in it. “ –Everything in a string will be printed out as it was entered Even math operations “ ” Java knows how to add strings –It returns a new string with the characters of the second string after the characters of the first With no added space

Georgia Institute of Technology Methods Classes in Java define methods –Recipes or functions f(x) = x 2 –May take input –May produce an output Two Types –Object method Sent as a message to an object Implicitly passed the current object –Class method Sent as a message to a class

Georgia Institute of Technology Method Exercise In DrJava’s interaction pane try these –Object methods "HI".toLowerCase() "This is a string".indexOf("is") " This is ".trim() –Class methods Math.abs(13) Math.abs(-13) Math.min(3,4) Character.getNumericValue('A')

Georgia Institute of Technology Message Always Have Parenthesis You can tell that out.println() is sending a message –Because of the () Messages always have () –Even if there are no parameters (arguments) If you are sending data along with a message it goes inside the parentheses –Separated by commas –Math.min(3,4);

Georgia Institute of Technology Common Errors Did you make any mistakes when you typed in the examples? –If you use the wrong case it won’t work > math.abs(-3) Error: Undefined class 'math‘ –If you misspell something it won’t work > Mat.abs(-3) Error: Undefined class 'Mat‘ > Math.ab(-3) Error: No 'ab' method in 'java.lang.Math' Use the up arrow key in DrJava to bring up the previous statement and fix it

Georgia Institute of Technology "Hi" is a String Object The compiler turns literal strings into string objects –Objects of the String class –In package java.lang Object methods are invoked by sending a message –with the same name as the method –the same type, number, and order of input parameters –to the object

Georgia Institute of Technology API Exercise The Classes defined as part of the Java language are documented in the API – Find the documentation for the following classes –String and Math –Find documentation for the methods used in the previous exercise –Try out some other methods for these classes

Georgia Institute of Technology Java Packages Java groups related classes into packages Common Packages –java.lang Contains basic classes for the language –System, Math, Object, … –java.io Contains classes for input and output –java.awt Contains basic user interface classes –javax.swing Contains more advanced user interface classes

Georgia Institute of Technology Class Methods versus Object Methods In the API documentation how can you tell which are class methods and which are object methods? –Look for the keyword static on the method –If it has the keyword static then it is a class method –If there is no keyword static then it is an object method

Georgia Institute of Technology What do Objects Look Like? Objects are created with space for their data Objects have a reference to the object that represents the class –Object of the class “Class” Name = Food Fields = Name, Price Methods = getName, setName, getPrice, setPrice, getCalories Food : Class Name = “Fries” Price = 1.99 Fries: FoodWaffles: Food Name =“Waffles” Price = 2.99

Georgia Institute of Technology Java is Case Sensitive Some programming languages are case sensitive –Meaning that double isn’t the same as Double –Or string isn’t the same as String In Java primitive types are all lowercase –double, float, int, Class names start with an uppercase letter –So String and System are the names of classes

Georgia Institute of Technology Java Naming Conventions In Java only Class names start with an uppercase letter –System, BufferedImage, Picture All other names start with lowercase letters but uppercase the first letter of each additional word –picture, fileName, thisIsALongName

Georgia Institute of Technology Identifying Classes Exercise Which of these are primitive types, and which are the names of classes? –int –Picture –char –Double –Math –double –Integer –String

Georgia Institute of Technology Summary You can print out things using System.out.println(expression); or System.out.print(expression); You represent text using String objects –In pairs of double quotes String is a class –In the package java.lang –You can look up methods in the API –You can invoke methods on a string object "Hi".toLowerCase(); Class methods can be executed using the class name “dot” method name –Math.min(3,4); –A method is a class method if it has keyword “static”