Arrays in Java An array is a collection of elements of the same type Declaration (we will use integers in this example) int[] A; int A[]; after this the.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

CS18000: Problem Solving and Object-Oriented Programming.
Written by: Dr. JJ Shepherd
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
1 Data Structures: Introduction CSC Data Types & Data Structures Applications/programs read data, store data temporarily, process it and finally.
Primitive Data Types byte, short, int, long float, double char boolean Are all primitive data types. Primitive data types always start with a small letter.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Java Unit 9: Arrays Declaring and Processing Arrays.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
CSC3170 Introduction to Database Systems
Hello AP Computer Science!. What are some of the things that you have used computers for?
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Array Basics l Arrays in Classes and Methods l Programming.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
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!
From C++ to Java A whirlwind tour of Java for C++ programmers.
Chapter 10. Arrays Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Computer Programming with JAVA.
Chap. 1 Classes, Types, and Objects. How Classes Are Declared [ ] class [extends ] [implements,, … ] { // class methods and instance variable definitions.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
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.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Classes - Intermediate
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Methods.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
OOP Basics Classes & Methods (c) IDMS/SQL News
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
Introduction to Computer Science / Procedural – 67130
Software Development Java Classes and Methods
Programming Language Concepts (CIS 635)
Something about Java Introduction to Problem Solving and Programming 1.
CSC 113 Tutorial QUIZ I.
Introduction to Java Programming
An Introduction to Java – Part I, language basics
Object Oriented Programming (OOP) LAB # 5
JAVA Constructors.
Data Structures: Introduction
Object Oriented Programming
Method of Classes Chapter 7, page 155 Lecture /4/6.
Data Structures: Abstract Data Types (ADTs)
Data Structures: Introduction
Presentation transcript:

Arrays in Java An array is a collection of elements of the same type Declaration (we will use integers in this example) int[] A; int A[]; after this the elements do not exist yet ! A = new int[10]; this means we have now 10 integer variables: A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8], A[9] for (int i=0; i < 10; i=i+1) A [i] = i; makes A[0] = 0; A[1] = 1; A[8] = 8; A[9] = 9; int i = A.length; gives the number of elements (10 in this case)

Example with Arrays in Java Statistics about a list of numbers which is entered by keyboard. The numbers are all positive from 1 to 10. The last number entered will be the 0. After that the computer should answer the % of 1, the % of 2, etc. (see Program6) Enter a number: 3 The % of 1s was 10% Enter a number: 6 The % of 2s was 0% Enter a number: 9 The % of 3s was 30% Enter a number: 5 The % of 4s was 0% Enter a number: 3 The % of 5s was 20% Enter a number: 7 The % of 6s was 10% Enter a number: 9 The % of 7s was 10% Enter a number: 5 The % of 8s was 0% Enter a number: 1 The % of 9s was 20% Enter a number: 3 The % of 10s was 0% Enter a number: 0

The arguments of a program Now we can understand something about what is String args[] on the beginning of the main method. It is an array of Strings. The values of these String, as well as the length of the array are given when the program is called. public class Program7 { public static void main(String args[]) { for(int i=0; i < args.length; i++) System.out.println(“Argument “+i+” is “+args[i]); } This program will show all the parameters passed to the program when it was called, for example if we write java Program7 peter paul mary the program will write Argument 1 is peter Argument 2 is paul Argument 3 is mary

Classes and Objects in Java We can say that there are two types of classes from which we can create objects: –Classes provided by the language: there is a large list of classes provided by the language which the user can use. Many of them are in libraries which must be imported (java.io.* contains the BufferedReader class) –Classes created by the user: sometimes the user wants to have an object of a class which doesn't exist. For example, an object of a class “Student” which should have all the information for a student of the university. The user can define: The variables which an object of this class will have The methods which can be applied to an object of this class

Lets start by using an existing class: the String A class is defined primarily by the methods you can apply to an object of such class There is a special type of method called constructor, which creates a new object of this class. Example: String myName; //the object doesn't exist yet myName = new String(“Nelson Baloian”); This is a call to the constructor. In order to apply a method to an object object_variable.method(parameters)

Methods of the String class Length of a String: int i = myName.length(); –i will have the value 14 i-th character: char c = myName.charAt(0); –c will contain the character N char c = myName.charAt(3); - c will contain the character s Sub sequence : String sub = myName.substring(7); –sub will contain the string “Baloian” String sub = myName.substring(3, 6); - sub will contain the string “son”

Some methods of the String class Search for sub sequences: String s = new String(“Helo, Nelson, Hello”); int i = s.indexOf(“elo”); - i will have the value 1, it searches for the first appearance Comparing: boolean onnajiDesKa = s1.equals(s2); –the variable onnajiDes will have the value true if the text of the string s1 is equal to the text in the string s2 int i = s1.compareTo(s2); - i will have the following values depending on s1 and s2 0 if s1==s2, >0 if s1>s2, <0 if s1<s2 The value of the string is according to where they would appear in a dictionary, first means smaller

Finding all the sub-strings of a String We will pass 2 strings as arguments to the program and the program will answer how many times the second string appears in the first one public class Program8 { public static void main(String args[]) { int i, app = 0; String first = args[0]; String second = args[1]; do { i = first.indexOf(second); if (i == -1) break; app = app+1; first = first.substring(i+1); } while(true); System.out.println(“Found “ +app+” times”); }

User Defined Classes (the Clock) public class Clock { int hour, minutes; Clock(int x, int y) { hour = x; minutes = y; } public int getHour() {//same for address and yearBorn return hour;// for yearBorn the return type is int } public void setName(String x) { // same for address and yearBorn name = x; }// for yearBorn the parameter is of type int public int age(int thisYear) { return thisYear - yearBorn }

User Defined Classes (The Student) public class Student { String name, address; int yearBorn; Student(String x, String y, int z) { name = x; address = y; yearBorn = z; } public String getName() {//same for address and yearBorn return name;// for yearBorn the return type is int } public void setName(String x) { // same for address and yearBorn name = x; }// for yearBorn the parameter is of type int public int age(int thisYear) { return thisYear - yearBorn }

How to write methods public String getName( ) { return name; } Public, private, protected or static Type of value the method will return (void in case it does not return any value) Parameters of the method (none in this case) Name of the method (given by the programmer) The instructions should be written within the brackets {}

How to write constructor methods Student ( String x, String y, int z ) { name = x; address = y; yearBorn = z } No need to write anything here Parameters the constructor will receive to create the new object Name of the method must be the class name We can write more than one constructor Student ( ) { name = “N.N.”; address = “Tokyo”; yearBorn = 2000; } This constructor puts default values to the new object

How to use-user defined classes The file with the user-defined class should be compiled The class file should be at the same directory with the file containing the program. public class Program9 { public static void main(String args[]) { Student s1,s2; s1= new Student(); s2 = new Student(“Simon”,”Santiago”,1998); s1.show(); s2.show(); s1.setName(“John Smith”); s1.setAddress(“123 5th Ave. New York”); String n = s2.getName(); s2.setName(n+” Baloian”); s1.show(); s2.show(); }

Extending an existing class: Inheritance One of the most powerful features of object oriented programming is the possibility to extend an existing class, even if the implementation of the original class is not known. This is called inheritance Original Class Definition of new variables and methods Extended Class Super-class of the extended class

Extending the Student class public class WasedaStudent extends Student{ int[] marks; int nmarks; WasedaStudent(String x, String y, int z, int n) { super(x,y,z);// a call to the constructor of the superclass marks = new int[n]; nmarks = 0; } public void putNewMark(int x) { marks[nmarks] = x; nmarks = nmarks +1; } public double showAverage() { double sum = 0.0;int i; for(i=0; i < nmarks; i=i+1) sum = sum + marks[i]; return sum/nmarks; }

Using the extended class public class Program10 { public static void main(String args[]) { WasedaStudent ws1,ws2; ws1 =new WasedaStudent(“student1”,”Musashino-shi”,1980,5); ws2 =new WasedaStudent(“student2”,”Shinjuku-ku”,1981,7); ws1.putNewMark(10); ws1.putNewMark(5); ws1.putNewMark(4); ws2.putNewMark(5); ws2.putNewMark(8); ws2.putNewMark(7);ws2.putNewMark(4); System.out.println(ws1.showAverage()); System.out.println(ws2.showAverage()); ws1.show(); ws2.show(); } Note that we can use all the methods of the superclass