CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,

Slides:



Advertisements
Similar presentations
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.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
Road Map Introduction to object oriented programming. Classes
Access to Names Namespaces, Scopes, Access privileges.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Java Syntax Primitive data types Operators Control statements.
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
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.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
ArrayList, Multidimensional Arrays
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
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.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance
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.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
CIT 590 Intro to Programming First lecture on Java.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
10-Nov-15 Java Object Oriented Programming What is it?
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
OOP Basics Classes & Methods (c) IDMS/SQL News
Arrays and Array Lists CS 21a. Problem 1: Reversing Input Problem: Read in three numbers and then print out the numbers in reverse order Straightforward.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
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[]
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Information and Computer Sciences University of Hawaii, Manoa
Topic: Classes and Objects
Chapter 7 User-Defined Methods.
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Namespaces, Scopes, Access privileges
Class Structure 16-Nov-18.
Class Structure 28-Nov-18.
Group Status Project Status.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
Recap Week 2 and 3.
Namespaces, Scopes, Access privileges
Arrays and Array Lists CS 21a.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Class Structure 25-Feb-19.
Java Programming Language
Corresponds with Chapter 5
Presentation transcript:

CIT 590 Intro to Programming Java lecture 4

Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private, protected and what is the default? Objects interacting with other objects

Types Primitive data types If you want to store just a single value, the following types are used int, double, boolean, char int x; x = 3; double y; y = 3.5;

Strings Strings are basic enough that Java uses syntax that reminds you of primitive data types String s = “alphabetagamma”; s = s + “456”; s now has the value alphabetagamma456.

Objects Almost all of Java programming is done in an object oriented way. To create instances of objects the syntax is Classname variablename = new Classname(constructor arguments) Every java program will have a class and therefore will create one or more objects.

Java class public class Thing { // instance variables int property1; String property2; //constructor public Thing (int property1, String property2) { this.property1 = property1; this.property2 = property2; } //other methods method1 method2 public static void main(String[] args) { Thing t = new Thing(3, “ab”); t.method1(appropriate arguments); t.method2(appropriate arguments); }

Reminder of method syntax Return type name(Argument data type1 argumentName1, Argument data type 2 argumentName2) { return something with the correct return type } int function(double num, String s) { int x = 7; return x; }

Returning from a method in Java If a function returns nothing the return type is void. In those cases you are allowed to write return; but nothing else is allowed. For functions that return something, Java is strict about its returns. Every path through a method must return. Every path through a method must return the same data type. Every path through a method must return something whose data type is the same as that committed to in the method definition.

Strings Actually Strings are objects. String s = “Hurrah for the red and the blue”; s.substring(0,6) gives you Hurrah s.charAt(2) gives you the char ‘r’

The plus operator + means addition for integers and concatenation for strings int x = 3; int y = 4; System.out.println(x + y + “ = “ + x + y); This will give you “7 = 34” Remember to evaluate left to right and keep track of what datatype you are dealing with.

Arrays String[] - an array of strings; int[] – an array of ints int[] numbers = new int[10]; Array of 10 numbers Indexing is the same as python for (int i = 0 ; i < numbers.length; i++) { // do stuff with numbers[i] }

Arraylists Think of Arraylists in exactly the same way as lists in Python. You will have to import java.util.ArrayList; Most people just import java.util.* at the start of their programs. The key difference is the way you initialize one of them ArrayList numberList = new ArrayList (); ArrayList stringList = new ArrayList (); ArrayListExperiments.java

Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows for class datatypes ArrayList is ok ArrayList is not ok. Instead you have to write ArrayList Similarly if you have a dictionary in Python that maps string keys to integer values the way to declare that in Java is HashMap and not HashMap 13

toString() method of a class Same as the __str__ method in Python Return a string. DO NOT PRINT a string public String toString() { String returnString = “”; //modify returnString over here in some way return returnString; } example Card.java

Unit testing with objects assertEquals does not work ‘out of the box’ when it comes to objects For HW9, it is totally OK to use the toString() method or knowledge of the internals of the objects to compare two objects assertEquals(object1.toString(), object2.toString()); Totally OK for HW9

16 Methods may have local variables A method may have local (method) variables Formal parameters are a kind of local variable int add(int m, int n) { int sum = m + n; return sum; } m, n, and sum are all local variables The scope of m, n, and sum is the method These variables can only be used in the method, nowhere else The names can be re-used elsewhere, for other variables

17 Blocks (Compound statements) Inside a method or constructor, whenever you use braces, you are creating a block, or compound statement: int absoluteValue(int n) { if (n < 0) { return -n; } else return n; } The scope of a variable is the block in which it is defined.

18 Nested scopes 1 int fibonacci(int limit) { 2 int first = 1; 3 int second = 1; 4 while (first < 1000) { 5 System.out.print(first + " "); 6 int next = first + second; 7 first = second; 8 second = next; 9 } 10 System.out.println( ); 11 }

19 Declarations in a method The scope of formal parameters is the entire method The scope of a variable in a block starts where you define it and extends to the end of the block if (x > y) { int larger = x; } else { int larger = y; } return larger; Scoped to the if block Scoped to the else block Illegal 

20 The for loop The for loop is a special case You can declare variables in the for statement The scope of those variables is the entire for loop This is true even if the loop is not a block void multiplicationTable() { int i; for (i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) System.out.print(" " + i * j); System.out.println(); } }

Access modifiers Do not worry about the subclass column right now. World means a different class, different package.

Access modifiers The best practice is to have the instance variables be private. The best practice is to have most methods be public Because we often do want to access and potentially modify the instance variables, a very common set of methods are getters and setters Assume we have a class with a private instance variable called counter. public int getCounter() { return counter; } public void setCounter(int counter) { this.counter = counter; }

23 null If you declare a variable to have a given object type, for example, Person john; String name;...and if you have not yet assigned a value to it, for example, with john = new Person(); String name = “John Smith";...then the value of the variable is null null is a legal value, but there isn’t much you can do with it It’s an error to refer to its instance variables, because it has none It’s an error to send a message to it, because it has no methods The error you will see is NullPointerException