G51PRG-Sem2 A Reality Check a sort of quiz Dave Elliman.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

Written by: Dr. JJ Shepherd
Inheritance Inheritance Reserved word protected Reserved word super
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Java Tutorial Write Once, Run Anywhere. Java - General Java is: –platform independent programming language –similar to C++ in syntax –similar to Smalltalk.
Java Tutorial Write Once, Run Anywhere. Java - General Java is: –platform independent programming language –similar to C++ in syntax –similar to Smalltalk.
 Java is:  platform independent programming language  similar to C++ in syntax  similar to Smalltalk in mental paradigm  Java has some interesting.
OOP Languages: Java vs C++
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Java Tutorial Write Once, Run Anywhere. Java - General Java is: –platform independent programming language –similar to C++ in syntax –similar to Smalltalk.
CSC3170 Introduction to Database Systems
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Java Methods & Classes – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
Java Tutorial Write Once, Run Anywhere. Java - General Java is: –platform independent programming language –similar to C++ in syntax –similar to Smalltalk.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Programming Languages and Paradigms Object-Oriented Programming.
CSE 131 Computer Science 1 Module 1: (basics of Java)
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Programming in Java CSCI-2220 Object Oriented Programming.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Review of Java Basic Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – conditional statements –Flow of Control.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
OOP Basics Classes & Methods (c) IDMS/SQL News
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Information and Computer Sciences University of Hawaii, Manoa
Objects as a programming concept
JAVA MULTIPLE CHOICE QUESTION.
Chapter No. : 1 Introduction to Java.
Java Review Hello..This ppt is to give you an introduction about java and why it is soo special The segments all discussed here are the crucial parts of.
Programming Language Concepts (CIS 635)
Java Programming Language
Object Oriented Programming
Introduction to Data Structure
CIS 199 Final Review.
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

G51PRG-Sem2 A Reality Check a sort of quiz Dave Elliman

G51PRG-Sem2 A Story with a Moral “My brother”

G51PRG-Sem2 What Is a Computer?

G51PRG-Sem2 What Is a Computer Program?

G51PRG-Sem2 What Bits and Pieces Must a Computer Have?

G51PRG-Sem2 What is the Point in Having Programming Languages like Java?

G51PRG-Sem2 What does javac do?

G51PRG-Sem2 What does java do?

G51PRG-Sem2 What is the trick that enables java to run on any computer?

G51PRG-Sem2 There are two fundamental types of data object in Java what are they?

G51PRG-Sem2 A variable in java has the name fred. What does the java run time need to know about fred?

G51PRG-Sem2 I enter at the keyboard. What type am I creating?

G51PRG-Sem2 A byte in computer memory contains What does it represent?

G51PRG-Sem2 A Simple Java Program: public static void main(String args[]) { System.out.println(“ I Love G51PRG”); } What is the meaning of the word static in this program?

G51PRG-Sem2 What does new really do in Java?

G51PRG-Sem2 What is output by this program? public static void main(String args[]) { String fred; fred = “fred”; fred.toUpperCase(); System.out.println(fred); }

G51PRG-Sem2 What is the value of x? int x; x = 2 * / 2 + 2; Why?

G51PRG-Sem2 A Proper Object import java.io.*; public class ReadDouble { double readDouble() { String aLine = ""; double d = 0.0; boolean failed = false; BufferedReader input; input = new BufferedReader(new InputStreamReader(System.in)); try { aLine = input.readLine(); } catch (Exception e) { failed = true; } try { d = Double.parseDouble(aLine); } catch (Exception e) { failed = true; } if(failed) { System.out.println("Invalid Coefficient!"); System.exit(-1); } return d; } what filename should be used?

G51PRG-Sem2 myArray has room for 8 elements the elements are accessed by their index in Java, array indices start at myArray =

G51PRG-Sem2 Declaring Arrays int myArray[]; declares myArray to be an array of integers myArray = new int[8]; sets up 8 integer-sized spaces in memory, labelled myArray[0] to myArray[7] int myArray[] = new int[8]; combines the two statements in one line

G51PRG-Sem2 Assigning Values refer to the array elements by index to store values in them. myArray[0] = 3; myArray[1] = 6; myArray[2] = 3;... can create and initialise in one step: int myArray[] = {3, 6, 3, 1, 6, 3, 4, 1};

G51PRG-Sem2 Iterating Through Arrays for loops are useful when dealing with arrays: for (int i = 0; i < myArray.length; i++) { myArray[i] = getsomevalue(); }

G51PRG-Sem2 Arrays of Objects So far we have looked at an array of primitive types. –integers –could also use doubles, floats, characters… Often want to have an array of objects –Students, Books, Loans …… Need to follow 3 steps.

G51PRG-Sem2 Declaring the Array 1. Declare the array private Student studentList[]; –this declares studentList 2.Create the array studentList = new Student[10]; –this sets up 10 spaces in memory that can hold references to Student objects 3. Create Student objects and add them to the array: studentList[0] = new Student("Cathy", "Computing");

G51PRG-Sem2 Java Methods & Classes

G51PRG-Sem2 Classes ARE Object Definitions OOP - object oriented programming code built from objects Java these are called classes Each class definition is coded in a separate.java file Name of the object must match the class/object name

G51PRG-Sem2 Simple class class Fruit{ int grams; int cals_per_gram; }

G51PRG-Sem2 Methods... Class Fruit{ nt grams; int cals_per_gram; int total_calories() { return(grams*cals_per_gram); } }

G51PRG-Sem2 Another Class public class Point { public double x, y; private attribute; public Point() { x = 0; y = 0; size = 1; } public double getSize() { return size; } public void setSize(int newSize) { size = newSize; } }

G51PRG-Sem2 Source Files Put: public class Fred { } INFred.java A Source file can have only one public class in it

G51PRG-Sem2 Methods A method is a named sequence of code that can be invoked by other Java code. A method takes some parameters, performs some computations and then optionally returns a value (or object). Methods can be used as part of an expression statement. public float convertCelsius(float tempC) { return( ((tempC * 9.0f) / 5.0f) ); }

G51PRG-Sem2 Method Signatures A method signature specifies: –The name of the method. –The type and name of each parameter. –The type of the value (or object) returned by the method. –The checked exceptions thrown by the method. –Various method modifiers. –modifiers type name ( parameter list ) [throws exceptions ] public float convertCelsius (float tCelsius ) {} public boolean setUserInfo ( int i, int j, String name ) throws IndexOutOfBoundsException {}

G51PRG-Sem2 Using objects Here, code in one class creates an instance of another class and does something with it … Fruit plum=new Fruit(); int cals; cals = plum.total_calories(); Dot operator allows you to access (public) data/methods inside Fruit class

G51PRG-Sem2 Public/private Methods/data may be declared public or private meaning they may or may not be accessed by code in other classes … Good practice: –keep data private –keep most methods private well-defined interface between classes - helps to eliminate errors

G51PRG-Sem2 Creating objects Following code creates an instance of the Fruit class Fruit plum; defines the plum object plum = new Fruit(); creates it in memory the content of the Fruit class must be defined in another file Fruit.java

G51PRG-Sem2 Constructors The line plum = new Fruit(); invokes a constructor method with which you can set the initial data of an object You may choose several different type of constructor with different argument lists eg Fruit(), Fruit(a)...

G51PRG-Sem2 Overloading Can have several versions of a method in class with different types/numbers of arguments Fruit(){grams=50;} Fruit(a,b){ grams=a;cals_per_gram=b; } By looking at arguments Java decides which version to use

G51PRG-Sem2 Object Oriented Programming instance variablesmethods an object a program speed = 45.7; gear = 3; break() changeGears(g) messages

G51PRG-Sem2 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 –Interface same despite different data types car auto- matic manual Super class Subclasses draw()

G51PRG-Sem2 Example: Russian Roulette /6 5/6 2/6 4/6 3/6 4/6 2 5/6 3/6 2/6 1/6

G51PRG-Sem2 Web of message calls... model player2 player1 pass( ) revolver pass( ) revolver load() trigger()

G51PRG-Sem2 Roulette Model Player Revolver model player1 player2 revolver new Classes instances

G51PRG-Sem2 Inheritance... Important feature of OOP - new classes can be based on existing classes eg. Could define a `specialized’ type of Fruit class called Citrus … Has all methods of Fruit plus possibly some new ones eg class Citrus extends Fruit{ void squeeze(){….} }

G51PRG-Sem2 Inheritance II How to use … eg. Citrus lemon = new Citrus(); lemon.squeeze(); lemon.total_calories(); old methods exist alongside new methods …

G51PRG-Sem2 Overriding Even more powerful concept of OOP –can override the functionality of one method in a descendant class eg. Add method peel() to Fruit class. Since Citrus extends Fruit this method will also be available to an instance of Citrus But can redefine content of peel() inside of Citrus - the new definition hides the earlier...

G51PRG-Sem2 Libraries Java comes with libraries for creating GUIs and network applications and for embedding in Web pages- java.applet.Applet eg import java.awt.*; compile to byte code - can be run by any system with a Java interpreter - portable! Relatively robust and secure

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

G51PRG-Sem2 Where to Revise Eckel, B. Thinking in Java –Free online book: books/eckel/ - Sun Tutorials: utorial/index.html