Dept of Computer Science University of Maryland College Park

Slides:



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

Introduction to Programming G51PRG University of Nottingham Revision 1
INTRODUCTION Chapter 1 1. Java CPSC 1100 University of Tennessee at Chattanooga 2  Difference between Visual Logic & Java  Lots  Visual Logic Flowcharts.
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.
Chapter 1: Introduction
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.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
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.
CS107 Introduction to Computer Science Java Basics.
Basics Programming Concepts. Basics A computer program is a set of instructions to tell a computer what to do Machine language = circuit level language.
CS107 Introduction to Computer Science Java Basics.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to Java Java Translation Program Structure
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
By Mr. Muhammad Pervez Akhtar
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
Java FilesOops - Mistake Java lingoSyntax
Introducing Java Chapter 3 Review. Why Program in Java? Java, is an object-oriented programming language. OOP languages evolved out of the need to better.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Introduction to 1. What is Java ? Sun Microsystems Java is a programming language and computing platform first released by Sun Microsystems in The.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Introduction to Object Oriented
Conditionals, Block Statements, Style
Computer Programming Your First Java Program: HelloWorld.java.
Information and Computer Sciences University of Hawaii, Manoa
Basic concepts of C++ Presented by Prof. Satyajit De
Lecture 1b- Introduction
String Comparison, Scanner
Working with Java.
CSC201: Computer Programming
Computer Organization, Eclipse Intro
Dept of Computer Science University of Maryland College Park
Object-Oriented Programming Using Java
GC101 Introduction to computer and program
Chapter 3 Syntax, Errors, and Debugging
Lecture 1: Introduction to JAVA
Introduction to Computer Science / Procedural – 67130
Dept of Computer Science University of Maryland College Park
Introduction to.
Java Primer 1: Types, Classes and Operators
Data types and variables
CompSci 230 Software Construction
Switch, Rounding Errors, Libraries
Engineering Innovation Center
Statements, Comments & Simple Arithmetic
Introduction to C++ Programming
Hands-on Introduction to JAVA
Chapter 1: Computer Systems
Fundamentals of Programming
Units with – James tedder
Anatomy of a Java Program
Focus of the Course Object-Oriented Software Development
Object Oriented Programming in java
Computer Programming-1 CSC 111
How to Run a Java Program
Presentation transcript:

Dept of Computer Science University of Maryland College Park CMSC 131 Object-Oriented Programming I Java Introduction Dept of Computer Science University of Maryland College Park This material is based on material provided by Ben Bederson, Bonnie Dorr, Fawzi Emad, David Mount, Jan Plane

Java Programming Language Java  Why is it special? An object-oriented programming language Developed in early 1990's by James Gosling at Sun Microsystems for controlling programmable devices. Unlike C++, Java programs are portable between machines – why? C/C++ programs are compiled into machine code and is executed directly by CPU. Different machines may behave differently. Java programs are compiled into Java bytecode. This is a machine code for a “virtual computer” called the Java Virtual Machine (JVM). A JVM interpreter reads bytecode and simulates its execution.

Java Virtual Machine Java compilers produce Java bytecode To run Java bytecode, must have bytecode interpreter (“Java Virtual Machine”) on client machine JVM client source code object code Java compiler .java .class JVM client

Java Programming Concepts Object The principal entities that are manipulated by a Java program Class  A blueprint or template for the structure of an object Method  Java’s term for a procedure or subroutine. Every method belongs to some class, and a class may have many methods Main Method: There must be exactly one method called main. This is where execution begins Statements: Individual instructions. Examples: Declarations  declare (and initialize) variables Assignment  compute and assign a new value to a variable Method invocation  execute a method (which may return a result) Control flow  alter the order in which statements are executed

Sample Program Let’s look at each component of this program /* * My first sample Java program. */ public class MyFirstProgram { public static void main( String[ ] args ) { int secondsPerMinute = 60; // let’s create some variables int minutesPerLecture = 50; int totalSeconds = secondsPerMinute * minutesPerLecture; System.out.println( "There are " + totalSeconds + " seconds in a lecture.“ ); } Program Output There are 3000 seconds in a lecture. At this point we will go over each element of the program. Statements are executed top to bottom println vs. print Variables in our example are called local variables You can put blank lines in almost anytime you want Proper indenting helps readability Let’s look at each component of this program Let’s run this program in Eclipse

Java Program Organization Class: The structure around which all Java programs are based. A typical Java program consists of many classes, and typically, each class resides in its own file, whose name is based on the class’s name. The class is delimited by curly braces { … }. File name: MyFirstProgram.java: public class MyFirstProgram { … (contents of the class go here) … } A class consist of a combination of data units, called variables, and computation units, called methods.

Java Program Organization Methods: This is where all the computation takes place. Each method has a name, a list of arguments enclosed in parentheses, and body, enclosed in curly braces. public static void main( String[ ] args ) { … (contents of the main method go here) … } Variables: These are the data items that the methods operate on. Variables can be of various types, integers, for example: int secondsPerMinute = 60; int minutesPerLecture = 50;

Java Program Organization Statements: Each method consists of some number of statements. There are many different types of statements, which perform diverse tasks: Declarations  specify variable types (and optionally initialize) int x, y, z; // three integer variables String s = “Howdy”; // a character string variable boolean isValid = true; // a boolean (true or false) variable Assignments  assign variables new values x = 13; Method invocation  call other methods System.out.println( “Print this message“ ); Control flow  determine the order of statement execution Expressions/Operators  Java provides various operators, which allow you to manipulate the variables. x = (2 * y – z) / 32;

Other Java Elements: Comments Comments: are used to indicate the programmer’s intent. They do not affect the program’s execution, (ignored by compiler) but are an important part of any program. Two ways: Line comments: After // the rest of the line is a comment. These are often used to explain a single line of code Block comments: The text between any pair: /* … */ this can run over many lines or on a single line Comments are essential for good programming

Debugging Java Programs Types of errors “Compile time”  caught by Eclipse / Java compiler Syntax errors Disobeys the rules of the language; violates language’s grammar Type errors: misuse of variables “Run time”  appear during program execution Semantic errors Obeys the rules of the language but does not express them meaning you intended; Division by 0 Crash or hang or wrong outputs (because of mistakes in programming) Eclipse helps catch compile time errors Red  error Yellow  warning Debugging Process of finding and fixing problems To minimize debugging frustration  use “unit” testing Write a small part, thoroughly test it, cycle back