{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Length samplingdatalength monthnamelength page 9 4 9

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: uot;, "April", "May", "June", "July", "August", "September", "October", "November", "December" }; number.length samplingData.length monthName.length Page 9 4 9 12 11/2/2008 Variable-size Declaration • In Java, we are not limited to fixed-size array declaration. • The following code prompts the user for the size of an array and declares an array of int size; designated size: int number; size= Integer.parseInt(JOptionPane.showInputDialog(null, "Size of an array:")); number = new int[size]; Page 10 11/2/2008 Arrays of Objects • In Java, in addition to arrays of primitive data types, we can declare arrays of objects • An array of primitive data is a powerful tool, but an array of objects is even more powerful. • The use of an array of objects allows us to model the application more cleanly and logically. Page 11 11/2/2008 public class Person { private String name; private int age; private char gender; public Person() {age=0; name=" "; gender=' ';} public Person(String na, int ag, char gen) {setAge(ag); setName(na); setGender(gen); } public Person(Person pr) { setPerson(pr);} public void setPerson(Person p) { age=p.age; gender =p.gender; name=p.name. substring(0, p.name.length()); } public void setAge (int a) {age=a;} public void setGender (char g) {gender=g;} public void setName(String na) {name=na.substring(0, na.length());} public int getAge(){return age;} public char getGender () {return gender;} public String getName () { return name;} } Page 12 11/2/2008 The Person Class • We will use Person objects to illustrate the use of an array of objects. public class Person { private String name; private int age; private char gender; pu...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online