Download presentation
Presentation is loading. Please wait.
1
Introduction to Java part 2
Barb Ericson Georgia Institute of Technology May 2006 Georgia Institute of Technology
2
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
3
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
4
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
5
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
6
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? 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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
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
20
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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.