Introduction to Java part 2

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
CMT Programming Software Applications
Chapter 2 - Introduction to Java Applications
Georgia Institute of Technology Introduction to Java part 2 Barb Ericson Georgia Institute of Technology May 2006.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
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?
Lecture 2: Classes and Objects, using Scanner and String.
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 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.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2005.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Working With Objects Tonga Institute of Higher Education.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
 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.
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
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.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
3.1 Objects. Data type. Set of values and operations on those values. Primitive/Built-In types. n Usually single values n Can build arrays but they can.
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Introduction to Java Applications
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Introduction to Java part 2
Workshop for Programming And Systems Management Teachers
Console Output, Variables, Literals, and Introduction to Type
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
2.5 Another Java Application: Adding Integers
Declaring Variables – Mod 4
Chapter 3: Using Methods, Classes, and Objects
Data types and variables
Creating and Modifying Text part 2
Introduction to Objects
Chapter 2 - Introduction to Java Applications
Introduction to Java, and DrJava part 1
Declaring Variables – Mod 4
MSIS 655 Advanced Business Applications Programming
Declaring Variables – Mod 4
Barb Ericson Georgia Institute of Technology Oct 2005
Workshop for Programming And Systems Management Teachers
Manipulating Pictures, Arrays, and Loops
Introduction to Java Programming
Introduction to Java, and DrJava
Anatomy of a Java Program
Introduction to Java Applications
Using Objects (continued)
Introduction to Java part 2
Unit 3: Variables in Java
Introduction to Java, and DrJava
Introduction to Java, and DrJava part 1
Introduction to C Programming
Workshop for Programming And Systems Management Teachers
Introduction to Objects
Presentation transcript:

Introduction to Java part 2 Georgia Institute of Technology

Georgia Institute of Technology Learning Goals Understand at a conceptual level How do you print output? How do you represent text? // String 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 The difference between the two is that println will print the value of the variable or expression and then move to the next 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 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 System.out is a way to access the class variable called “out” in the System class. out.println() is sending the println() message to the “out” object. 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(128 + 234); System.out.println("Hi" + "There"); System.out.println("128 + 234"); Try using System.out.print() instead What is the difference? Case does matter in Java. System.out.println must start with an uppercase S. Click on the console tab and see the output. Try the same above with a semicolon at the end. In Java a semicolon shows the end of a statement. When the compiler sees double quotes it recognizes the characters as part of a string. Georgia Institute of Technology

Georgia Institute of Technology

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 “128 + 234” 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

Georgia Institute of Technology Methods Classes in Java define methods Recipes or functions f(x) = x2 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 Class methods can also be invoked by sending them as a message to an object since an object has a link to its class. Georgia Institute of Technology

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') The Character class is a wrapper class for char values. It also contains some static (class) methods. Georgia Institute of Technology

Messages 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.m in(3,4); Georgia Institute of Technology

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

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

Georgia Institute of Technology API Exercise The Classes defined as part of the Java language are documented in the API http://java.sun.com/j2se/1.5.0/docs/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

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” Fries: Food Waffles: Food Name = “Fries” Price = 1.99 Name =“Waffles” Price = 2.99 Food : Class Name = Food Fields = Name, Price Methods = getName, setName, getPrice, setPrice, getCalories To get the class object that represents the class use object.getClass(); To check if an object is an instance of a class use instanceof(className); There is actually an array of Method objects that holds the methods for a class. There is also an array of Field objects which describes the fields in the class. Georgia Institute of Technology

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 A convention is the usual way of doing something. Java code will compile if you don’t follow these conventions but it will be hard for other programmers to understand your code. 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

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” Georgia Institute of Technology