CS 211: Existing Classes in the Java Library

Similar documents
CSCI 1103: File I/O, Scanner, PrintWriter

CSCI 1103: File I/O, Scanner, PrintWriter

Reading Input from Text File

Object Oriented Programming. Java-Lecture 1

COMP200 INPUT/OUTPUT. OOP using Java, based on slides by Shayan Javed

Fundamentals of Programming Data Types & Methods

CS 211: Potpourri Enums, Packages, Unit Tests, Multi-dimensional Arrays Command Line Args

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination

AP Computer Science Unit 1. Programs

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. File Input and Output

A token is a sequence of characters not including any whitespace.

Programming with Java

Introduction to Java Unit 1. Using BlueJ to Write Programs

CS 211: Methods, Memory, Equality

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

Java Libraries. Lecture 6 CGS 3416 Fall September 21, 2015

Chap. 3. Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L,

Classes and Objects Part 1

First Java Program - Output to the Screen

CSE1720 Delegation Concepts (Ch 2)

Adam Blank Lecture 2 Winter 2019 CS 2. Introduction to Programming Methods

Lecture 8 " INPUT " Instructor: Craig Duckett

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

Chapter 4 Classes in the Java Class Libraries

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

Object-Based Programming. Programming with Objects

Chapter 2: Data and Expressions

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

Faculty of Science COMP-202A - Foundations of Computing (Fall 2013) - All Sections Midterm Examination

Midterms Save the Dates!

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

Building Java Programs

COMP6700/2140 Std. Lib., I/O

Chapter 2: Data and Expressions

CS 112 Introduction to Programming

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Java Input/Output. 11 April 2013 OSU CSE 1

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

Chapter 6 Lab Classes and Objects

COMP 250 Winter 2011 Reading: Java background January 5, 2011

CS 151. Exceptions & Javadoc. slides available on course website. Sunday, September 9, 12

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

4. Java Project Design, Input Methods

File I/O Array Basics For-each loop

Classes and Objects Miscellany: I/O, Statics, Wrappers & Packages. CMSC 202H (Honors Section) John Park

CS111: PROGRAMMING LANGUAGE II

AP Computer Science A

COMP-202 More Complex OOP

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

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Text User Interfaces. Keyboard IO plus

Welcome to the Using Objects lab!

A Quick and Dirty Overview of Java and. Java Programming

Welcome to the Using Objects lab!

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Midterms Save the Dates!

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

Computational Expression

COMP-202 Unit 8: Basics of Using Objects

Faculty of Science COMP-202A - Foundations of Computing (Fall 2015) - All Sections Midterm Examination

A+ Computer Science -

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Midterm Examination

Introduction to Java (All the Basic Stuff)

Basic Computation. Chapter 2

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

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Computer Programming, I. Laboratory Manual. Experiment #5. Strings & Text Files Input

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

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

Basic Computation. Chapter 2

CompSci 125 Lecture 05. Programming Style, String Class and Literals, Static Methods, Packages

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

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

Elementary Programming

Chapter 6 Lab Classes and Objects

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

File class in Java. Scanner reminder. File methods 10/28/14. File Input and Output (Savitch, Chapter 10)

Building Java Programs

double float char In a method: final typename variablename = expression ;

Building Java Programs

CS1083 Week 2: Arrays, ArrayList

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

AP CS Unit 3: Control Structures Notes

Chapter 2 ELEMENTARY PROGRAMMING

Computational Expression

Full file at

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

Final Exam Practice. Partial credit will be awarded.

Using Classes and Objects

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Using Java Classes Fall 2018 Margaret Reid-Miller

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

CS 231 Data Structures and Algorithms, Fall 2016

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Formatting Output & Enumerated Types & Wrapper Classes

Transcription:

CS 211: Existing Classes in the Java Library Chris Kauffman Week 3-2

Logisitics Logistics P1 Due tonight: Questions? Late policy? Lab 3 Exercises Thu/Fri Play with Scanner Introduce it today Goals Class Library and import Math, Array, String, Scanner Simple input from users/files Next week: Make Classes

Classes public class C { public static int f; public static void m(){ int i;...; } } // static field // static method // local variable All methods and fields in java live within a class (or interface) Classes are partly a namespace: a place for names to exist static methods and fields belong to the class: There is one for the whole class Consider CallCounts.java to see a demo of static methods and fields Draw some pictures to see how this looks in memory Class definitions live in global memory

Java Namespaces No such thing as a "global" variable No such thing as a "global" function Every variable in a scope Every scope in a class Every class in a package static means class-level There is one of it (method/field) It is not associated with any particular object It can be accessed through the class using dot Related concept: namespace, library system, package Access to stuff in classes is controlled public - everybody (use this for the first project) Soon private, protected, default

It s a packaged world Every method/field is part of a class or interface Every class/interface is part of a package The java library is divided into packages of related classes java.lang: Essentials, automatically imported (Math, String, Integer, Object) java.util: Mostly essential stuff (Scanner, Arrays) java.util.concurrent: For multiple processors javax.swing: GUI stuff (windows, buttons) There is a default package that unspecified classes live in Default package is screwy: other packages can t look inside Command line and DrJava don t care Eclipse and NetBeans probably do Pay attention to project specs

Mostly Static Classes java.lang.math Javadoc for Math class For mathy operations and a few useful constants Calculate the square root of pi? Calculate E to the 2.75 power? java.util.arrays Javadoc for Arrays class Useful ops for arrays Sort an array of doubles? of booleans? Nicely stringify an array of integers for printing? Answers are in the provided MathAndArrays.java

Math and Arrays are oddities Most classes in java don t consist of all static members. Most classes you do stuff like SomeClass s = new SomeClass(arg1,arg2); s.dosomething(arg3); But you ll never do Math m = new Math(); Arrays a = new Arrays(stuff); because these don t have a constructor

String: A Proper Object of Sequential Characters String s; s = new String("hello"); System.out.println(s); String t = "sweet stuff"; Keeps track of length int n = s.length(); int m = t.length() - 1;

Wait, that s really confusing A field length (for arrays) Versus a method length() (function for String) char ca[] = new char[10]; String s = "0123456789"; if(ca.length == s.length()){ System.out.println("Same size"); }

Methods of Strings Start reading Java Docs: http://docs.oracle.com/javase/8/docs/api/java/lang/string.html Look for the following methods Dynamically construct a string How to retrieve a specific character from a string How to concatenate two strings Determine if a string starts with another string How to pull a substring out of a string Compare two strings for equality? How to add characters onto the end of a string

Which brings me to my next point From http://www.codinghorror.com/blog/2006/09/ when-understanding-means-rewriting.html

Concatenation Diagram 1 What does String a = "hello"; String b = " world"; String c = a+b; actually do in memory? Diagram 2 How about String s = ""; for(int i=0; i<10; i++){ s = s + i; }

String Equality Show a memory Diagram String a = new String("hello"); String b = a; String c = new String("hello"); String d = a + ""; What is printed System.out.println(a == b); System.out.println(a.equals(b)); System.out.println(a == c); System.out.println(a.equals(c));

Scanner Sometimes you need input. Scanner is good for this. // Short demo of the scanner class for input import java.util.scanner; public class ScannerDemo{ public static void main(string args[]){ Scanner input = new Scanner(System.in); int i = input.nextint(); double d = input.nextdouble(); String s = input.next(); System.out.println("" + i + " " + d + " " +s); } } See the larger ScannerDemoBig.java for more info How do you know about all these methods for Scanner? What about String? or System? Where do you suppose mathematical functions are stored?

Constructors for Scanner Read from the Terminal // Constructs a new Scanner that produces values // scanned from the specified input stream. Scanner(InputStream source) Scanner in = new Scanner(System.in); Read from a String // Constructs a new Scanner that produces values // scanned from the specified string. Scanner(String source) Scanner in = new Scanner("Give me a 1 Give me a 2");

Scanner Basics In java.util Several constructors, for System.in and File sources nextint(), nextdouble(), next(), etc. nextline(): whole line hasnext(): true if more to read close(): when reading files

File class Lives in the package java.io import java.io.file; "An abstract representation of file and directory pathnames." How do we get at it? File f = new File("some-file.txt"); String s = "stuff.dat"; f = new File(s); Useful for many things but our primary interest at the moment is for reading from files using a Scanner

Reading from files with Scanner // Constructs a new Scanner that produces values // scanned from the specified file. Scanner(File source) open Scanner fin = new Scanner(new File("myfile.txt")) read int i = fin.nextint(); Frequently done in a loop close fin.close(); See ScanAFile.java

A Closing Problem: The Longest Word public static String longestword(scanner in) Takes an open Scanner Reads to the end of input Returns the longest word in the stream Ties go to earlier word Return "" if no words in the stream > import java.util.scanner; > LongestWord.longestWord(new Scanner("word1 word123")) "word123" > String s = "some gargantuan words and tiny words"; > Scanner in = new Scanner(s); > LongestWord.longestWord(in) "gargantuan" > LongestWord.longestWord(new Scanner(" ")) ""