MIS 222 – Lecture 12 10/9/2003.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

1 More on Arrays and Loops Reading for this Lecture: –Section 5.4, , Break and Continue in Loops Arrays and For-each Loops Arrays and Loops.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Introduction to arrays Data in economy size packages.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
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.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
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.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
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.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Java Part I By Wen Fei, HAO. Program Structure public class ClassName { public static void main(String[] args) { program statements } user defined methods.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
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];
Method overloading contd class OverloadDemo { public static void main(String args[]) { Overload ob = new Overload(); int resI; double resD; // call all.
Method Examples CS 139 Algorithm Development 10/06/2008.
Floating Point ValuestMyn1 Floating Point Values Internally, floating point numbers have three pairs: a sign (positive or negative), a mantissa (has a.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Looping Examples I.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. System.out.println(“Please enter grade.
Arrays 3/4 By Pius Nyaanga.
Chapter 10 – Exception Handling
using System; namespace Demo01 { class Program
Chapter 10 Arrays.
USING ECLIPSE TO CREATE HELLO WORLD
Computer Programming Methodology Input and While Loop
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
Counted Loops.
Method Mark and Lyubo.
Chapter 10 Arrays.
Functions Used to write code only once Can use parameters.
Chapter 6 Arrays.
Pass by Reference, const, readonly, struct
תרגול מס' 3 עבודה עם מחרוזות (Strings) מתודות (Methods) העברת פרמטרים
Pemrograman Dasar Methods PTIIK - UB.
An Introduction to Java – Part I, language basics
Defining methods and more arrays
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Unit-2 Objects and Classes
Code Animation Examples
Recursive GCD Demo public class Euclid {
Lecture 4 2d Arrays CSE /26/2018.
class PrintOnetoTen { public static void main(String args[]) {
Announcements Lab 6 was due today Lab 7 assigned this Friday
1D Arrays and Lots of Brackets
1D Arrays and Lots of Brackets
Methods and Data Passing
Object Oriented Programming
Methods and Data Passing
CSS161: Fundamentals of Computing
CMPE212 – Reminders Course Web Site:
Generic Programming.
Array Review Selection Sort
Methods/Functions.
Arrays 3/4 June 3, 2019 ICS102: The course.
Chapter 6 Arrays.
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
PROGRAMMING ASSIGNMENT I
Corresponds with Chapter 5
Presentation transcript:

MIS 222 – Lecture 12 10/9/2003

Assignment 11 Demo 8.1.10

Arrays Company Profits 1 4000 2 122154 3 55255 4 -564654 5 541818 6 int[] companies = new int[8]; companies[0] = 4000; companies[1] = 122154; companies[2] = 55255; companies[3] = -564654; companies[4] = 541818; companies[5] = 781849; companies[6] = 121518; companies[7] = 121518; Company Profits 1 4000 2 122154 3 55255 4 -564654 5 541818 6 781849 7 -121518 8 843257 Exact same data types Similar information

Array Literals int[] companies = {4000, 122154, 55255, -564654, 541818, 781849, 121518, 121518}; enclosed in brackets separated by commas String[] letters = {“a”, “b”, “c”}; System.out.println(letters[2]); System.out.println(letters[3]);

Array Literals Example 8.2.1

Shallow Copy Array Copying int[] companies = {4000, 122154, 55255, -564654, 541818, 781849, 121518, 121518}; int[] myComps = companies; myComps[0] = 1; System.out.println(companies[0]); companies 8475 Shallow Copy myComps 8475 1 2 3 4 5 6 7 4000 122154 55255 -564654 541818 781849 -121518 843257 8475

Deep Copy Array Copying int[] companies = {4000, 122154, 55255, -564654, 541818, 781849, 121518, 121518}; int[] myComps =new int[companies.length]; System.arraycopy(companies, 0, myComps, 0, companies.length); myComps[0] = 1; System.out.println(companies[0]); companies 8475 Deep Copy 1 2 3 4 5 6 7 4000 122154 55255 -564654 541818 781849 -121518 843257 8475 myComps 9985 1 2 3 4 5 6 7 4000 122154 55255 -564654 541818 781849 -121518 843257 9985

Arrays as Arguments (to methods) Average() int[] int 8475 companies 4000 122154 55255 -564654 541818 781849 -121518 843257 1 2 3 4 5 6 7 25

Arrays as Arguments Here’s what it looks like: public class Average { public static void main(String[] args){ double[] stuff = {5,15.5,55.55,1}; double avg = average(stuff); System.out.println(“average of stuff is “ + avg); } public static double average(double[] nums){ double total = 0; for(int i = 0; i < nums.length; i++){ total += nums[i]; return total / num.length;

Arrays as Arguments public class Average { public static void main(String[] args){ double[] stuff = {5,15.5,55.55,1}; double avg = average(stuff); System.out.println(“the third element is “ + stuff[2]); } public static double average(double[] nums){ double total = 0; for(int i = 0; i < nums.length; i++){ total += nums[i]; //malicious code nums[2] = -999999999; return total / num.length;

Arrays as Arguments public class Average { public static void main(String[] args){ double[] stuff = {5,15.5,55.55,1}; double avg = average(stuff); System.out.println(“the third element is “ + stuff[2]); } public static double average(double[] nums){ //body same as previous slide stuff 8475 nums 1 2 3 8475 5 15.5 55.55 1 8475

Example 8.3.7