Arrays in Java
Download
Report
Transcript Arrays in Java
Arrays in Java
• An array is a collection of elements of the same type
• Declaration (we will use integers in this example)
int[] A;
int A[];
after this the elements do not exist yet !
A = new int[10];
this means we have now 10 integer variables: A[0], A[1], A[2],
A[3], A[4], A[5], A[6], A[7], A[8], A[9]
for (int i=0; i < 10; i=i+1)
A[i] = i;
makes A[0] = 0; A[1] = 1; ....... A[8] = 8; A[9] = 9;
int i = A.length; gives the number of elements (10 in this case)
Example with Arrays in Java
• Statistics about a list of numbers which is entered by keyboard.
The numbers are all positive from 1 to 10. The last number
entered will be the 0. After that the computer should answer the
% of 1, the % of 2, etc. (see Program6)
Enter a number: 3
10%
Enter a number: 6
0%
Enter a number: 9
30%
Enter a number: 5
0%
Enter a number: 3
20%
Enter a number: 7
The % of 1s was
The % of 2s was
The % of 3s was
The % of 4s was
The % of 5s was
The % of 6s was
The arguments of a program
• Now we can understand something about what is String args[] on
the beginning of the main method. It is an array of Strings. The
values of these String, as well as the length of the array are given
when the program is called.
public class Program7 {
public static void main(String args[]) {
for(int i=0; i < args.length; i++)
System.out.println(“Argument “+i+” is “+args[i]);
}
}
• This program will show all the parameters passed to the program
when it was called, for example if we write
java Program7 peter paul mary the program will write
Argument 1 is peter
Argument 2 is paul
Argument 3 is mary
Classes and Objects in Java
• We can say that there are two types of classes
from which we can create objects:
– Classes provided by the language: there is a large list
of classes provided by the language which the user can use.
Many of them are in libraries which must be imported
(java.io.* contains the BufferedReader class)
– Classes created by the user: sometimes the user wants to
have an object of a class which doesn't exist. For example,
an object of a class “Student” which should have all the
information for a student of the university. The user can
define:
• The variables which an object of this class will have
• The methods which can be applied to an object of this class
Lets start by using an existing
class: the String
• A class is defined primarily by the methods you can
apply to an object of such class
• There is a special type of method called constructor,
which creates a new object of this class.
• Example:
String myName; //the object doesn't exist yet
myName = new String(“Nelson Baloian”);
This is a call to the constructor.
In order to apply a method to an object
object_variable.method(parameters)
Methods of the String class
• Length of a String: int i = myName.length();
– i will have the value 14
• i-th character: char c = myName.charAt(0);
– c will contain the character N
char c = myName.charAt(3);
- c will contain the character s
• Sub sequence:
String sub = myName.substring(7);
– sub will contain the string “Baloian”
String sub = myName.substring(3, 6);
- sub will contain the string “son”
Some methods of the String class
• Search for sub sequences:
String s = new String(“Helo, Nelson, Hello”);
int i = s.indexOf(“elo”);
- i will have the value 1, it searches for the first appearance
• Comparing: boolean onnajiDesKa = s1.equals(s2);
– the variable onnajiDes will have the value true if the text of
the string s1 is equal to the text in the string s2
int i = s1.compareTo(s2);
- i will have the following values depending on s1 and s2
0 if s1==s2,
>0 if s1>s2,
<0 if s1<s2
The value of the string is according to where they would
appear in a dictionary, first means smaller
Finding all the sub-strings of a String
We will pass 2 strings as arguments to the program and the
program will answer how many times the second string
appears in the first one
public class Program8 {
public static void main(String args[]) {
int i , app = 0;
String first = args[0];
String second = args[1];
do {
i = first.indexOf(second);
if (i == -1) break;
app = app+1;
first = first.substring(i+1);
} while(true);
System.out.println(“Found “ +app+” times”);
}
}
User Defined Classes (the Clock)
public class Clock {
int hour, minutes;
Clock(int x, int y) {
hour = x; minutes = y;
}
public int getHour() {
return hour;
}
//same for address and yearBorn
// for yearBorn the return type is int
public void setName(String x) { // same for address and
yearBorn
name = x;
}
// for yearBorn the parameter is of type int
}
public int age(int thisYear) {
return thisYear - yearBorn
}
User Defined Classes (The Student)
public class Student {
String name, address;
int yearBorn;
Student(String x, String y, int z) {
name = x; address = y; yearBorn = z;
}
public String getName() { //same for address and yearBorn
return name;
// for yearBorn the return type is int
}
public void setName(String x) { // same for address and
yearBorn
name = x;
}
// for yearBorn the parameter is of type int
}
public int age(int thisYear) {
return thisYear - yearBorn
}
How to write methods
Type of value the method will
return (void in case it does not
return any value)
Name of the method
(given by the programmer)
public String getName( ) {
Public, private,
protected or static
return name;
}
The instructions should be
written within the brackets {}
Parameters of the
method (none in this case)
How to write constructor methods
Name of the method
must be the class name
Parameters the constructor will
receive to create the new object
Student ( String x, String y, int z ) {
name = x; address = y; yearBorn = z
}
No need to write anything here
We can write more than one constructor
Student ( ) {
name = “N.N.”; address = “Tokyo”;
yearBorn = 2000;
}
This constructor puts default values to the new object
How to use-user defined classes
The file with the user-defined class should be compiled
The class file should be at the same directory with the file
containing the program.
public class Program9 {
public static void main(String args[]) {
Student s1,s2;
s1= new Student();
s2 = new Student(“Simon”,”Santiago”,1998);
s1.show(); s2.show();
s1.setName(“John Smith”);
s1.setAddress(“123 5th Ave. New York”);
String n = s2.getName(); s2.setName(n+” Baloian”);
s1.show(); s2.show();
}
}
Extending an existing class:
Inheritance
• One of the most powerful features of object
oriented programming is the possibility to extend
an existing class, even if the implementation of the
original class is not known. This is called
inheritance
Original Class
Super-class of the extended class
Definition of new variables and methods
Original Class
Extended Class
Extending the Student class
public class WasedaStudent extends Student{
int[] marks;
int nmarks;
WasedaStudent(String x, String y, int z, int n) {
super(x,y,z);
// a call to the constructor of
the superclass
marks = new int[n]; nmarks = 0;
}
}
}
public void putNewMark(int x) {
marks[nmarks] = x;
nmarks = nmarks +1;
public double showAverage() {
double sum = 0.0; int i;
for(i=0; i < nmarks; i=i+1)
sum = sum + marks[i];
return sum/nmarks;
}
Using the extended class
public class Program10 {
public static void main(String args[]) {
WasedaStudent ws1,ws2;
ws1 =new
WasedaStudent(“student1”,”Musashino-shi”,1980,5);
ws2 =new
WasedaStudent(“student2”,”Shinjuku-ku”,1981,7);
ws1.putNewMark(10); ws1.putNewMark(5);
ws1.putNewMark(4);
ws2.putNewMark(5); ws2.putNewMark(8);
ws2.putNewMark(7);ws2.putNewMark(4);
System.out.println(ws1.showAverage());
System.out.println(ws2.showAverage());
ws1.show(); ws2.show();
}
}
Note that we can use all the methods of the superclass