Java Programing PSC 120 Jeff Schank. Let’s Create a Java Program 1.Open Eclipse 2.Create a project: File -> New -> Java Project 3.Create a package: File.

Slides:



Advertisements
Similar presentations
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Advertisements

Spring Semester 2013 Lecture 5
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Access to Names Namespaces, Scopes, Access privileges.
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
Where do objects come from? Objects are instances of classes We instantiate classes: –e.g.new chapter1.Terrarium() –There are three parts to this expression:
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
SCOPE & I/O CSC 171 FALL 2004 LECTURE 5. CSC171 Room Change Thursday, September 23. CSB 209 THERE WILL BE A (group) QUIZ! - topic: the CS department at.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Initialization George Blank Subba Reddy Daka. Importance of Initialization Java classes are initialized and have predictable default values. These values.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Ruby (on Rails) CSE 190M, Spring 2009 Week 4. Constructors Writing a new class is simple! Example: class Point end But we may want to initialize state.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Introduction to Programming Writing Java Beginning Java Programs.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Coding Standards and Best Practices Coding Standards Introduction: After completing this chapter, you will able to keep your code up to standards.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
CPS120: Introduction to Computer Science Decision Making in Programs.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Programming in Java CSCI-2220 Object Oriented Programming.
Introduction to Programming Writing Java Beginning Java Programs.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.
Introduction to Object-Oriented Programming Lesson 2.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
CITA 342 Section 1 Object Oriented Programming (OOP)
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
What Is a Package? A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Topics Instance variables, set and get methods Encapsulation
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Java Programing ANB/PSC 290 Jeff Schank.
Information and Computer Sciences University of Hawaii, Manoa
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Java Primer 1: Types, Classes and Operators
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3: Using Methods, Classes, and Objects
Chapter 5 Classes.
CompSci 230 Software Construction
JavaScript: Functions.
Namespaces, Scopes, Access privileges
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Pemrograman Dasar Methods PTIIK - UB.
Namespaces, Scopes, Access privileges
Object Oriented Programming in java
Java Programming Language
Object Oriented Programming
Unit-2 Objects and Classes
CSG2H3 Object Oriented Programming
Presentation transcript:

Java Programing PSC 120 Jeff Schank

Let’s Create a Java Program 1.Open Eclipse 2.Create a project: File -> New -> Java Project 3.Create a package: File -> New -> Package 4.Create a Class: File -> New -> Class

How to say “Hello World!”

Let’s Add Some Numbers

Let’s Format the Results

Classes Let’s create another class called “Agent” File -> New -> Class

Data Now, let’s add some data—in this case a vocabulary

Methods Now, let’s add a method

Let’s Say Something

Let’s Say Something Randomly

Variables and Their Types As we just saw, we define the objects that will interact in our simulation by defining classesclasses Once a class is completely defined, then it can be instantiated many times – For example, we could define a class called “Person” and then make 1000 persons that interact in our simulation. Classes have members that occupy fields in a class A class can have indefinitely many fields and a field is either occupied by variables or methodsvariablesmethods When defining classes, I prefer to place the variables first and methods second in a class, but Java does not care how they are ordered Let’s look at some of the types of variables we can define in a class.

Example MyClass

Access Modifiers Variables (and methods) have specifications for how they are accessed There are four types of access modifiers: no explicit modifier, public, private, and protected. – public modifier—the field is accessible from all classes. – private modifier—the field is accessible only within its own class. – protected modifier—the field is accessible within its own class, package, and subclass. – no explicit modifier—the field is accessible within its own class and package

Methods Methods specify how objects do things (how they behave) Methods also specify how objects interact with other objects Methods have at least five features: 1.Modifiers—such as public, private, and others listed above. 2.The return type—the data type of the value returned by the method, or void if the method does not return a value. 3.The method name—the rules for field names apply to method names as well, but the convention is a little different. 4.The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses. 5.The method body, enclosed between braces—the method’s code, including the declaration of local variables, goes here.

Example Method 1. Modifier 2. Return Type 3. Method Name 4. Parameter List 5. Body

Example Method 1. Modifier 2. Return Type 3. Method Name 4. Parameter List 5. Body

Example Method 1. Modifier 2. Return Type 3. Method Name 4. Parameter List 5. Body

Logical Operators 1.&& means roughly “and” 2.|| means roughly “or” 3.== means roughly “equals” 4.! means roughly “not” 5.!= means roughly “not equal to” 6.> means “greater than” 7.>= means “greater than or equal to” 8.< means "less than” 9.<= means "less than or equal to"

&& and ||

! and !=

Arithmetic Operators 1.+ Additive operator but it is also used for String concatenation. 2.– Subtraction operator 3.*Multiplication operator 4./Division operator

Examples: +

If-then Statement Body Conditions

If-then Example

For Statements Probably, the next most commonly used control statement is the for statement. For control statements are one of several control statements that allow you to perform a number of operations over and over again for a specified number of steps (the others are while and do- while).while and do- while For statements typically have three statements as arguments and then a body that is repeated (there are variations on this theme).

A common form Modifier Arguments Body

Example

Another Example The maximum value for an integer is But, since it does not stop at this value, it would generate an error.

Switch Statement

Scope of a Variable The scope of a variable is the region of a program within which, a variable can be referenced. In Java, the largest scope a variable can have is at the level of the class. So, if variables are declared in a class field, they can be referenced anywhere in the class including inside methods.

Examples

This