Objects Classes Strings Review CSC 123 Fall 2018 Howard Rosenthal

Size: px
Start display at page:

Download "Objects Classes Strings Review CSC 123 Fall 2018 Howard Rosenthal"

Transcription

1 Objects Classes Strings Review CSC 123 Fall 2018 Howard Rosenthal

2 Lesson Goals Review objects and classes Review Encapsulation Review the Random class Review the String class Review arrays of String objects Learn practical applications including encryption 2

3 3

4 What is an Object? An object has characteristics or attributes. An object also has behaviors The attributes are the data associated with an object The behaviors are the methods used to manipulate the objects We ve already seen and made a bit of use of some objects including: arrays String Scanner Object oriented programming is all about manipulating objects to perform tasks this matches the way we really think about things 4

5 Some Examples Objects Shoe A remote control A computer mouse or keyboard Non-objects Blue Fast Rough Interesting Return Digest Many of the non-objects are either attributes or methods of the object 5

6 Some Common Characteristics of Objects An object is made of tangible material (the pen is made of plastic, metal, ink). An object holds together as a single whole (the whole pen, not a fog). An object has properties (the color of the pen, where it is, how thick it writes...). An object can do things and can have things done to it. 6

7 Identifying Objects An object has identity (each object is a distinct individual). An object has state (it has various properties, which might change). An object has behavior (it can do things and can have things done to it). 7

8 A Simple Example Is the tube of tennis balls an object? Yes. It has identity (my tube of balls is different than yours), it has states (opened, unopened), properties (brand name, location), and behavior (although not much). Is each tennis ball an object? Yes. It is OK for objects to be part of other objects. Although each ball has nearly the same state and behavior as the others, each has its own identity. Could the top two balls be considered a single object? Not ordinarily. Each has its own identity independent of the other. If they were joined together with a stick you might consider them as one object. Is the color of the balls an object? No. It is a property of each ball. Is your understanding of tennis balls an object? Probably not, although it is unclear what it is. Perhaps it is a property of the object called "your brain." 8

9 Encapsulation Encapsulation is a computer language feature that packages attributes and behaviors into a single unit. The data and the methods comprise a single entity When we create objects from the class Scanner it has attributes i.e. System.in and behaviors the various methods 9

10 Object Oriented Programming In object-oriented programming, the programmer uses a programming language (such as Java) to describe various objects. When the program is run (after being compiled) the objects are created (out of main storage) by constructors that are a part of the object and they start "doing things" by running their methods. Creating an object allows us to access its methods. The methods must execute in the correct order. For an application, the first method to run is the method named main(). There should be only one method named main() in an application. In a small application, main() might do by itself all the computation that needs to be done. In a larger application, main() will create objects and use their methods. 10

11 Classes A class is a template, or blueprint from which objects are created A programmer may define a class using Java, or may use predefined classes that come in class libraries. - We ve already done that!!! A class is merely a plan for a possible object. It does not by itself create any objects. When a programmer wants to create an object the new operator is used with the name of the class. Creating an object is called instantiation. Up to now we have worked with single classes that have included the main method Individual classes usually don t include a main method, rather the main method instantiates objects from classes, and these objects are then used by the various methods (including the main method) Every time we create an object from a class using the keyword new the object has a reference variable that refers to that type of object If the reference is lost then the object becomes garbage As we will see, garbage can be collected 11

12 An Example The Circle Class Is Instantiated (1) double radius; class Circle double area() { return π*radius 2 } double circumference() { return 2*π*radius } three Circle objects radius 5.6; double area() double circumference() radius 19.2; double area() double circumference() radius 3.4; double area() double circumference() 12

13 An Example The Circle Class Is Instantiated (2) Circle ovalguy; ovalguy = new Circle(5.6); or Circle ovalguy = new Circle(5.6); Remember- the new does the instantiation Until then all you ve created is a reference variable In Lesson 8 we describe how to build the constructors needed to write classes and create objects. 13

14 A Little More About Classes Each class actually has three parts Description of the instance variables. These can be constants or initialized during object creation They can also be changed by the methods Description of the constructors. A constructor accepts the input parameters sent when creating the object with the new operator An object can have several different constructors Description of the methods. Each object can have multiple methods (think of the Scanner object) Creating separate class objects without a main program and using constructors will be a key part of CS

15 Java Libraries and Packages Packages are large files that contain multiple classes Packages are nothing more than logical groupings of classes You can import a single class or an entire package Importing an entire package does not increase the size of the executable code, which only includes those classes actually used We ve done this already for a few months with the Scanner class: import java.packagename.classname; i.e. import java.util.scanner; import java.packagename.*; i.e. import java.util.*; 15

16 16

17 The Random class (1) The Random class is another class that is imported with import java.util.random; or with import java.util.*; The Random class has many different random methods, making it more useful than the Math.random() method Remember Math.random() is a method within the Math class, while Random is a separate class with its own methods Remember how we achieved the ability to read input from the screen: Scanner keyboard = new Scanner(System.in); Then we had a keyboard reader that could take advantage of all the methods in the class: int number = keyboard.nextint(); double decnumber = keyboard.nextdouble(); 17

18 The Random class (2) So lets create a new object called randy from the class Random Random randy = new Random(); Note that there are no data attributes in this instantiation When no seed is provided the seed defaults to the 64 bit integer (type long) representation of time of day randy is a reference variable that now has a value (a memory location) You can also create a Random object with a fixed seed which is a long integer (or therefore byte, short or int due to casting up); You then get the same sequence of numbers each time you run the program Random randy1 = new Random(seed);//seed is any type of integer including long This can also be done using setseed(seed): Random randy = new Random(); randy.setseed(seed); In either case we can then use the methods of Random: int number = randy.nextint(n); // returns an integer greater than or equal to zero but less than n (where n is an integer) In general the format is: referencevariabletoobject.method(any necessary parameters) 18

19 The Random class (3) Method & Description boolean nextboolean() This method returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence. void nextbytes(byte[] bytes) This method generates random bytes and places them into a user-supplied byte array. double nextdouble() This method returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence. float nextfloat() This method returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence. double nextgaussian() This method returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence. int nextint() This method returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. int nextint(int n) This method returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. long nextlong() This method returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence. void setseed(long seed) This method sets the seed of this random number generator using a single long seed. 19

20 The Random class (4) An example with dice Random die = new Random(); int dicesum = (die.nextint(6)+1) + (die.nextint(6)+1); Modify DiceRollSim.java (from Lesson 4 use the Random class instead of the Math class; Then try it by using a fixed seed that you read in. You can create the Random class directly with this seed or use setseed. Programs are: DiceRollSimRandomClass.java DiceRollSimRandomClassWithSeed.java DiceRollSimRandomClassSetSeed.java Those two lines would give us the sum of the two dice rolled You could create a coin object and then flip it. 20

21 Putting It All Together Passing Arrays and Using Random Write a program that does the following: The main program requests from the user the dimensions of a two dimensional array. The main program creates a two-dimensional array. The main program calls a method booleanfull that randomly fills the two-dimensional array with either true or false values in each cell. The main program then calls a second method called calcpercent that returns the percentage of elements in the two dimensional array that are true. The main program then prints out the array and on the next line prints out the percent of cells in the array that have the value true formatted to two decimal places. Hint: You ll need to create a Random object and use the method nextboolean() in the booleanfull method See RandomArrayBoolean.java 21

22 The java.lang package This package is imported automatically into every application, so you don t need to use an import statement The java.lang package includes many useful classes Math class (discussed previously and not to be confused with the Random class) String class very useful class within java String operations are handled much better in modern object-oriented languages than in older third generation languages such as Fortran 22

23 23

24 The String Class The String class allows you to create String objects a String object is not a primitive data type a String variable is a reference to a String object An example: String mydog = new String( Fido ); Because String objects are so common there is a shortcut used to initialize or assign an reference value to a String object: String mydog = Fido ; Remember this assigns the address of the String literal Fido to the reference mydog 24

25 The String Class (2) Visualizing A String Object mydog Fido length() method2() method3()

26 The String Class (3) There are many methods that are available for use with a String object int length() is a method that returns the String length: mydog.length() returns 4 Fido has four characters An empty String has no characters and length equals 0 char charat(int i) returns the character at position i We start counting from 0 as the index of the first character in the String the leftmost character has index 0 mydog.charat(2) returns d String next() returns a reference to the next input String next() is a Scanner method i.e. keyboard.next() This method skips whitespaces and returns the next group of characters as a String 26

27 String Concatenation Review (1) Concatenation is the process of joining or linking String objects together The operator is + A String is a special object that has this one special operator, but it is still not primitive!! When you concatenate you create a new object at a new reference address the old objects remain String s = Sam ; String t = Thomas ; String w = s + +t; //w then has the value Sam Thomas w.length() has the value 10 27

28 String Concatenation Review (2) Visualizing String Concatenation s Sam length() method2() method3() t Thomas length() method2() method3() w Sam Thomas length() method2() method3()

29 Strings and Characters - Review A + B = 131 (integer) A + B = AB (String) A + B = AB (String) + A + B = AB (String) A gets cast to String A + B + = 131 (String) = 7 (String) = 34 (String) Key is that without parentheses we are reading left to right 29

30 String Operations Are Implemented Via Methods String touppercase() converts a String from lower to upper case String s1 = lower ; String s2 = s1.touppercase(); System.out.printf( %s %s\n, s1, s2); The output is: lower LOWER Note: s1 is not capitalized.; the method returns a new String reference that s2 refers to All Java String operations can be found at: There is also a new document called Class String Documentation in the Reference material area Do Programming Exercises 1 here UpperCaseConversion.java 30

31 Common String Methods (1) Method Explanation Example char charat(int index) int compareto(string t) int comparetolgnorecase(string t) String concat(string t) s.charat(i) returns the character at index i. All Strings are indexed from 0. compares two Strings, character by character, using the ASCII values of the characters.s.compareto(t) returns a negative integer/ 0/positive integer if the String s lexicographically precedes/equals/ follows the String t. similar to compareto(...) but ignores differences in case. s.concat(t) returns s with t appended. boolean endswith(string suffix) s.endswith(t) returns true if t is a suffix of s. boolean startswith(string prefix) s.startswith(t) returns true if t is a prefix of s. boolean equals(object t) (The strange parameter will make sense later. For now, think of the parameter as String.) boolean equalslgnorecase(string t) s.equals(t) returns true if s and t are identical. s.equalsignorecase(t) returns true if s and t are identical, ignoring case. String s = "Titanic"; s.charat(3) returns 'a' String s = Shrek ; String t = Star Wars ; String u = Shrek ; s.compareto(t) returns a negative number. s.compareto(u) returns 0. t.compareto(s) returns a positive number. String s = E.T. ; String t = e.t. ; s.comparetoignorecase(t) returns 0. String s = Spider ; s.concat( -Man ) returns Spider-Man. String s = Forrest Gump ; s.endswith( ump ) returns true String s = Jurassic Park ; s.startswith( Jur ) returns true s.startswith( jur ) returns false String s = FINDING NEMO ; String t = Finding Nemo ; s.equals(t) returns false s.equals( FINDING NEMO ) returns true String s = FINDING NEMO ; String t Finding Nemo ; s.equalsignorecase(t) returns true 31

32 Common String Methods (2) Method Explanation Example int indexof(string t) int indexof(string t, int from) s.indexof(t) returns the index in s of the first occurrence of t and returns 1 if t is not a substring of s. s.indexof(t, from) returns the index in s of the first occurrence of t beginning at index from; an unsuccessful search returns - 1. String s = The Lord Of The Rings ; s.indexof( The ) returns 0; s.indexof( Bilbo ) returns 1. String s = The Lord Of The Rings ; s.indexof( The, 6) returns 12; int length() s.length() returns the number of characters in s. String s = Jaws ; s.length() returns 4 String replace(char oldchar, char newchar) String tolowercase() String touppercase() String trim() s.replace(oldch, newch) returns a String obtained by replacing every occurrence of oldch with newch. s.tolowercase() returns a String formed from s by replacing all uppercase characters with lowercase characters. s.touppercase() returns a String formed from s by replacing all lowercase characters with uppercase characters. s.trim() returns the String with all leading and trailing white space removed. String s = Harry Potter ; s.replace ( r, m ) returns "Hammy Pottem" String s = The Lion King ; s.tolowercase() returns "the lion king" String s = The Lion King ; s. touppercase() returns "THE LION KING" String s = Attack of the Killer Tomatoes ; s.trim() returns "Attack of the Killer Tomatoes" 32

33 String Operations substring (1) The substring method is overloaded String substring( int start) String substring( int start, int end ) Create a new String object that contains the characters of the method's String from the index start to the end of the String. Create a new String object that contains the characters of the method's String from the index start and continuing to index end-1. 33

34 String Operations substring (2) Line of code: String line = "buttercup" ; New String buttercup String a = line.substring( 0 ); String b = line.substring( 6 ); String c = line.substring( 9 ); String d = line.substring( line.length() ); String e = line.substring( line.length()+1 ); String f = line.substring( -1 ); buttercup (a new String is created, containing all the characters of the original) cup (an empty String is created) (an empty String is created) EXCEPTION EXCEPTION 34

35 String Operations substring (3) Line of code: String line = "buttercup" ; New String "buttercup" String a = line.substring( 0, 6 ); "butter" String b = line.substring( 6, 9 ); String c = line.substring( 0, 1 ); "cup" "b" String d = line.substring( 0, 0 ); "" String e = line.substring( 5, 5 ); "" String f = line.substring( 4, line.length()+1 ); EXCEPTION 35

36 More on Strings and String Methods - References String encrypt(string msg, int shift) really is accepting a String reference, not the String itself This is the same thing as passing array references Strings are immutable You can change the value in the reference pointer, but not the String itself String s = Big ; s = s.tolowercase(); // A new String is created s Big big 36

37 More on Strings and String Methods - equals Be careful with == and Strings. you are comparing references, not Strings themselves use boolean equals(str1) to compare 2 Strings strx.equals(str1) The comparison is done character by character based on the Unicode value In the example: String s = new String( ABC ); String t = new String( ABC ); s ==t is not true (s and t refer to different objects) s.equals(t) is true- they have the same String value 37

38 More on Strings and String Methods compareto (3) Being able to compare Strings allows us to alphabetize (sort) them, which in turn allows us to search them use int s.compareto(t) or int s.comparetoignorecase(t) String s = Shrek ; String t = Star Wars ; String u = Shrek ; s.compareto(t) returns a negative number. s.compareto(u) returns 0. t.compareto(s) returns a positive number. In other words, if the String that is invoking the method occurs earlier in an alphabetical list than the parameter the number that is returned is negative Remember object.method(parameter list) invokes a method 38

39 The String Class Some Special Characteristics We typically create Strings without the use of new String day6 = Friday ; This creates a new object named day6 However if I now execute the statement String dayf = Friday ; //a new object is not created, dayf just refers to the same object as day6 But if I now execute the following statement: String friday = new String( Friday ); //a new object is created See StringTestExample.java 39

40 Visualizing the Friday Example day6 Friday dayf friday Friday method1 method2 o o o method1 method2 o o o 40

41 A Few Special String Methods String names = String.join(,, Peter, Paul, Mary ); //first comma indicates the delimiter, the other commas separates the String(s) // sets names to Peter, Paul, Mary Or if String [] arraynames = { Peter, Paul, Mary }; Then String names = String.join(,, arraynames);//note the space after the first comma // sets names to Peter, Paul, Mary You can also split the names apart into an array: String names = Peter, Paul, Mary ; String [] result = names.split(, ); // result[0] = Peter, result [1] = Paul, result[2] = Mary 41

42 String Methods An Example Write a program that encrypts a message using the Caesar Cypher. The program accepts one line of text and a character shift value. Eliminate all non- alpha characters and blanks and print the result as all capitals. Enter a message on one line: Hello Jim XRAY Enter an integer in the range of The encrypted message is OLSSVQPTEYHF Strategy Capitalize Eliminate non-alpha characters Shift there are 26 characters so we wrap around if character values go beyond 90, the ASCII value of Z See CaesarCipher.java 42

43 The Null Value and Strings null is a special value that means "no object." Your program should set a reference variable to null when it is not referring to any object unless you plan on using it shortly thereafter. Until a reference variable refers to an actual object you cannot use the methods associate with the object Programs often set variables to null when they are declared: String a = null;// null is a value here, not a String class NullDemo1 { public static void main (String[] arg) { String a = "Random Jottings"; String b = null; String c = ""; if ( a!= null ) System.out.printf( a is %s\n, a ); if ( b!= null ) System.out.printf("b is %s\n, b ); if ( c!= null ) System.out.printf( c is %s\n, c ); } } 43

44 The nextline() Method (1) nextline() advances the Scanner to the beginning of the next line and returns a reference to a String object comprised of the the characters that were read (or skipped if you wish) in the process, including whitespace, but not including the newline character System.out.printf( Enter message on one line: ); String message = keyboard.nextline(); System.out.printf( Enter an integer in the range 0-25: ); int shift = keyboard.nextint(); If the inputs are: Cato 8 then message refers to Cato and shift gets the value 8. 44

45 The nextline() Method (2) However, if you reverse the process: System.out.printf( Enter an integer in the range 0-25: ); int shift = keyboard.nextint(); System.out.printf( Enter message on one line: ); String message = keyboard.nextline(); The results are different: In the second case once you read the 8 the Scanner still hasn t moved to the next line nextint() doesn t read one int and move to the next line The nextline() then see nothing prior to the line return and returns an empty String In this case using next instead of nextline fixes the problem because next looks for a non-blank character See NextLineTester.java Then do Programming Exercises 2 CountingWords 45

46 Putting It all Together String Arrays It is possible to have arrays of objects We have already seen that 2-dimensional arrays are arrays of arrays Arrays of String objects can be used to organize, alphabetize, etc., an array of Strings When you create an array of Strings you may do so as follows: String [] names = new String [5]; When you create this String each element of the array is initialized to null. You can then populate individual cells as follows: for (int i=0; i < names.length; i++) { System.out.printf( Please enter a String\n ); names[i] = keyboard.nextline(); } At this point names[i] has the value of a String You can access any method for the String i.e. names[i].charat(j) Do Programming Exercises 3 RandomStrings; V2 uses an array of String objects 46

47 Example: Declaring A String Reference and Instantiating The Elements String [] names = new String[4]; names[0] = new String( Karen ); names[1] = new String ( Jose ); names[2] = new String ( Victor ); names[3] = new String ( Morgan ); Remember We are declaring an Array of String Objects 47

48 Picturing Instantiation Of An Array of Objects Karen names names[0] names[1] names[2] names[3] method1 method2 o o o Victor method1 method2 o o o Jose method1 method2 o o o Morgan method1 method2 o o o names[0] through names[3] are reference variables, each of which refers to a String but remember when you concatenate or print a String variable it prints the actual instance value. 48

49 The Mysterious String [] args String [] args is simply an array of Strings i.e. reference pointer to objects It allows you to send inputs to the main method via the command line public class TestMain { public static void main(string [] args) { System.out.printf("Hello %s\n, args[0]); System.out.printf("Hello args[1]); } } If you execute: java TestMain Jerry Newman The output is: Hello Jerry Hello Newman What happens is that Java implicitly creates an array for you of a length equal to the number of input parameters i.e. in the above program what happens is: String [] args = { Jerry, Newman }; You have an array args of type String and args.length is equal to 2. See the TestMain application 49

50 Programming Exercises 1 UpperCaseConversion Write a program that accepts a String and displays another String composed of the characters of the first String but with all lowercase letters capitalized. Any non-alphabetical letters, such as punctuation, should be left unchanged. For example, the String When Homer blew up the nuclear plant, he yelled #!#!#!& DOH &&####!!!! should become WHEN HOMER BLEW UP THE NUCLEAR PLANT, HE YELLED #!#!#!& DOH &&####!!!! 50

51 Programming Exercises 2 CountingWords Write a method that accepts a String and returns the number of words in the String. For example, the String This sentence has too many words in it. has 8 words. 51

52 Programming Exercises 3 RandomStrings Write a program that prints 25 random String objects of length 4 such that each String is composed of uppercase alphabetical characters Do it by storing the String objects in an array String [] randstr = new String [25]; which you then print out all at once line by line. 52

Objects and Classes 1: Encapsulation, Strings and Things CSC 121 Fall 2014 Howard Rosenthal

Objects and Classes 1: Encapsulation, Strings and Things CSC 121 Fall 2014 Howard Rosenthal Objects and Classes 1: Encapsulation, Strings and Things CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Understand objects and classes Understand Encapsulation Learn about additional Java classes The

More information

Objects and Classes 1: Encapsula3on, Strings and Things CSC 121 Fall 2015 Howard Rosenthal

Objects and Classes 1: Encapsula3on, Strings and Things CSC 121 Fall 2015 Howard Rosenthal Objects and Classes 1: Encapsula3on, Strings and Things CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand objects and classes Understand Encapsulation Learn about additional Java classes The Random

More information

Objects and Classes 1: Encapsula3on, Strings and Things CSC 121 Fall 2016 Howard Rosenthal

Objects and Classes 1: Encapsula3on, Strings and Things CSC 121 Fall 2016 Howard Rosenthal Objects and Classes 1: Encapsula3on, Strings and Things CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand objects and classes Understand Encapsulation Learn about additional Java classes The Random

More information

A variable is a name for a location in memory A variable must be declared

A variable is a name for a location in memory A variable must be declared Variables A variable is a name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name int total;

More information

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Built in Libraries and objects CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Objects and Built in Libraries 1 Classes and Objects An object is an

More information

We now start exploring some key elements of the Java programming language and ways of performing I/O

We now start exploring some key elements of the Java programming language and ways of performing I/O We now start exploring some key elements of the Java programming language and ways of performing I/O This week we focus on: Introduction to objects The String class String concatenation Creating objects

More information

Using Java Classes Fall 2018 Margaret Reid-Miller

Using Java Classes Fall 2018 Margaret Reid-Miller Using Java Classes 15-121 Fall 2018 Margaret Reid-Miller Today Strings I/O (using Scanner) Loops, Conditionals, Scope Math Class (random) Fall 2018 15-121 (Reid-Miller) 2 The Math Class The Math class

More information

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly Basic Computation Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly Parentheses and Precedence Parentheses can communicate the order in which arithmetic operations are performed examples: (cost +

More information

Classes and Objects Part 1

Classes and Objects Part 1 COMP-202 Classes and Objects Part 1 Lecture Outline Object Identity, State, Behaviour Class Libraries Import Statement, Packages Object Interface and Implementation Object Life Cycle Creation, Destruction

More information

STUDENT LESSON A10 The String Class

STUDENT LESSON A10 The String Class STUDENT LESSON A10 The String Class Java Curriculum for AP Computer Science, Student Lesson A10 1 STUDENT LESSON A10 The String Class INTRODUCTION: Strings are needed in many programming tasks. Much of

More information

More non-primitive types Lesson 06

More non-primitive types Lesson 06 CSC110 2.0 Object Oriented Programming Ms. Gnanakanthi Makalanda Dept. of Computer Science University of Sri Jayewardenepura More non-primitive types Lesson 06 1 2 Outline 1. Two-dimensional arrays 2.

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Final Exam. CSC 121 Fall Lecturer: Howard Rosenthal. Dec. 13, 2017

Final Exam. CSC 121 Fall Lecturer: Howard Rosenthal. Dec. 13, 2017 Your Name: Final Exam. CSC 121 Fall 2017 Lecturer: Howard Rosenthal Dec. 13, 2017 The following questions (or parts of questions) in numbers 1-17 are all worth 2 points each. The programs have indicated

More information

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

More information

appreciate the difference between a char and a string understand and use the String class methods

appreciate the difference between a char and a string understand and use the String class methods 1 8 THE STRING CLASS Terry Marris 16 April 2001 8.1 OBJECTIVES By the end of this lesson the student should be able to appreciate the difference between a char and a string understand and use the String

More information

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

More information

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are

More information

Programming with Java

Programming with Java Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

More information

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method Appendix 3 Java - String charat() Method This method returns the character located at the String's specified index. The string indexes start from zero. public char charat(int index) index -- Index of the

More information

Final Exam. CSC 121 Fall Lecturer: Howard Rosenthal. Dec. 13, 2017

Final Exam. CSC 121 Fall Lecturer: Howard Rosenthal. Dec. 13, 2017 Your Name: Final Exam. CSC 121 Fall 2017 Lecturer: Howard Rosenthal Dec. 13, 2017 The following questions (or parts of questions) in numbers 1-17 are all worth 2 points each. The programs have indicated

More information

Basic Computation. Chapter 2

Basic Computation. Chapter 2 Basic Computation Chapter 2 Outline Variables and Expressions The Class String Keyboard and Screen I/O Documentation and Style Variables Variables store data such as numbers and letters. Think of them

More information

"Hello" " This " + "is String " + "concatenation"

Hello  This  + is String  + concatenation Strings About Strings Strings are objects, but there is a special syntax for writing String literals: "Hello" Strings, unlike most other objects, have a defined operation (as opposed to a method): " This

More information

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while CSCI 150 Fall 2007 Java Syntax The following notes are meant to be a quick cheat sheet for Java. It is not meant to be a means on its own to learn Java or this course. For that you should look at your

More information

Basic Computation. Chapter 2

Basic Computation. Chapter 2 Walter Savitch Frank M. Carrano Basic Computation Chapter 2 Outline Variables and Expressions The Class String Keyboard and Screen I/O Documentation and Style Variables Variables store data such as numbers

More information

STRINGS AND STRINGBUILDERS. Spring 2019

STRINGS AND STRINGBUILDERS. Spring 2019 STRINGS AND STRINGBUILDERS Spring 2019 STRING BASICS In Java, a string is an object. Three important pre-built classes used in string processing: the String class used to create and store immutable strings

More information

AP Computer Science A

AP Computer Science A AP Computer Science A 1st Quarter Notes Table of Contents - section links Click on the date or topic below to jump to that section Date : 9/8/2017 Aim : Java Basics Objects and Classes Data types: Primitive

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

Introduction to Computer Programming

Introduction to Computer Programming Introduction to Computer Programming Lecture #8 - Stringing Along Using Character and String Data How Do Computer Handle Character Data? Like all other data that a computer handles, characters are stored

More information

CS 1301 Ch 8, Part A

CS 1301 Ch 8, Part A CS 1301 Ch 8, Part A Sections Pages Review Questions Programming Exercises 8.1 8.8 264 291 1 30 2,4,6,8,10,12,14,16,18,24,28 This section of notes discusses the String class. The String Class 1. A String

More information

Creating Strings. String Length

Creating Strings. String Length Strings Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and

More information

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All This chapter discusses class String, from the java.lang package. These classes provide the foundation for string and character manipulation

More information

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on: Data and Expressions Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration And Use Of Variables Expressions

More information

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Understand Object Oriented Programming The Syntax of Class Definitions Constructors this Object Oriented "Hello

More information

Midterm Exam 2 Thursday, November 15th, points (15% of final grade) Instructors: Jim Williams and Marc Renault

Midterm Exam 2 Thursday, November 15th, points (15% of final grade) Instructors: Jim Williams and Marc Renault Computer Sciences 200 Midterm Exam 2 Thursday, November 15th, 2018 100 points (15% of final grade) Instructors: Jim Williams and Marc Renault (Family) Last Name: (Given) First Name: CS Login Name: NetID

More information

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents Variables in a programming language Basic Computation (Savitch, Chapter 2) TOPICS Variables and Data Types Expressions and Operators Integers and Real Numbers Characters and Strings Input and Output Variables

More information

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Strings

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Strings Computer Science 252 Problem Solving with Java The College of Saint Rose Spring 2016 Topic Notes: Strings This semester we ve spent most of our time on applications that are graphical in nature: Manipulating

More information

Strings in Java String Methods. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings.

Strings in Java String Methods. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings. Java also provides many methods with the String class to allow us to manipulate text. These

More information

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All Advanced Java This chapter discusses class String, class StringBuilder and class Character from the java.lang package. These classes provide

More information

Input. Scanner keyboard = new Scanner(System.in); String name;

Input. Scanner keyboard = new Scanner(System.in); String name; Reading Resource Input Read chapter 4 (Variables and Constants) in the textbook A Guide to Programming in Java, pages 77 to 82. Key Concepts A stream is a data channel to or from the operating system.

More information

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16 Intro to Strings Lecture 7 CGS 3416 Spring 2017 February 13, 2017 Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 1 / 16 Strings in Java In Java, a string is an object. It is not a primitive

More information

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals 1 Recall From Last Time: Java Program import java.util.scanner; public class EggBasketEnhanced { public static void main(string[]

More information

Section 2: Introduction to Java. Historical note

Section 2: Introduction to Java. Historical note The only way to learn a new programming language is by writing programs in it. - B. Kernighan & D. Ritchie Section 2: Introduction to Java Objectives: Data Types Characters and Strings Operators and Precedence

More information

Exam 1 Answer Key CSC 123 Fall 2018 Lecturer: Howard Rosenthal Oct. 17, 2018

Exam 1 Answer Key CSC 123 Fall 2018 Lecturer: Howard Rosenthal Oct. 17, 2018 Your Name: Exam 1 Answer Key CSC 123 Fall 2018 Lecturer: Howard Rosenthal Oct. 17, 2018 The following questions in numbers 1-11 are all worth 3 points each (including each true and false). The programs

More information

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008)

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008) Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008) In previous chapters, methods were called with parameters that were primitive data types. This chapter discusses how to use object

More information

Computational Expression

Computational Expression Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name

More information

Getting started with Java

Getting started with Java Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving

More information

A+ Computer Science -

A+ Computer Science - import java.util.scanner; or just import java.util.*; reference variable Scanner keyboard = new Scanner(System.in); object instantiation Scanner frequently used methods Name nextint() nextdouble() nextfloat()

More information

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination First Name: Last Name: McGill ID: Section: Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination Thursday, December 11, 2008 Examiners: Mathieu Petitpas [Section 1] 14:00

More information

Welcome to the Using Objects lab!

Welcome to the Using Objects lab! Welcome to the Using Objects lab! Learning Outcomes By the end of this lab: 1. Be able to define chapter 3 terms. 2. Describe reference variables and compare with primitive data type variables. 3. Draw

More information

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017 Intro to Strings Lecture 7 COP 3252 Summer 2017 May 23, 2017 Strings in Java In Java, a string is an object. It is not a primitive type. The String class is used to create and store immutable strings.

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

Unit 4: Classes and Objects Notes

Unit 4: Classes and Objects Notes Unit 4: Classes and Objects Notes AP CS A Another Data Type. So far, we have used two types of primitive variables: ints and doubles. Another data type is the boolean data type. Variables of type boolean

More information

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Covered in this chapter Classes Objects Methods Parameters double primitive type } Create a new class (GradeBook) } Use it to create an object.

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University January 15, 2015 Chapter 2: Data and Expressions CS 121 1 / 1 Chapter 2 Part 1: Data

More information

More on Strings. Lecture 10 CGS 3416 Fall October 13, 2015

More on Strings. Lecture 10 CGS 3416 Fall October 13, 2015 More on Strings Lecture 10 CGS 3416 Fall 2015 October 13, 2015 What we know so far In Java, a string is an object. The String class is used to create and store immutable strings. Some String class methods

More information

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS These are sample pages from Kari Laitinen s book "A Natural Introduction to Computer Programming with Java". For more information, please visit http://www.naturalprogramming.com/javabook.html CHAPTER 5

More information

c) And last but not least, there are javadoc comments. See Weiss.

c) And last but not least, there are javadoc comments. See Weiss. CSCI 151 Spring 2010 Java Bootcamp The following notes are meant to be a quick refresher on Java. It is not meant to be a means on its own to learn Java. For that you would need a lot more detail (for

More information

Computer Science 145 Midterm 1 Fall 2016

Computer Science 145 Midterm 1 Fall 2016 Computer Science 145 Midterm 1 Fall 2016 Doodle here. This is a closed-book, no-calculator, no-electronic-devices, individual-effort exam. You may reference one page of handwritten notes. All answers should

More information

Variables and Assignments CSC 121 Spring 2017 Howard Rosenthal

Variables and Assignments CSC 121 Spring 2017 Howard Rosenthal Variables and Assignments CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Understand variables Understand how to declare and use variables in Java Programs Learn how to formulate assignment statements

More information

A+ Computer Science -

A+ Computer Science - Visit us at www.apluscompsci.com Full Curriculum Solutions M/C Review Question Banks Live Programming Problems Tons of great content! www.facebook.com/apluscomputerscience import java.util.scanner; Try

More information

Chapter 4 Classes in the Java Class Libraries

Chapter 4 Classes in the Java Class Libraries Programming Fundamental I ACS-1903 Chapter 4 Classes in the Java Class Libraries 1 Random Random The Random class provides a capability to generate pseudorandom values pseudorandom because the stream of

More information

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4 Assignments Lecture 6 Complete for Project 1 Reading: 3.1, 3.2, 3.3, 3.4 Summary Program Parts Summary - Variables Class Header (class name matches the file name prefix) Class Body Because this is a program,

More information

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University April 21, 2015 Chapter 2: Data and Expressions CS 121 1 / 53 Chapter 2 Part 1: Data Types

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba Laboratory Session: Exercises on classes Analogy to help you understand classes and their contents. Suppose you want to drive a car and make it go faster by pressing down

More information

4. Java language basics: Function. Minhaeng Lee

4. Java language basics: Function. Minhaeng Lee 4. Java language basics: Function Minhaeng Lee Review : loop Program print from 20 to 10 (reverse order) While/for Program print from 1, 3, 5, 7.. 21 (two interval) Make a condition that make true only

More information

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

More information

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Lesson 02 Data Types and Statements MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Topics Covered Statements Variables Data Types Arithmetic

More information

8/25/17. Demo: Create application. CS2110, Recita.on 1. Arguments to method main, Packages, Wrapper Classes, Characters, Strings.

8/25/17. Demo: Create application. CS2110, Recita.on 1. Arguments to method main, Packages, Wrapper Classes, Characters, Strings. CS2110, Recita.on 1 Arguments to method main, Packages, Wrapper Classes, Characters, Strings Demo: Create application To create a new project that has a method called main with a body that contains the

More information

Final Exam. CSC 121 Fall 2015 TTH. Lecturer: Howard Rosenthal. Dec. 15, 2015

Final Exam. CSC 121 Fall 2015 TTH. Lecturer: Howard Rosenthal. Dec. 15, 2015 Final Exam. CSC 121 Fall 2015 TTH Lecturer: Howard Rosenthal Dec. 15, 2015 Your Name: Key The following questions (or parts of questions) in numbers 1-17 are all worth 2 points each. The programs have

More information

COMP-202 Unit 8: Basics of Using Objects

COMP-202 Unit 8: Basics of Using Objects COMP-202 Unit 8: Basics of Using Objects CONTENTS: Concepts: Classes and Objects Creating and Using Objects Introduction to Basic Java Classes (String, Scanner, ArrayList,...) Introduction (1) As we know,

More information

Introduction to Java Applications

Introduction to Java Applications 2 Introduction to Java Applications OBJECTIVES In this chapter you will learn: To write simple Java applications. To use input and output statements. Java s primitive types. Basic memory concepts. To use

More information

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Java application programming } Use tools from the JDK to compile and run programs. } Videos at www.deitel.com/books/jhtp9/ Help you get started

More information

Final Exam. CSC 121 Spring Lecturer: Howard Rosenthal. May 17, 2017

Final Exam. CSC 121 Spring Lecturer: Howard Rosenthal. May 17, 2017 Final Exam. CSC 121 Spring 2017 Lecturer: Howard Rosenthal May 17, 2017 Your Name: Answer Key The following questions (or parts of questions) in numbers 1-17 are all worth 2 points each. The programs have

More information

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II) CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II) Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs How to generate

More information

Chapter. Let's explore some other fundamental programming concepts

Chapter. Let's explore some other fundamental programming concepts Data and Expressions 2 Chapter 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design 2007 Pearson Addison-Wesley. All rights reserved Data and Expressions Let's explore some

More information

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a Spring 2017 Name: TUID: Page Points Score 1 28 2 18 3 12 4 12 5 15 6 15 Total: 100 Instructions The exam is closed book, closed notes. You may not use a calculator, cell phone, etc. i Some API Reminders

More information

CMPT 125: Lecture 3 Data and Expressions

CMPT 125: Lecture 3 Data and Expressions CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,

More information

CST242 Strings and Characters Page 1

CST242 Strings and Characters Page 1 CST242 Strings and Characters Page 1 1 2 3 4 5 6 Strings, Characters and Regular Expressions CST242 char and String Variables A char is a Java data type (a primitive numeric) that uses two bytes (16 bits)

More information

Reading Input from Text File

Reading Input from Text File Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Lab 5 Reading Input from Text File Eng. Mohammed Alokshiya November 2, 2014 The simplest

More information

More on variables and methods

More on variables and methods More on variables and methods Robots Learning to Program with Java Byron Weber Becker chapter 7 Announcements (Oct 12) Reading for Monday Ch 7.4-7.5 Program#5 out Character Data String is a java class

More information

Chapter 2 ELEMENTARY PROGRAMMING

Chapter 2 ELEMENTARY PROGRAMMING Chapter 2 ELEMENTARY PROGRAMMING Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk ١ Objectives To write Java programs to perform simple

More information

Full file at

Full file at Java Programming, Fifth Edition 2-1 Chapter 2 Using Data within a Program At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional

More information

Lecture Notes for CS 150 Fall 2009; Version 0.5

Lecture Notes for CS 150 Fall 2009; Version 0.5 for CS 150 Fall 2009; Version 0.5 Draft! Do not distribute without prior permission. Copyright 2001-2009 by Mark Holliday Comments, corrections, and other feedback appreciated holliday@email.wcu.edu Chapter

More information

COMP-202 Unit 5: Basics of Using Objects

COMP-202 Unit 5: Basics of Using Objects COMP-202 Unit 5: Basics of Using Objects CONTENTS: Concepts: Classes, Objects, and Methods Creating and Using Objects Introduction to Basic Java Classes (String, Random, Scanner, Math...) Introduction

More information

Primitive Data Types: Intro

Primitive Data Types: Intro Primitive Data Types: Intro Primitive data types represent single values and are built into a language Java primitive numeric data types: 1. Integral types (a) byte (b) int (c) short (d) long 2. Real types

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All rights reserved. Java application A computer program that executes when you use the java command to launch the Java Virtual Machine

More information

Text. Text Actions. String Contains

Text. Text Actions. String Contains Text The Text Actions are intended to refine the texts acquired during other actions, for example, from web-elements, remove unnecessary blank spaces, check, if the text matches the defined content; and

More information

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003 Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 Java Programs A Java program contains at least one class definition. public class Hello { public static void

More information

A Java program contains at least one class definition.

A Java program contains at least one class definition. Java Programs Identifiers Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 A Java program contains at least one class definition. public class Hello { public

More information

Methods CSC 121 Fall 2016 Howard Rosenthal

Methods CSC 121 Fall 2016 Howard Rosenthal Methods CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods

More information

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016 First Name (Print): Last Name (Print): Student Number: The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II 2016 Instructor: Dr. Bowen Hui Tuesday, April 19, 2016 Time: 6:00pm

More information

Methods CSC 121 Spring 2017 Howard Rosenthal

Methods CSC 121 Spring 2017 Howard Rosenthal Methods CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods

More information

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming Part I 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Assignments Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Lecture 6 Complete for Lab 4, Project 1 Note: Slides 12 19 are summary slides for Chapter 2. They overview much of what we covered but are not complete.

More information

2.5 Another Application: Adding Integers

2.5 Another Application: Adding Integers 2.5 Another Application: Adding Integers 47 Lines 9 10 represent only one statement. Java allows large statements to be split over many lines. We indent line 10 to indicate that it s a continuation of

More information

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics Java Programming, Sixth Edition 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional Projects Additional

More information

CS 106A, Lecture 9 Problem-Solving with Strings

CS 106A, Lecture 9 Problem-Solving with Strings CS 106A, Lecture 9 Problem-Solving with Strings suggested reading: Java Ch. 8.5 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5

More information

String. Other languages that implement strings as character arrays

String. Other languages that implement strings as character arrays 1. length() 2. tostring() 3. charat() 4. getchars() 5. getbytes() 6. tochararray() 7. equals() 8. equalsignorecase() 9. regionmatches() 10. startswith() 11. endswith() 12. compareto() 13. indexof() 14.

More information