Question

A function that takes in as input the colour of a car, and returns the number of cars of that colour present in a file that contains the colour of cars parked in a lot. Create the file, and put in col...

A function that takes in as input the colour of a car, and returns the number of cars of that colour present in a file that contains the colour of cars parked in a lot. Create the file, and put in colour of cars (the number of entries doesn't matter, but make sure there is repetition) E.g Blue Red Blue Green Black Red ............. and so on

0 0
Add a comment Improve this question Transcribed image text
Answer #1

input.txt

Blue Red Blue Green Black Red Blue Red Blue Green Black Red Blue Red Blue Green Black Red Blue Red Blue Green Black Red Blue Red Blue Green Black Red Blue Red Blue Green Black Red Blue Red Blue Green Black Red Blue Red Blue Green Black Red Blue Red Blue Green Black Red Blue Red Blue Green Black Red Blue Red Blue Green Black Red Blue Red Blue Green Black Red

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
  
public class CarColour {
  
   //mehtod to count number of cars with color 'col'
   public static int countCars(String col) throws FileNotFoundException
   {
       int count = 0;
       //reading file
       Scanner sc = new Scanner(new File("F://input.txt"));
      
       String data = "";
      
       //reading data from file and appending it to data.
       while(sc.hasNext())
       {
           data = sc.nextLine();
       }
       sc.close();
      
       //extracting all the words from data
       String colors[] = data.split(" ");
      
       //if word is equal to col then increase count.
       for(int i = 0 ; i < colors.length ; i ++)
       {
           if(colors[i].equalsIgnoreCase(col))
               count++;
       }
      
       return count;
   }

   public static void main(String[] args) throws FileNotFoundException {
      
       System.out.println("Number of cars with color: Red==>" + countCars("Red"));
   }

}

Console 3 . :isrrnirnaied C Number of cars with color: arColour [Java Applicatio n ( Pr()43ram liksNaw Red-=>24

If there is anything else you need, then please mention it in the comments section.

Add a comment
Know the answer?
Add Answer to:
A function that takes in as input the colour of a car, and returns the number of cars of that colour present in a file that contains the colour of cars parked in a lot. Create the file, and put in col...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 3. Write a program that contains the following functions a) A function that takes in...

    Question 3. Write a program that contains the following functions a) A function that takes in as input the amount a person has to pay to buy a house, and the monthly payments made. Your function should display how much has been payed and how much is left to pay every month until the mortgage has been payed (Ignore interest and the like) b) A function that takes in as input the color of a car, and returns the number...

  • You manage the inventory for a car dealership. Data on current inventory can be found in the week 2 data set excel file. Management would like you to assess current inventory on the dealership lot....

    You manage the inventory for a car dealership. Data on current inventory can be found in the week 2 data set excel file. Management would like you to assess current inventory on the dealership lot. Using the Week 2 Data Set, create and calculate the following in Microsoft® Excel®: Show a a pie chart which summarizes the colors of the cars in the sample. 2. Show a bar chart which summarizes the number of each of the models of cars...

  • Problem1: BMPmain.c, BMPfns.c, BMPfns.h, Makefile The file 'BMPmain.c' contains a main() function which is already written...

    Problem1: BMPmain.c, BMPfns.c, BMPfns.h, Makefile The file 'BMPmain.c' contains a main() function which is already written for you and attached with this homework. When appropriate functions are provided, main() will create a .bmp image file. Your job is to write 'BMPfns.c' which contains the functions which main() uses to create .bmp image files. You must also write the header file 'BMPfns.h' to #include in BMPmain.c and which contains prototypes of the functions defined in BMPfns.c . Problem2: BMPcheckerboard.c, BMPfns.c, BMPfns.h,...

  • ****PLEASE CODE IN C++ and line doc explaining what is happening!***** **HERE IS THE INPUT FILE(.txt)*****...

    ****PLEASE CODE IN C++ and line doc explaining what is happening!***** **HERE IS THE INPUT FILE(.txt)***** Ottawa Senators New York Rangers Boston Bruins Montreal Canadiens Montreal Canadiens Toronto Maple Leafs New York Rangers Chicago Blackhawks Montreal Maroons Detroit Red Wings Detroit Red Wings Chicago Blackhawks Boston Bruins New York Rangers Boston Bruins Toronto Maple Leafs Detroit Red Wings Montreal Canadiens Toronto Maple Leafs Montreal Canadiens Toronto Maple Leafs Toronto Maple Leafs Toronto Maple Leafs Detroit Red Wings Toronto Maple Leafs...

  • Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that...

    Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that will process monthly sales data for a small company. The data will be used to calculate total sales for each month in a year. The monthly sales totals will be needed for later processing, so it will be stored in an array. Basic Logic for main() Note: all of the functions mentioned in this logic are described below. Declare an array of 12 float/doubles...

  • The ACME Manufacturing Company has hired you to help automate their production assembly line. Cameras have...

    The ACME Manufacturing Company has hired you to help automate their production assembly line. Cameras have been placed above a conveyer belt to enables parts on the belt to be photographed and analyzed. You are to augment the system that has been put in place by writing C code to detect the number of parts on the belt, and the positions of each object. The process by which you will do this is called Connected Component Labeling (CCL). These positions...

  • Question 2 : Virus Wars A popular subgenre of strategy game is the so-called Virus War...

    Question 2 : Virus Wars A popular subgenre of strategy game is the so-called Virus War format, where the player is shown a field of cells, each with a virus count, and may attack other cells within a certain range. We are going to write some classes in Python to implement a simple Virus Wars game. 2a) The position class (9 points) Everything within this game is going to require a position, so it makes sense to define it up...

  • Python Project

    AssignmentBitmap files map three 8-bit (1-byte) color channels per pixel. A pixel is a light-emitting "dot" on your screen. Whenever you buy a new monitor, you will see the pixel configuration by its width and height, such as 1920 x 1080 (1080p) or 3840x2160 (4K). This tells us that we have 1080 rows (height), and each row has 1920 (width) pixels.The bitmap file format is fairly straight forward where we tell the file what our width and height are. When...

  • Help with Data Science python notebook, Question 1 Create a function called vowel_parse() that takes a...

    Help with Data Science python notebook, Question 1 Create a function called vowel_parse() that takes a single string as input. This string is the name of the input directory. It returns a dictionary. This dictionary has keys that are the names of the individual input files. The dictionary values are the number of words in that file that have adjacent vowels ('aa', 'ae', 'oo', 'ia', etc.). Use a regular expression to find these, do not hard code every possible combination...

  • The following are screen grabs of the provided files Thanks so much for your help, and have a n...

    The following are screen grabs of the provided files Thanks so much for your help, and have a nice day! My Java Programming Teacher Gave me this for practice before the exam, butI can't get it to work, and I need a working version to discuss with my teacher ASAP, and I would like to sleep at some point before the exam. Please Help TEST QUESTION 5: Tamagotchi For this question, you will write a number of classes that you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT