0

I need to program for school but I'm stuck right now.

Introduction

I have a file named "carlist.txt." This file has on each line information about a car in this order: vehicle_idnumber num_of_passengers fuel_capacity mpg make

So, a typical line might look like this: 487833 6 20 25 Ford

When you read in the file, you are guaranteed it is “clean”. The data are always:int int int int String over and over. This makes your work a lot simpler.

You also need to add a “method” to your vehicle object definition. This method should be a function that returns a double. The function computes the range of the vehicle. All it does is take the fuelcap and multiply it by the mpg. Your “turn-in” program needs to solve the following tasks.

Program 1:

Write a method which walks the array of cars and prints out the total number of each make of car. Your output should look something like: (these are made up numbers!)

MAKE NUMOFCARS

Ford 17 Subaru 23 GM 8 Porsche 5 Toyota 35 Suburban 12

Program 2: Write a method which analyzes all the cars and prints out the car id that has the highest range and the car id that has the lowest range. Note: range means fuel_cap * mpg. So, for example you might print out: (note, these are made-up numbers!)

Highest range: 987654 with a range of 430 miles Lowest range: 234567 with a range of 223 miles

In the example above the big number is the car vehicle number and the “miles” number is the range.


For program 1, I have to create an array (not array list!) but I don't how to deal with the fact that I don't know the size of the array upfront (since I don't know how many carmodels the list contains). How can I add car models or how do I handle the fact that one list might contain 5 car models and the next list contains 15 different models. How do I create this array out of my list?

Alex W
  • 1

1 Answers1

0

That is indeed a very weirdly worded question. Given the text you posted, I would assume that "array" here is being used generically to just mean "ordered collection" instead of a literal Java array. If not, you are right that when reading in a file, you have no way to know how long the file is going to be before you finish, so there is no efficient way to make a fixed-length array of the right length to hold an entry for every line.

If you absolutely have to, I can think of 3 ways:

  1. Use an ArrayList when building the collection, then call .toArray() on in when you are done to get it as an array. This is pretty harmless, but also totally pointless unless you need to pass it to a function that happens to only take arrays (which is maybe what is going on).
  2. Make a first pass through the file to find the length, then make an array of that length and do a seccond pass through the file to fill that array. (See here for getting the number of lines in a file).
  3. Make an array that is way bigger than you could possibly need and just keep track of how much you fill it up. Then, when you are walking the array, stop after you get to that number (or when you hit a null).

TBH though if your teacher actually wanted you to do any of those things (especially 2 or 3) they are absolutely insane. I'm guessing this is a misunderstanding somehow.

QuinnFreedman
  • 2,242
  • 2
  • 25
  • 42
  • Hi, thank you for your answer and help. I used number 3 to solve the problem and to write the code. It works now - I'm so happy! In my last program I used an ArrayList but the teacher told me that we are not allowed to use that function in this class. I don't think it's a misunderstanding. He probably just starts at the very beginning with easy things. You might consider it "insane" and that's probably true for an experienced JAVA programmer but not for 11th grade highschool :-)! – Alex W Mar 07 '21 at 02:29