Chapter 2 Classes and Methods I Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.

Slides:



Advertisements
Similar presentations
Chapter 3 Fundamental Data Types of Java
Advertisements

Chapter 14 Recursion Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,, E. Reingold.
Chapter 4 Decision Making Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold.
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
IT151: Introduction to Programming
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
 2005 Pearson Education, Inc. All rights reserved Introduction.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Outline Java program structure Basic program elements
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Invitation to Computer Science 5th Edition
Chapter 6 Iteration Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Java: Chapter 1 Computer Systems Computer Programming II.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 1 What is Programming? Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 2: Java Fundamentals
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
A First Simple Program /* This is a simple Java program. Call this file "Example.java".*/ class Example { // Your program begins with a call to main().
Introduction to programming in the Java programming language.
Chapter 15 Text Processing and File Input/Output Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Creating a Java Application and Applet
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Chapter 9 Nested Loops and Two-Dimensional Arrays Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Fundamentals of Programming I Overview of Programming
Chapter 3 Syntax, Errors, and Debugging
Chapter 3 GC 101 Java Fundamentals.
Introduction to the C Language
Chapter 3 Introduction to Classes, Objects Methods and Strings
Advanced Programming Lecture 02: Introduction to C# Apps
Introduction to Java Programming
Chapter 1: Computer Systems
MSIS 655 Advanced Business Applications Programming
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Computer Programming-1 CSC 111
Instructor: Alexander Stoytchev
Presentation transcript:

Chapter 2 Classes and Methods I Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold

Chapter Preview In this chapter we will: describe structure of Java programs present techniques for creating and using class instances introduce two classes for doing output: –OutputBox for text output –DrawingBox for graphical output

Running Java Programs Enter the program source code in a data file called Hitwall.java using an editor Compile the source program by typing javac Hitwall.java Execute the compiled program by typing java Hitwall

Program Elements – Part 1 white space –blank lines and spaces includes in program source listings to make things more readable comments –lines beginning with two slashes // –single or multiple lines enclosed by /* */ –that allow the programmer to insert notes to help other people understand the program documentation –program comments and data files describing a program’s structure and behavior

Program Elements – Part 2 import directive –tells the Java compiler which packages the program makes use of packages –predefined collections of programs providing services to many programmers (e.g. CSLib.* package used throughout the text) class heading –needs to be included at the beginning of every program –class name must match root of file name –each class must be stored in a file of its own

Program Elements – Part 3 main method –the chief computational unit of a Java application –executed first when application is run functions –also known as methods –define operations that may be applied to a Java data object (class instance) body –Java statements that contain the implementations of classes and their methods

Program Elements – Part 4 variable declaration –statement giving the name and data type of a data location used by the program executable statement –statement which manipulates variables or determines program control during execution atomic statements –single Java expressions terminated by a ; variable assignment statement –executable statement which copies a particular value to a data location

Identifiers Java uses identifiers to name –variables –methods –classes –packages Syntax rules –Must begin with a letter (upper- or lower-case) –May be followed by any number (including 0) of letters and digits –The characters $ and _ are considered letters –Java identifier names are case sensitive –May not duplicate a Java keyword (e.g. class or main)

Building a Simple Class import CSLib.*; public class WarningMouse { // Exterminate a rodent. // Author: C. Mickunas 11/21/00 public void shout() { TrickMouse alert; alert = new TrickMouse(); alert.setTitle(“WARNING”); alert.speak(“Look out!”); }

Method shout( ) public method –Means that methods in other classes (or clients) may use it void return type –Means it does not return a value to the caller (client)

OutputBox Class int literals 0, 1, -1, 2, -2, 3, -3, … double literals 3.45, -48.2, 33.0, … print –Display text representation of argument and leave output cursor on the same line println –Display text representation of argument and advance output cursor to the next line

Using OutputBox import CSLib.*; public class Forecast { // Give the weather forecast. // Author: E. Reingold 11/12/00 public void predict() { OutputBox out; out = new OutputBox(); out.print(“The temperature will be ”); out.print(-10); out.println(“ degrees.”); out.println(“That’s cold, folks!”); }

DrawingBox Window A DrawingBox window is divided into a rectangular grid of picture elements (pixels) The size of a pixel depends on the resolution (the number of pixels in the grid) of your workstation monitor A typical screen resolution might be 1028 by 768 pixels. Pixels may have two colors (black or white) or many colors (depending on the resolution of the monitor)

Using DrawingBox import CSLib.*; public class Concentric { // Draw concentric circles. // Author: A. Baranowicz 12/2/00 public void drawThem() { DrawingBox g; g = new DrawingBox(); g.setDrawableSize(300, 300); g.drawOval(110, 110, 80, 80); g.drawOval(95, 95, 110, 110); g.drawOval(80, 80, 140, 140); }

Concentric Client Program import CSLib.*; public class ConcentricClient { public static void main(String[] args) { Concentric circles; circles = new Concentric(); circles.drawThem(); }

Color DrawingBox defines several symbolic constants to simplify selecting drawing colors Color.white, Color.black, Color.red, Color.blue, etc. You may select the color to draw with by calling setColor before drawing g.SetColor(Color.blue); g.fill Rectangle(0, 0, 50, 25);