Week51 APCS-AB: Java Intro Introduction to Java, Classes & Objects October 4, 2005.

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

Programming for Beginners
INTRODUCTION Chapter 1 1. Java CPSC 1100 University of Tennessee at Chattanooga 2  Difference between Visual Logic & Java  Lots  Visual Logic Flowcharts.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
CHAPTER 1: AN OVERVIEW OF COMPUTERS AND LOGIC. Objectives 2  Understand computer components and operations  Describe the steps involved in the programming.
JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Explain the Java virtual machine.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
1 Fall 2008ACS-1903 Chapter 1 Topics Java History Java Programs Why Program? Computer Systems: Hardware and Software Programming Languages What Is a Program.
How to Create a Java program CS115 Fall George Koutsogiannakis.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 1 “ Introduction to Java and OOP”
Outline Java program structure Basic program elements
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 1 Introduction.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Getting Started with Java
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Introducing Java.
Lecture 1 Introduction to Java MIT- AITI 2004 What is a Computer Program? For a computer to be able to do anything (multiply, play a song, run a word.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
A First Program Using C#
 Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library.
An intro to programming. The purpose of writing a program is to solve a problem or take advantage of an opportunity Consists of multiple steps:  Understanding.
Introduction to Python
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
Lecture 1 Introduction to Java MIT-AITI Ethiopia 2004.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
The Java Programming Language
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Chapter 1: Introduction to Programs, and Java 1. Objectives To review programs (§ ). To understand the relationship between Java and the World Wide.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Introduction to programming in the Java programming language.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
We will talking about story of JAVA language. By Kristsada Songpartom.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
Week51 APCS-A: Java Assignments & Syntax October 3, 2005.
CS 177 Recitation Week 1 – Intro to Java. Questions?
ITP 109 Week 2 Trina Gregory Introduction to Java.
Chapter 1: Introduction to Computers and Programming.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Chapter 1 Object Orientation: Objects and Classes.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Week 21 Introduction to Programming Ms. Knudtzon C Period Lecture 3.
Working with Java.
Content Programming Overview The JVM A brief look at Structure
Chapter 3 GC 101 Java Fundamentals.
Lecture 1: Introduction to JAVA
Console Output, Variables, Literals, and Introduction to Type
Introduction to Computer Science / Procedural – 67130
Computer Programming Methodology Introduction to Java
Java programming lecture one
Statements, Comments & Simple Arithmetic
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introducing Java.
In this class, we will cover:
Instructor: Alexander Stoytchev
Presentation transcript:

week51 APCS-AB: Java Intro Introduction to Java, Classes & Objects October 4, 2005

week52 The Java Programming language Section 1.4 of your book has a history of Java as a programming language Developed in the early 90s by James Gosling at Sun Microsystems The Java Platform has three major groups:  Java 2 Platform, Standard Edition (J2SE) We focus on this one  Java 2 Platform, Enterprise Edition (J2EE)  Java 2 Platform, Micro Edition (J2ME)

week53 A simple Java Program // HelloWorld.java // Author: Ms.K // Demonstrates a simple java program public class HelloWorld{ public void sayHello(){ System.out.println(“Hello”); } /* This is an example of a multi-line comment in the book it talks about the main method: public static void main(String [ ] args) we won’t be programming main until later */ }

week54 How Java works public class myProgram{ sayHello(); } Compiler (javac) Output (code) Program in the Java language Checks for errors, makes sure your code will run Java bytecode.class file Hello Class! Java Virtual Machine (JVM) Translates the bytecode to something the underlying platform (computer) understands, and runs your program

week55 Programming Languages Languages can be:  High level or low level  Platform dependent or independent  Procedural, functional, logic, or object-oriented

week56 Java is… A high-level, Object-oriented, Platform independent Programming Language What does that mean?

week57 Java as a Language High-level – More like a real language, can be read by a human and understood, made more for ease of use for people than for the computer Object-oriented – think about problems from the things (objects) that are in the problem and what those objects can do (methods)  This allows you to be able to: design code in a natural way easily add new capabilities without retesting all the code Platform independent – code that you write on one computer can be transported and run on another computer without re-compiling

week58 OO vs. Functional Programming Scheme is a functional programming language  Programs are written using only operations to be performed on inputs, without using any temporary variables to store intermediate results (definition from Java is an object-oriented programming language  Programs are written to model objects in the world, the focus is on what objects are part of the problem, what those objects encapsulate and how they are manipulated, instead of on the procedures or functions used

week59 OO Programming as Modeling The parts of the model are objects in the problem domain What are the properties of objects?  Objects can be organized into categories We will talk later about inheritance & polymorphism  Objects do things They have methods to accomplish tasks or to communicate with other objects  Objects know things They have internal state, variables that hold information about themselves

week510 Classes A class describes (abstractly) a category of objects  Think of a class as a blueprint for an object – it describes the boundaries of what an object can know or can do Instantiation is the process of creating the object instance from the class  An object must be instantiated before we can use it

week511 What does this all mean? What kinds of problems is Scheme geared to help us with? What kinds of problems is Java geared to help us with? What does this all tell us about the relative power of these two different languages?  Remember, programming languages are tools.

week512 Lewis & Loftus: Chapter One Read/Skim it at your leisure - it is an overview of a lot of the ideas that we will cover (or a basic intro to some of the things that we may not have time to really discuss much)  1.1 Computer Processing (overview, binary, analog/digital)  1.2 Hardware Components (architecture, I/O devices, memory, cpu)  1.3 Networks (connections, local-area & wide-area networks, Internet, World Wide Web, urls)  1.4 Java Programming Language (Sample program, comments, identifiers & reserved words, white space)  1.5 Program Development (language levels, editors, compilers & interpreters, development environments, syntax & semantics, errors)  1.6 Object-Oriented Programming (Problem-Solving, OO Software Principles)

week513 Role Play Class Activity: Bamboozler, Acrobat & Calculator  (from

week514 APCS-AB: Java Intro Introduction to Java & BlueJ October 5, 2005

week515 Java for Modeling As we mentioned yesterday, one of the primary uses of Java (or any OOP language) is to model objects in the real world So let’s try to model something simple, a dog

week516 public class Dog { // This is a comment, used to describe code } Creating a Dog class in Java So everyone can access it Tell java that this is a class Name the class Curly brace starts the class This is the start of a one-line comment Curly brace to end the class CODE GOES HERE: what the Dog knows and what it does

week517 Object/Instance A specific description, an instance of a class  A specific dog that we are modeling An object knows things and does things  Also called attributes and behaviors Things an object knows are variables  Data that is unique for an object of a given type Things an object does are methods Variables and methods are defined as part of the class  Variable values are set only for a specific instance

week518 Dog Class public class Dog{ String breed; String name; int size; public void bark(){ } } These are variables. Variables have a data type and a name This is a method. Right now it doesn’t do anything A dog object would have individual values for each of these variables

week519 My Dog Instance Breed: Bulldog Name: Bully Size: 24 Color: Purple This dog can bark (invoke the method)

week520 What’s a Method? Things an object does void bark() { System.out.println(“Ruff! Ruff”); } The method doesn’t return anything The name of the method The empty parenthesis means there are no parameters in the method Brackets again! This is a statement; this tells the computer what you want to do in this method

week521 What’s a Statement? A line of code that gives a specific instruction Must end with a semicolon int x; /* a declaration is a type of statement */ x = 5; // an assignment statement System.out.println(“Ruff! Ruff”); /* a print statement */

week522 What if you want to teach your dog to bark differently? Make the dog say “Grrruffff”, “Bow-wow”, or “Ruff Ruff”  Could change the Dog program each time and recompile like we did yesterday  Or we could have the method take a parameter (a String) {{ Explore this together in BlueJ }}

week523 BlueJ

week524 How BlueJ works BlueJ is a freely available program  An interactive development environment (IDE) designed to help students learn to program  Makes it easy to sit down, write, and compile a program we’ll see later that it’s not always easy in other environments where you have to deal with classpaths, file systems, DOS commands, etc  Has visualization of class structure (very helpful for beginning and advanced programmers)  Can directly create objects and then interact with their methods

week525 Starting in BlueJ BlueJ has a template that gives you some starting code It automatically creates: An instance variable A constructor An example method, with example javadoc comments

week526 Homework Installing Java and BlueJ at home (handout) Mid-quarter comments (for the teacher)  How is this class going for you?

week527 Lab 1: Introduction to Java and BlueJ Open BlueJ (and make shortcut on desktop) Make new project called “Eyes” Add class from file (“JavaEyes.java”) Compile it Run it  Call Methods on it Look at the code (open editor)

week528 APCS-AB: Java Assignments & Syntax October 6, 2005

week529 Syntax Syntax is…  The rules governing the structure of a language  The rules that the compiler understands  Defines meaning of the various symbols used in a language In Java if you get the syntax wrong, the compiler won’t know what you are trying to do So it won’t do anything (it gives you an error)

week530 Concatenation Joining together or appending one string to the end of another String literals cannot extend across multiple lines in your program  You must use the plus sign to concatenate two string literals together System.out.println(“This is a bad program because it extends across multiple lines without concatenating the strings together. This will not compile.”); System.out.println(“This is a good program” + “ because it uses the + sign to extend” + “ the program across multiple lines.”);

week531 Variables A variable is a name for a location in memory that stores data When you declare a variable, you tell the compiler to reserve a certain size piece of memory to store data in (based on the type of data)  We will talk more about the types of data tomorrow Example:  String name;  int number;

week532 Variables Variables need a data type and a identifier. Think of a variable as a cup. A container. It holds something.  The data type tells Java the size of the container.  The identifier is there so you know which container is yours. name number

week533 Assignments When you declare a variable, you just set aside the memory - but that space is “blank” To actually place something in the variable, you need to assign a value to it:  name = “Henry”;  number = 123; number “Henry” 123 name

week534 Assignment Statements More examples:  String anotherName = “Andrew Udovic”;  int num = 5 + 4*23; In Java, here are the basic rules:  On the left side is always the variable name, the “container” in which we are going to store data  On the right side could be a single piece of data, or an expression that has to be evaluated The single piece of data, or the result of whatever is evaluated is then “assigned” to the variable (ie - placed into the memory location)  You always end each statement with a semi-colon

week535 A program example public class Example { String name; int number; //make the containers public void doAssignment(){ name = “Nick”; number = 123; printStuff(); } private void printStuff(){ System.out.println(“The name in this program is: “ + name); System.out.println(“The number is” + number); }

week536 Additional assignments If we reassign a value to a variable, the new value is written to memory.. What happens to the old value? int myVar = 123; myVar = ; If we just reference (use) a variable, the value is not changed System.out.print( “myVar is: “ + myVar); // after this call, myVar still has stored in it

week537 Strongly Typed Java doesn’t let us store the wrong kind of data in our variables  The compiler will cause an error if you try to use the wrong kind of data for a variable. If we declare a variable of type String: String s; Then we can’t put an integer into that String s = 1232; //Compile error: incompatible types - found int but expected java.lang.String

week538 Constants Sometimes we want to store a piece of data that will never change  For example, we may want to say that a table at lunch can hold no more than 12 people. If we just use the number 12 in our program, people looking at our code may not know where it came from So we give it a name like MAX_PEOPLE to help explain what the piece of data is doing in a program Also, lets say that we suddenly get new lunch tables that have a maximum of 15 people…  We only have to change the data in one place, instead of trying to find all the places that the number 12 was used in our program Constants are not variables (the value can never change)  they hold one value for the durations of the program

week539 Constants in Java To declare a constant in Java, you use the reserved word final By convention, we use uppercase letters to name constants so that we can tell them about from regular variables final int MAX_PEOPLE = 12; The compiler will then prevent anybody from changing the value once it has been set to the initial value

week540 Naming Practices in Programming Class names start with a capital letter  public class Dog { } Object names start with a lowercase letter  Dog fido = new Dog(); Method names and variables start with lowercase, but use capital letters for subsequent words  public void changeMyOil() {}  int myAge; Constants use all caps  MAX_INT_SIZE

week541 Arithmetic Operators What if we want to have a counter variable?  Something that starts at 0 and then counts up, one-by-one depending on what else we may be doing in a program int x = 0; So we could do: x = x + 1;  So what would x equal? How does this all work? We do this a lot in programming, so there’s shortcut for this (called the increment operator)  x++;// is the same as x = x+1;

week542 Other Arithmetic Operators Minus  x = x -1; Multiplication  x = x * 5; Division:  x = x/2; What do you think will happen if we try to divide by zero? Modulo  x = 5%2; You have the special operator for increment, do you think there are any similar operators for these functions?