CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.

Slides:



Advertisements
Similar presentations
Introduction to Programming Java Lab 1: My First Program 11 January JavaLab1.ppt Ping Brennan
Advertisements

Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
INTRODUCTION Chapter 1 1. Java CPSC 1100 University of Tennessee at Chattanooga 2  Difference between Visual Logic & Java  Lots  Visual Logic Flowcharts.
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.
IT151: Introduction to Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Your First Java Program: HelloWorld.java
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
 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.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Object-oriented analysis (OOA) techniques are used to (1) study existing objects to see if they can be reused or adapted for new uses, and (2) define new.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2  Using Objects 1 Chapter 2 Using Objects.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
Intro.1 Computer Science 4: Java for Video Games Jam Jenkins D122 LSRC Dr. Dietolf Ramm D226 LSRC Instructors
Games and Simulations O-O Programming in Java The Walker School
CompSci Computer Science 4: Java for Video Games Jam Jenkins D305 LSRC Dietolf (Dee) Ramm D226 LSRC Instructor
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
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.
Introduction to Object-Oriented Programming
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 1: Introduction 1 Chapter 1 Introduction.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
CSE 131 Computer Science 1 Module 1: (basics of Java)
Classes CS 21a: Introduction to Computing I First Semester,
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Copyright © Curt Hill Turtles The beginning of media computation.
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.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.
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 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Eclipse Project. Installing Visit to download a copy for your home computerhttp:// –Get Release version 3.0 (or.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Creating a Java Application and Applet
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
11 ITI 1120 Lab # 2 Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
1/16: Intro to Java, Languages, and Environments Types of programming languages –machine languages –assembly languages –high-level languages Java environment.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object.
Introduction to Java Import Scanner class to use in our program
The eclipse IDE IDE = “Integrated Development Environment”
CompSci 230 Software Construction
Chapter 2 Using Objects.
Anatomy of a Java Program
AN INTRODUCTION TO OBJECTS AND CLASSES
Classes CS 21a: Introduction to Computing I
Introduction to Computer Science and Object-Oriented Programming
How to Run a Java Program
Presentation transcript:

CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program

CompSci 42.2Intro to Java The Plan  Go over MoveTest.java  Similar to Horstmann p. 48  Basic coding conventions  Review with GreeterTest.java (Horstmann)  More terminology with Greeter.java (Horstmann)  Homework 0 reminder  Homework 1 assigned (due in 1 week)

CompSci 42.3Intro to Java Why know the lingo?  It’s difficult to read the textbooks if you don’t understand the words  Your compiler error messages use specific words with specific meanings  You need to be able to express your questions so others can understand them  The answers you receive will use the lingo

CompSci 42.4Intro to Java Terminology to Know  Package  Class  Import  Keyword  Public  Object  Identifier  Declaration  Definition  Body  Static  Void  Return  Method  Main  Parameter  String  Array  Type  Variable  Local  Constructor  Initialize  Assign  Arguments  Comments  Calling a method  System.out.println

CompSci 42.5Intro to Java MoveTester.java import java.awt.Rectangle; public class MoveTest { public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30]

CompSci 42.6Intro to Java MoveTest.java import java.awt.Rectangle; public class MoveTest { public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] abstract windowing toolkit package Rectangle class java package import means to bring into the program classes or packages not in the package java.lang import is a keyword – it is a word with a special meaning

CompSci 42.7Intro to Java MoveTest.java import java.awt.Rectangle; public class MoveTest { public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] public means usable by everything, public is also a keyword class means instruction and data for making objects, class is a keyword MoveTest is the name of the class A class name must match the file name. Names are also called identifiers. Identifiers and keywords are mutually exclusive.

CompSci 42.8Intro to Java MoveTest.java import java.awt.Rectangle; public class MoveTest { public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); } class declaration class definition. class body {} Starts and ends class body

CompSci 42.9Intro to Java MoveTest.java import java.awt.Rectangle; public class MoveTest { public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] Static means one per class void means no return value main is the name of the method

CompSci 42.10Intro to Java MoveTest.java import java.awt.Rectangle; public class MoveTest { public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] String is a sequence of characters [] means an array args is a parameter

CompSci 42.11Intro to Java MoveTest.java import java.awt.Rectangle; public class MoveTest { public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] method declaration method definition method body {} Starts and ends method body

CompSci 42.12Intro to Java MoveTest.java import java.awt.Rectangle; public class MoveTest { public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] Rectangle is a type (also a class) cerealBox is a variable Creates a Rectangle Calls the constructor of the Rectangle class

CompSci 42.13Intro to Java MoveTest.java import java.awt.Rectangle; public class MoveTest { public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] Declaring the cerealBox variable of type Rectangle Initializing the cerealBox variable

CompSci 42.14Intro to Java MoveTest.java import java.awt.Rectangle; public class MoveTest { public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] Assignment operator Pronounced “gets” 1.Computes the right hand side 2.Assigns value to left hand side

CompSci 42.15Intro to Java MoveTest.java import java.awt.Rectangle; public class MoveTest { public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] Arguments – order matters

CompSci 42.16Intro to Java MoveTest.java import java.awt.Rectangle; public class MoveTest { public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] Comments Ignored by compiler

CompSci 42.17Intro to Java MoveTest.java import java.awt.Rectangle; public class MoveTest { public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] Rectangle object Also a local variable Local variables are declared in method bodies Calling the translate method On the cerealBox object

CompSci 42.18Intro to Java MoveTest.java import java.awt.Rectangle; public class MoveTest { public static void main(String[] args) { Rectangle cerealBox = new Rectangle(5, 10, 20, 30); // move the rectangle cerealBox.translate(15, 25); // print the moved rectangle System.out.println(cerealBox); } Prints java.awt.Rectangle[x=20, y=35, width=20, height=30] Calling the println method For console output

CompSci 42.19Intro to Java Why know and follow the Java Coding Conventions?  Helps understand code  makes purpose of identifiers clear  delineates separate pieces of code  assists in avoiding syntax errors  Expected if source code is to be viewed at any time by anyone other than the original author  Helps standardize

CompSci 42.20Intro to Java Coding Conventions  Capitalization  Class identifier  Variable identifier  Method identifier  Indentation  Braces  Body of code (also called a code block)  See course webpage for a complete description

CompSci 42.21Intro to Java GreeterTest.java public class GreeterTest { public static void main(String[] args) { Greeter worldGreeter = new Greeter("World"); System.out.println(worldGreeter.sayHello()); Greeter daveGreeter = new Greeter("Dave"); System.out.println(daveGreeter.sayHello()); }

CompSci 42.22Intro to Java Greeter.java public class Greeter { public Greeter(String aName) { name = aName; } public String sayHello() { String message = "Hello, " + name + "!"; return message; } private String name; } Constructor Used to initialize instance variables Has no return type, not even void Name is the same as the class name Declaration of instance variable Outside method body Inside class body

CompSci 42.23Intro to Java public class Greeter { public Greeter(String aName) { name = aName; } public String sayHello() { String message = "Hello, " + name + "!"; return message; } private String name; } Greeter.java Empty parameter list String concatenation Private means only accessible within class body

CompSci 42.24Intro to Java Introduction to Java Downloading Source Code  Set snarf site to:

CompSci 42.25Intro to Java Introduction to Java Using Eclipse  Starting Eclipse  Making a project  Importing files into a project  Editing source code  Compiling source code  Executing source code  Saving project

CompSci 42.26Intro to Java Starting Eclipse  Double Click on Math Apps on the Desktop  Double Click on eclipse.exe

CompSci 42.27Intro to Java Making a project  File->New->Project  Java->Java Project  Click on Next  For Project Name type Sample  Click on Finish  Switch to Java Perspective? Yes

CompSci 42.28Intro to Java Importing files into a project  File->Import  Zip file  Click on Next  From zip file Click on Browse  Click on Desktop  Click pong.jar  Open  For Into folder Click on Browse and select Sample  ok  Finish

CompSci 42.29Intro to Java Executing source code  Click on + by Sample  Click on + by pong  Click on Pong.java  Run->Run as->Java Application  Play the game  Using the mouse  Using the keys ‘i’ and ‘m’  (click on red square near bottom right to end)

CompSci 42.30Intro to Java Editing source code  Double Click on PongLoop.java  Go to line 69 where it says position1.y-=2; (you’ll see 69:23 in the lower right hand corner as you move the cursor in the code)  Change it to say position1.y-=8;  Click on the disk icon to save (or File->Save)

CompSci 42.31Intro to Java Compiling source code  Always be sure to save any changes before compiling (done in the previous step)  (compile is done automatically when you save)

CompSci 42.32Intro to Java Executing the modified code  Click on Pong.java  Run->Run as->Java Application  (or click on icon with white triangle in green circle)  (this will repeat the last Run->Run as->Java Application)  What changed?

CompSci 42.33Intro to Java Saving project  During class projects are saved on the desktop  Before leaving the lab be sure to save your project to your acpub account

CompSci 42.34Intro to Java Saving project  Click on Sample  File->Export  Jar file  Click on Next  Click on Export java source files and resources  For Select the export destination  Browse  ICC229 (My Computer)  Select P drive  Create and/or change into cps4 folder  Use filename mypong.jar  Click on Finish

CompSci 42.35Intro to Java Assignment #1  Create your Class Web Page  See assignment on web  Due Tuesday, 1/24  (Assoignment #0 Due Today!)