Lecture 02, Fall 2018 Friday September 7

Similar documents
CS 231 Data Structures and Algorithms, Fall 2016

Java Bytecode (binary file)

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Java's Memory Management

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Lecture 07, Fall 2018 Wednesday September 19

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

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

Programming Language Concepts: Lecture 2

COP 3330 Final Exam Review

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

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Programming Language Concepts: Lecture 2

Chapter 6 Classes and Objects

Chapter 14 Abstract Classes and Interfaces

Chapter 3 Intro to Java

Chapter 4 Defining Classes I

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

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Java Object Oriented Design. CSC207 Fall 2014

Chapter 2: Programming Concepts

} Each object in a Java program has an identifier (name) } This includes:

Lecture 1 - Introduction (Class Notes)

Object Declaration. <class name>: the name of the class to which the object belongs <object name>: the name of the object (any valid identifier)

Java Fundamentals (II)

COMP 250 Winter 2011 Reading: Java background January 5, 2011

CS-202 Introduction to Object Oriented Programming

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

CS 251 Intermediate Programming Methods and Classes

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Declarations and Access Control SCJP tips

Cpt S 122 Data Structures. Introduction to C++ Part II

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Introduction to Programming Using Java (98-388)

5.6.1 The Special Variable this

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

Lecture 2, September 4

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

Chapter 10 Introduction to Classes

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

Brief Summary of Java

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Simple Java Programs. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CS108 Lecture 16: User Defined Classes. Overview/Questions

Supporting Class / C++ Lecture Notes

Data Structures (list, dictionary, tuples, sets, strings)

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

Introduction to Java

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

Inheritance and Interfaces

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Chapter 6 Introduction to Defining Classes

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

CS 231 Data Structures and Algorithms Fall 2018

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

CS558 Programming Languages

CS 177 Recitation. Week 1 Intro to Java

Basics of Java Programming

Getting Started with Java. Atul Prakash

CSE : Python Programming

CS 231 Data Structures and Algorithms Fall Arrays Lecture 07 - September 19, Prof. Zadia Codabux

Object Orientation Fourth Story. Bok, Jong Soon

CS 251 Intermediate Programming Methods and More

Java Programming Tutorial 1

G52CPP C++ Programming Lecture 9

CE221 Programming in C++ Part 1 Introduction

INSTRUCTIONS TO CANDIDATES

CS 11 java track: lecture 1

Programming overview

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

MIDTERM REVIEW. midterminformation.htm

Introduction to Java. Handout-1d. cs402 - Spring

Sri Vidya College of Engineering & Technology

Lesson 12: OOP #2, Accessor Methods (W03D4)

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

QUIZ. What is wrong with this code that uses default arguments?

CS 116 Week 8 Page 1

Key Differences Between Python and Java

CS263: Runtime Systems Lecture: High-level language virtual machines

Chapter Two Bonus Lesson: JavaDoc

class objects instances Fields Constructors Methods static

Assignment 1 due Monday at 11:59pm

Lecture 17. For Array Class Shenanigans

And Even More and More C++ Fundamentals of Computer Science

C02: Overview of Software Development and Java

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

CS112 Lecture: Defining Instantiable Classes

CS 11 java track: lecture 3

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

Objects as a programming concept

Algorithms and Programming I. Lecture#12 Spring 2015

CS260 Intro to Java & Android 03.Java Language Basics

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

CT 229 Arrays in Java

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

Transcription:

Anatomy of a class Oliver W. Layton CS231: Data Structures and Algorithms Lecture 02, Fall 2018 Friday September 7

Follow-up Python is also cross-platform. What s the advantage of Java? It s true: Python does run flexibly on all OSes that support Python. Bytecode compilation optimizes your source code and does all the checking for syntax errors/figuring out variable types/validity/etc. The JVM converts the bytecode to instructions that the computer actually can execute. Remember, Python has to do all this, plus execute your code when you run the interpreter. This means Java code can be faster than Python code (but not always). double vs. float (draw on board)

Quick review: What are the big differences between Java and Python? 1. Java is compiled, Python is interpreted. 2. Java requires a 'main method'. 3. Java is a statically typed language, Python is dynamically typed. 4. Java is an object-oriented language.

Quick review: What are the big differences between Java and Python? Java Java example Python Python example compiled javac Main.java -> java Main interpreted python main.py Needs main method. Is executed when program runs. public static void main(string[] args) Not required. Top-level code just runs. closest thing: def main(): or if name == ' main ': Statically typed int x = 3; Dynamically typed x = 3 Object-oriented Code is generally enclosed in a class. Supports OO

Anatomy of a Java class Dive into example: Die.java

Instance variables Instance variables go inside the class definition, usually at the very top. Instance variables and methods are called fields and belong to a class.

Naming Quick word about naming: Java classes should be title case, like we discussed last time (e.g. HelloWorld). Variables should be camel case (e.g. facevalue ). Underscores are supported.

Inline comments Unlike Python, which uses the # symbol for single line comments, Java uses two forward slashes for single-line (inline) comments: // current face value private int facevalue;

Multiline comments 1/2 Unlike Python, which uses the triple quote ''' for multiline comments, Java uses /**/ (// with two *s in between), with the * symbol at the beginning of each line: /** * Die.java * Represents a 6-sided die * Oliver W. Layton * CS231: Data structures and algorithms, Fall 2018 * Project XX: YY name */ public class Die { // current face value private int facevalue;... // more code } // end of Die class

Multiline comments 2/2 When you turn in your projects, please add comment the top of each.java file to include: Filename Description of the class Your name Our class name Which project you're turning in. Use the multi-line style comments above each method signature to give a brief description what it does.

final keyword In Java, we can use the final keyword to indicate to the Java compiler that a variable should never change. final variables MUST be initialized at declaration. If someone tries to change the value, Java will not allow us to compile the program. A naming convention for constants is that they are all caps, words separated by underscores. You don't need to follow this, but it helps to visually remember that the variable is a constant.

Access modifiers: public Who can access fields in a class? Examples below refer to the Student/Teacher/Undergraduate/Graduate drawing from class public means anyone can access the field, the field is exposed to the world. Example usage: public int grade; in the class Student. Syntax for access is: ClassObject.field. For example: mike.grade, where mike is an object of type Student and grade is an instance variable in the class Student. Whether you created a subclass, a separate independent class, or I created totally separate program and found your.class files, I could access your field data if it's public

Access modifiers: private Example usage: private int grade; The private keyword is generally the preferred choice for ALL class fields because that means nothing outside the class to which the field belongs can access the data. WE control the access via set/get methods. Not even subclasses can access private fields.

Access modifiers: default Example usage: int grade; No explicit modifier; it's omitted. It's like public, but only classes within the same package can access the data. Package: you can think of this for now as other classes within the same working folder on your computer (e.g. in School1 folder). Default is sometimes called package scope for this reason.

Access modifiers: protected Example usage: protected int grade; Same as default access, except subclasses located in different packages can access the data. We're not concerned with the distinction between protected and default in CS231.

void keyword Used when a method has no return value. For example, let's say the following is defined in the class Die: public void setvalue(int newvalue) { // code here... } We may call it as follows: thedie.setvalue(2); where thedie is an object of type Die.

this keyword (1/2) Consider this set method: public class Die { // current face value private int facevalue; } public void setvalue(int facevalue) { this.facevalue = facevalue; } that is called on the object thedie: thedie.setvalue(2);

this keyword (2/2) this refers to the facevalue instance variable belonging to thedie, NOT the parameter value passed in. this is needed in the above example because there is a name conflict between the parameter name and the instance variable. this allows you to differentiate between the two and refer to the instance variable. It's similar to self in Python.

Instantiating an object in memory Here's code to make a new object: public static void main(string[] args) { Die thedie = new Die(); } The new keyword tells Java to allocate memory for a new object instance. This is called instantiation -- we instantiated the Die object.

Default constructor (1/2) The last piece after new (Die()) is a call to what's called the default constructor. Wait!!! We didn't write a constructor! How does it know... Every Java object automatically comes with a parameter-less constructor for any type of object that essentially does nothing except instance variables are set to default values (e.g. 0 for int, 0.0 for double, etc).

Default constructor (2/2) Often, we'll want to customize what happens in the constructor, when a new object is created. For example, set the facevalue to 1 when we create a new Die, rather than the default value of 0. We can do something called override the default constructor --- define a method with exactly the same method signature and then Java will call OUR default constructor, rather than the built-in one: public Die() { facevalue = 1; }