/
1 CS1110  Stepwise refinement,  wrapper classes   29 Sept 1 CS1110  Stepwise refinement,  wrapper classes   29 Sept

1 CS1110 Stepwise refinement, wrapper classes 29 Sept - PowerPoint Presentation

lindy-dunigan
lindy-dunigan . @lindy-dunigan
Follow
393 views
Uploaded On 2016-07-12

1 CS1110 Stepwise refinement, wrapper classes 29 Sept - PPT Presentation

Prelim 8PM Thursday 8 October next week If you have a conflict and have not been contacted email Maria Witlox We will give a makeup Do not ask the instructor of a course with a prelim conflict to give you a Makeup ID: 401580

class dept object integer dept class integer object amp wrapper string href instance studies link int courses methods vector

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "1 CS1110 Stepwise refinement, wrapper ..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

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, email Maria Witlox. We will give a makeup. Do not ask the instructor of a course with a prelim conflict to give you a Makeup.

Thursday: A handout will explain what is on prelim 1 Sunday: 1-3PM. Review SessionA3 is due Wed night on the CMS

Thursday’s lecture: no reading, but be there or be square (or lost)!Recursion can be a difficult topic, but we’ll make it easy.

Purpose of lecture

: Give you examples of the stepwise-refinement development of methods that process strings; while showing you the OO structure of a real program.Slide2

2

Best. Study tip. Ever.

Cornell’s Learning Strategies Center posts a lot of great information on study skills, taking exams, time & stress management, etc. lsc.sas.cornell.edu/Sidebars/Study_Skills_Resources/SKResources.htmlEvery day after classes, retrieve your notes …

--- you do take notes, don’t you? --- …and read them over. This takes very little time, and yet: really makes material “stick” in one’s mind, andhelps you figure out what you don’t understand early on, so you can get it straightened out faster.This was a real game-changer for me.Slide3

3

Wrapper classes. Read Section 5.1 of class text

a0

Integer???5

Soon, we’ll 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 important extra help.Slide4

Class Vector

4

An instance of class Vector maintains an expandable/shrinkable list of objects. Use it whenever you need to maintain a list of things.Values of primitive types cannot be placed directly into the list of a Vector. That’s why we have the wrapper classes. In the interactions pane, we will do a few things, like these:import java.util.*;Vector v= new Vector();vv.add(new Integer(2));v.add(3);

v.add(‘c’);In newer versions of Java, v.add(1) is allowed; the 1 is wrapped in an Integer object and the name of that object is added to v. Doesn’t work in older versions.Slide5

5

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 type Wrapper classint Integerlong Longfloat Floatdouble Doublechar Characterboolean Boolean

Each wrapper class has:Instance methods, e.g. equals, constructors, toString,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.

Integer k= new Integer(63); int j= k.intValue();Slide6

Example of a program that deals with Strings

Creating a web page giving liberal studies courseshttp://

www.cs.cornell.edu/gries/ccgb/index.html6Java program reads the online Courses of Study webpages and extracts the courses that are liberal studies courses in A&S and CALS.It builds tables of A&S, CALS, CA, HA, KCM, LA, and SBA courses andproduces the liberal studies course website

String manipulation is key concern of this lecture. But OO structure of the program will also be discussed Slide7

Class

Webpage7

Fields contain:url, as a Stringurl, as an object of classset of links on the page…

Methods:Webpage(String url) (constructor)isHtmlPage()getLinks()getReader() = an object that lets one read the webpage line by line… Slide8

Class

DeptLink8

Fields contain:• dept name• link to its webpage in CoS• Vector of all its courses <li>

<a href="CoScourses.php?college=AS&dept=American+Studies">American Studies</a></li>Slide9

9

/** Constructor: an instance who dept name and link are

contained in s. s has the form … <a href="xxx">dept name</a> … where the xxx is a relative URL in directory

LibStudies.prefix Note: if s is not proper, dept name and link will be null. */<li><

a href="CoScourses.php?college=AS&dept=American+Studies">American Studies</a></li

>

The form is: … <a

href

=“xxx”>dept name</a> …Slide10

10

/** Constructor: an instance who dept name and link are

contained in s. s has the form … <a href="xxx">dept name</a> … where the xxx is a relative URL in directory LibStudies.prefix

Note: if s is not proper, dept name and link will be null. */public DeptLink(String s) {

}

Remove … <a

href

=

"

from

s

;

Set

k

to index of

"

> of the a tag;

Store the link

xxx

in local variable

lk

;

s

= s.substring(k+2);

Set k to index of </a>;

Store dept name and

lk

in dept and link. Slide11

Class Courses

11

Fields contain:course e.g. ANTHR 1200title everything else you see to the leftcategory HA-AS, HA CA-AS, CA SBA-AS, SBA etc.

<tr><td nowrap><a href="CoSdetail.php college=AS&number

=1400&prefix=ANTHR& title=The+Comparison+of+Cultures+%40+%28CA-AS%29">ANTHR&nbsp;1400</a></td><td>

The Comparison of Cultures @ (CA-AS)

</td></

tr

>Slide12

12

/** Constructor: instance for course

given by s, in form: <tr><td nowrap><a href="...">course name</a> </td><td>title</td></tr>

cat contains the category. If s not proper, course, title, category will be null. */public Course (String s, String cat) {}If s doesn’t start with

<tr><td nowrap><a >, return;Remove from

s

everything before the course name;

Save course name in local variable

courseName

;

Remove from

s

everything before the title;

Save title in local variable

t

;

Store

courseName

,

t

, cat in the fields;