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

Slides:



Advertisements
Similar presentations
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Objects, Methods, Parameters, Input Lecture 5, Thu Jan
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Aalborg Media Lab 17-Jun-15 Software Design Lecture 4 “ Using Classes & Objects”
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.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
INF 523Q Chapter 2: Objects and Primitive Data (Examples)
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
Expressions, Data Conversion, and Input
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 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.
1 The String Class Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks,
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
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.
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.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Introduction to Programming
CSC 1051 M.A. Papalaskari, Villanova University CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
Using Classes and Objects Chapters 3 Creating Objects – Section 3.1 The String Class – Section 3.2 The Scanner Class – Section 2.6 Instructor: Scott Kristjanson.
© 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.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Using Classes and Objects (Chapter 3) Copyright © 2012 Pearson Education, Inc.
CSCI 51 Introduction to Computer Science Joshua Stough February 3, 2009.
1 Chap. 3 Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L, 3.1 – 3.6.
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.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
1 CMPT 126 Java Basics Using Classes and Objects.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
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
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Variables in Java A variable holds either
Formatting Output & Enumerated Types & Wrapper Classes
Project 1.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Objectives You should be able to describe: Interactive Keyboard Input
CSC 1051 – Data Structures and Algorithms I
Maha AlSaif Maryam AlQattan
Classes, Libraries & Packages
Something about Java Introduction to Problem Solving and Programming 1.
Introduction to Classes and Methods
Introduction to Java Brief history of Java Sample Java Program
Outline Creating Objects The String Class The Random and Math Classes
Chapter 3 Using Classes and Objects
Presentation transcript:

***** 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 comes with a standard class library, but libraries can be obtained separately from third party vendors. Classes in class libraries contain methods that are valuable to the programmer. API’s (Application Programming Interfaces) are clusters of related classes. Java Database API contains classes that help programmers use databases Java Swing API contains classes that help programmers use graphical components.

***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Packages Classes are also grouped in packages. Packages are more fundamental and language based than API’s. See text appendix “M” for classes and packages. Example packages from Java’s standard class library: PackageClassUse java.utilScanner Random Inputting from keyboard and files Generates random numbers java.langMath String Various math constants and functions Handling text java.textDecimalFormatFormatting decimal numbers

***** SWTJC STEM ***** Chapter 3-1 cg Java Class Packages To use a package in a program, it must first be imported. Add the import declaration at the beginning of a program, after the package statement and before any other executable code. import packageName.*; Imports all classes in packageName. import packageName.className Imports on className from packageName Example: import java.text.DecimalFormat; Imports only the DecimalFormat class from the java.text package. The java.lang package does not have to be imported. String and Math methods are always available.

***** SWTJC STEM ***** Chapter 3-1 cg Declaring An Object To declare an object, use this statement: ClassName objectName = new ClassName (parameters); Where: ClassName is the name of the class of the object. objectName is your choice of name for the variable object. “=“ is the assignment operator. new is a Java reserved word. ( parameters) is a set of data values use to initialize or setup the object. Except for Math class objects, all others must be declared before they are used.

***** SWTJC STEM ***** Chapter 3-1 cg 35 String Class The String class provides a convenient way to manage and manipulate text. The String class is in the java.lang package and is automatically imported. See the chart below for a partial summary of available methods or appendix “M” for a complete list: String ManipulationMethod length of stringlength() character at position ncharAt(int index) find substring: start m and end n-1substring(int m, int n) change to upper casetoUpperCase()

***** SWTJC STEM ***** Chapter 3-1 cg 35 String Class Ex. // Variable object declaration String phrase = "Change is inevitable"; String mutation1, mutation2, mutation3, mutation4; System.out.println ("Original string: \"" + phrase + "\""); System.out.println ("Length of string: " + phrase.length());... Outputs Original string: "Change is inevitable" Length of string: 20 Example from Program “StringMutation” (See text CDROM Chapter 3)

***** SWTJC STEM ***** Chapter 3-1 cg 35 String Class Ex. mutation1 = phrase.concat (", except from vending machines."); mutation2 = mutation1.toUpperCase(); mutation3 = mutation2.replace ('E', 'X'); mutation4 = mutation3.substring (3, 30);...Outputs Mutation #1: Change is inevitable, except from vending machines. Mutation #2: CHANGE IS INEVITABLE, EXCEPT FROM VENDING MACHINES. Mutation #3: CHANGX IS INXVITABLX, XXCXPT FROM VXNDING MACHINXS. Mutation #4: NGX IS INXVITABLX, XXCXPT F Example from Program “StringMutation” (See text CDROM Chapter 3)

***** SWTJC STEM ***** Chapter 3-1 cg 32 Scanner Class The Scanner class provides a convenient way to read input values of various types. The Scanner class accepts input from various sources including the keyboard, a file, or a string. Must be imported from java.util package For keyboard, use “System.in” in the constructor. See the chart below for a partial summary of available methods or appendix “M” for a complete list: Data TypeMethod StringnextLine() BytenextByte() IntegernextInteger() DoublenextDouble()

***** SWTJC STEM ***** Chapter 3-1 cg 32 Scanner Class An object declaration looks like this: Scanner scan = new Scanner (System.in); Declares a “new” object variable named “scan” (your choice of object variable name). The Scanner class has a special constructor method that constructs or creates this new object. The parameter “System.in” tells the constructor to use the PC’s keyboard to accept data entry. The input stream may contain more than one data item, called a token. Tokens are separated by white space, the default delimiter. To change the delimiter use useDelimiter(String pattern). Example: scan.useDelimiter(“,”); changes the delimiter to a comma.

***** SWTJC STEM ***** Chapter 3-1 cg 32 Scanner Class Ex. 1 Example Program “Echo” (See text CDROM Chapter 2) //****************************************************** // Echo.java Author: Lewis/Loftus // // Demonstrates the use of the nextLine method of the Scanner class // to read a string of text from the user. //****************************************************** import java.util.Scanner; // First import the Scanner class Class Package Dot Operator

***** SWTJC STEM ***** Chapter 3-1 cg 32 Scanner Class Ex. 1 Example 1 Program “Echo” public class Echo { // // Reads a character string from the user and prints it. // public static void main (String[] args) { String message; Scanner scan = new Scanner (System.in); // Class objectName = new Class (constructor parameters); System.out.println ("Enter a line of text:"); message = scan.nextLine(); System.out.println ("You entered: \"" + message + "\""); } ObjectMethod

***** SWTJC STEM ***** Chapter 3-1 cg 32 Scanner Class Ex. 2 import java.util.Scanner; public class CalculateSum { public static void main(String[] args) { double firstNum, secondNum, sum; Scanner scan = new Scanner(System.in); System.out.println("Enter first number to add:"); firstNum = scan.nextDouble(); System.out.println("Enter second number to add:"); secondNum = scan.nextDouble(); sum = firstNum + secondNum; System.out.println(firstNum + " + " + secondNum + " = " + sum ); } Example 2 Program “CalculateSum” Input tokens on separate line.

***** SWTJC STEM ***** Chapter 3-1 cg 32 Scanner Class Ex. 3 import java.util.Scanner; public class CalculateSumMultiple { public static void main(String[] args) { double firstNum, secondNum, sum; Scanner scan = new Scanner(System.in); System.out.println("Enter first and second number to add:"); firstNum = scan.nextDouble(); secondNum = scan.nextDouble(); sum = firstNum + secondNum; System.out.println(firstNum + " + " + secondNum + " = " + sum ); } Example 3 Program “CalculateSumMultiple” Input tokens on same line with white space as delimiter.

***** SWTJC STEM ***** Chapter 2 cg 16 Scanner Class Ex. 4 import java.util.Scanner; public class Average2Nums { public static void main(String[] args) { int firstNum, secondNum; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter first number to average:"); firstNum = scan.nextInt(); System.out.println("Enter second number to average:"); secondNum = scan.nextInt(); average = (double) (firstNum + secondNum) / 2; System.out.println("Average of " + firstNum + " and " + secondNum + " is " + average ); } Designates the keyboard Casts the sum to double before dividing by 2

***** SWTJC STEM ***** Chapter 2 cg 16 Scanner Class Ex. 5 import java.util.Scanner; public class ConvertSeconds { public static void main(String[] args) { int totalSeconds, hours, minutes, seconds; Scanner scan = new Scanner(System.in); totalSeconds = scan.nextInt(); seconds = totalSeconds; hours = seconds / 3600; // Integer division seconds = seconds % 3600; //modulo minutes = seconds / 60; seconds = seconds % 60; System.out.println(totalSeconds + " sec is " + hours + " hours, " + minutes + " minutes, and " + seconds + " seconds"); } Why? Because hours & seconds are integer!