DOC PREVIEW
Duke CPS 100E - Java: Base Types

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Java: Base TypesJava: Operators for Base TypesJava: ObjectsJava: ClassesJava: Methods (Functions)Java: Sample ClassJava: Sample Class (cont)Slide 8Java StringsJava Control of Flow (by example)Slide 11What can an Object do (to itself)?Objects and valuesObjects, values, classesCompSci 100E2.1Java: Base TypesAll information has a type or class designationBuilt-in TypesCalled : primitive types or base typesboolean, char, bye, short, int, long, float, doublePrimarily use: int, double, booleanNeed to declare before using; defined (created) when declaredint x, y;// declaration only ? Initialization // some people group declarations at frontx = 39; // usey = 3 + x;int num = 10; // declaring when first usedEach type has its limitationsint: whole numbers only, limited rangedouble: implements scientific notation: fractions and wider rangeboolean: true or false values onlyCompSci 100E2.2Java: Operators for Base TypesFamiliar operators available: +, -, *, /, %For ints, / yields whole number quotient% yields the remainderWhat are o12/5 ?o13%5 ?What is the meaning of (2*pancakes + capacity -1)/capacity*5?Also have comparison operators: <, <=, ==, !=, >=, >Do not use = (assignment) where you mean ==Learn about operator precedence from text (p22)Most pretty intuitive – well designed languageHowever, when in doubt, use parenthethesMost of these operators have no meaning for objectsCompSci 100E2.3Java: ObjectsObjects are instantiations of a classUse new operator to createInvokes constructor which initializes objectAssume we have class DLicense(Class names start with capital letter)DLicense john; // declareo(Object names (e.g. john) start with lower case letter)john = new DLicense(); // createDLicense susan = new DLicense( ); //combine DLicense bud = new License(”Ted Bud”,”6/4/1989”);oCreation may allow or require argumentsoDepends on constructor(s)CompSci 100E2.4Java: ClassesCombine data and function in one packageUse new operator to createInvokes constructor which initializes objectDataStore in instance variables (fields)oMay be primitive types oroMay be objectsMethodsOften called functions, subroutines, or proceduresUsually do things to and with the instance variablesCompSci 100E2.5Java: Methods (Functions)Classes usually define one or more methodsStructure:access return_type name(parameters) {// method body}Accessor methods return infoReturn_type indicates type of info returnedOften have empty parameter listMutator methods change stateUsually parameters are involved in making the changesOften no info returned, thus return_type is voido(Object names (e.g. john) start with lower case letter)Invoking methodsUse: object.method(params);object is implicit parameterCompSci 100E2.6Java: Sample Classpublic class DLicense { private String name; private String dob; private double height; public DLicense() { // constructor name = ””; dob = ””; height = 0.0; } public DLicense(String nm, String bd){ name = nm; dob = bd; height = 0.0; } // methods to followCompSci 100E2.7Java: Sample Class (cont) public String getName(){ return name; } public String getDOB() { return dob; } public double getHeight() { return height; } public void setHeight(double ht) { height = ht; } public String display() { return name + ” ” + height + ”\” born: ” + dob; }CompSci 100E2.8Java: Sample Class (cont) public static void main(String[] args) { DLicense john; john = new DLicense(); john.setName(”John Smith”); john.setHeight(72.0); DLicense susan = new DLicense(); susan.setName(”Sue Peggy”); susan.setHeight(66.5); DLicense bud = new License(”Ted Bud”,”6/4/1989”); bud.setHeight(68.0); System.out.println(bud.display()); System.out.println(john.getName()); System.out.println(susan.getHeight()); }}CompSci 100E2.9Java StringsString is a classDo not need new to create StringString msg = ”hello”;String constants (literals) use ”, not ’Can join strings (concatenate) with +String mail = ”John says ” + msg;Most common String methods:int length(); // get number of chars in it String substring(int start, int stop); // substring gets part of stringint indexOf(String key); //finds loc of keyCompSci 100E2.10Java Control of Flow (by example)ifif (a > b) { msg = ”good”;}if-elseif (w.hasMore()) { System.out.println(w.next()); count = count + 1;} else System.out.println(”All done”);CompSci 100E2.11Java Control of Flow (by example)whilen = 10;while (n > 0){ System.out.println(”down to ” + n); n = n – 1;}forfor (n = 10; n > 0; n--) System.out.println(”down to ” + n);do-whilen = 10;do { System.out.println(”down to ” + n); n = n – 1;} while (n > 0);CompSci 100E2.12What can an Object do (to itself)?http://www.cs.duke.edu/csed/java/jdk1.4/docs/api/index.htmlLook at java.lang.ObjecttoString()Used to print (System.out.println) an object, overriding toString() can result in 'useful' information being printed, also used in String concatenation: String s = x + y;Default is basically a pointer-valueequals()Determines if guts of two objects are the same, must override, e.g., for using a.indexOf(o) in ArrayList aDefault is ==, pointer equalityhashCode()Hashes object (guts) to value for efficient lookupCompSci 100E2.13Objects and valuesPrimitive variables are boxes think memory location with valueObject variables are labels that are put on boxesString s = new String("genome");String t = new String("genome");if (s == t) {they label the same box}if (s.equals(t)) {contents of boxes the same}stWhat's in the boxes? "genome" is in the boxesCompSci 100E2.14Objects, values, classesFor primitive types: int, char, double, booleanVariables have names and are themselves boxes (metaphorically)Two int variables assigned 17 are equal with ==For object types: String, Sequence, othersVariables have names and are labels for boxesIf no box assigned, created, then label applied to nullCan assign label to existing box (via another label)Can create new box using newObject types are references or pointers or labels to


View Full Document

Duke CPS 100E - Java: Base Types

Documents in this Course
Topics

Topics

9 pages

Lecture

Lecture

3 pages

Notes

Notes

2 pages

Hashing

Hashing

19 pages

Lecture

Lecture

59 pages

Lecture

Lecture

6 pages

Lecture

Lecture

4 pages

Lecture

Lecture

20 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

7 pages

Lecture

Lecture

8 pages

Lecture

Lecture

10 pages

Lecture

Lecture

4 pages

Notes

Notes

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Lecture

Lecture

13 pages

Lecture

Lecture

6 pages

Lecture

Lecture

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

5 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

10 pages

Sets

Sets

14 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Test 1

Test 1

7 pages

Load more
Download Java: Base Types
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java: Base Types and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java: Base Types 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?