3 Replies - 609 Views - Last Post: 20 October 2011 - 03:23 AM Rate Topic: -----

#1 Xeyphone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-October 11

Do not know what im doing wrong (static, random access file problems)

Posted 20 October 2011 - 12:47 AM

So for a school project I have been asked to create a taxi simulation. I am required to use random access files to store my data with each run of the simulation. I have 2 different classes, in the class TestingOnly is where the simulation occurs. Here is the TestingOnly code:

import java.util.*;
import java.io.*;
import java.io.IOException;
import java.io.RandomAccessFile;
public class TestingOnly {
    
public static  double distanceForLong;
      public static  double tripCostForLong = 0 ;
      public static  double distanceLongRandom;
      public static  int amountOfTripsForLong= 0;
      public static  Random rndmLong = new Random();
      public static  Random bookedLong = new Random();
      public static  double totalCostForLong = 0;
      public static  double amountOfTanksUsedLong = 0;
      public static  double amountOfLitersUsedLong = 0;
      public static  double rateLong = .5;
      public static  double milageLong = 7.6;
      public static  int longtripLong = 50;
      public static  int tankLong = 65;
      public static  int minimumLong = 10; 
      public static  int startLong = 4;
      public static  int bookedstartLong = 6;
      public static  double longDistance =0;
      public static  double randomTime = 0;
      public static  long timeTaken =0;
      public static  double distanceForShort;
      public static  double tripCostForShort = 0 ;
      public static  double distanceShortRandom;
      public static  int amountOfTripsForShort= 0;
      public static  Random rndmShort = new Random();
      public static  Random bookedShort = new Random();
      public static  double totalCostForShort = 0;
      public static  double amountOfTanksUsedShort = 0;
      public static  double amountOfLitersUsedShort = 0;
      public static  double rateShort = .5;
      public static  double milageShort = 7.6;
      public static  int shorttripShort = 15;
      public static  int tankShort = 65;
      public static  int minimumShort = 10; 
      public static  int startShort = 4;
      public static  int bookedstartShort = 6;
      public static  double shortDistance = 0;
      public static  long timeTakenShort= 0;
      public static  Random timeTakenRandomShort = new Random();
      public static  int randomTimeTaken = 0;
      public static  int randomTimeTakenTemp = 0;
      public static  double randomTimeLong = 0;
      public static  double randomWait = 0;
      public static int arrayCounter = 0;
      public static int arrayCounterLong = 0;
      public static double longArray[] = new double[1000];
      public static double shortArray[] = new double[1000];
      public static int amountOfTimes = 0;
      public static long shortTime = 0;
      public static double longTime = 0;
      public static Scanner kb = new Scanner(System.in);
      public static int io = 0;
      public static double average = 0;
      public static double averageLong = 0;
     static RandomShort file = new RandomShort();

    public static void main(String args[]) throws IOException, NullPointerException{
        System.out.println("How many times do you want to run the simulation?");
        System.out.println(" ");
        amountOfTimes = kb.nextInt();
        for(io = 0; io <= amountOfTimes; io++)
        {
        shortTime = Shorttrip();
        shortArray[arrayCounter] = shortTime;
        RandomShort.editShort(shortTime);
        longTime = longerTrips();
        longArray[arrayCounterLong] = longTime;
        timeTakenShort = 0;
        longDistance = 0;
        timeTaken = 0;
        RandomShort.closeshort();
        }
        for(int j = 0; j <= amountOfTimes; j ++)
        {
        average += shortArray[j];
        averageLong += longArray[j];
        }
        average = average/amountOfTimes;
        averageLong = averageLong/amountOfTimes;
        
        System.out.println("The average amount of Hours it took for a taxi driver doing short trips to reach the 350DHS limit is " + average/60 + " And the long trips " + averageLong/60);
    }
        public static long Shorttrip()throws IOException, NullPointerException{
        
 
        for(totalCostForShort = 0; totalCostForShort <= 350; amountOfTripsForShort++ )
        {
            distanceShortRandom = rndmShort.nextInt(shorttripShort) + 1;
           int bookeddShort = bookedShort.nextInt(1) + 1;
           randomTime= rndmShort.nextInt(1) + 1;
                   if(bookeddShort == 0)
                   {
                       tripCostForShort = distanceShortRandom*rateShort + startShort;
                   }
                   else if(bookeddShort== 1 )
                   {
                       tripCostForShort = distanceShortRandom*rateShort + bookedstartShort;
                   
                   }
                   if ( tripCostForShort < 10)
                   {
                       tripCostForShort = 10;
                   }
           totalCostForShort = totalCostForShort + tripCostForShort;
          
           amountOfLitersUsedShort = amountOfLitersUsedShort + (distanceShortRandom / milageShort);
           shortDistance = distanceShortRandom + shortDistance;
           amountOfTanksUsedShort =(amountOfLitersUsedShort /tankShort);
           
           timeTakenShort = (long) (distanceShortRandom * randomTime + rndmShort.nextInt(10) +1 + timeTakenShort);
           
        }
        
        arrayCounter ++;
       
        return timeTakenShort;
        
        }
        public static double longerTrips()throws IOException, NullPointerException
        {
       
        for(totalCostForLong = 0; totalCostForLong <= 350; amountOfTripsForLong++ )
        {
           randomTimeLong = rndmLong.nextInt(1) + 1;
            distanceLongRandom = 15 + rndmLong.nextInt(longtripLong);
           int bookeddLong = bookedLong.nextInt(1) + 1;
                   if(bookeddLong == 0)
                   {
                       tripCostForLong = distanceLongRandom*rateLong + startLong;
                   }
                   else if(bookeddLong== 1 )
                   {
                       tripCostForLong = distanceLongRandom*rateLong + bookedstartLong;
                   
                   }
                   if ( tripCostForLong < 10)
                   {
                       tripCostForLong = 10;
                   }
           totalCostForLong = totalCostForLong + tripCostForLong;
          
           amountOfLitersUsedLong = amountOfLitersUsedLong + (distanceLongRandom / milageLong);
           longDistance = distanceLongRandom + longDistance;
           amountOfTanksUsedLong =(amountOfLitersUsedLong /tankLong);
            
           timeTaken =(long) (longDistance/randomTimeLong);
        }
        arrayCounterLong ++;
return timeTaken;
        }
}


To manage the random access files i have created a second class because i have had troubles with using Random access files because of static class references. here is the code for the Random access files:




import java.util.*;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomShort {
    public void main(String[] args) throws IOException{
       RandomAccessFile shortData = new RandomAccessFile("TaxiDataShort.txt", "rw");
    }
    
    public static void editShort(long x) throws IOException{
        shortData.writeLong(x);
    }
    
    public static void closeshort() throws IOException{
     shortData.close();
    }

}




Any idea what I'm doing wrong? if so, your input would be greatly appreciated. If i am using random access files completely wrong, it would be appreciated if you could explain what I'm doing wrong and what i need to improve on :)

Thanks for your time.

Is This A Good Question/Topic? 0
  • +

Replies To: Do not know what im doing wrong (static, random access file problems)

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Do not know what im doing wrong (static, random access file problems)

Posted 20 October 2011 - 12:51 AM

Are you getting an error message? If so, post it exactly, cut and paste. If not, describe the problem. What's happening or not happening that you need help with?
Was This Post Helpful? 0
  • +
  • -

#3 Xeyphone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-October 11

Re: Do not know what im doing wrong (static, random access file problems)

Posted 20 October 2011 - 12:54 AM

Error message :
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: class IOException
location: class java.io
at TestingOnly.<clinit>(TestingOnly.java:3)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

I have also for some reason got an error where its not recognizing the random access file import for some reason, it was working for an hour or two then it just does not recognize it anymore :/
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1148
  • View blog
  • Posts: 2,522
  • Joined: 05-May 05

Re: Do not know what im doing wrong (static, random access file problems)

Posted 20 October 2011 - 03:23 AM

public static void editShort(long x) throws IOException{
        shortData.writeLong(x);
    }



shortData isn't defined in editShort() and it's not a class member. That's a compile-time error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1