COMP 202. Programming With Arrays

Similar documents
COMP 202. Programming With Arrays

H212 Introduction to Software Systems Honors

Java Bootcamp - Villanova University. CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

&KDSWHU$UUD\VDQG9HFWRUV

Chapter 7. Arrays are objects that help us organize large amounts of information

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

Introduction to Arrays

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

COMP 202. Building Your Own Classes. CONTENTS: Anatomy of a class Constructors and Methods (parameter passing) COMP 202 Objects 2 1

Assignment 1 is due tonight at 11:59pm. Assignment 2 is due next Thursday at 11:59pm

Chapter 6: Arrays. Presentation slides for. Java Software Solutions. for AP* Computer Science 3rd Edition

11/19/2014. Arrays. Chapter 6: Arrays. Arrays. Arrays. Java Software Solutions for AP* Computer Science A 2nd Edition

Arrays Chapter 7. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

COMP 202. Building Your Own Classes. CONTENTS: Anatomy of a class Constructors and Methods (parameter passing) Instance Data. COMP Objects 2 1

2/2/18. Assignment 2 is due Wednesday 14 February at 11:59pm. Reading for Monday s lecture is the remainder of Chapter 7 and all of Chapter 8

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Arrays - Review. Initializer Lists. The for-each Loop. Arrays, Part 2. Dr. Papalaskari 1. CSC 1051 Data Structures and Algorithms I

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Building Your Own Classes

Classes and Objects Part 1

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

JAVA WRAPPER CLASSES

A variable is a name for a location in memory A variable must be declared

Arrays, Part 3: Multidimensional arrays

CMPT 126: Lecture 6 Arrays

Java Review. Java Program Structure // comments about the class public class MyProgram { Variables

CS1083 Week 2: Arrays, ArrayList

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

7.3 Arrays of Strings (Objects) 7.3 Arrays of Strings (Objects) 7.3 Tunes.java. 7.3 Arrays of Objects 9/11/13. ! A UML diagram for the Tunes program

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

We now start exploring some key elements of the Java programming language and ways of performing I/O

Chapter 7: Arrays CS 121. April 9, Department of Computer Science College of Engineering Boise State University. Chapter 7: Arrays CS / 41

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

H212 Introduction to Software Systems Honors

TOPICS TO COVER:-- Array declaration and use.

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

( &% class MyClass { }

Arrays: An array is a data structure that stores a sequence of values of the same type. The data type can be any of Java s primitive types:

Chapter. Let's explore some other fundamental programming concepts

Admin. CS 112 Introduction to Programming. Recap: Exceptions. Summary: for loop. Recap: CaesarFile using Loop. Summary: Flow Control Statements

CS111: PROGRAMMING LANGUAGE II

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Array. Prepared By - Rifat Shahriyar

CS 112 Introduction to Programming

CSCI 2010 Principles of Computer Science. Basic Java Programming. 08/09/2013 CSCI Basic Java 1

CSC Java Programming, Fall Java Data Types and Control Constructs

CMPT 125: Lecture 4 Conditionals and Loops

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Full file at

COMP 202 Java in one week

This Week s Agenda (Part A) CS121: Computer Programming I. The Games People Play. Data Types & Structures. The Array in Java.

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Java I/O and Control Structures

Java Coding 3. Over & over again!

Java I/O and Control Structures Algorithms in everyday life

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Example: Computing prime numbers

STUDENT LESSON A15 ArrayList

REMINDER CS121/IS223. Agenda. A Recap on Arrays. Array of Cars for the Car Race? Arrays of Objects (i)

CS121/IS223. Collections Revisited. Dr Olly Gotel

Programming with Java

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers;

ECE 122. Engineering Problem Solving with Java

Chapter 3. Selections

CS162: Introduction to Computer Science II

COMP 202. Java in one week

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Introduction to Programming Using Java (98-388)

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

2: Basics of Java Programming

Selection Statements and operators

Chapter 8 Multi-Dimensional Arrays

Spring 2010 Java Programming

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

Introduction to Object-Oriented Programming

Formatting Output & Enumerated Types & Wrapper Classes

ECE 122. Engineering Problem Solving with Java

CSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

CS111: PROGRAMMING LANGUAGE II

Classes and Objects Miscellany: I/O, Statics, Wrappers & Packages. CMSC 202H (Honors Section) John Park

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

Lists using ArrayList

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

CSC 1051 Data Structures and Algorithms I

AP CS Unit 3: Control Structures Notes

Last Class. More on loops break continue A bit on arrays

ECE 122. Engineering Problem Solving with Java

Introduction to Java & Fundamental Data Types

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Interfaces, collections and comparisons

Arrays. Eng. Mohammed Abdualal

Computer Science is...

Transcription:

COMP 202 Programming With Arrays CONTENTS: Arrays, 2D Arrays, Multidimensional Arrays The Array List Variable Length parameter lists The For-each Statement Thinking Like A Programmer: Designing for arrays COMP 202 - Arrays 1

Part 1 Array Basics COMP 202 - Arrays 2

Arrays An array is an ordered list of values The entire array has a single name marks Each value has a numeric index 0 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 An array of size N is indexed from zero to N-1 This array holds 10 values that are indexed from 0 to 9 COMP 202 - Arrays 3

Arrays A particular value in an array is referenced using the array name followed by the index in brackets For example, the expression System.out.println(marks[2]); refers to the value 94 (which is the 3rd value in the array) That expression represents a place to store a single integer, and can be used wherever an integer variable can For example, it can be assigned a value, printed, or used in a calculation COMP 202 - Arrays 4

Arrays An array stores multiple values of the same type That type can be primitive types or objects Therefore, we can create an array of integers, or an array of characters, or an array of String objects, etc. In Java, the array itself is an object Therefore the name of the array is an object reference variable, and the array itself is instantiated separately COMP 202 - Arrays 5

Declaring Arrays The marks array could be declared as follows: int[] marks = new int[10]; Note: that the type of the array does not specify its size, but each object of that type has a specific size The type of the variable marks is int[] (an array of integers) It is set to a new array object that can hold 10 integers COMP 202 - Arrays 6

Array Object with elements of primitive type (e.g., integer) marks 0 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 COMP 202 - Arrays 7

Why Arrays? Imagine you want to write a program that asks the user to enter in 50 student midterm marks. The program would then display each grade with the student s ID number and then the average for that assignment in a nice report. How would you do it? One way would be to provide 100 variables: int mark1, mark2, mark3, (until 50) int ID1, ID2, ID3, (until 50) The array provides for a way to create only two variables: int marks[] = new marks[50]; int IdNumbers[] = new int[50]; Isn t this nicer? Imagine trying to calculate the average with 50 variables COMP 202 - Arrays 8

MarksArray.java import java.util.scanner; public class MarksArray { final static int LIMIT = 50; public static void main (String[] args){ int[] marks = new int[limit]; double average = 0.0; int sum; int lowmark, position; Scanner scan = new Scanner(System.in); // Initialize the array values for (int index = 0; index < LIMIT; index++){ System.out.println("mark " + (index+1) + ":"); marks[index] = scan.nextint(); COMP 202 - Arrays 9

MarksArray.java // calculate average sum = 0; for (int index = 0; index < LIMIT; index++) sum += marks[index]; average = (double) sum / marks.length; System.out.println("The average mark is " + average); // gets students with lowest grade; position = 0; for (int index = 0; index < LIMIT; index++) { if (marks[index] < marks[position]) position = index; System.out.println("Lowest mark " + marks[position] + " by student " + position); COMP 202 - Arrays 10

Declaring Arrays Some examples of array declarations: double[] prices = new double[500]; boolean[] flags; flags = new boolean[20]; char[] productid = new char[1750]; COMP 202 - Arrays 11

Declaring Array Variations int[] x, y, z; int x[], y, z; In the first example: int[] identifies that x, y and z are all arrays. In the second example x[] identifies that only x is an array, y and z are simply integers. COMP 202 - Arrays 12

Bounds Checking Once an array is created, it has a fixed size An index used in an array reference must specify a valid element That is, the index value must be in bounds (0 to N-1) The Java interpreter will throw an exception if an array index is out of bounds This is called automatic bounds checking COMP 202 - Arrays 13

Bounds Checking For example, if the array codes can hold 100 values, it can only be indexed using the numbers 0 to 99 If count has the value 100, then the following reference will cause an ArrayOutOfBoundsException: System.out.println (codes[count]); It s common to introduce off-by-one errors when using arrays problem for (int index=0; index <= 100; index++) codes[index] = index*50 + epsilon; COMP 202 - Arrays 14

Bounds Checking Each array object has a public constant called length that stores the size of the array It is referenced using the array name (just like any other object): if (scores.length < 10) Note that length holds the number of elements, not the largest index COMP 202 - Arrays 15

ReverseNumbers.java public class ReverseNumbers { public static void main (String[] args) { Scanner scan = new Scanner (System.in); double[] numbers = new double[10]; System.out.println ("The size of the array: " + numbers.length); for (int index = 0; index < numbers.length; index++) { System.out.print ("Enter number " + (index+1) + ": "); numbers[index] = scan.nextdouble(); System.out.println ("The numbers in reverse:"); for (int index = numbers.length-1; index >= 0; index--) System.out.print (numbers[index] + " "); System.out.println (); How does this work? COMP 202 - Arrays 16

public class TravelLog { public static void main (String[] args) { Scanner scan = new Scanner (System.in); int[] travellog = new int[8]; int sum = 0, entry; double average; TravelLog.java System.out.println ("Let's fill the travel log for the first " + travellog.length + " hours."); for (int i=0; i<travellog.length; i++) { System.out.print ("Enter the number of kilometers covered " + " in hour " + (i+1) + " : "); travellog[i] = scan.nextint(); for (int i=0; i<travellog.length; i++)sum += travellog[i]; System.out.println ("The total distance covered is " + sum + " km."); average = (double)sum / travellog.length; DecimalFormat fmt = new DecimalFormat ("0.###"); System.out.println ( Average speed " + fmt.format(average) + " km/h."); System.out.print ( Review Trip (-1 to exit)? "); entry = scan.nextint(); while (entry>0 && entry<=travellog.length) { System.out.println ( Kilometres covered " + travellog[entry-1]); System.out.print( Review Trip (-1 to exit)? "); entry = scan.nextint(); Run through by hand/eye COMP 202 - Arrays 17

import java.util.scanner; public class LetterCount { public static void main (String[] args) { final int NUMCHARS = 26; Scanner scan = new Scanner (System.in); int[] upper = new int[numchars]; int[] lower = new int[numchars]; char current; int other = 0; LetterCount.java System.out.println ("Enter a sentence:"); String line = scan.nextline(); for (int ch = 0; ch < line.length(); ch++) { current = line.charat(ch); if (current >= 'A' && current <= 'Z') upper[current-'a']++; else if (current >= 'a' && current <= 'z') lower[current-'a']++; else other++; for (int letter=0; letter < upper.length; letter++) { System.out.print ( (char) (letter + 'A') ); System.out.print (": " + upper[letter]); System.out.print ("\t\t" + (char) (letter + 'a') ); System.out.println (": " + lower[letter]); System.out.println ("Non-alphabetic characters: " + other); COMP 202 - Arrays 18

Initializer Lists An initializer list can be used to instantiate and initialize an array in one step The values are delimited by braces and separated by commas Examples: int[] units = {147, 323, 89, 933, 540, 269, 97, 114, 298, 476; char[] lettergrades = {'A', 'B', 'C', 'D', 'F'; COMP 202 - Arrays 19

Initializer Lists Note that when an initializer list is used: the new operator is not used no size value is specified The size of the array is determined by the number of items in the initializer list An initializer list can only be used in the declaration of an array COMP 202 - Arrays 20

Primes.java public class Primes { public static void main (String[] args) { int[] primes = {2, 3, 5, 7, 11, 13, 17, 19; System.out.println ("Array length: " + primes.length); System.out.println ("The first few prime numbers are:"); for (int prime = 0; prime < primes.length; prime++) System.out.print (primes[prime] + " "); System.out.println (); COMP 202 - Arrays 21

Part 2 Arrays and Parameters COMP 202 - Arrays 22

Arrays as Parameters An entire array can be passed to a method as a parameter convert(int[] alist) { convert(units); Note: Like any other object, the reference to the array is passed, making the formal and actual parameters aliases of each other Changing an array element in the method changes the original An array element can be passed to a method as well, and will follow the parameter passing rules of that element's type convertone(int i) { convertone (units[3]); Arrays can also be returned: int[] oddlist(int limit) COMP 202 - Arrays 23

Problem Write a program that asks the user for positive integer indicating the number of values they want to enter. Create an array to be the exact size as the number of values they want to enter If the user does not input a positive value the program asks them to input again The program will then call a function called average that will accept an array as parameter and return a double value representing the average of all the values in the array. COMP 202 - Arrays 24

Part 3 Arrays and Objects COMP 202 - Arrays 25

Arrays of Objects The elements of an array can be object references The following declaration reserves space to store 25 references to String objects String[] words = new String[10]; It does NOT create the String objects themselves Each object stored in an array must be instantiated separately COMP 202 - Arrays 26

Array of String Objects At declaration: words 0 1 2 3 4 5 6 7 8 9 - - - - - - - - - - words At a few string object created and put into the array 0 1 2 3 4 5 6 7 8 9 COMP 202 - Arrays 27 - - - - - - - hello world something

GradeRange.java public class GradeRange { public static void main (String[] args) { String[] grades = {"A", "A-", "B+", "B", "B-", "C+", "C", "D", "F"; float[] gradepoint = { 4.0f, 3.7f, 3.3f, 3.0f, 2.7f, 2.3f, 2.0f, 1.0f, 0.0f; int[] cutoff = {85, 80, 75, 70, 65, 60, 55, 50, 0; System.out.println ("GRADE" + "\t" + "GP" + "\t" + "CUTOFF"); for (int level = 0; level < cutoff.length; level++) System.out.println (grades[level] + "\t" + gradepoint[level] + "\t" + cutoff[level]); What does this do? COMP 202 - Arrays 28

FeedTheLitter.Java public class FeedTheLitter { public static void main (String[] args) { final int NUM_CATS = 3; Cat[] litter = new Cat[NUM_CATS]; for (int i=0; i<num_cats; i++) { litter[i]=new Cat(); System.out.println("\nCat " + (i+1) + " :\n" + litter[i]); System.out.println("\n******** Let's feed our cats ********\n"); for (int i=0; i<num_cats; i++) { System.out.println(); litter[i].eat(i*2.0f); System.out.println("\nCat " + (i+1) + " :\n" + litter[i]); COMP 202 - Arrays 29

public class Cat { double weight; int age; boolean isfriendly; Cat.java public Cat() { weight = 3.8; age = 2; moodswing(); public void moodswing(){isfriendly = ((int)(math.random()*2) == 0); public String tostring(){ String sweight = "I weight " + weight + " kg.\n"; String sage = "I'm " + age + " years old.\n"; String sfriendly = (isfriendly)? "I'm the nicest cat in the world" : "One more step and I'll attack."; return (sweight+sage+sfriendly); public double eat(float food){ weight += food; System.out.println("it wasn't Fancy Feast's seafood fillet..."); wail(); return weight; public void wail() { System.out.println("Miiiiaaawwwwwww!"); moodswing(); COMP 202 - Objects 2 30

Objects with Arrays Objects can have arrays as instance variables Therefore, fairly complex structures can be created simply with arrays and objects The software designer must carefully determine an organization of data and objects that makes sense for the situation COMP 202 - Arrays 31

Problem Create a class called MyCD storing information about an individual CD. Then create another class called MyInventory that has a main method that creates an array of MyCD objects. The program will then display a menu: Add CD, List CDs, Quit. Add CD adds a CD to the next available cell. List CD lists all the filled cells Quit ends the program COMP 202 - Arrays 32

Possible Other Problem Define a student with a name, an id, marks for final, midterm and 5 assignments. define proper setter and getter methods define tostring method Define a class that supports a group of students allows to enter students to group allows to calculate average for midterm, final, etc. Define a starter class that creates the student group for 202. COMP 202 - Arrays 33

Part 4 Multidimensional Arrays COMP 202 - Arrays 34

Two-Dimensional Arrays A one-dimensional array stores a simple list of values A two-dimensional array can be thought of as a table of values, with rows and columns A two-dimensional array element is referenced using two index values To be precise, a two-dimensional array in Java is an array of arrays COMP 202 - Arrays 35

For Example int x[][] = new int[3][4]; x[1][1] = 5; 0 1 2 0 1 2 3 5 COMP 202 - Arrays 36

TwoDArray.java public class TwoDArray { public static void main (String[] args) { int[][] table = new int[5][10]; // Load the table with values for (int row=0; row < table.length; row++) for (int col=0; col < table[row].length; col++) table[row][col] = row * 10 + col; // Print the table for (int row=0; row < table.length; row++) { for (int col=0; col < table[row].length; col++) System.out.print (table[row][col] + "\t"); System.out.println(); What does this do and how? COMP 202 - Arrays 37

Multidimensional Arrays An array can have as many dimensions as needed, creating a multidimensional array Each dimension subdivides the previous one into the specified number of elements Each array dimension has its own length constant Because each dimension is an array of array references, the arrays within one dimension could be of different lengths COMP 202 - Arrays 38

For Example double x[][][] = new[5][5][5]; x[1][2][4] = 5.3 COMP 202 - Arrays 39

Part 5 The ArrayList Object COMP 202 - Arrays 40

The ArrayList Class Often programmers want the functionality of an array but the freedom to not have to manage an array. Often programmers want to insert and delete items from an array. Arrays have the restriction that their sizes are fixed. ArrayList however can grow dynamically. COMP 202 - Arrays 41

The ArrayList Class An object of class ArrayList is similar to an array in that it stores multiple values However, an ArrayList only stores objects does not have the indexing syntax that arrays have The methods of the ArrayList class are used to interact with the elements of a vector The ArrayList class is part of the java.util package COMP 202 - Arrays 42

The ArrayList Class ArrayList() boolean add(object obj) void add(int index, Object obj) Object remove(int index) Object set(int index, Object obj) void clear() boolean contains(object obj) int indexof(object obj) Object get(int index) boolean isempty() int size() COMP 202 - Arrays 43

Beatles.java public class Beatles { public static void main (String[] args) { ArrayList band = new ArrayList(); band.add ("Paul"); band.add ("Pete"); band.add ("John"); band.add ("George"); System.out.println (band); int location = band.indexof ("Pete"); band.remove (location); System.out.println (band); System.out.println ("At index 1: " + band.get(1)); band.add (2,"Ringo"); System.out.println (band); System.out.println ("Size of the band: " + band.size()); What does this do? COMP 202 - Arrays 44

The ArrayList Class An important difference between an array and an ArrayList is that a ArrayList can be thought of as dynamic, able to change its size as needed Each ArrayList initially has a certain amount of memory space reserved for storing elements If an element is added that doesn't fit in the existing space, more room is automatically acquired COMP 202 - Arrays 45

The ArrayList Class The ArrayList class is implemented using an array Whenever new space is required, a new, larger array is created, and the values are copied from the original to the new array To insert an element, existing elements are first copied, one by one, to another position in the array The implementation of ArrayList in the API is not very efficient for inserting elements COMP 202 - Arrays 46

Part 6 Variable length parameter list COMP 202 - Arrays 47

Variable length parameter list A method can also have a variable length parameter list which automatically gets converted to an array inside the method: public int sum(int... alist) { int total = 0; for (int num : alist) total += num; return total; A method can have only one variable length parameter and it must be after all other parameters in the parameter list An overloaded method with an exact number of parameters always has precedence over the variable length list method COMP 202 - Arrays 48

Part 7 The For-each Statement COMP 202 - Arrays 49

The Foreach statement public int[] sum(int[] alist) { int total = 0; for (int i=0; i<alist.length; i++) total += alist[i]; return total; is equivalent to public int sum(int[] alist) { int total = 0; for (int num : alist) total += num; return total; COMP 202 - Arrays 50

For-each Statement The foreach statement works on arrays and on any object whose class implements the Iterable interface (which consists of only one method which returns an implementation of the Iterator interface), such as the ArrayList class. In the previous Beatles example, we could have printed out the members of the band using: for (Object temp : band) System.out.println(temp); COMP 202 - Arrays 51

Part 7 Thinking Like A Programmer COMP 202 - Arrays 52

Designing for Arrays Arrays are a mass storage structure Design your program with these rules: Identify large units of data If the data is simple then use a regular array If the data is complex use an array of objects If the data is massive then use a file or database (arrays use too much memory when data is massive) Construct your object to be used for the array Use encapsulation to fully implement all the features of the data you want to store. COMP 202 - Arrays 53

Try these problems Problem 1: Sum 50 integer numbers and print them out. Problem 2: Implement a bank and bank account program. Problem 3: Matrix Multiplication Discuss for each the design issues from the previous slide. Do this for each problem. Then pick one to program. COMP 202 - Arrays 54

Part 8 Command-line Arguments

Command-Line Arguments The signature of the main method indicates that it takes an array of String objects as a parameter These values come from command-line arguments that are provided when the interpreter is invoked For example, the following invocation of the interpreter passes an array of three String objects into main: > java DriverProg rain dogs cats The words rain dogs cats are stored at indexes 0-2 of the parameter args COMP 202 Arrays2 56

Echo.java public class Echo { public static void main(string args[]) { int i; if (args.length == 0) { System.out.println("No commandline arguemts!"); return; for(i=0;i<args.length;i++) System.out.println(args[i]); COMP 202 Arrays2 57

Try this problem Write a main method that checks its argument to see if the user input extra text at the command-line called a flag. A flag is a command to the program. Assume that the flag h (dash followed by a letter h) asks the program to display a help message. How would you do this? COMP 202 Arrays2 58

Wrapper Classes Two categories of data in Java : primitive data types and objects. We use wrapper classes to manage primitive data as objects Each wrapper class represents a particular primitive type Integer everest = new Integer(8850); We have just «wrapped» the primitive integer value 8850 into an object referenced by the everest variable. All wrapper classes are part of the java.lang package: Byte, Short, Integer, Long, Float, Double, Character, Boolean, Void. COMP 202 Arrays2 59

Some Methods of Integer Class Integer (int value) byte bytevalue() double doublevalue() float floatvalue() int intvalue() long longvalue() Example: String s = 500 ; int number; number = Integer.parseInt(s); static int parseint (String str) static String tobinarystring(int num) COMP 202 Arrays2 60

Transforming Strings into Integers public class AddNumbers { public static void main(string args[]) { int sum = 0; // add all input values for (int i = 0; i < args.length; i++) // transform input Strings into integers // and add them to sum sum += Integer.parseInt(args[i]); System.out.println( The sum is: + sum); COMP 202 Arrays2 61