Fri Nov 07, 2008 4:17 pm
int[] a;
int[] a = new int[100];
int[] a; or as int a[];
int[] a = new int[100];
for (int i = 0; i < 100; i++)
a[i] = i; // fills the array with 0 to 99
public class TestParking
{
public static void main(String[] args)
{
ParkingTimes frustration = new ParkingTimes();
frustration.add(12.3);
frustration.add(35);
frustration.add(17);
frustration.add(5);
frustration.add(10.3);
System.out.println ("minimum time spent looking for parking: " + frustration.min());
System.out.println ("maximum time spent looking for parking: " + frustration.max());
System.out.println ("average time spent looking for parking: " + frustration.ave());
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package parkingtimes;
/**
*
* @author sam
*/
public class ParkingTimes extends Object{
private double[] times = new double[1000];
private int num = 0;
public ParkingTimes() {
}
public void add(double t) {
times[num] = t;
num++;
}
public double ave() {
if (num <= 0) {
return 0.0;
}
double avg = 0.0;
for (int i = 0; i < num; i++) {
avg += times[i];
}
return avg / num;
}
public double max() {
if (num <= 0) {
return 0.0;
}
double max = times[0];
for (int i = 1; i < num; i++) {
if (times[i] > max) {
max = times[i];
}
}
return max;
}
public double min() {
if (num <= 0) {
return 0.0;
}
double min = times[0];
for (int i = 1; i < num; i++) {
if (times[i] < min) {
min = times[i];
}
}
return min;
}
public void increaseSize(){
double []newarray= new double[times.length*2];
for(int i=0;i<times.length;i++)
{
newarray[i]=times[i];
}
times = null;
times = newarray.clone();
// System.err.println(times.length);
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.