Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 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!

3 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

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

5 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[].

6 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:

7 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”

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

9 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?

10 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() http://nfp.cba.utulsa.edu/bajaja/mis3023/Text/javanotes4.1/c4/s2.html -How do we re-write the code to change gamesWon to a local variable within main() ?

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

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

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

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

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


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

Similar presentations


Ads by Google