CS 335 Lecture 02 Java Programming

Similar documents
CS 335 Java Programming Controls. Fall 2007

CS 335 Graphics, Image Processing, User Interface Design

CS335 Graphics and Multimedia

Methods (Deitel chapter 6)

Methods (Deitel chapter 6)

Chapter 5 - Methods Prentice Hall, Inc. All rights reserved.

Introduction to Java

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

CS 231 Data Structures and Algorithms, Fall 2016

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Language Features. 1. The primitive types int, double, and boolean are part of the AP

CS 251 Intermediate Programming Methods and Classes

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

Lecture 5: Methods CS2301

Chap. 3. Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L,

System.out.print(); Scanner.nextLine(); String.compareTo();

JAVA: A Primer. By: Amrita Rajagopal

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

CS 251 Intermediate Programming Methods and More

Packages & Random and Math Classes

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

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

The Java Series. Java Essentials Advanced Language Constructs. Java Essentials II. Advanced Language Constructs Slide 1

Java Bytecode (binary file)

Class, Variable, Constructor, Object, Method Questions

CS506 Web Design & Development Final Term Solved MCQs with Reference

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

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

CS 11 java track: lecture 3

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Mathematics/Science Department Kirkwood Community College. Course Syllabus. Computer Science CSC142 1/10

CS111: PROGRAMMING LANGUAGE II

LECTURE 2 (Gaya College of Engineering)

CSE1720 Delegation Concepts (Ch 2)

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

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

A Quick Tour p. 1 Getting Started p. 1 Variables p. 3 Comments in Code p. 6 Named Constants p. 6 Unicode Characters p. 8 Flow of Control p.

Quiz on Tuesday April 13. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 4. Java facts and questions. Things to try in Java

Do not start the test until instructed to do so!

Inheritance and Interfaces

Full file at

Class Libraries and Packages

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

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Core JAVA Training Syllabus FEE: RS. 8000/-

Selected Java Topics

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Formatting Output & Enumerated Types & Wrapper Classes

Kickstart Intro to Java Part I

Lecture Set 4: More About Methods and More About Operators

Page 1. Human-computer interaction. Lecture 1b: Design & Implementation. Building user interfaces. Mental & implementation models

Lesson 3: Accepting User Input and Using Different Methods for Output

CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

ITI Introduction to Computing II

Zheng-Liang Lu Java Programming 45 / 79

Chapter 4. Defining Classes I

Chapter 6 Introduction to Defining Classes

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

CS110D: PROGRAMMING LANGUAGE I

Text User Interfaces. Keyboard IO plus

CSCE3193: Programming Paradigms

CS3157: Advanced Programming. Outline

Lecture 02, Fall 2018 Friday September 7

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

CS 11 java track: lecture 1

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

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

javagently jg3e H:\ ProgTwo Programming II Lecture 7 A case study (class containing an array) and model diagrams. 02/02/2003 Dr Andy Brooks 1

Pace University. Fundamental Concepts of CS121 1

COT 3530: Data Structures. Giri Narasimhan. ECS 389; Phone: x3748

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

CS-202 Introduction to Object Oriented Programming

Course Outline. Introduction to java

Syntax of Variable Declarations. Variables Usages as Expressions. Self-assignment Statements. Assignment. Scoping and Naming Rules

1 Shyam sir JAVA Notes

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

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

Introduction to Classes and Objects. David Greenstein Monta Vista High School

CSC 1214: Object-Oriented Programming

PIC 20A The Basics of Java

The Java programming environment. The Java programming environment. Java: A tiny intro. Java features

Chapter 1: Introduction to Computers, Programs, and Java

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

BM214E Object Oriented Programming Lecture 8

More About Objects and Methods

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

CS 161: Object Oriented Problem Solving

Programming. Syntax and Semantics

Part 8 Methods. scope of declarations method overriding method overloading recursions

CS 161: Object Oriented Problem Solving

Lecture Set 4: More About Methods and More About Operators

CS 180 Final Exam Review 12/(11, 12)/08

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

School of Informatics, University of Edinburgh

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

Full file at

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

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

Using Java Classes Fall 2018 Margaret Reid-Miller

Transcription:

1 CS 335 Lecture 02 Java Programming

Programming in Java Define data Calculate using data Output result Java is object-oriented: Java program must: Merge data and functions into object Invoke functions to operate on data 2 Define data and functions (in a class) Invoke functions to compute things

Object-Oriented Programming: Classes A class is an object definition, and includes data and functions on that data: public class MyCourseGrade { int pset1; int pset2; : int final_exam; object data 3 } computeaverage() { : } object method

Classes Class: code which defines an object Object: a variable (data + methods) which is an instance of a class Java program: a bunch of class definitions, variables, etc. 4

Classes One special class (the "mother of all classes") contains main(), and this is where flow of control begins: class Test main Kernel of execution is here! class AnotherClass (like MyCourseGrade) memberfunc1() 5 memberfunc2()

Notes Java flow of control starts in main(), in whichever class main() is defined There can only be one class per file (unless you are defining subclasses) The filename must match the class name in a Java source file! 6

Example 1 A Java program with one class and one member called main(): import java.io.*; 7 public class Test { public static void main( String[] ) throws IOException { System.out.println("That it, folks!"); } }

Example 1: Scoping class Test member functions data (none defined) main() 8

Example 2 Add another member function: 9 import java.io.*; public class Test { public static void main( String[] ) throws IOException { System.out.println( That it, folks!"); } public void dostuff() { System.out.println( doing stuff." ); } }

Example 2: Scoping class Test member functions dostuff() data (none defined) main() 10

Example 3 Define an object of class Test and make a function call import java.io.*; public class Test { public static void main( String[] ) throws IOException { Test t; // t is of type "Test" t = new Test(); // allocate object t.dostuff(); // call member function System.out.println( "That it, folks!"); } 11

Example 3 (continued) public void dostuff() { System.out.println( "I'm doing stuff." ); } } 12 Notes: Static methods cannot access nonstatic class members directly main() must always be static

Example 4 13 Test.java: import java.io.*; public class Test { public static void main( String[] ) throws IOException { Stuff t; t = new Stuff(); t.dostuff(); System.out.println( "That it, folks!" ); } } Put main class and a different class in separate files:

Example 4 Stuff.java: public class Stuff { public void dostuff() { System.out.println( "I'm doing stuff." ); } } Notes One class per file To compile: javac Test.java 14

Java I/O The System object provides a way to manage I/O from a more traditional "stream" (terminal window). GUI-based I/O requires the action() method to deal with GUI mouse events. The System object requires no action() method 15 But terminal I/O is inadequate in a browser-based (GUI) environment.

Summary of Some Basic Java Constructs Everything is related to objects: Data declaration: 16 int i; // declare i to be an int Test t; // declare t to be // an object of type Test i = 0; // set the int i equal to 0 t = new Test(); // initialize t and allocate space // using a constructor

Java Constructs Flow of Control: Traditional, but with object-oriented syntax for function calls and member functions Where control starts in the Applet class is important Executable statements Similar to C/C++: while, for, if/else, switch, etc. 17

Java Control Structures Selection: If, If/Else, Switch Repetition (looping): While, For, Do/While Assignment: Expressions, increment/decrement 18

19 Java Reserved Words

20 Example: Average

21

22 Assignments and Expressions

23 Increment/Decrement

24 Java Operators

Java Operators Order is important in post/pre increment operators Increment operators (and combined operators like *=) implicitly are assignments; they must always have an LVALUE as a target One can still confuse = with == 25

26 Primitive Data Types

Java Data Types Java is a strongly-typed language Primitive data types are meant to be portable across all platforms Note that char is 16 bits in Java, not 8! 27

CS 335 Lecture 03 Java Programming Methods Fall 2003 28

Java Methods Methods are functions/procedures attached to data via classes Methods are always invoked on an object, either implicitly or explicitly As with procedures, methods do not need to return a value (void is an option) As with functions, methods can return a value 29

The Math Class The Math class is an object library, and one must use a Math object to invoke methods: System.out.println (Math.sqrt (16.0)); 30

31

Example class class SquareRoot { public static void void main(string[] args) args) { String outputstring; double result; result = Math.sqrt(64.0); // // result = mysquare(result); outputstring = Double.toString(result); // // outputstring = "The "The result is is " + outputstring; System.out.println(outputString); 32 }

Example (Continued) public static double mysquare (double s) s) { return s*s; } } 33

Method Definition Note user-defined method square() Parameter passing: reference and value Type definitions and coercion of arguments Explicit cast necessary when there are no promotion rules 34

Defining Methods Note scope and purpose of method: constructor accessor mutator Parameter passing vs. object-based data access Public/private method access and data hiding 35

36 Java s Allowed Promotions

The Java API Packages Java.awt: abstract windowing toolkit javax.swing: Swing GUI components java.util: date, time, random numbers, etc. java.sql: database connectivity java.io: streams, etc java.rmi: remote method invocation 37

Random Numbers Random number generation is important in most gaming/simulation situations java.util contains the random number package Typical call (returns value between 0 and 1) Math.random(); 38 value = 1+(int)(Math.random()*12)