Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.

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

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Introduction to Programming
CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
Datalogi A 1: 8/9. Book: Cay Horstmann: Big Java or Java Consepts.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Computer Science A 1: 3/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Chapter 3 Control Statements F Selection Statements –Using if and if...else –Nested if Statements –Using switch Statements –Conditional Operator F Repetition.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Introduction to Methods
Java Unit 9: Arrays Declaring and Processing Arrays.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
SOFTWARE AND PROGRAMMING 1 In-class open-book TEST1 on 6/02 Lab SH131: Ms Mihaela Cocea (from ) Room London Knowledge Lab Emerald.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
CSC Programming I Lecture 6 September 4, 2002.
Java Expressions MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Department of Computer Engineering Methods Computer Programming for International Engineers.
Java Methods 11/10/2015. Learning Objectives  Be able to read a program that uses methods.  Be able to write a write a program that uses methods.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Java I/O Basics MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh Bajaj,
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Java Fundamentals MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
CIS199 Test Review 2 REACH.
Chapter VII: Arrays.
Chapter 7 User-Defined Methods.
Introduction to Computer Science / Procedural – 67130
Yanal Alahmad Java Workshop Yanal Alahmad
Programmazione I a.a. 2017/2018.
Starting Out with Java: From Control Structures through Objects
One-Dimensional Array Introduction Lesson xx
Business Programming Concepts II
An Introduction to Java – Part I, language basics
Methods/Functions.
Presentation transcript:

Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh Bajaj, All rights reserved

Objectives Understand the use of arrays in Java Get introduced to writing different methods in Java within the same Class (not just the main() method) Let’s get started!

Arrays An array is a data structure consisting of a numbered list of items, where all the items are of the same type. An array occupies a contiguous portion of memory Once we create an array, we cannot change its size. Of course, we can change the values of individual elements in the array int[] arrayOfInt = new int[100]; for(int i=0;i<100;i++) arrayOfInt[i] = 2*i;//fills the array with even numbers //from 0 to 198

Arrays The main(String[] args) method has an array as its argument This array is the args array, and is an array of strings (every element in the array is a separate string). The args array is numbered from 0 on up. args[0] is the first argument we may want to pass to the main method, from the command line or from the Java Editor. So, the program can be executed differently, depending on the arguments passed to it (much like a method) Up to now, we have written main methods where we do not pass any argument either from the command line (the dos prompt) or from EJE editor.

Arrays public class MainArgumentsTest { public static void main(String args[]) { if(args.length >1 ) { System.out.println ("Incorrect number of parameters"); System.out.println ("Usage: java MainArgumentsTest or "); System.out.println ("Usage: java MainArgumentsTest WordToPrintOut"); System.exit(0); }//if if(args.length==0) { System.out.print ("The program ran, and you did not enter any word to”) System.out.println(“print out"); System.exit(1); }//if if(args.length==1) { System.out.println ("The program ran, and you entered " + args[0]); System.exit(1); }//if } //main } //class Fun In class Assignment: Take MonthlyPayment.java and rewrite as MonthlyPaymentWithArgs.java, where username, loan amount, Number of months and interest rate are passed as elements of args[].

Arrays -An array is an object in java. It is created by using the new keyword. The new keyword allocates memory for the array. -We can also create an array without the new keyword by enumerating the elements as below:

Fun In Class Example Let us write a Java Program called CreateCustomArrayOfNames -that takes in the size of the array from the command line (array size is an integer), -creates an array called Names of strings of that size, -initializes each string in the array to “XXXXX” -asks the user to input names, until the user types in “exit” -Checks to find the first empty position (signified by “XXXXX” and puts in the name there -If the array is full, prints out “No More Space”

Methods A subroutine or a method is a black box. We use it to divide our tasks into subtasks, each one performed by a separate method. This is called functional decomposition, or divide and conquer. Each method in java returns a type value (or an object of a class like a String) If we don’t want the method to return anything, java uses the void type. This is the same as C and C++. Void means: don’t return a meaningful type. Some languages have procedures & functions. Procedures do things, while functions return values. C, C++ and Java only have functions or methods, where a method returning the void type is equivalent to a procedure.

Methods Memory maps of local variables and parameters Using public static double addNum( double n1, double n2) In MyCalculatorwithMethods.java as an example -What does the memory map look like? -How is this method called? -How does the return statement work in the method? -How do we use a local variable total in the method? Rewriting addNum() as a void method Print out the total within the method What does the return statement look like?

Methods Analyze GuessingGame2 In Class Assignment: Take the solution to MyCalculator, and split it up into public static methods. Call it MyCalculatorWithMethods.java. printMenu() What should the main() method do? addNum() subtractNum() divideNum() multiplyNum() expNum() meanNum() -How do we re-write the code to change gamesWon to a local variable within main() ?

Methods -What will the output be? -What does the memory map look like?

Methods -What will the output be? -What does the memory map look like?

Methods -What will the output be? -What does the memory map look like?

Methods -What will the output be? -What does the memory map look like?

Methods -What will the output be? -What does the memory map look like? InclassExercise: Break into methods