Introduction to Java. March 1, 2001 CBRSS and the John M. Olin Institute for Strategic Studies Lars-Erik Cederman

Similar documents
JAVA MOCK TEST JAVA MOCK TEST II

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Answer1. Features of Java

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Arrays Classes & Methods, Inheritance

Inheritance and Polymorphism

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

Object Orientated Analysis and Design. Benjamin Kenwright

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

PROGRAMMING FUNDAMENTALS

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 00. WELCOME TO OBJECTVILLE. Speaking the Language of OO

Principles of Object Oriented Programming. Lecture 4

The Java language has a wide variety of modifiers, including the following:

Java Fundamentals (II)

Programming in the Large II: Objects and Classes (Part 2)

Object-Oriented Concepts

Java Object Oriented Design. CSC207 Fall 2014

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Programming Language Concepts: Lecture 2

COP 3330 Final Exam Review

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Lecture 2: Java & Javadoc

Introduction to Computational Modeling of Social Systems

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

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

Compaq Interview Questions And Answers

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Java Professional Certificate Day 1- Bridge Session

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

2. The object-oriented paradigm

Java for Non Majors Spring 2018

Preview from Notesale.co.uk Page 3 of 36

FAQ: Classes & Objects

Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of

G Programming Languages - Fall 2012

Object Oriented Programming

Object Oriented Programming with Java

Classes, Objects, and OOP in Java. June 16, 2017

PROGRAMMING LANGUAGE 2

Tutorial 8 Date: 15/04/2014

C++ Inheritance and Encapsulation

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

2. The object-oriented paradigm!

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

The high-level language has a series of primitive (builtin) types that we use to signify what s in the memory

Introduction to Java

Fundamentals of Programming Data Types & Methods

Java. Representing Data. Representing data. Primitive data types

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University


CSEN401 Computer Programming Lab. Topics: Introduction and Motivation Recap: Objects and Classes

1.00 Lecture 13. Inheritance

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Chapter 2: Java OOP I

Course Outline. Introduction to java

Lecture 1: Overview of Java

BSc. (Hons.) Software Engineering. Examinations for / Semester 2

OVERRIDING. 7/11/2015 Budditha Hettige 82

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Object Oriented Programming: Based on slides from Skrien Chapter 2

AP CS Unit 6: Inheritance Notes

9 Working with the Java Class Library

Inheritance Abstraction Polymorphism

Introduction to Programming Using Java (98-388)

Class, Variable, Constructor, Object, Method Questions

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

2 rd class Department of Programming. OOP with Java Programming

CS260 Intro to Java & Android 03.Java Language Basics

What are the characteristics of Object Oriented programming language?

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

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

Overview of Java s Support for Polymorphism

Unit 3 INFORMATION HIDING & REUSABILITY. Interface:-Multiple Inheritance in Java-Extending interface, Wrapper Class, Auto Boxing

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

About this exam review

Practice for Chapter 11

STRUCTURING OF PROGRAM

Chapter 5. Inheritance

CS 11 java track: lecture 1

CMSC 132: Object-Oriented Programming II

Fundamentos de programação

CS171:Introduction to Computer Science II

OOP in Java Review. CS356 Object-Oriented Design and Programming October 1, 2014

Following is the general form of a typical decision making structure found in most of the programming languages:

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Chapter 14 Abstract Classes and Interfaces

Introduction to OOP Using Java Pearson Education, Inc. All rights reserved.

Transcription:

Introduction to Java March 1, 2001 CBRSS and the John M. Olin Institute for Strategic Studies Lars-Erik Cederman

Outline Java overview simple structures object-orientation roulette example How to compile etc. Readings and documentation

Java Conceived by Sun in the early 1990s Became the new standard for the web thanks to platform-independence Java C syntax syntax C++ object model

Some Java Buzzwords Robust Portable Object-oriented (see Schildt Module 1)

The Java solution to platformindependence Mac myprogram.java ------ ----- -- -------- 010111001 Interpreter Interpreter 010111001 Compiler myprogram.class ------ ----- -- -------- Windows

C-like syntax int i; int age; int drinks; drinks = 0; age = 18; if (age > 20) { for (i = 0; i<10; i++) drinks++;

Example class Example { public static void main (String args[]) { System.out.println("Java drives the Web.");

Example2 class Example2 { public static void main (String args[]) { int var1; int var2; var1= 1024; System.out.println("var1 contains " + var1); var2 = var1/ 2; System.out.print("var2 contains var1/ 2"); System.out.println(var2);

CheckerBoard: Desired output BWBWBWBW WBWBWBWB BWBWBWBW WBWBWBWB BWBWBWBW WBWBWBWB BWBWBWBW WBWBWBWB

CheckerBoard: Code class CheckerBoard { public static void main (String args[]) { boolean black = true; for (int i=0; i<8; i++) { for (int j=0; j<8; j++) { if (black) System.out.print("B"); else System.out.print("W"); black =!black; black =!black; System.out.println();

Simple datatypes int a = 12; double d = 3.14; char c = 'X'; boolean b = false; Operators: +, -, *, /, ==, >, <, &&,, ++, -- More in Schildt Module 2!

Object Oriented Programming methods instance variables messages break() speed = 45.7; gear = 3; changegears(g) an object a program

Interface vs. implementation Objects hide their functions and data User only has to be familiar with the interface of an object, not its implementation

The three principles of OOP Encapsulation Objects hide their functions (methods) and data (instance variables) Inheritance Each subclass inherits all variables of its superclass Polymorphism manual car Super class automatic Subclasses Interface same despite different dat1types draw() draw()

class Car { public boolean ismanual; private double speed; Class declarations public void start (double s) { ; speed = s; public void stop() { speed = 0.0; public double getspeed() { return speed;

Class declarations class Manual extends Car { private int gear; public void shiftgears(int g) { gear = g;

class Driver { Car mycar; Creating and using objects public void buycar(boolean ismanual) { if (ismanual) mycar = new Manual(); else mycar = new Car(); mycar.ismanual = ismanual; public void drive() { if (mycar.ismanual) mycar.shiftgears(1); mycar.start(45.0); mycar.stop();

Example: Russian Roulette 1 1/6 5/6 2 2/6 4/6 3/6 1 4/6 3/6 2 5/6 2/6 1 1/6 2

Web of message calls... model play( revolver ) load() trigger() revolver load() trigger() player1 play( revolver ) player2 play( revolver )

Classes Model Player Roulette model new new player1 player2 instances new Revolver revolver

Roulette: Model import java.util.random; public class Model { public static void main(string[] args) { int numiter = 20; int counter = 0; Random generator = new Random(); one game for (int i=0; i<numiter; i++) { generator.setseed(i); Revolver revolver = new Revolver(generator); Player player1= new Player(1); Player player2 = new Player(2); player1.setother(player2); player2.setother(player1); int outcome = player1.play(revolver); if (outcome == 1) counter++; System.out.println("1 dies with probability " + (double)counter/(double)numiter);

Roulette: Player public class Player { int id; Player other; public Player(int i) { id = i; public void setother(player o) { other = o; public int play(revolver r) { r.load(); boolean dead = r.trigger(); if (dead) return id; else return other.play(r);

Roulette: Revolver import java.util.random; public class Revolver { int bullets; Random generator; public Revolver(Random g) { generator g; bullets = 0; public void load() { bullets++; public boolean trigger() { return generator.nextint(6)+1<= bullets;

Russian Roulette 0: 12 1: 1212 2: 12 3: 12 4: 1 5: 12 6: 12121 7: 1212 8: 12 9: 1212 10: 121212 11: 121 12: 12 13: 121 14: 12 15: 1212 16: 1212... 1's prob. of dying = 0.521605... 1,000 replications: 0.522

Documentation Schildt, H. Java2: A Beginner's Guide Eckel, B. Thinking in Java http://java.sun.com/ (select "Java Tutorial") http://www.borland.com/techpubs/jbuilder/