types in java

23
Types in Java • 8 Primitive Types – byte, short, int, long – float, double – boolean – Char • Also some Object types: e.g. “String” • But only single items. What if we want to store lots of items – e.g. midsem marks?

Upload: yehuda

Post on 06-Jan-2016

31 views

Category:

Documents


0 download

DESCRIPTION

Types in Java. 8 Primitive Types byte, short, int, long float, double boolean Char Also some Object types: e.g. “String” But only single items. What if we want to store lots of items – e.g. midsem marks?. Arrays. Arrays are data structures that can hold a series of values - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Types in Java

Types in Java• 8 Primitive Types

– byte, short, int, long– float, double– boolean– Char

• Also some Object types: e.g. “String”

• But only single items. What if we want to store lots of items – e.g. midsem marks?

Page 2: Types in Java

Arrays

• Arrays are data structures that can hold a series of values– An array uses an index to access a particular

value

Page 3: Types in Java

Primitive Type Syntax

double number = 3;

Page 4: Types in Java

Array Syntax

double[ ] number;

Page 5: Types in Java

Array Syntax

double[ ] number = {1,2,3} ;

String s[ ] = {"This", "is", "an", "array"};

Page 6: Types in Java

Array Syntax

double[ ] number;

Can also initialize with the “new” construct:

double [ ] number = new double[3];

Page 7: Types in Java

Array Implementation

Every time an array is initialized, using new, or by {… },

• a contiguous block of memory is assigned for it

• the array name (the identifier number) is assigned the start address (In Java, this address is the reference for this variable).

• number[3] gets an address 3 shifted from the start

Page 8: Types in Java

Parallel Arrays

• int[ ] rollNum= {6016, 6024, 6078};• double[ ] midsemMarks

= {61.7, 54 , 74.2 };• String[ ] name = {"AbhishekA", "AbhishekS",

"Ankit"};

• In practice, parallel arrays are hard to maintain. • Better is to define an object with data: rollNum,

marks, and name, and define arrays of these objects

Page 9: Types in Java

Printing an Array• class ArrayPrint• {• public static void main (String arg[])• {• double numbers[] = {1,2,3}; • double numbers2[] = {1,2,3,4}; • String s[] = {"This", "is", "an", "array"};• printArray (s,4);• }

• public static void printArray (String[] arr, int length)

• { for(int j=0;j<length;j++)• System.out.println("Element "+j+"=

"+arr[j]);• }• }

Page 10: Types in Java

Array Length

Array objects have some predefined variables

e.g. length

String s[ ] = {"This", "is", "an", "array"};

System.out.println("Length of s: "+ s.length);

Dot Syntax: If x is an object, then x.y tells you that y is some property computed for the object x.

Page 11: Types in Java

Finding the averageclass Average { public static void main (String arg[]) { float dailyRainFall[] = {12.3, 13.5, 4.2, 2.4, 1.1, 0, 10.8}; int i;

float average = 0; for (i=0; i<7; i++) { average += dailyRainFall[i]; } average /= 7; System.out.println (“Average rain fall: ” + average + “

mm”); }}

Page 12: Types in Java

Finding the averagefor (i=0; i<7; i++) { average += dailyRainFall[i]; }average /= 7;

Everytime I have another day of rain, I need to change the number “7” in my program

Instead, I can use

for (i=0; i<dailyRainFall.length; i++) { average += dailyRainFall[i]; }average /= dailyRainFall.length;

Page 13: Types in Java

class ArrayPrintclass ArrayPrint{ public static void main (String arg[]) { String s[ ] = {"This", "is", "a", "test", "array"}; printArray(s); System.out.println( "s[0] length is: "+s[0].length() ); }

public static void printArray (String[] arr) { for(int j=0;j<arr.length;j++) System.out.println("Element "+j+"= "+arr[j]); }

// exercise: define lengthOfStringArray (String[] s)}

Page 14: Types in Java

Finding maximumclass Maximum {

public static void main (String arg[]) {

int n = 100;

double numbers[] = new double[n];

Initialize (numbers, n);

System.out.println (“Maximum: ” + FindMax (numbers, n));

}

Page 15: Types in Java

Finding maximum public static void Initialize (double

numbers[], int length) {

int i;

for (i=0; i<length; i++) {

numbers[i] = Math.sin(2*Math.PI/(i+1)) + Math.cos(2*Math.PI/(i+2));

}

}

Page 16: Types in Java

Finding maximum public static double FindMax (double numbers[],

int length) { double max = numbers[0]; int i;

for (i=1; i<length; i++) { if (numbers[i] > max) { max = numbers[i]; } } return max; }} // end class

Page 17: Types in Java

Methods to return more than one parameter

• Want to identify both maximum marks, and also who has this maximum marks– Need to return two values from FindMax– Use a 2-element array as return type

Page 18: Types in Java

Finding max and max indexclass Maximum { public static void main (String arg[]) { int n = 100; double numbers[] = new double[n]; double result[];

Initialize (numbers, n); result = FindMax (numbers, n); System.out.println ("Maximum: " + result[0]

+ ", Position: " + (int)result[1]); }

Page 19: Types in Java

Finding max and max index public static void Initialize (double

numbers[], int length) {

int i;

for (i=0; i<length; i++) {

numbers[i] = Math.sin(2*Math.PI/(i+1)) + Math.cos(2*Math.PI/(i+2));

}

}

Page 20: Types in Java

Finding max and max index public static double[] FindMax (double numbers[],

int length) { double result[] = {numbers[0], 0}; int i;

for (i=1; i<length; i++) { if (numbers[i] > result[0]) { result[0] = numbers[i]; result[1] = i; } } return result; }} // end class

Page 21: Types in Java

Array Variables

• Array variables are typed by their array declaration

• Is this legal?

int[] rollNum= {6016, 6024, 6078}; int[] a = {1,2,3,4}; a = rollNum;

• Now the part of memory previously accessed through “a” is lost. “a” and “rollNum” now point to the same place in memory.

Page 22: Types in Java

Passing Arrays• The method findAverage() is defined thus:

public static double findAverage (double[] arr) { for (int i = 1; i<arr.length; i++) { arr[0] += arr[i]; }

return (arr[0] / arr.length) ; // will this work? }

• Q. I invoke this method on the array numbers[ ]. findAverage(numbers). Does numbers[0] remain what it was or does it change?

• Since method arguments in Java are passed by reference – so numbers[0] has the same address as arr[0] inside the method; and modifications to arrays inside the method are reflected outside the method

Page 23: Types in Java

class ArrayMidsems

class ArrayMidsems{ public static void main (String arg[]) { int[] rollNum= {6016, 6024, 6078}; double[] midsemMarks = {61.7, 54 , 74.2 }; String[] name = {"Abhishek A", "Abhishek S", "Ankit"}; System.out.println (getMarks(rollNum, midsemMarks, 6024)); }

public static void printArray (String[] arr, int length) { for(int j=0;j<length;j++) System.out.println("Element "+j+"= "+arr[j]); }

public static double findMax (double numbers[]) { double max = numbers[0]; for (int i=1; i<numbers.length; i++) { if (numbers[i] > max) max = numbers[i]; } return max; }

public static double findAverage (double[] arr) {

double sum = 0;for (int i = 0; i<arr.length; i++)

{ sum += arr[i];}return (sum / arr.length) ;

}

/* gets the marks of i-th student */ public static double getMarks (double[] marks, int i) { if (i > marks.length-1)

{ System.out.println ("Array index out of bounds"); return -999;

} return marks[i]; }

/* returns the marks for student with target rollNum */ public static double getMarks (int[] n, double[] marks, int target) { if (n.length !=marks.length)

{ System.out.println ("Array lengths don't match!"); return -999;

} for (int j=0; j< n.length; j++)

{ if (n[j] == target) return marks[j]; }return -999;

}} // end ArrayMidsems