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

Size: px
Start display at page:

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

Transcription

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

2 Lesson Goals Understand objects and classes Understand Encapsulation Learn about additional Java classes The Random class The String class The StringBuilder class The File Class The DecimalFormat class 2

3 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 strings Object oriented programming is all about manipulating objects to perform tasks this matches the way we really think about things 3

4 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 4

5 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. 5

6 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). 6

7 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." 7

8 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 8

9 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) and they start "doing things" by running their 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. 9

10 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. 10

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

12 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 12

13 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 import java.packagename.* import java.util.* 13

14 The Random class(1) The Random class is another class that is imported with the import java.util.* It has a many more methods and is therefore more useful than the Math.random() method (even though that basic Math package did not have to be imported 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: number = keyboard.nextint(); dnumber = keyboard.nextdouble(); 14

15 The Random class(2) So let create a new object called randy from the class Random Random randy = new Random(); // note that there are no data attributes, also, that randy is a reference variable that now has a value. Then we can use the methods of Random: 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: referencetoobject.method 15

16 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. 16

17 The Random class(4) An example with dice Random die = new Random(); int dice = (die.nextint(6)+1) + (die.nextint(6)+1); Those two lines would give us the sum of the two dice rolled You could create a coin object and then flip it. 17

18 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 18

19 The String Class The String class allows you to create string objects a String object is not a 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 19

20 The String Class (2) There are many methods that are available on a String object int length() returns the String length: mydog.length() returns 4 An empty String has no characters and length 0 char charat(int i) returns the character at position i (remember we start counting from 0): mydog.charat(2) = d String next() returns a reference to the next input string i.e. keyboard.next() skips whitespaces and returns the next string of characters 20

21 String Concatenation Concatenation is the process of joining or linking strings together The operator is + We ve already seen concatenation: System.out.println( A boy who is +age + is a man ); When you concatenate you create a new reference address String s = Sam String t = Thomas String w = s + +t = Sam Thomas w.length = 10 21

22 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 22

23 String Operations String touppercase() converts a string from lower to upper case String s1 = lower String s2 = s1.touppercase(); System.out.println(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 23

24 String Operations substring (1) There are two versions of substring substring( int from ) substring( int from, int to ) Create a new object that contains the characters of the method's string from the index to the end of the string. Create a new object that contains the characters of the method's string from the index to index to-1. 24

25 String Operations substring (2) Line of code: String line = "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 ); New String buttercup 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 25

26 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 26

27 String Operations 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 characters (a-z) and print the result as all capitals. See CaesarCipher 27

28 More on Strings and String Methods (1) String encrypt(string msg, int shift) really is accepting a String reference, not the string itself Strings are immutable You can change the value in the reference pointer, but not the string itself String s = Big ; s = s.tolowercase(); s Big big 28

29 More on Strings and String Methods (2) Be careful with == and strings. you are comparing references, not strings themselves use boolean equals(str1) to compare 2 strings strx.equals(str1) In the example: String s = new String( ABC ); String t = new String( ABC ); s ==t is not true s.equals(t) is true 29

30 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. Programs often set variables to null when they are declared: String a = null; class NullDemo1 { public static void main (String[] arg) { String a = "Random Jottings"; String b = null; String c = ""; if ( a!= null ) System.out.println( a ); if ( b!= null ) System.out.println( b ); if ( c!= null ) System.out.println( c ); } } 30

31 The nextline() Method (1) nextline() returns the remaining information on the current line, and returns the information that was skipped System.out.print( Enter message on one line: ); String message = keyboard.nextline(); System.out.print( Enter an integer in the range 0-25 ); int shift = keyboard.nextint(); If the inputs are Cato 8 then message points to Cato and shift gets the value 8. 31

32 The nextline() Method (2) However, if you reverse the process: System.out.print( Enter an integer in the range 0-25 ); int shift = keyboard.nextint(); System.out.print( 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 The nextline() then see nothing prior to the line return and returns an empty string 32

33 The StringBuilder Class (1) StringBuilder is a different class than String, even though they do many of the same things With the StringBuilder Class you do not create a new object with each alteration of the strings The StringBuilder class may improve performance for concatenation and modification The String class is preferable and more efficient for read-only strings While there are some similar methods, remember that the type of object determines which method will be executed and that some overloaded method may execute differently A complete list of StringBuilder Methods can be found at: Also uploaded into references 33

34 The StringBuilder Class (2) To create a StringBuilder object: StringBuilder sb = newstringbuilder(); //initial capacity is 16 characters StringBuilder sb = newstringbuilder(50); //initial capacity is 50 characters StringBuilder sb = newstringbuilder( Hello ); //initializes sb to Hello The capacity of a StringBuilder object expands automatically 34

35 The StringBuilder Class (3) How the equals method works for StringBuilder vs. String String s = new String( Monkey Business ); String t = new String( Monkey Business ); System.out.println(s.equals(t)); In the case above true is printed out StringBuilder s = new StringBuilder( Monkey Business ); StringBuilder t = new StringBuilder( Monkey Business ); System.out.println(s.equals(t)); In the case above false is printed out 35

36 The Mysterious String [] args String [] args is simply an array of strings It allows you to send inputs to the main method via the command line See the TestMain application 36

37 Files A File is a collection of data saved under a single name Files can be structured or free text Java creates File objects Once a File is instantiated you can read or write from the file This is very convenient when testing programs, since you can use the same data over and over again 37

38 Input and Output There are many data sources and destinations that can be used 38

39 Processing Streams A processing stream operates on the data supplied by another stream. Often a processing stream acts as a buffer for the data coming from another stream. Character streams are intended exclusively for character data. Byte streams are intended for general purpose input and output. A buffer is a block of main memory used as a work area. For example, disks usually deliver data in blocks of 512 bytes, no matter how few bytes a program has asked for. Usually the blocks of data are buffered and delivered from the buffer to the program in the amount the program asked for. 39

40 Using Files in java Programs (1) You must include the following statement before the class statement import java.io.* You must also include the statement throws IOException after the parentheses of any method that uses File class methods: public static void main (String [] args) throws IOException You must also create a file in the program using the new parameter File inputfile = new File( filename ); If the filename isn t in the same directory as the class file you must use the full path name Finally create a Scanner for the new input file Scanner input = new Scanner(inputFile); Once this is done you can use the same input functions as you have used previously See TestFile1 program 40

41 Using Files in java Programs (2) Using an output file involves almost the same process File outputfile = new File(filename); PrintWriter output = new PrintWriter(outputFile); Now you can use output to write to the file i.e. output.println((message +intvar + is whatever ); See EncryptFile program to demonstrate all these principles 41

42 The DecimalFormat Class (1) Allows you to easily format decimal number by pattern pattern looks like: 0.### The # tells you how many places to display Need to create a separate formatter for each pattern Use import java.text.* to use the DecimalFormat class 42

43 The DecimalFormat Class (2) DecimalFormat myformat = new DecimalFormat( 0o.### ); double x = ; double y =.9876 System.out.println(myformat.format(x) + + myformat.format (y)); myformat.applypattern( ##.## ); // changes the pattern System.out.println(myformat.format (x) + + myformat.format (y)); output: Note: Decimal Format always rounds (up or down) 43

44 The DecimalFormat Class (3) value of double format pattern output string ###.###" " " ###.#" "123.5" ###" "123" ###" " " " " " " "0.00" "89.01" "0." "89." " " " " "000.0" "-123.5" "0.00" "-89.01" "0." "-89." -1.1 " " " " 0.0" "- 89.0" 44

45 Programming Exercises Class (1) Exercise 2. Uppercase Conversion 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 &&####!!!! 45

46 Programming Exercises Class (2) Exercise 8. Counting Words 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. 46

47 Programming Exercises Lab (1) Exercise 3. Random Strings Write a program that print 25 random strings of length 4 such that each string is composed of uppercase alphabetical characters. 47

48 Programming Exercises Lab (2) Exercise 12. College Transcript A text file stores the courses you have taken along with the corresponding grade (A,B,C, D, F) that you received in each course. The file might look like: Intro to Sociology Physics Experimental Psych o o o A B C Write a program that uses such a file to calculate a GPA. Print the GPA with two decimal places. 48

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 Classes Strings Review CSC 123 Fall 2018 Howard Rosenthal

Objects Classes Strings Review CSC 123 Fall 2018 Howard Rosenthal Objects Classes Strings Review CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Review objects and classes Review Encapsulation Review the Random class Review the String class Review arrays of String objects

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

File Input and Output Review CSC 123 Fall 2018 Howard Rosenthal

File Input and Output Review CSC 123 Fall 2018 Howard Rosenthal File Input and Output Review CSC 123 Fall 218 Howard Rosenthal Lesson Goals The File Class creating a File object Review FileWriter for appending to files Creating a Scanner object to read from a File

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java Java Data types and input/output Sharma Chakravarthy Information Technology Laboratory (IT Lab) Computer Science and Engineering Department The University of Texas at Arlington, Arlington, TX 76019 Email:

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

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

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

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

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

Computational Expression

Computational Expression Computational Expression Scanner, Increment/Decrement, Conversion Janyl Jumadinova 17 September, 2018 Janyl Jumadinova Computational Expression 17 September, 2018 1 / 11 Review: Scanner The Scanner class

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

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

Fundamentals of Programming Data Types & Methods

Fundamentals of Programming Data Types & Methods Fundamentals of Programming Data Types & Methods By Budditha Hettige Overview Summary (Previous Lesson) Java Data types Default values Variables Input data from keyboard Display results Methods Operators

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

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

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

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

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

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

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

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

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

Lab5. Wooseok Kim

Lab5. Wooseok Kim Lab5 Wooseok Kim wkim3@albany.edu www.cs.albany.edu/~wooseok/201 Question Answer Points 1 A or B 8 2 A 8 3 D 8 4 20 5 for class 10 for main 5 points for output 5 D or E 8 6 B 8 7 1 15 8 D 8 9 C 8 10 B

More information

Important Java terminology

Important Java terminology 1 Important Java terminology The information we manage in a Java program is either represented as primitive data or as objects. Primitive data פרימיטיביים) (נתונים include common, fundamental values as

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

Elementary Programming

Elementary Programming Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Learning Outcomes Learn ingredients of elementary programming: data types [numbers, characters, strings] literal

More information

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

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

More information

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O 1 Sending Output to a (Text) File import java.util.scanner; import java.io.*; public class TextFileOutputDemo1 public static void

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

Text User Interfaces. Keyboard IO plus

Text User Interfaces. Keyboard IO plus Text User Interfaces Keyboard IO plus User Interface and Model Model: objects that solve problem at hand. User interface: interacts with user getting input from user giving output to user reporting on

More information

Lab5. Wooseok Kim

Lab5. Wooseok Kim Lab5 Wooseok Kim wkim3@albany.edu www.cs.albany.edu/~wooseok/201 Question Answer Points 1 A 8 2 A 8 3 E 8 4 D 8 5 20 5 for class 10 for main 5 points for output 6 A 8 7 B 8 8 0 15 9 D 8 10 B 8 Question

More information

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2017 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

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

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA 1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

More information

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value Lecture Notes 1. Comments a. /* */ b. // 2. Program Structures a. public class ComputeArea { public static void main(string[ ] args) { // input radius // compute area algorithm // output area Actions to

More information

Computer Science 300 Sample Exam Today s Date 100 points (XX% of final grade) Instructor Name(s) (Family) Last Name: (Given) First Name:

Computer Science 300 Sample Exam Today s Date 100 points (XX% of final grade) Instructor Name(s) (Family) Last Name: (Given) First Name: Computer Science 300 Sample Exam Today s Date 100 points (XX% of final grade) Instructor Name(s) (Family) Last Name: (Given) First Name: CS Login Name: NetID (email): @wisc.edu Circle your Lecture: Lec001

More information

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time Tester vs. Controller Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG For effective illustrations, code examples will mostly be written in the form of a tester

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

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

St. Edmund Preparatory High School Brooklyn, NY

St. Edmund Preparatory High School Brooklyn, NY AP Computer Science Mr. A. Pinnavaia Summer Assignment St. Edmund Preparatory High School Name: I know it has been about 7 months since you last thought about programming. It s ok. I wouldn t want to think

More information

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process Entry Point of Execution: the main Method Elementary Programming EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG For now, all your programming exercises will

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

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we

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

Section 2.2 Your First Program in Java: Printing a Line of Text

Section 2.2 Your First Program in Java: Printing a Line of Text Chapter 2 Introduction to Java Applications Section 2.2 Your First Program in Java: Printing a Line of Text 2.2 Q1: End-of-line comments that should be ignored by the compiler are denoted using a. Two

More information

ing execution. That way, new results can be computed each time the Class The Scanner

ing execution. That way, new results can be computed each time the Class The Scanner ing execution. That way, new results can be computed each time the run, depending on the data that is entered. The Scanner Class The Scanner class, which is part of the standard Java class provides convenient

More information

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation. Chapter 4 Lab Loops and Files Lab Objectives Be able to convert an algorithm using control structures into Java Be able to write a while loop Be able to write an do-while loop Be able to write a for loop

More information

Object Oriented Programming. Java-Lecture 1

Object Oriented Programming. Java-Lecture 1 Object Oriented Programming Java-Lecture 1 Standard output System.out is known as the standard output object Methods to display text onto the standard output System.out.print prints text onto the screen

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

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

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

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes Entry Point of Execution: the main Method Elementary Programming EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG For now, all your programming exercises will be defined within the

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

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

BASIC INPUT/OUTPUT. Fundamentals of Computer Science BASIC INPUT/OUTPUT Fundamentals of Computer Science Outline: Basic Input/Output Screen Output Keyboard Input Simple Screen Output System.out.println("The count is " + count); Outputs the sting literal

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

COMP 110 Programming Exercise: Simulation of the Game of Craps

COMP 110 Programming Exercise: Simulation of the Game of Craps COMP 110 Programming Exercise: Simulation of the Game of Craps Craps is a game of chance played by rolling two dice for a series of rolls and placing bets on the outcomes. The background on probability,

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

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

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

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

Lesson 3: Accepting User Input and Using Different Methods for Output

Lesson 3: Accepting User Input and Using Different Methods for Output Lesson 3: Accepting User Input and Using Different Methods for Output Introduction So far, you have had an overview of the basics in Java. This document will discuss how to put some power in your program

More information

AP Programming - Chapter 6 Lecture

AP Programming - Chapter 6 Lecture page 1 of 21 The while Statement, Types of Loops, Looping Subtasks, Nested Loops I. The while Statement Note: Loop - a control structure that causes a sequence of statement(s) to be executed repeatedly.

More information

Introduction to Java Unit 1. Using BlueJ to Write Programs

Introduction to Java Unit 1. Using BlueJ to Write Programs Introduction to Java Unit 1. Using BlueJ to Write Programs 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

Simple Java Input/Output

Simple Java Input/Output Simple Java Input/Output Prologue They say you can hold seven plus or minus two pieces of information in your mind. I can t remember how to open files in Java. I ve written chapters on it. I ve done it

More information

CS 101 Fall 2006 Midterm 1 Name: ID:

CS 101 Fall 2006 Midterm 1 Name:  ID: You only need to write your name and e-mail ID on the first page. This exam is CLOSED text book, closed-notes, closed-calculator, closed-neighbor, etc. Questions are worth different amounts, so be sure

More information

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette COMP 250: Java Programming I Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette Variables and types [Downey Ch 2] Variable: temporary storage location in memory.

More information

Robots. Byron Weber Becker. chapter 6

Robots. Byron Weber Becker. chapter 6 Using Variables Robots Learning to Program with Java Byron Weber Becker chapter 6 Announcements (Oct 5) Chapter 6 You don t have to spend much time on graphics in Ch6 Just grasp the concept Reminder: Reading

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

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements More Things We Can Do With It! More operators and expression types More s 11 October 2007 Ariel Shamir 1 Overview Variables and declaration More operators and expressions String type and getting input

More information

JAVA Programming Fundamentals

JAVA Programming Fundamentals Chapter 4 JAVA Programming Fundamentals By: Deepak Bhinde PGT Comp.Sc. JAVA character set Character set is a set of valid characters that a language can recognize. It may be any letter, digit or any symbol

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

Variables and Assignments CSC 121 Fall 2015 Howard Rosenthal

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

More information

Chapter 2. Elementary Programming

Chapter 2. Elementary Programming Chapter 2 Elementary Programming 1 Objectives To write Java programs to perform simple calculations To obtain input from the console using the Scanner class To use identifiers to name variables, constants,

More information

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled Interpreted vs Compiled Python 1 Java Interpreted Easy to run and test Quicker prototyping Program runs slower Compiled Execution time faster Virtual Machine compiled code portable Java Compile > javac

More information

First Java Program - Output to the Screen

First Java Program - Output to the Screen First Java Program - Output to the Screen These notes are written assuming that the reader has never programmed in Java, but has programmed in another language in the past. In any language, one of the

More information

Formatting Output & Enumerated Types & Wrapper Classes

Formatting Output & Enumerated Types & Wrapper Classes Formatting Output & Enumerated Types & Wrapper Classes Quick review of last lecture September 8, 2006 ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor: Alexander Stoytchev

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information