Question

In: Computer Science

Ask the user for the name of a car maker. Display the oldest and newest car...

Ask the user for the name of a car maker. Display the oldest and newest car from that maker. Modify your display to include the VIN of the car. Format the output in columns of 15, 25, 5, and 18.

Standard Input                 Files in the same directory
Toyota
  • car-list.txt

Required Output

What car make are you looking for?\n
Oldest Toyota\n
         Toyota                      MR2 1985 WAUFFAFL2CN997894\n
Newest Toyota\n
         Toyota                    Venza 2013 WAUEH54B01N735764\n

Standard Input                 Files in the same directory
Chevrolet
  • car-list.txt

Required Output

What car make are you looking for?\n
Oldest Chevrolet\n
      Chevrolet                  Corvair 1960 1N6BF0KM1FN003702\n
Newest Chevrolet\n
      Chevrolet                 Colorado 2012 WVWAA7AJ4BW878497\n

Standard Input                 Files in the same directory
Lamborghini
  • car-list.txt

Required Output

What car make are you looking for?\n
Oldest Lamborghini\n
    Lamborghini              Murciélago 2002 3GYFK66N43G898137\n
Newest Lamborghini\n
    Lamborghini                Reventón 2008 ZHWGU5BR6EL542311\n
Standard Input                 Files in the same directory
Kia
  • car-list.txt

Required Output

What car make are you looking for?\n
Oldest Kia\n
            Kia                   Sephia 1996 1N6AA0CC7CN222465\n
Newest Kia\n
            Kia                    Forte 2012 2V4RW3D14AR530852\n
Standard Input                 Files in the same directory
GMC
  • car-list.txt

Required Output

What car make are you looking for?\n
Oldest GMC\n
            GMC                    Jimmy 1992 1YVHZ8BA1A5450350\n
Newest GMC\n
            GMC                    Yukon 2013 1FTFW1E82AK523715\n

Standard Input                 Files in the same directory
Volvo
  • car-list.txt

Required Output

What car make are you looking for?\n
Oldest Volvo\n
          Volvo                      850 1995 JH4KC1F95FC996591\n
Newest Volvo\n
          Volvo                      S60 2011 WA1DGAFE7ED674601\n

Heres cars.txt that you wanted:

make     model year
Ford   Expedition 2003
Mazda  B-Series   1989
Ford   Freestar   2003
Hyundai    Elantra    2001
Hyundai    Entourage  2008
Chevrolet  Camaro 2011
Chevrolet  Monte Carlo    2006
Chevrolet  Blazer 1996
Chevrolet  Aveo   2005
Chevrolet  Corvette   1999
Mercedes-Benz  E-Class    2006
Dodge  Avenger    1995
Pontiac    Grand Prix 1973
Mitsubishi Outlander  2011
MINI   Clubman    2011
Suzuki Aerio  2007
Dodge  Dakota Club    1992
Chevrolet  Astro  2002
Chevrolet  Tahoe  1996
Mitsubishi Mirage 1994
Porsche    944    1991
Hyundai    Elantra    1994
Mercury    Grand Marquis  1998
Volkswagen Golf   2001
Jaguar XJ Series  2005
Toyota Echo   2005
GMC    Safari 2002
GMC    Sierra 1500    2000
Chevrolet  Cobalt 2005
Jeep   Patriot    2008
Mazda  Navajo 1991
Chevrolet  Malibu 2001
Saab   900    1990
Mercury    Grand Marquis  1998
Hummer H1 2004
Subaru Loyale 1993
Jeep   Wrangler   1999
Ford   Mustang    1994
Austin Mini Cooper S  1963
Mercedes-Benz  M-Class    1998
Jeep   Wrangler   2006
Honda  Civic  1997
Plymouth   Voyager    1994
Ford   Club Wagon 1997
Audi   5000S  1984
Saturn VUE    2003
Oldsmobile Achieva    1994
Mercedes-Benz  G55 AMG    2006
Chevrolet  Express 3500   1997
Lexus  ES 1992
Cadillac   Allante    1992
Hyundai    Tiburon    1997
Pontiac    Grand Prix 1965
Ford   Focus  2000
Mitsubishi Chariot    1987
Chrysler   Prowler    2001
Land Rover Discovery  2012
Volkswagen Scirocco   1984
Ford   Bronco 1984
Hyundai    Accent 1996
Volkswagen Routan 2012
Volkswagen Golf   2003
GMC    Terrain    2010
Ford   F150   2009
GMC    Sierra 2011
Dodge  Ram Van 1500   2000
Chrysler   300    2009
Oldsmobile Achieva    1997
Land Rover Discovery  2008
Toyota 4Runner    2002
Porsche    911    1995
Toyota Land Cruiser   2002
Land Rover Defender   1994
Chevrolet  Lumina 1997
Audi   TT 2002
Chrysler   Town & Country 2009
Nissan Frontier   2000
Toyota Tercel 1997
Buick  Riviera    1997

I would prefer the sorting algorithm types like Bubble and Selection searches if you can. If you want car-list.txt, I can't put it in the question because it was too long. Make sure this in Java and also have notes on the program and the right formatting, thank you.

Solutions

Expert Solution

Car.java

public class Car {
private String vin, make, model;
private int year;
  
public Car()
{
this.vin = this.make = this.model = "";
this.year = 0;
}

public Car(String vin, String make, String model, int year) {
this.vin = vin;
this.make = make;
this.model = model;
this.year = year;
}

public String getVin() {
return vin;
}

public String getMake() {
return make;
}

public String getModel() {
return model;
}

public int getYear() {
return year;
}
  
@Override
public String toString()
{
return(String.format("%15s %25s %8d %18s", getMake(), getModel(), getYear(), getVin()));
}
}

CarAppMain.java (Main class)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class CarAppMain {
  
private static final String FILENAME = "car-list.txt";
  
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Car> cars = readFile(FILENAME);
char yesNo;
System.out.print("What car make are you looking for? ");
String make = sc.nextLine().trim();
System.out.println(getOldNewCar(cars, make));
}
  
private static ArrayList<Car> readFile(String filename)
{
ArrayList<Car> cars = new ArrayList<>();
Scanner fileReader;
try
{
fileReader = new Scanner(new File(filename));
while(fileReader.hasNextLine())
{
// assuming fields in a line of the input file are separated by comma
String[] data = fileReader.nextLine().trim().split(",");
String make = data[0];
String model = data[1];
int year = Integer.parseInt(data[2]);
String vin = data[3];
cars.add(new Car(vin, make, model, year));
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println(filename + " could not be found! Exiting..");
System.exit(0);
}
return cars;
}
  
private static String getOldNewCar(ArrayList<Car> cars, String make)
{
ArrayList<Car> makes = new ArrayList<>();
String resultStr = "";
for(Car c : cars)
{
if(c.getMake().equalsIgnoreCase(make))
makes.add(c);
}
if(makes.isEmpty())
{
System.out.println("Sorry, no cars found for make " + make + "!\n");
return resultStr;
}
// find the oldest and newest cars
int minYear = makes.get(0).getYear();
int minIndex = 0;
for(int i = 0; i < makes.size(); i++)
{
if(makes.get(i).getYear() < minYear)
{
minYear = makes.get(i).getYear();
minIndex = i;
}
}
resultStr += "Oldest " + make + ":\n" + makes.get(minIndex) + "\n";
  
int maxYear = makes.get(0).getYear();
int maxIndex = 0;
for(int i = 0; i < makes.size(); i++)
{
if(makes.get(i).getYear() > maxYear)
{
maxYear = makes.get(i).getYear();
maxIndex = i;
}
}
resultStr += "Newest " + make + ":\n" + makes.get(maxIndex) + "\n";
return resultStr;
}
}

********************************************************* SCREENSHOT *******************************************************

INPUT FILE (car-list.txt) - This file needs to be created before running the code and this file should be created within the same working directory where all the .java files will be residing.

CONSOLE OUTPUT :


Related Solutions

In java, ask the user for a filename. Display the oldest car for every manufacturer from...
In java, ask the user for a filename. Display the oldest car for every manufacturer from that file. Files Given: car-list.txt car-list-1.txt car-list-2.txt car-list-3.txt (they are too big for question) Required Output: Standard Input                 Files in the same directory car-list-1.txt car-list.txt car-list-1.txt car-list-2.txt car-list-3.txt Enter filename\n Oldest cars by make\n Acura Legend 1989 2T1BPRHE8EC858192\n Audi 80/90 1988 2GKALMEK4E6424961\n Bentley Continental Flying Spur 2006 WBA3G7C5XFK430850\n BMW 600 1959 1N6AA0ED2FN639969\n Bugatti Veyron 2009 SCFEBBBC5AG474636\n Buick Century 1987 2HNYD18625H455550\n Cadillac DeVille 1994...
Using the given file, ask the user for a name, phone number, and email. Display the...
Using the given file, ask the user for a name, phone number, and email. Display the required information. These are the Files that I made: import java.util.Scanner; public class Demo5 { public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); System.out.println("New number creation tool"); System.out.println("Enter name"); String name = keyboard.nextLine(); System.out.println("Enter phone number"); String phoneNumber = keyboard.nextLine(); System.out.println("Enter email"); String email = keyboard.nextLine(); Phone test1 = new SmartPhone(name, phoneNumber, email); System.out.print(test1); System.out.println("Telephone neighbor: " + ((SmartPhone) test1).getTeleponeNeighbor()); }...
Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
This is to be done using MIPS assembly language. Display the following menus, ask user to...
This is to be done using MIPS assembly language. Display the following menus, ask user to select one item. If 1-3 is selected, display message “item x is selected”, (x is the number of a menu item), display menus again; if 0 is selected, quit from the program. 1. Menu item 1 2. Menu item 2 3. Menu item 3 0. Quit Please select from above menu (1-3) to execute one function. Select 0 to quit
Your Application should ask the user to enter their name and their salary.
Create a C# Console Application, name the solution Homework 6 and the project TaxRate.Your Application should ask the user to enter their name and their salary. Your application should calculate how much they have to pay in taxes each year and output each amount as well as their net salary (the amount they bring home after taxes are paid!). The only taxes that we will consider for this Application are Federal and FICA. Your Application needs to validate all numeric...
Write the pseudocode that prompts the user for their first and last name. Display the first...
Write the pseudocode that prompts the user for their first and last name. Display the first initial of their first name and their last name to the user. Ask the user to input a phone number. The program checks which part of Colorado a phone number is from using the values below. If the second digit of the phone number is one of the below digits, print the phone number and which part of Colorado it is from. If none...
sort by the following (name, address, dependent and gender) of these fields and ask the user...
sort by the following (name, address, dependent and gender) of these fields and ask the user which field to sort by !. this mean the following java must sort by address if we need, by name , by dependent , and by gender it depend on the following java it must have an option which we need to sort. please i need your help now, you just add the sorting on the following java. // Use a custom comparator. import...
Task 2.5: Write a script that will ask the user for to input a file name...
Task 2.5: Write a script that will ask the user for to input a file name and then create the file and echo to the screen that the file name inputted had been created 1. Open a new file script creafile.sh using vi editor # vi creafile.sh 2. Type the following lines #!/bin/bash echo ‘enter a file name: ‘ read FILENAME touch $FILENAME echo “$FILENAME has been created” 3. Add the execute permission 4. Run the script #./creafile.sh 5. Enter...
PYTHON Modify the program in section Ask the user for a first name and a last...
PYTHON Modify the program in section Ask the user for a first name and a last name of several people.  Use a loop to ask for user input of each person’s first and last names  Each time through the loop, use a dictionary to store the first and last names of that person  Add that dictionary to a list to create a master list of the names  Example dictionary: aDict = { "fname":"Douglas", "name":"Lee" } ...
Name the script thirsty.sh. Ask the user if they are thirsty. Read the user's response. If...
Name the script thirsty.sh. Ask the user if they are thirsty. Read the user's response. If they answer no or No, print an appropriate message and exit. If they answer yes or Yes, ask what they would like to drink. Read the user's response. If they answer water print "Clear crisp and refreshing." If they answer beer print "Let me see some id." If they answer wine print "one box or two." If they answer anything else print "Coming right...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT