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.

Slides:



Advertisements
Similar presentations
Introduction to Programming G51PRG University of Nottingham Revision 1
Advertisements

IT151: Introduction to Programming
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Your First Java Program: HelloWorld.java
©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods.
How to Create a Java program CS115 Fall George Koutsogiannakis.
CMT Programming Software Applications
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Method exercises Without IF. Setup Create one new project to hold all of these. In that project, create an empty class called WorkIt to hold all of your.
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
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.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Chapter 2 How to Compile and Execute a Simple Program.
Chapter 3 Getting Started with C++
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Comments are for people Header comments supply basic information about the artifact.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
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.
The Java Programming Language
Programming With C.
Board Activity Find your seat on the seating chart Login – Remember your login is your first initial your last name and the last three numbers of your.
Classes CS 21a: Introduction to Computing I First Semester,
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Comments in Java. When you create a New Project in NetBeans, you'll notice that some text is greyed out, with lots of slashes and asterisks:
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.
Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open.
Introduction to programming in the Java programming language.
8/31: Intro to Java, Languages, and Environments Types of programming languages –machine languages –assembly languages –high-level languages Java environment.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
Basic Program Construction
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.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Documentation and Style. Documentation and Comments  Programs should be self-documenting.  Use meaningful variable names.  Use indentation and white.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Methods.
OOP Basics Classes & Methods (c) IDMS/SQL News
11 ITI 1120 Lab # 2 Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
CSC 110 – Intro to Computing - Programming
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
1/16: Intro to Java, Languages, and Environments Types of programming languages –machine languages –assembly languages –high-level languages Java environment.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Introduction to 1. What is Java ? Sun Microsystems Java is a programming language and computing platform first released by Sun Microsystems in The.
Computer Programming Your First Java Program: HelloWorld.java.
Java Course Review.
Introduction to.
USING ECLIPSE TO CREATE HELLO WORLD
Intro to Java.
Writing Methods.
Tonga Institute of Higher Education
Chapter 1: Computer Systems
Take out a piece of paper and PEN.
Anatomy of a Java Program
Introduction to Computer Science
Methods/Functions.
Presentation transcript:

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  Two kinds of things found in a class  Variables store data about the class  Methods provide programmers with ways of asking the class to do work

Files in a Java program  A Java program is always made up of one or more classes  Most files in a Java program have a single class  A Java program will have many different files, typically one for each class.  The code is stored in a file with the same name as the class and the extension.java.  The java compiler creates a file with the same name, but the extension.class.

Defining a Class class myClass {... } 1. Starts with the word “class” 2. Followed by the class name 3. Followed by an open curly bracket 4. Followed by method and variable definitions. 5. Followed by a close curly bracket

Methods  A method is a piece of java code that does a job  Defined within the curly braces of a class definition  Two types  1. Static methods (also called class methods) do a job for a programmer  2. Instance methods manipulate the data within a class  For now we will talk about static methods

The parts of a method 1. Visibility Public - any part of the Java program can use it Private - can only be used within the same class 2. Type of method (static?) 3. Return type (what type of information it returns) 4. Name 5. Parameters - Data given to the method to do its job. 6. Body - The statements that get executed when the method is called.

Parts of a Method public static void main (String[ ] args) { System.out.print(“Hey there!”); } 1. Visibility 2. Static 3. Return type (void means nothing returned) 4. Method name 5. List of parameters 6. Body inside curly brackets

Indenting Classes  Variables and methods in a class should be indented  Close bracket at end should be indented the same as class  Open bracket can be on same line as class or next line  Classes are usually in their own file class myClass {//start of myClass void method1() {//start of method1 code; }//end method1 }// end myClass

Indenting Methods and Functions  Statements inside the method or function must be indented  Close bracket at end should be indented the same as the definition  Open bracket can be on same line or next line void myMethod() { statement1; }

Running a Java method  The body of a method between the curly brackets contain one or more statements  Statements are separated from one another by semicolons  When a method is called, it executes the statements one at a time  When Java is run, it looks for a method called main and runs it  The main method can call other methods, either in the same class or in other classes.

Some common methods  System.out.println(“I pwn all noobs”);  System.out.println prints whatever is inside the parentheses to the console  Moves the console cursor to a new line when it is done.  System.out.print(“U just got served”);  Similar to System.out.println, but does not move the cursor to a new line.

Formatting Java Programs  Java is case sensitive. E.g. System.out.println is not the same as system.out.println.  Java is white space insensitive  Can put any number of spaces or new line characters between statements  Should use spaces to indent to make programs readable  // (two slashes) in Java start a comment: // This is a comment  Everything from the // to the end of line is ignored

Summary  Java programs are made up of classes  Classes have variables that store data  Methods do work for the programmer  Class and method definitions have multiple parts.  The body of a method contains one or more statements.  End with a semicolon  Are executed in order when the method is run  The main method is run when a Java program starts  System.out.print/println send output to the console  Comments and extra white space are ignored

Project 0  Work on Project 0  Due date : Tomorrow!