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

Slides:



Advertisements
Similar presentations
1001ICT Programming 1 Semester 1, 2011 Lecture 6 Using Java Classes (Textbook, Chapter 3, Sections 3.2 to 3.7 ONLY)
Advertisements

The Random and String Classes The import statement.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
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 Day 6. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 DUE Problem set 2 posted 
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
COMP 14 Introduction to Programming Miguel A. Otaduy May 14, 2004.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Outline Last Time: Creating Objects The GregorianCalendar Class.
***** 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.
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.
Writing Classes (Chapter 4)
Class Library, Formatting, Wrapper Classes, and JUnit Testing
Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
Topics Chapter 2: –Data conversion Chapter 3 –Object creation and object references –The String class and its methods –The Java standard class library.
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:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Lecture Notes – Classes and Objects (Ch 7-8) Yonglei Tao.
CSC 1051 M.A. Papalaskari, Villanova University CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain, or a.
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.
Application Programming Interfaces. Java comes with a bunch of classes that are already written. Java comes with a bunch of classes that are already written.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3.
CSE 1201 Object Oriented Programming Using Classes and Objects.
CSCI 51 Introduction to Computer Science Joshua Stough February 3, 2009.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
1 Wrapper Classes  Sometimes we want to use a primitive type as an object, so there are wrapper classes that will help us.  In particular, we need to.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Collaboration Policy Programming Tests are individual work. You may not work with others in the class.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
3-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.
Programming in Java (COP 2250) Lecture 7 Chengyong Yang Fall, 2005.
ELC 312 Day 4. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Agenda Questions? Problem set one due Problem set two Posted in WebCT  Due Sept.
1 CMPT 126 Java Basics Using Classes and Objects.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
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.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Chapter 2: Objects and Primitive Data Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
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.
Variables in Java A variable holds either
Introduction to programming in java
Formatting Output & Enumerated Types & Wrapper Classes
Using Classes and Objects
Classes, Libraries & Packages
Chapter 3: Using Classes and Objects
Using Classes and Objects
Objects and Primitive Data
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Packages & Random and Math Classes
Using java libraries CGS3416 spring 2019.
Chap 1. Getting Started Objectives
Chap 4. Programming Fundamentals
Presentation transcript:

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

From last time: Account Declaring an Account object: Account acct1 = new Account ("Ted Murphy", 72354, ); Now acct1 is a usable object reference variable. Compare to: Scanner scan = new Scanner(System.in); Object ref Object itself Using Account methods: use dot on object ref variable acct1.deposit (25.85); // changes balance in object acct1.addInterest(); Here we are calling methods of Account, namely deposit and addInterest 2 “Ted Murphy”, 72354, acct1

From last time: Account Account acct1 = new Account ("Ted Murphy", 72354, ); Now acct1 is a usable object reference variable. Picture: blue areas depict memory areas Variable Name: acct1, value: pointer to object Note how the object content is in a different memory area Compare to int variable: An int variable is self-contained in one memory area int height = 60; Variable Name: height, value: 60 3 “Ted Murphy”, 72354, acct1 height60

Sec. 3.2 The String class We can create a String object using its constructor, listed on pg. 119: String greeting = new String(“Hi! How are you”); But we usually use the more convenient form allowed by Java String greeting = “Hi! How are you?”; Either way, we end up the a String object with contents “Hi! …”, and an object reference named greeting 4 Hi! How are you? greeting

Sec. 3.2 The String class Now that we have an object variable (AKA reference), we can call String methods: int len = greeting.length(); // length method if (greeting.equals(“Hello”)) … // equals method System.out.println(greeting.toUpperCase()); The last prints HI! HOW ARE YOU? 5 Hi! How are you? greeting

6 Class Libraries A class library is a collection of classes that we can use when developing programs The Java standard class library is part of any Java development environment Its classes are not part of the Java language per se, but we rely on them heavily Various classes we've already used ( System, Scanner, String ) are part of the Java standard class library (Look them up on Sun website) Other class libraries can be obtained through third party vendors, or you can create them yourself

7 Packages The classes of the Java standard class library are organized into packages Some packages in the standard class library are: Package java.lang java.applet java.awt javax.swing java.net java.util javax.xml.parsers Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities Network communication Utilities XML document processing

8 The import Declaration When you want to use a class contained in a package, you can use its fully qualified name java.util.Scanner scan =... Or you can import the package containing the class and just use the class name Scanner import java.util.Scanner; Scanner scan =... To import all classes in a particular package, you can use the * wildcard character import java.util.*;

9 The import Declaration All classes of the java.lang package are imported automatically into all programs It's as if all programs contain the following line: import java.lang.*; That's why we didn't have to import the System or String classes explicitly in earlier programs The Scanner class, on the other hand, is part of the java.util package, so that class must be imported as part of its package

The Random Number Generator Math.random() Needed for project 2, throwing dice Computer-generated truly random numbers are impossible We can call them pseudorandom numbers But they are such good fakes they can be used with confidence 10

Tiny Math.random() Example public class Random1 { public static void main (String[] args) { double num1 = Math.random(); // one random no. (< 1.0) double num2 = Math.random(); // another one System.out.println ("Random nos: " + num1 +" " + num2); } Note: random() is a static method of the Math class So we use. to call it. 11

Some generated numbers > run Random1 Random nos: > run Random1 Random nos: > run Random1 Random nos: We can turn these into ints using a little arithmetic… 12

Math.random(): Random ints public class Random2 { public static void main (String[] args) { // Using *20, then convert to int // to generate random ints 0-19 int num1 = (int)(Math.random()*20);// one random no. int num2 = (int)(Math.random()*20);// another one System.out.println ("Random ints: " + num1 + " " + num2); } 13

Random2 results: random ints 0-19 > run Random2 Random ints: 8 12 > run Random2 Random ints: > run Random2 Random ints: 0 8 Throwing dice: 6 possibilities so use (int)Math.random()*6) for 6 outcomes 0, 1, 2, 3, 4, 5 Add one to shift to 1, 2, 3, 4, 5, 6 14

15 Formatting Output: Sec 3.6 Look at NumberFormat and DecimalFormat classes in the text They provide you with ways to output numbers with a predefined precision For example: Printing double value of Pi … Printing only 2 decimal digits3.14