Numbers Implicit This Static Methods and Fields Packages Access Modifiers Main Overloading Exceptions Etc.

Similar documents
DM503 Programming B. Peter Schneider-Kamp.

Introduction to Programming Using Java (98-388)

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

1 Shyam sir JAVA Notes

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

CSC Java Programming, Fall Java Data Types and Control Constructs

Lecture 22: Java. Overall Structure. Classes & Objects. Every statement must end with ';' Carl Kingsford, , Fall 2015

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

2. The object-oriented paradigm!

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

DM550 Introduction to Programming part 2. Jan Baumbach.

Assertions, pre/postconditions

CONSTRUCTORS. CS302 Introduction to Programming University of Wisconsin Madison Lecture 19. By Matthew Bernstein

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

An overview of Java, Data types and variables

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

1/16/2013. Program Structure. Language Basics. Selection/Iteration Statements. Useful Java Classes. Text/File Input and Output.

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Self-test Java Programming

Java Bytecode (binary file)

Selected Java Topics

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

Chapter 6 Classes and Objects

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

ITI Introduction to Computing II

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Object Oriented Programming in C#

Simple Component Writer's Guide

CS 231 Data Structures and Algorithms, Fall 2016

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Midterm Exam CS 251, Intermediate Programming October 8, 2014

2. The object-oriented paradigm

Programming overview

Computer Science II (20073) Week 1: Review and Inheritance

c) And last but not least, there are javadoc comments. See Weiss.

ITI Introduction to Computing II

Logistics. Final Exam on Friday at 3pm in CHEM 102

Declarations and Access Control SCJP tips

F I N A L E X A M I N A T I O N

Basics of Object Oriented Programming. Visit for more.

JAVA BASICS II. Example: FIFO

Program Fundamentals

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Getting started with Java

Operators and Expressions

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Subclassing for ADTs Implementation

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Selected Questions from by Nageshwara Rao

How to Design Programs

Chapter 4 Defining Classes I

Practice exam for CMSC131-04, Fall 2017

3. Java - Language Constructs I

3.1 Class Declaration

COMP-202 More Complex OOP

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Advanced Computer Programming

JAVA GUI PROGRAMMING REVISION TOUR III

20 Most Important Java Programming Interview Questions. Powered by

Final Exam CS 152, Computer Programming Fundamentals December 9, 2016

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Contracts and Abstraction Casts Checking a Type Interfaces

Java Primer 1: Types, Classes and Operators

Java for Non Majors Spring 2018

Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

This exam is open book. Each question is worth 3 points.

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

( &% class MyClass { }

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Java Threads. Introduction to Java Threads

About this exam review

Lecture 2, September 4

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

CSC 1214: Object-Oriented Programming

Framework Fundamentals

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

CS 11 java track: lecture 1

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

CHAPTER 7 OBJECTS AND CLASSES

More on Objects in JAVA TM

Pace University. Fundamental Concepts of CS121 1

CS260 Intro to Java & Android 03.Java Language Basics

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

COP 3330 Final Exam Review

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

Transcription:

Numbers Implicit This Static Methods and Fields Packages Access Modifiers Main Overloading Exceptions Etc. 1

Integers Java s number system: byte an integer between -128 and 127 short an integer between -32768 and 32767 int an integer between -2147483648 and 2147483647 long an integer between -9223372036854775808 and 9223372036854775807 Each set of numbers forms a ring with its operations: Byte b = 127; b + 1-128 b + 2-127 b + 2-3 126 2

Floating-Point Numbers float an inexact real between -3.4 10 38 and 3.4 10 38 Smallest non-zero number: -1.4 10-45 Digits of precision: about 6 double an inexact real between -1.79 10 308 and 1.79 10 308 Smallest non-zero number: 4.94 10-324 Digits of precision: about 15 Addition overflows to a special "infinity" value, and sometimes loses precision: double d = 5; d + 0.0000000000000001 5.0 d + 1e100 1e100 3

Numbers as Objects An int cannot be used as an Object But Java provides pre-defined classes Integer, Double, etc. Object o = new Integer(5); o.intvalue() 5 List l = new Cons(new Integer(7), new Empty()); ((Integer)l.nth(0)).intValue() 7 4

Numbers Implicit This Static Methods and Fields Packages Access Modifiers Main Overloading Exceptions Etc. 5

Implcit This In a method, this. is addeded implicitly to the front of field uses and method calls that would be undefined otherwise class Car { String make; Car(String make) { this.make = make; boolean issame(car c) { return make.equals(c.make); boolean isford() { issame(new Car("Ford")); 6

Implcit This In a method, this. is addeded implicitly to the front of field uses and method calls that would be undefined otherwise class Car { String make; Car(String m) { make = m; boolean issame(car c) { return make.equals(c.make); boolean isford() { issame(new Car("Ford")); 7

Numbers Implicit This Static Methods and Fields Packages Access Modifiers Main Overloading Exceptions Etc. 8

Static Methods It s possible to define a function (as opposed to a method) by using the keyword static class Anything { static boolean biggerthanfive(int n) { return n > 5; Anything.biggerThanFive(10) true Statics have to be in a class, but you don t have to use new The function name is prefixed by the class where it s declared You can t use this in a static method there s no implicit argument 9

Static Fields A static field is like a top-level definition class Anything { static int n = 12; 17 + Anything.n 29 Anything.n = 15; 10

Final Fields A final field is like a constant definition class Anything { final int n = 12; 17 + Anything.n 29 Anything.n = 15; not allowed 11

Numbers Implicit This Static Methods and Fields Packages Access Modifiers Main Overloading Exceptions Etc. 12

Packages Every class declaration resides in a package Roughly, the source files in the same directory are all in a package by default The package keyword declares the following class files to be in a particular package package org.plt.lists; abstract class List {...... 13

Using Packages To use classes/function from another package, you can prefix the name with the package name class Anything { static boolean biggerthanfive(int n) { return n > java.lang.math.sqrt(25); 14

Importing Packages You can use import to have a prefix applied to any name that would otherwise be undefined import java.lang; class Anything { static boolean biggerthanfive(int n) { return n > Math.sqrt(25); 15

Importing Packages Actually, java.lang is always imported automatically class Anything { static boolean biggerthanfive(int n) { return n > Math.sqrt(25); java.lang is where Object, Integer, etc. come from 16

Numbers Implicit This Static Methods and Fields Packages Access Modifiers Main Overloading Exceptions Etc. 17

Access Control By default, anything you declare can be seen by anything in the same package, and only by things in the same page For example, this library is probably useless, since no one outside the package can see List: package org.plt.lists; abstract class List {...... 18

Public Access The public modifier makes a declaration visible to everyone package org.plt.lists; public abstract class List {...... 19

Method Access Modifiers like public must be used on methods and constructors, too, to make them visible package org.plt.lists; public abstract class List { public abstract int length(); abstract boolean myhelper(); public class Empty extends List { public Empty() { public int length() { return 0; boolean myhelper() {...... 20

Private Access The private modifier restricts access to the current class public class Cons extends List { private Object first; private List rest; public Cons(Object f, List r) { first = f; rest = r; public Object getfirst() { return first;... List l = new Cons(new Integer(7),...); ((Cons)l).first not allowed ((Cons)l).getFirst() Integer(val = 7) 21

Protected Access The protected modifier allows access only within the class or subclasses 22

Numbers Implicit This Static Methods and Fields Packages Access Modifiers Main Overloading Exceptions Etc. 23

Main As you may have noticed, few people actually run Java programs in ProfessorJ... A standalone program is started by calling the Main function of a designated class class MyProgram { public static void main(string[] cmdlineargs) {... 24

Numbers Implicit This Static Methods and Fields Packages Access Modifiers Main Overloading Exceptions Etc. 25

Overloading When you change the input part of a method s contract, you effectively change the name of the method: class Anything { int x; boolean y; Anything(int x, boolean y) { this.x = x; this.y = y; boolean issame(int v) { return v == this.x; boolean issame(boolean v) { return v == this.y; new Anything(1, false).issame(1) true new Anything(1, false).issame(false) true new Anything(1, false).issame(17) false new Anything(1, false).issame("hi") no such method 26

Overloading Can Be Tricky class Car { String make; Car(String make) { this.make = make; boolean issame(car c) { return make.equals(c.make); class Ford extends Car { String model; Ford(String model) { super("ford"); this.model = model; boolean issame(ford c) { return (make.equals(c.make) && model.equals(c.model)); Car c1 = new Ford("Pinto"); Car c2 = new Ford("Fiesta"); c1.issame(c2) true uses boolean issame(car c) 27

Overloading Can Be Tricky class Car { String make; Car(String make) { this.make = make; boolean issame(car c) { return make.equals(c.make); class Ford extends Car { String model; Ford(String model) { super("ford"); this.model = model; boolean issame(ford c) { return (make.equals(c.make) && model.equals(c.model)); Car c1 = new Ford("Pinto"); Car c2 = new Ford("Fiesta"); ((Ford)c1).isSame(c2) true uses boolean issame(car c) 28

Overloading Can Be Tricky class Car { String make; Car(String make) { this.make = make; boolean issame(car c) { return make.equals(c.make); class Ford extends Car { String model; Ford(String model) { super("ford"); this.model = model; boolean issame(ford c) { return (make.equals(c.make) && model.equals(c.model)); Car c1 = new Ford("Pinto"); Car c2 = new Ford("Fiesta"); c1.issame((ford)c2) true uses boolean issame(car c) 29

Overloading Can Be Tricky class Car { String make; Car(String make) { this.make = make; boolean issame(car c) { return make.equals(c.make); class Ford extends Car { String model; Ford(String model) { super("ford"); this.model = model; boolean issame(ford c) { return (make.equals(c.make) && model.equals(c.model)); Car c1 = new Ford("Pinto"); Car c2 = new Ford("Fiesta"); ((Ford)c1).isSame((Ford)c2) false uses boolean issame(ford c) 30

Numbers Implicit This Static Methods and Fields Packages Access Modifiers Main Overloading Exceptions Etc. 31

Exceptions The throw form is similar to Scheme s error When a method can raise an exception, the exception must be declared (usually) abstract class List {... abstract Object nth(int n) throws NthException; class Empty extends List {... Object nth(int n) throws NthException { throw NthException("index too big"); class Cons extends List {... Object nth(int n) throws NthException { if (n == 0) return first; else return rest.nth(n - 1); 32

Catching Exceptions The try... catch form is used to catch an exception and continue evaluation class Anything { static Object nthorfalse(list l, int n) { try { return l.nth(n); catch (NthException e) { return new Boolean(false); 33

Numbers Implicit This Static Methods and Fields Packages Access Modifiers Main Overloading Exceptions Etc. 34

A Few More Language Contructs switch a shortcut for if... else if... threads doing multiple things at a time characters more than 255! implicit string conversion "hello" + 1 "hello1"... but not too many other things 35

Lots More Libraries GUIs (Swing) Container classes (lists, vectors, tables, etc.) Big numbers Stream I/O Cryptography Networking............ many standard libraries, many more available 36