33
Java Programing PSC 120 Jeff Schank

Java Programing

  • Upload
    jenna

  • View
    113

  • Download
    1

Embed Size (px)

DESCRIPTION

Java Programing. PSC 120 Jeff Schank. Let’s Create a Java Program. Open Eclipse Create a project: File -> New -> Java Project Create a package: File -> New -> Package Create a Class: File -> New -> Class. How to say “Hello World!”. Let’s Add Some Numbers. Let’s Format the Results. - PowerPoint PPT Presentation

Citation preview

Page 1: Java Programing

Java Programing

PSC 120Jeff Schank

Page 2: Java Programing

Let’s Create a Java Program

1. Open Eclipse2. Create a project: File -> New -> Java Project3. Create a package: File -> New -> Package4. Create a Class: File -> New -> Class

Page 3: Java Programing

How to say “Hello World!”

Page 4: Java Programing

Let’s Add Some Numbers

Page 5: Java Programing

Let’s Format the Results

Page 6: Java Programing

Classes

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

Page 7: Java Programing

Data

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

Page 8: Java Programing

Methods

• Now, let’s add a method

Page 9: Java Programing

Let’s Say Something

Page 10: Java Programing

Let’s Say Something Randomly

Page 11: Java Programing

Variables and Their Types• As we just saw, we define the objects that will interact in our simulation

by defining classes • 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 methods • 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.

Page 12: Java Programing

Example MyClass

Page 13: Java Programing

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

Page 14: Java Programing

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.

Page 15: Java Programing

Example Method

1. Modifier

2. Return Type

3. Method Name

4. Parameter List

5. Body

Page 16: Java Programing

Example Method

1. Modifier

2. Return Type

3. Method Name

4. Parameter List

5. Body

Page 17: Java Programing

Example Method

1. Modifier

2. Return Type

3. Method Name

4. Parameter List

5. Body

Page 18: Java Programing

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"

Page 19: Java Programing

&& and ||

Page 20: Java Programing

! and !=

Page 21: Java Programing

Arithmetic Operators

1. + Additive operator but it is also used for String concatenation.

2. – Subtraction operator3. * Multiplication operator4. / Division operator

Page 22: Java Programing

Examples: +

Page 23: Java Programing

If-then Statement

Body

Conditions

Page 24: Java Programing

If-then Example

Page 25: Java Programing

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).

• For statements typically have three statements as arguments and then a body that is repeated (there are variations on this theme).

Page 26: Java Programing

A common form

Modifier Arguments

Body

Page 27: Java Programing

Example

Page 28: Java Programing

Another Example

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

Page 30: Java Programing

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.

Page 31: Java Programing

Examples

Page 32: Java Programing

Examples