1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
I am Lyosha [343]
3 years ago
5

Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an

ArrayList list. Program should work for input file containing info for any number of cars. (You should not assume that it will always be seven lines in the input file). Use notepad to create input file "inData.txt". File should be stored in the same folder where all files from BlueJ for this program are located.
Class Car describes one car object and has variables vin, make, model all of String type, cost of type double, and year of int type. Variable vin is vehicle identification number and consist of digits and letters . In addition, class Car has methods:

public double getCost() // returns car’s cost

public String getMake() // returns car’s make

public boolean isExpensive() // returns true if car has cost above 30000 and false

//otherwise

public boolean isAntique() // returns true if car’s has above 50 years, and false

// otherwise

public String toString() // returns string with all car’s data in one line

// separated by tabs.

Design class CarList that has instance variable list of ArrayList type. Variable list is initialized in the constructor by reading data for each car from an input file. Each line of input file "inData.txt" has vin, make, model, cost, and year data in this order, and separated by a space. The data for the first five cars in the input file should be as follows:

1234567CS2 Subaru Impreza 27000 2017

1233219CS2 Toyota Camry 31000 2010

9876543CS2 Ford Mustang 51000 1968

3456789CS2 Toyota Tercel 20000 2004

4567890CS2 Crysler Royal 11000 1951

Add remaining two rows for the two additional cars of your choice.

Class CarList also has the following methods:

public void printList() // Prints title and list of all cars (each row prints one car)
public void printExpensiveCars() //Prints "List of expensive cars:" followed by
// list of expensive cars (one car per row)

public Car cheapestCar() // Method returns Car object with lowest cost.
public int countCarsWithModel(String model) // Method accept model and
// returns count of cars with given model.

//Method antiqueCarList is extra credit.

public ArrayList antiqueCarList () // Returns ArrayList of all antique
//cars from the list.

The last three methods just return the specified data type. Do not print anything within those methods. Just return result, and have explanation printed at the place where those methods are invoked.

Class TestCarList will have main method. In it, instantiate an object from CarList class and use it to invoke each of the five methods from CarList class.

Do not forget to append throws IOException to the constructor of CarList class and main method header in class TestCarList. In addition, you have to provide

import java.io.*;

import java.util.*;

in order to use Scanner and ArrayList classes from Java.

SUBMIT a single word or PDF document named p1yourLastNameCS152 with the following:

Your name, class section and project number and date of submission in the upper left corner
Copy of the code for each class in separate rectangle (table of size 1x1 with single cell)
Copy of your input file
Picture of program run from BlueJ.
Computers and Technology
1 answer:
vaieri [72.5K]3 years ago
3 0

Answer:

Java code is explained below

Explanation:

// Car.java

import java.util.Calendar;

public class Car {

  private String vin;

  private String make;

  private String model;

  private double cost;

  private int year;

  public Car(String vin, String make, String model, double cost, int year) {

      this.vin = vin;

      this.make = make;

      this.model = model;

      this.cost = cost;

      this.year = year;

  }

  public String getModel() {

      return model;

  }

  public double getCost() // returns car’s cost

  {

      return cost;

  }

  public String getMake() // returns car’s make

  {

      return make;

  }

  public boolean isExpensive() // returns true if car has cost above 30000 and

                                  // false

  // otherwise

  {

      if (cost > 30000)

          return true;

      else

          return false;

  }

  public boolean isAntique() // returns true if car’s has above 50 years, and

                              // false

  // otherwise

  {

      int cyear = Calendar.getInstance().get(Calendar.YEAR);

      if ((cyear-year) > 50)

          return true;

      else

          return false;

  }

  public String toString() // returns string with all car’s data in one line

  // separated by tabs.

  {

      return vin + "\t" + make + "\t" + model + " " + cost + "\t" + year;

  }

}

___________________________

// CarList.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;

public class CarList {

  private ArrayList<Car> type;

  public CarList() throws FileNotFoundException {

      this.type = new ArrayList<Car>();

      Scanner sc1 = new Scanner(new File("inData.txt"));

      while (sc1.hasNextLine()) {

          String str = sc1.nextLine();

          String arr[] = str.split(" ");

          type.add(new Car(arr[0], arr[1], arr[2], Integer.parseInt(arr[3]),

                  Integer.parseInt(arr[4])));

      }

      sc1.close();

  }

 

  public void printList()

  {

      System.out.println("Displaying cars:");

      for(int i=0;i<type.size();i++)

      {

          System.out.println(type.get(i));

      }

  }

  public void printExpensiveCars()

  {

     

      System.out.println("List of expensive cars:");

      for(int i=0;i<type.size();i++)

      {

          if(type.get(i).isExpensive())

          {

              System.out.println(type.get(i));

          }

      }

  }

 

  public Car cheapestCar()

  {

      int cheapIndx=0;

      double min=type.get(0).getCost();

      for(int i=0;i<type.size();i++)

      {

      if(min>type.get(i).getCost())

      {

          min=type.get(i).getCost();

          cheapIndx=i;

      }

      }

      return type.get(cheapIndx);

  }

  public int countCarsWithModel(String model)

  {

      int cnt=0;

      for(int i=0;i<type.size();i++)

      {

          if(type.get(i).getModel().equals(model))

          {

              cnt++;

          }

      }

      return cnt;

  }

 

  public ArrayList<Car> antiqueCarList ()

  {

      ArrayList<Car> arl=new ArrayList<Car>();

      for(int i=0;i<type.size();i++)

      {

          if(type.get(i).isAntique())

          arl.add(type.get(i));

      }

      return arl;

  }

}

_________________________________

// TestCarList.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;

public class TestCarList {

  public static void main(String[] args) throws FileNotFoundException {

      CarList cl = new CarList();

      cl.printList();

      cl.printExpensiveCars();

      ArrayList arl = cl.antiqueCarList();

      System.out.println("Displaying the AntiqueCar list:");

      for (int i = 0; i < arl.size(); i++) {

          System.out.println(arl.get(i));

      }

      System.out.println("Displaying the cheapest car :");

      System.out.println(cl.cheapestCar());

      System.out.println("No of Royal model Cars :"+ cl.countCarsWithModel("Royal"));

  }

}

____________________________

Output:

Displaying cars:

1234567CS2   Subaru   Impreza 27000.0   2017

1233219CS2   Toyota   Camry 31000.0   2010

9876543CS2   Ford   Mustang 51000.0   1968

3456789CS2   Toyota   Tercel 20000.0   2004

4567890CS2   Crysler   Royal 11000.0   1951

List of expensive cars:

1233219CS2   Toyota   Camry 31000.0   2010

9876543CS2   Ford   Mustang 51000.0   1968

Displaying the AntiqueCar list:

9876543CS2   Ford   Mustang 51000.0   1968

4567890CS2   Crysler   Royal 11000.0   1951

Displaying the cheapest car :

4567890CS2   Crysler   Royal 11000.0   1951

No of Royal model Cars :1

You might be interested in
In a multiprogramming and time-sharing environment, several users share the system simultaneously. This situation can result in
kap26 [50]

<u>Explanation:</u>

a.

  • One user can read the private data of another user-privacy.
  • One user can prevent another user from getting anything done-denial of service.

Indeed, in any multiprogramming and time-sharing environment since the computers of users are networked together, then it is very possible for a bad actor to read the private data of another user, or even prevent another user from accessing their computer by employing a denial of service attack on the network. In other words, the network would be unable to handle the request of users since has been overloaded by another user.

b. a. Yes - if we can ensure that the operating system prevents any sharing of data between users, either for reading or writing and fairly shares the computer, then we can achieve the same level of security.

7 0
3 years ago
Write a test program that creates two Rectangle objects—one with width 4 and height 40 and the other with width 3.5 and height 3
BARSIC [14]

Answer:

public class Rectangle {

   private double width;

   private double heigth;

   public Rectangle(double width, double heigth) {

       this.width = width;

       this.heigth = heigth;

   }

   public double getWidth() {

       return width;

   }

   public void setWidth(double width) {

       this.width = width;

   }

   public double getHeigth() {

       return heigth;

   }

   public void setHeigth(double heigth) {

       this.heigth = heigth;

   }

   public double perimeter(double width, double heigth){

       double peri = 2*(width+heigth);

       return peri;

   }

   public double area(double width, double heigth){

       double area = width*heigth;

       return  area;

   }

}

class RectangleTest{

   public static void main(String[] args) {

       //Creating two Rectangle objects

       Rectangle rectangle1 = new Rectangle(4,40);

       Rectangle rectangle2 = new Rectangle(3.5, 35.7);

       //Calling methods on the first Rectangel objects

       System.out.println("The Height of Rectangle 1 is: "+rectangle1.getHeigth());

       System.out.println("The Width of Rectangle 1 is: "+rectangle1.getWidth());

       System.out.println("The Perimeter of Rectangle 1 is: "+rectangle1.perimeter(4,40));

       System.out.println("The Area of Rectangle 1 is: "+rectangle1.area(4,40));

       // Second Rectangle object

       System.out.println("The Height of Rectangle 2 is: "+rectangle2.getHeigth());

       System.out.println("The Width of Rectangle 2 is: "+rectangle2.getWidth());

       System.out.println("The Perimeter of Rectangle 2 is: "+rectangle2.perimeter(4,40));

       System.out.println("The Area of Rectangle 2 is: "+rectangle2.area(4,40));

   }

}

Explanation:

  • Firstly A Rectangle class is created with two fields for width and heigth, a constructor and getters and setters the class also has methods for finding area and perimeters
  • Then a RectangleTest class containing a main method is created and two Rectangle objects are created (Follow teh comments in the code)
  • Methods to get height, width, area and perimeter are called on each rectangle object to print the appropriate value
5 0
2 years ago
Write a program that reads a list of words. Then, the program outputs those words and their frequencies. The input begins with a
tekilochka [14]

Answer:

The following are the program to the given question:

import java.util.*;//import package for user input

public class Make  //define class Make

{

public static void main(String[] ask)  //define a main method

{

  Scanner obx = new Scanner(System.in);   //declare the Scanner class object

  int size=obx.nextInt(); //get the size of the array from the user

  int a[]=new int[size]; //declare an integer array with given size

  String word[]=new String[size]; //declare a string array with given size

  for(int i=0;i<size;i++) //set the for loop  

  word[i]=obx.next(); //get string input from the user

  for(int i=0;i<size;i++)   //iterates with the array, increase the count

  {

    for(int j=0;j<size;j++)   //defining a loop to check value

    {

      if(word[i].equals(word[j]))  //use if to check that elements of words          

      a[i]++;//increamenting array size

    }

  }

 System.out.print("\n");  //use print for break line  

  for(int i=0;i<size;i++)//set for loop to print the following result

    System.out.println(word[i]+" "+a[i]);//print value

 }

}

Output:

Please find the attachment file.

Explanation:

  • First, establish the predefined package required and instead define the main class and define the main method within the class and method.
  • Declare the scanner class object and receive an object size in the 'size' variable thru the array.
  • Next create two arrays, the integer type "a" with both the input size given and the string type "word" with the input size supplied.
  • Define the loop for which string array elements are acquired and set two to again for loop, which increases with loop iteration the integer data type array by one.
  • Lastly, set the loop for which this result is printed.

5 0
3 years ago
Whats the answer to 9? i really need help with this in the next 20 minutes. ty if u help me!
gayaneshka [121]

Answer:

A very precise number B

Explanation:

Hope you pass

3 0
3 years ago
Which of the following statements is true of alert files?
ozzi

Answer:

They are only generated by Wireshark.

3 0
3 years ago
Other questions:
  • In addition to talking to other doctors remotely, telegraphy technology?
    5·1 answer
  • You can precede a subquery with the ___ operator to create a conditiion that is true if one or more rows are obstained when the
    10·1 answer
  • Remember to save _____ and be certain that you have your files saved before closing out.
    11·1 answer
  • What network setting do i need for a workgroup?
    5·1 answer
  • To resize columns in a subform, press and hold or right-click the subform in the navigation pane, and tap or click ____ on the s
    12·1 answer
  • What conversion factor should be used to convert from meters to Gigameters?
    7·1 answer
  • You can minimize the Ribbon with a command contained on the _____.
    7·1 answer
  • Explain the three schemes via which the binding of instructions and data to memory addresses can be done. In each scheme, how th
    13·1 answer
  • Which of the following is an example of phishing attack? csp
    8·1 answer
  • What are the benefits and drawbacks of a desktop utilising virtualisation and a server?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!