Java Programing ANB/PSC 290 Jeff Schank.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

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.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
11-Jun-15 Just Enough Java. 2 What is Java? Java is a programming language: a language that you can learn to write, and the computer can be made to understand.
Access to Names Namespaces, Scopes, Access privileges.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
COMP More About Classes Yi Hong May 22, 2015.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
1 CSC 201: Computer Programming I B. S. Afolabi. Introduction  3 unit course  2 hours of lecture/week Thursdays 4.00pm – 6.00pm Mondays 4.00pm – 6.00pm.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Methods. 2 A sequence of statements can be packaged together as a unit and re-used. A method is a named unit of re-usable code. modifier returnType methodName(
Chapter 4: Loops and Files
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.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Programming in Java CSCI-2220 Object Oriented Programming.
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
Introduction to Programming Writing Java Beginning Java Programs.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
CompSci 100E JB1.1 Java Basics (ala Goodrich & Tamassia)  Everything is in a class  A minimal program: public class Hello { public static void main(String[]
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Information and Computer Sciences University of Hawaii, Manoa
Topic: Classes and Objects
Java Language Basics.
Some Eclipse shortcuts
Lecture 5: Some more Java!
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Lecture 2: Data Types, Variables, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
Primitive Data, Variables, Loops (Maybe)
Namespaces, Scopes, Access privileges
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Statements, Comments & Simple Arithmetic
5 Variables, Data Types.
Pemrograman Dasar Methods PTIIK - UB.
An Introduction to Java – Part I, language basics
Classes & Objects: Examples
Recap Week 2 and 3.
Lecture 5: For Loops Building Java Programs: A Back to Basics Approach
Namespaces, Scopes, Access privileges
COM-152: Computer Programming Types, Variables, Operators Part 1 of 2
Code Refresher Test #1 Topics:
Chapter 2 Programming Basics.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming Language
More on Creating Classes
In this class, we will cover:
Just Enough Java 17-May-19.
Methods/Functions.
Introduction to java Part I By Shenglan Zhang.
Corresponds with Chapter 5
Presentation transcript:

Java Programing ANB/PSC 290 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 package talker; public class Talkers { public static void main(String[] args) { }

How to say “Hello World!” package talker; public class Talkers { public static void main(String[] args) { System.out.println("Hello World!"); }

Let’s Add Some Numbers package talker; public class Talkers { public static void main(String[] args) { System.out.println(23 + 149 + 50291); }

Let’s Format the Results package talker; public class Talkers { public static void main(String[] args) { System.out.println("23 + 149 + 50291 = " + (23 + 149 + 50291)); }

Classes Let’s create another class called “Agent” File -> New -> Class package talker; public class Agent { }

Data Now, let’s add some data—in this case a vocabulary public class Agent { public String[] vocabulary = new String[] { "I'm fine!", "I wish I had a different name.", "I'm hungry", "I ate too much pizza.", "I think, therefore I exist.", "I'm fine but i'll be better when class is over!" }; }

Methods Now, let’s add a method public class Agent { public String[] vocabulary = new String[] { "I'm fine!", "I wish I had a different name.", "I'm hungry", "I ate too much pizza.", "I think, therefore I exist.", "I'm fine but i'll be better when class is over!" }; public void saySomething(int x){ int size = vocabulary.length; if(x>=0 && x < size){ System.out.println(vocabulary[x]); } else{ System.out.println("I'm not that smart!");

Let’s Say Something package talkers; public class Talkers { public static void main(String[] args) { Agent a = new Agent(); String s = a.saySomething(1); System.out.println(s); }

Let’s Say Something Randomly package talkers; public class Agent { public String[] vocabulary = new String[] {"I'm fine!", "I wish I had a different name.", "I'm hungry", "I ate too much pizza.", "I think, therefore I exist.", "I'm fine but i'll be better when class is over!" }; public String saySomething(int x){ if(x < vocabulary.length && x >= 0) return vocabulary[x]; else return "My vocabulary is small!"; } public String saySomethingRandomly(){ int lengthOfVocabulary = vocabulary.length; int randomInt = (int)(Math.random()*(double)lengthOfVocabulary); return vocabulary[randomInt];

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.

Example MyClass package talkers; public class MyClass { int n; //a declared integer int m = 1; //a declared integer with a value assigned to it double x; //declared a double variable, for storing real numbers double pi = Math.PI; //a double variable pi, with an approximation of //pi assigned to it, 3.141592654 boolean b; //declaration of a boolean variable boolean xyz = true; //declaration of a boolean variable assigned the value true boolean there_is_a_Martian_in_this_room = false; //it is often a good idea to make variable names that //have meaning to you. String s; //declaration of a string variable String myName = "Jeff Schank"; //declaration of a string variable and //assignment of a string. //We can also define array variables that can contain values for the type of //array. int[] integerArray; //declaration of an integer array variable int[] myNumbers = new int[100]; //declaration of an integer array with //100 slots for integers. But, no integers have been specified for the array int[] one_to_ten = {1,2,3,4,5,6,7,8,9,10}; // declaration of an integer array, //creation of an array with 10 slots, with values 1 to 10 assigned to the slots. }

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: Modifiers—such as public, private, and others listed above. The return type—the data type of the value returned by the method, or void if the method does not return a value. The method name—the rules for field names apply to method names as well, but the convention is a little different. 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. The method body, enclosed between braces—the method’s code, including the declaration of local variables, goes here.

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

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

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

Logical Operators && means roughly “and” || means roughly “or” == means roughly “equals” ! means roughly “not” != means roughly “not equal to” > means “greater than” >= means “greater than or equal to” < means "less than” <= means "less than or equal to"

&& and ||

! and !=

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

Examples: +

If-then Statement Conditions Body

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). For statements typically have three statements as arguments and then a body that is repeated (there are variations on this theme).

A common form Arguments Modifier Body

Example

Another Example The maximum value for an integer is 2147483647 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

Examples

This

Returning to Talkers public static Agent[] population; public static void makeAgents(int number){ population =new Agent[number]; for(int i = 0; i<number;i++){ population[i]=new Agent(i,population); }

Adding to the Agent Class public String name; public Agent[] population; public Agent(int number, Agent[] population){ //Constructor for Agent name = "Agent"+number; this.population = population; }

Adding to the Agent Class public void askAgent(){ int n = population.length; int i = (int)(Math.random()*n); Agent a = population[i]; System.out.println("\nBEGIN CONVERSATION"); System.out.println("What is your name? I'm "+ getName()+"."); System.out.println("My name is "+ a.getName()); System.out.println("How are you doing, "+ a.getName() + "?"); saySomethingRandom(); System.out.println("END CONVERSATION\n "); }

Back to Talkers public static void main(String[] args) { int n = 100; int steps = 10; makeAgents(n); for(int i=0;i<steps;i++){ Agent a = population[(int)(Math.random()*100)]; a.askAgent(); }