Introduction to Object-Oriented Programming



Similar documents
Install Java Development Kit (JDK) 1.8

CS 106 Introduction to Computer Science I

Introduction to Java

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Java Crash Course Part I

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Moving from CS 61A Scheme to CS 61B Java

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Java CPD (I) Frans Coenen Department of Computer Science

Installing Java (Windows) and Writing your First Program

JAVA ARRAY EXAMPLE PDF

Programmierpraktikum

Computer Programming C++ Classes and Objects 15 th Lecture

Konzepte objektorientierter Programmierung

LAB4 Making Classes and Objects

How to use the Eclipse IDE for Java Application Development

Basic Java Syntax. Program Structure

Object-Oriented Programming Lecture 2: Classes and Objects

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Chapter 1 Java Program Design and Development

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

CPLEX Tutorial Handout

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Java Programming Fundamentals

sveltest: A testing language

Crash Course in Java

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

J a v a Quiz (Unit 3, Test 0 Practice)

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

AP Computer Science Java Subset

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Comp 248 Introduction to Programming

CS170 Lab 11 Abstract Data Types & Objects

Object-Oriented Programming in Java

Java Software Development Kit (JDK 5.0 Update 14) Installation Step by Step Instructions

AP Computer Science Static Methods, Strings, User Input

Tutorial: Getting Started

Object Oriented Software Design

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

Contents. 9-1 Copyright (c) N. Afshartous

Installing Java. Table of contents

Java with Eclipse: Setup & Getting Started

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

Java Classes. GEEN163 Introduction to Computer Programming

ESPResSo Summer School 2012

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Lab 1A. Create a simple Java application using JBuilder. Part 1: make the simplest Java application Hello World 1. Start Jbuilder. 2.

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

2/1/2010. Background Why Python? Getting Our Feet Wet Comparing Python to Java Resources (Including a free textbook!) Hathaway Brown School

Java from a C perspective. Plan

Object Oriented Software Design

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Chapter 2 Introduction to Java programming

Chapter 1 Fundamentals of Java Programming

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

LAB 1. Familiarization of Rational Rose Environment And UML for small Java Application Development

Programming Fundamentals I CS 110, Central Washington University. November 2015

CSC 551: Web Programming. Spring 2004

6.1. Example: A Tip Calculator 6-1

1001ICT Introduction To Programming Lecture Notes

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Chapter 2 Basics of Scanning and Conventional Programming in Java

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

Sample CSE8A midterm Multiple Choice (circle one)

What Perl Programmers Should Know About Java

Chapter 5 Names, Bindings, Type Checking, and Scopes

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Using Files as Input/Output in Java 5.0 Applications

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

An Incomplete C++ Primer. University of Wyoming MA 5310

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

An Introduction to Computer Science

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Java Basics: Data Types, Variables, and Loops

CISC 181 Project 3 Designing Classes for Bank Accounts

Chapter 13 Storage classes

Introduction to Programming in Java

More on Objects and Classes

Using ilove SharePoint Web Services Workflow Action

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Fundamentals of Java Programming

Implementing Rexx Handlers in NetRexx/Java/Rexx

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Transcription:

Introduction to Object-Oriented Programming Programs and Methods Christopher Simpkins chris.simpkins@gatech.edu CS 1331 (Georgia Tech) Programs and Methods 1 / 8

The Anatomy of a Java Program It is customary for a programmer s first program in a new language to be Hello, World. Here s our HelloWorld.java program: public class HelloWorld { public static void main(string[] args) { System.out.println("Hello, world!"); The first line declares our HelloWorld class. class is the syntax for declaring a class, and prepending with the public modifer means the class will be visible outside HelloWorld s package. Because we didn t declare a package explicitly, HelloWorld is in the default package. More on packages in a future lectrue. The code between the curly braces, {... define the contents of the HelloWorld class, in this case a single method, main CS 1331 (Georgia Tech) Programs and Methods 2 / 8

public static void main(string[] args) In order to make a class executable with the java command, it must have a main method: public static void main(string[] args) {... The public modifier means we can call this method from outside the class. The static modifer means the method can be called without instantiating an object of the class. Static methods (and variables) are sometimes called class methods. void is the return type. In particular, main returns nothing. Sometimes such subprograms are called procedures and distinguished from functions, which return values. After the method name, main, comes the parameter list. main takes a single parameter of type String[] - an array of Strings. args is the name of the parameter, which we can refer to within the body of main CS 1331 (Georgia Tech) Programs and Methods 3 / 8

Methods The main method is a special method that is used as the entry point for a Java program. We can define other methods as well. Consider this method from NameParser.java: public static String extractlastname(string name) { int commapos = name.indexof(","); int len = name.length(); String lastname = name.substring(0, commapos).trim(); return lastname; Similar to our main method but: returns a String value takes a single parameter of type String CS 1331 (Georgia Tech) Programs and Methods 4 / 8

Method Parameters In this method: public static String extractlastname(string name) { int commapos = name.indexof(","); int len = name.length(); String lastname = name.substring(0, commapos).trim(); return lastname; name is a parameter (or formal parameter), a local scope variable within the extractlastname method. It is bound to a value when the method is called. In the statement: String lastname = extractlastname(fullname); the right-hand side, extractlastname(fullname), is a method invocation (or method call). We say that fullname is the argument (or actual parameter) to this invocation of the extractlastname method. CS 1331 (Georgia Tech) Programs and Methods 5 / 8

Local Variables Method parameters and variables declared inside the method are local to the method, invisible outside the method. Local variables "shadow" variables of the same name in an enclosing scope. public class Methods {... // Note that static variables cannot appear inside methods static String message = "Global message."; static int a = 5; public static int add(int a, int b) { String message = "Adding " + a + " and " + b; System.out.println(message); return a + b; In the add method, the parameter a shadows the static variable a, and the local variable message shadows the static variable message. CS 1331 (Georgia Tech) Programs and Methods 6 / 8

Methods as Expressions Methods that return values are expressions which can be used anywhere a value of the method s return type can be used. Given: public static int add(int a, int b) {... which returns an int, this: x + (x + y) is equivalent to this: x + add(x, y) See Methods.java. CS 1331 (Georgia Tech) Programs and Methods 7 / 8

Closing Thoughts Methods are subprograms with input (parameters), processing (a sequence of statements), and output (return value). Methods are a powerful form of procedural abstraction, another step in the building of complex programs from simple parts. CS 1331 (Georgia Tech) Programs and Methods 8 / 8