The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.

Slides:



Advertisements
Similar presentations
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
INF 523Q Chapter 2: Objects and Primitive Data (Examples)
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology- George Koutsogiannakis.
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Example: Solving Quadratic Equations Example:
***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Class Libraries & API’s A class library is a set of classes that supports the development of programs. Java.
Computer Programming Lab(4).
Using Classes and Objects Chapters 3 Section 3.3 Packages Section 3.4 Random Class Section 3.5 Math Class Section 3.7 Enumerated Types Instructor: Scott.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
Objects and Classes Plan for Today - we will learn about: the difference between objects and primitive values how to create (or construct) objects some.
COS 312 DAY 3 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 1 DUE right now Assignment 2 Posted – Due Feb 5 prior to class Using Classes and Objects.
Chapter 3 Using Classes and Objects. Chapter Scope Creating objects Services of the String class The Java API class library The Random and Math classes.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Topics Chapter 2: –Data conversion Chapter 3 –Object creation and object references –The String class and its methods –The Java standard class library.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
Math class services (functions) Primitive vs reference data types Scanner class Math class services (functions) Primitive vs reference data types Scanner.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data Animated Version.
Topics Class Basics and Benefits Creating Objects Using Constructors Calling Methods Using Object References Calling Static Methods and Using Static Class.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
***** SWTJC STEM ***** Chapter 3-1 cg 39 Math Class The Math class provides a convenient way to access higher math functions. The Math class is automatically.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Programming in Java (COP 2250) Lecture 9 Chengyong Yang Fall, 2005.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
CSE 1201 Object Oriented Programming Using Classes and Objects.
Variables and Types Java Part 3. Class Fields  Aka member variable, field variable, instance variable, class variable.  These are the descriptors of.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
COS 312 DAY 3 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 1 posted – Due Feb 2PM Assignment 2 Posted – Due Feb 2 PM Finish Data and Expressions.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
1 CMPT 126 Java Basics Using Classes and Objects.
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
Introduction to programming in java
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
(Dreaded) Quiz 2 Next Monday.
Formatting Output & Enumerated Types & Wrapper Classes
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
Chapter 3 Java Input/Output.
Classes, Libraries & Packages
Chapter 3: Using Classes and Objects
Something about Java Introduction to Problem Solving and Programming 1.
Introduction to Classes and Methods
Chapter 3 Input/Output.
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Introduction to Java Brief history of Java Sample Java Program
Math class services (functions)
Random Numbers while loop
Presentation transcript:

The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy Code”); Type Variable new class parameter Name name

String Class String is a special class. Sometimes JAVA treats it as a primitive. For instance, you can declare a String like you do a primitive: String myString = “Crazy Code”; ************************************************************ The string class has many methods. For instance, myString.length() returns the length of the string; System.out.println(myString.length()); //outputs 10 here

String Class Strings are also a special type of object because each time we change a string, we actually create a new one. This doesn’t matter much now, but will be a concern when we start making comparisons or passing parameters. *************************************************** Strings are indexed from 0 to length-1. Index HELLO!

String Class Look at the methods on page 84 and try to determine what this code does: String name = “Hello!”; name = name.substring(0, 5); name = name + “ Dolly”; System.out.println(name); HELLO!

String Class a) Listing 2.9 for practice b) Write a program that declares a string, prints out the string length using length(), finds the index of a substring within the string, and returns a substring from the beginning of the original string to the found index.

Wrappers Integer(int value); Double(double value); These store primitives as an object. You are expected to know the methods: comparTo; equals, intValue, toString and doubleValue for these. We’ll use these more later.

Random Class Used to generate psuedorandom numbers. First we create a new Random object. Random generator = new Random;

Random Class Here are some examples of using the generator: Random r = new Random(); int num; double dNum; num = r.nextInt(10) //generates between 0-9 num = r.nextInt(10)+1 // 1 – 10 dNum = r.nextDouble() // between 0 to 1 Dnum = r.nextDouble()*6 // between 0.0 and 5.999… Dnum = (int) r.nextDouble()*6 + 1 // 1 to 6

Random Class Write a program that simulates rolling a pair of dice and outputting the sum.

The Scanner Class Creates Scanner objects that input data from a device Declaration: Scanner scan = new Scanner(System.in); //inputs from the default input system

The Scanner Class nextInt() returns next integer value from the device. int I = scan.nextInt(); //reads an integer from the input device double r = scan nextDouble(); //reads a double Make sure you print something to open a window before calling Scanner methods for keyboard input.

The Scanner Class Example Scanner scan = new Scanner(System.in); System.out.println(“Enter an integer: “); int first = scan.nextInt(); System.out.println(“Enter another integer :”); int second = scan.nextInt(); System.out.println(“The sum is :” + (first+second));

The Scanner Class Example import java.util.Scanner; public class AddEm() { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println(“Enter an integer: “); int first = scan.nextInt(); System.out.println(“Enter another integer :”); int second = scan.nextInt(); System.out.println(“The sum is :” + (first+second)); }

Assignment Page 119 Programming projects. 2.2 – 2.4

The Math Class Accesses various mathematical methods: Absolute value, trig functions, inverse trig functions, exponential, ceiling, floor, powers, random (different from Random class) and square root. The Math class is accessed through static calls, for example Math.sqrt(4)

The Math Class Example: double a = 3; double b = 4; double c = sqrt(Math.pow(a,2)+Math.pow(a,2)); What is the value of c?

The Math Class Assignment: Page

DecimalFormat Class DecimalFormat formats decimal output: DecimalFormat hundredths = new DecimalFormat(“0.##”); //formats output to two decimal places. System.out.println(“Answer :”+hundredths.format( )); //prints out 3.23

DecimalFormat Class import java.text.DecimalFormat; public class TestFormat { public static void main(String[] args) { DecimalFormat hundredths = new DecimalFormat(“0.##”): System.out.println(hundredths.format( )); }

DecimalFormat Class Programming projects page , 2.10

NumberFormat Class Prints numbers in different formats: getCurrencyInstance() used to generate dollar format getPercentInstance() used to generate percent format Import java.text.NumberFormat Do initialize using new. Do this instead: NumberFormat money = NumberFormat.getCurrencyInstance() NumberFormat percent = NumberFormat.getPercentInstance();

NumberFormat Class NumberFormat money = NumberFormat.getCurrencyInstance(); System.out.println(“Ten Dollars :”+money.format(10.00)); Prints out $10.00 NumberFormat perc = NumberFormat.getPercentInstance(); System.out.println(perc.format(10.00)); Prints 1,000%

NumberFormat Class import java.text.NumberFormat; public class StringMeths { public static void main(String[] args) { NumberFormat cash = NumberFormat.getCurrencyInstance(); System.out.println(cash.format(10.00)); NumberFormat perc = NumberFormat.getPercentInstance(); System.out.println(perc.format(10.00)); }

NumberFormat Class Page