1 CS100J Wrapper classes, stepwise refinement 19 Feb When insults had class "A modest little person, with much to be modest about." Churchill "I never.

Slides:



Advertisements
Similar presentations
1 Value vs. reference semantics. Recall: Value semantics value semantics: Behavior where variables are copied when assigned to each other or passed as.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Java Software Solutions
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
1 CS1110 Stepwise refinement, wrapper classes 29 Sept Prelim: 8PM Thursday 8 October (next week) If you have a conflict and have not been contacted, .
1 CS100J Classes, stepwise refinement 25 February 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht.
1 A Sorting Example (a start for Lab 4 problem number 3)
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 CS100J 18 September 2007 More on Methods. Developing methods. Also: The inside-out rule; and the use of this and super. Read sec. 2.5 on stepwise refinement.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
1 CS100J 20 September 2005 More on Methods. Developing methods Also: About the use of this and super Read section 2.5 on stepwise refinement Listen to.
Static Class Members Wrapper Classes Autoboxing Unboxing.
1 CS100J 05 February 2005 Today’s topic: Customizing a class (continued) Quiz 1 is today Quiz 2 is next Tuesday Quote for the day: There is no reason anyone.
1 Assignment 8. Implementing type integer Due on Tuesday, 4 December, by midnight (submitted electronically). Download class List and a skeleton of class.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
CS/ENGRD 2110 FALL 2014 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
JavaServer Pages Syntax Harry Richard Erwin, PhD CSE301/CIT304.
Chapter 7 Objects and Memory. Structure of memory The fundamental unit of memory is called a bit, either 0 or 1. In most modern architectures, the smallest.
Class Library, Formatting, Wrapper Classes, and JUnit Testing
1 CS1110 Wrapper classes, stepwise refinement 27 Sept Prelim: Tuesday, 6 Oct, 7:30–9:00PM, Statler Auditorium Thursday: no reading. Be there or be square.
1 CS1110 Lecture Sept Developing (String-processing) programs; class Vector; wrapper classes Prelim: 7:30-9PM Thursday 7 October. Last name A-K:
1 Chapter 10 Object-Oriented Thinking. 2 Class Abstraction and Encapsulation Class abstraction means to separate class implementation details from the.
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
1 CS Sept 2010 Inside-out rule; use of this, super Developing methods (using Strings). Read sec. 2.5, stepwise refinement Listen to Plive, 2.5.1–2.5.4.
Static Class Methods CSIS 3701: Advanced Object Oriented Programming.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
10-Nov-15 Java Object Oriented Programming What is it?
CS1110 lecture 5 8 Feb 2010 Testing; class Object; toString; static variables/methods Reading for this lecture: Testing with JUnit (Appendix I.2.4 & pp.
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
CS/ENGRD 2110 FALL 2013 Lecture 4: The class hierarchy; static components 1.
1 CS100J Classes, stepwise refinement 14 Feb 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht oredr.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
CS100J 25 September 2003: Constructors We alluded to this topic several times in the past month. Now we make it all clear. Read Section 3.1.3, This.
1 CS1110 Wrapper classes, stepwise refinement 30 September Chocolate bar has grooves that divide it into squares. This is one example. Others would be.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
1 CS100J 08 September 2005 Today’s topic: Customizing a class (continued) Quote for the day: There is no reason anyone would want a computer in their home.
Variables and Types Java Part 3. Class Fields  Aka member variable, field variable, instance variable, class variable.  These are the descriptors of.
1 CS1110 Stepwise refinement, more on classes 24 Sep 2009 Application of String processing and stepwise refinement. Miscellaneous points about classes.
1 CS1110 Classes, stepwise refinement 17 Feb 2009 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of.
1 CS September 2008 Discussion of Methods: Executing method calls.If-statements. The return statement in a function. Local variables. For this and.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Wrapper Classes  Java offers a convenient way to incorporate, or wrap, a primitive data type into an object, for example, wrapping int into the class.
1 CS1110 Classes, stepwise refinement 12 Feb 2009 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht oredr.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
1 CS1110 Thursday, 10 Feb 2011 Discussion of Methods: Executing method calls. If-statements. The return statement in a function. Local variables. For this.
Wrapper Classes Use wrapper objects in Collections when you can’t use primitive types Primitive TypeWrapper Class byteByte shortShort intInteger longLong.
1 For more info: CS February 2009 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
1 CS February 2009 Inside-out rule; use of this and super Developing methods (using String ops). Read sec. 2.5 on stepwise refinement Listen to.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
1 CS1110 Classes, wrapper classes, Vectors. 10 Feb 2012 Miscellaneous points about classes. Discussion of wrapper classes and class Vector Use the text.
"I am enclosing two tickets to the first night of my new play; bring a friend, if you have one."
Java Programming: Guided Learning with Early Objects
Agenda Warmup AP Exam Review: Litvin A2
CS1110 Classes, stepwise refinement 23 Sep 2009
Chapter 10 Thinking in Objects
CS1110 Wrapper classes, stepwise refinement 23 Feb
Formatting Output.
Sit next to someone. Today, we do some work in pairs.
Sit next to someone. Today, we do some work in pairs.
Chapter 10 Thinking in Objects Part 2
CS100J 22 February 2005 More on Methods. Developing methods
CS100J Final Class on Classes 10 February 2005
Presentation transcript:

1 CS100J Wrapper classes, stepwise refinement 19 Feb When insults had class "A modest little person, with much to be modest about." Churchill "I never killed a man, but I read many obituaries with great pleasure." Clarence Darrow "Thanks for sending me a copy of your book; I'll waste no time reading it." Moses Hadas "He can compress the most words into the smallest idea of any man I know." Abraham Lincoln "I didn't attend the funeral, but I sent a nice letter saying I approved of it." Mark Twain "I am enclosing two tickets to the first night of my new play. Bring a friend... if you have one." George Bernard Shaw to Winston Churchill "Cannot possibly attend first night, will attend second... if there is one." Churchill "I feel so miserable without you; it's almost like having you here." Stephen Bishop "He is a self-made man and worships his creator." John Bright "I've just learned about his illness. Let's hope it's nothing trivial." Irvin Cobb "There's nothing wrong with you that reincarnation won't cure." Jack Leonard "He has the attention span of a lightning bolt." Robert Redford "He inherited some good instincts from his Quaker forebears, but by diligent hard work, he overcame them." James Reston (about Richard Nixon) Prelim 7:30-9:00 Thurs, 21 Feb, Olin 155.

2 Wrapper classes. Read Section 5.1 of class text a0 Integer ??? 5 At times, we wish to deal with an int value as an object. "Wrapper class" Integer provides this capability. An instance of class Integer contains, or "wraps", one int value. You can't change the value. The object is immutable. Integer(int) Integer(String) toString() equals(Object) intValue() Instance methods: constructors, toString(), equals, intValue. Static components: MIN_VALUE MAX_VALUE toString(int) toBinary(int) valueOf(String) parseInt(String) Static components provide extra help.

3 Each primitive type has a corresponding wrapper class. When you want to treat a primitive value of that type as an object, then just wrap the primitive value in an object of the wrapper class! Primitive typeWrapper class intInteger longLong floatFloat doubleDouble charCharacter booleanBoolean Each wrapper class has: Instance methods, e.g. constructors, toString, and equals Useful static constants and methods. You don't have to memorize the methods of the wrapper classes. But be aware of them and look them up when necessary. Use Gries/ Gries, Section 5.1, and ProgramLive, 5-1 and 5-2, as references.

4 Anglicize integers. Last time, we started looking at larger numbers first. We didn't finish. This time, we start looking at smaller numbers first. This may be better for you because it may be easier, more straightforward. We figure out what to do for 0 < n < <= n < <= n < 1, <= n < 1,000,000 Mañana principle. "Put off to tomorrow". When it is useful, "stub in" a method, with a good spec, to be written later, and write calls on it. Reasons to do so: (a)Same computation is required in several places. (b)Keep one method body from getting to long and cumbersome.

5 /** = integer n, in words. Precondition: 0 < n < 1,000,000. */ public static String ang(int n) { } Note: As we develop the body of function ang, we may find the need to anglicize some other integer m. W can call function ang to do it — as long as m < n. The reason for this restriction will be seen later. So, within ang, we can call ang. This is called recursion. We study recursion in the next two lectures. Sounds strange? You can see that it works if you remember how a function call is executed: 1.Draw frame for the call. 2.Assign args to parameters. 3.Execute method body. 4.Erase the frame for the call and return the value of the call.

6 stepwise refinement /** An instance represents the time of day in a time zone, in terms of hours, minutes, and seconds. The implemented time zones are: GMT: Greenwich Mean Time, GMT BST: British Summer Time, GMT+1 EST: Eastern Standard Time, GMT-5 hours (NY) EDT: Eastern Daylight Savings Time, GMT-4 hours (NY) CST: Central Standard Time, GMT-6 hours (Chicago) CDT: Central Daylight Savings Time, GMT-5 hours (Chicago) MST: Mountain Standard Time, GMT-7 hours (Phoenix) MDT: Mountain Daylight Savings Time, GMT-6 (Phoenix) PST: Pacific Standard Time, GMT-8 hours (LA) PDT: Pacific Daylight Saving Time, GMT-7 hours (LA) IND: India time, GMT+5:30 hours (New Delhi) India (IND) is included only to show that times are not always on hourly boundaries from GMT.

7 /** A time may appear negative or greater than 24 hours. This is because we allow a conversion of a time from one time zone to another, and a time of 0 hours GMT is -7 hours PDT (for example), while a time of 23:59 GMT is 29:29 IND. An instance of the class can show the time using a 24-hour clock or using the AM-PM designation; it is the user's choice. */ public class TimeJ { public static final String GMT= "GMT"; public static final String BST= "BST"; public static final String EST= "EST"; public static final String EDT= "EDT"; public static final String CST= "CST"; public static final String CDT= "CDT"; public static final String MST= "MST"; public static final String MDT= "MDT"; public static final String PST= "PST"; public static final String PDT= "PDT"; public static final String IND= "IND";

8 /** Class invariant: Variable time is a time in seconds on a day in time zone zone. The time may be negative or greater than 24 hours, as indicated in class specification (which says why). Field display12Hr has the meaning "the time should be viewed as a 12-hour clock”. */ private int time= 0; private String zone= "GMT"; private boolean display12Hr= false;

9 /** Constructor: instance with time 0 in GMT and a 24-hour clock */ public TimeJ() { } /** Constructor: s seconds, GMT, with 24-hour clock */ public TimeJ(int s) { this(); time= s; } /** Constructor: s seconds, zone z, with 12-hour clock iff b is true*/ public TimeJ(int s, String z, boolean b) { this(s); zone= z; display12Hr= b; } /** Constructor: h hours, m minutes, and s seconds in zone z. The time should be >-24 hours and <+48 hours; if not, 0 is used. If z is not a legal zone, make it GMT. The time should be displayed as am-pm iff b is true */ public TimeJ(int h, int m, int s, String z, boolean b) { }

10 /** = a string representation of the time. This is basically in the form "hours:minutes:seconds zone", but it differs depending on whether a 12- or 24-hour clock is wanted. We describe the difference with examples: In AM-PM mode, output could be: 06:20:05AM DST or 06:20:05PM DST In 24-hour mode: 06:20:05 DST or 18:20:05 DST If the time is negative or at least 24 hours, print it using the 24-hour mode, even if 12-hour mode is indicated. */ public String toString() { int sec; // Field s contains the time in seconds. Local int min; // variables hr, min, and sec will contain the corres- int hr; // ponding time broken into hours, minutes and seconds. String result= ""; // The string to be returned boolean amPM; // = "give description in AM-PM format”