COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.

Slides:



Advertisements
Similar presentations
Logic & program control part 2: Simple selection structures.
Advertisements

Java Programming Strings Chapter 7.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to Objects and Input/Output
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 28, 2005.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
COMP 110 Introduction to Programming Mr. Joshua Stough September 5, 2007.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough October 1, 2007.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
COMP 14 Introduction to Programming Miguel A. Otaduy May 14, 2004.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Objectives ► Become familiar with the class String ► ► Learn how to use input and output dialog boxes in a program ► ► Learn how to tokenize the input.
Lecture 2 Objectives Learn about objects and reference variables.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
CSCI 51 Introduction to Computer Science Joshua Stough February 3, 2009.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CSCI 51 Introduction to Computer Science Dr. Joshua Stough January 29, 2009.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Basic Computation
Building Java Programs
Introduction to Computer Science / Procedural – 67130
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Building Java Programs Chapter 2
Chapter 2: Basic Elements of Java
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Outline Creating Objects The String Class The Random and Math Classes
Building Java Programs Chapter 2
Building Java Programs
Chapter 2: Java Fundamentals cont’d
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007

Announcements Tutoring Tuesdays 6-9 Dey Hall 2 nd floor Feedback on programs, options: –Paper copies due in class.

Review System.out.println ("I said, \"Hi There!\""); I said "Hi There!" System.out.print ("Hello\n"); System.out.println ("World"); Hello World System.out.println ("Hello\rWorld"); Hello World System.out.println ("Hi\b\bHello"); Hello System.out.println ("Hello\tWorld"); HelloWorld

Review In the Java programming language: –a program is made up of one or more classes –a class contains one or more methods –a method contains program statements A Java application always contains a method called main Source code saved in a file with the extension.java File must have the same name as the class with the main method Use the dot (.) operator to call methods: Math.round

Review import statements if any public class ClassName { declare named constants and/or stream objects public static void main (String[] args) throws IOException { variable declarations executable statements }

Review Output System.out.print (stringExp); System.out.println (stringExp); –print vs. println Comments // this is a one-line comment –“comments out” the rest of the line after // /* this is a multi-line comment */ –“comments out” everything between /* and */

Review Questions What is stored in num? int num = (int) Math.round (12.7); What is printed to the screen? Assume the user enters 10 and that keyboard is already defined. System.out.print ("Enter a number: "); int num = Integer.parseInt (keyboard.readLine()); System.out.println (num * num); 13 Enter a number:

Assignment Operators Just for convenience count += 5;// count = count + 5; count -= 5; // count = count - 5; count *= 5; // count = count * 5; count /= 5; // count = count / 5;

Assignment Operators The right hand side of an assignment operator can be a complex expression The entire right-hand expression is evaluated first, then the result is combined with the original variable Therefore result /= (total-MIN) % num; is equivalent to result = result / ((total-MIN) % num);

Questions What is stored in total and count in the following statements? int total = 10, count = 5; total += count++; int total = 20, count = 3; total /= --count; String str = "COMP"; str += 110; total count 15 6 total count 10 2 str COMP110

Java Variables Primitive Variables –primitive data types (int, double,...) –stores the data in the memory location Reference Variables –stores an address in the memory location –"points to" another memory location

Objects More complex data type than a primitive data type Stored in the "other" memory location –is "pointed to" by a reference variable Is operated on by special operators called methods

Objects and Classes An object's data type is a class The class contains the data types that make up the object and what methods can operate on the object Examples: –String –Integer –Double

Primitive Variables int x = 45; When the computer sees x, it knows which memory location to look up the value in

Reference Variables Integer num; When the computer sees num, it knows which memory location to look for the address in It will read the address in num and look up a value in that memory location

Creating Objects We use the new operator to create objects, called instantiation Integer num; num = new Integer(78); parameter

Review the Terms We declare a reference variable of a class type. We use the new operator to instantiate an object of that class type. We store the address of that object in the reference variable.

Changing the Reference Var num = new Integer (50); The address of the newly-created object is stored in the already-created reference variable num

Garbage Collection What happened to the memory space that held the value 78? If no other reference variable points to that object, Java will "throw it away"

System.out.println (”Hello World!”); object method information provided to the method (parameters) Using Objects System.out object –represents a destination to which we can send output Example: –println method dot operator

Questions 1.True or False. A primitive variable is a variable that stores the address of a memory space. 2.The operator is used to create a class object. 3.In Java, the operator is used to access members of a class. It separates the class (or object) name from the method name. 4.True or False. Class objects are instances of that class. new dot (.) False True

The class String String variables are reference variables Given String name; –Equivalent Statements: name = new String("Lisa Simpson"); name = "Lisa Simpson";

Lisa Simpson

The class String The String object is an instance of class string The value “Lisa Simpson” is instantiated The address of the value is stored in name The new operator is unnecessary when instantiating Java strings String methods are called using the dot operator

Common String Methods String(String str) –constructor –creates and initializes the object char charAt(int index) –returns char at the position specified by index (starts at 0) int indexOf(char ch) –returns the index of the first occurrence of ch int compareTo(String str) –returns negative if this string is less than str –returns 0 if this string is the same as str –returns positive if this string is greater than str

Common String Methods boolean equals(String str) –returns true if this string equals str int length() –returns the length of the string String replace(char toBeReplaced, char replacedWith) –returns the string in which every occurrence of toBeReplaced is replaced with replacedWith String toLowerCase() –returns the string that is the the same as this string, but all lower case String toUpperCase() –returns the string that is the same as this string, but all upper case

String Examples String str = "Go Panthers!"; System.out.println (str.length()); System.out.println (str.charAt(3)); System.out.println (str.indexOf('!'); System.out.println (str.toLowerCase()); P go panthers!

Example Program MathStats.java Ask the user for 3 integers. Output the following: –the 3 numbers –the sum –the average –the sum squared –the square root of the sum

Force Errors Try the following in MathStats.java to see what error message is displayed: –comment out import statement –comment out throws clause –make sumSquare an int –don't initialize sum –don't cast average to double (semantic error)

Summary assignment operators (+=, /=,...) primitive variables vs. reference variables objects dot (.) operator String class

Next Time in COMP 110 Reading Assignment: Chapter 3 (pgs ) Using dialog boxes for I/O Tokenizing Strings Formatting output Reading from and writing to text files