Question

In: Computer Science

Create a class named “Car” which has the following fields. The fields correspond to the columns...

Create a class named “Car” which has the following fields. The fields correspond to the columns in the text file except the last one. i. Vehicle_Name : String ii. Engine_Number : String iii. Vehicle_Price : double iv. Profit : double v. Total_Price : double (Total_Price = Vehicle_Price + Vehicle_Price* Profit/100) 2. Write a Java program to read the content of the text file. Each row has the attributes of one Car Object (except Total_Price). 3. After reading the instances of a Car, “Total_Price” will be calculate, and then the object must be saved in an array of objects. 4. Write each object created to a seperate file, for example: "Honda.txt", “KIA.text” and “BMW.text” for the following sample input file. 5. Write a method named showCar(double eNumber),takes the Engine Number as parameter, and displays all the information of the particular car including Total Price. Sample Input File: Honda 33244231 80 5 KIA 5544546 60 4 BMW 9846343 110 3.5

Solutions

Expert Solution

import java.io.*;

class Car
{
        String Vehicle_Name;
        String Engine_Number;
        double Vehicle_Price;
        double Profit;
        double Total_Price;
        Car(String Vehicle_Name, String Engine_Number, double Vehicle_Price, double profit)
        {
                this.Vehicle_Name = Vehicle_Name;
                this.Engine_Number = Engine_Number;
                this.Vehicle_Price = Vehicle_Price;
                this.Profit = Profit;
                this.Total_Price = Vehicle_Price + Vehicle_Price* Profit/100;
        }
}
class TestCar
{
        public static void main(String args[]) throws Exception
        {
                File file = new File("/home/keerthi/Desktop/sample.txt"); 
                BufferedReader br = new BufferedReader(new FileReader(file)); 
                String st; 
                int i = 0;
                Car cars[] = new Car[10];
                File file1 = new File("/home/keerthi/Desktop/Honda.txt"); 
                File file2 = new File("/home/keerthi/Desktop/KIA.txt"); 
                File file3 = new File("/home/keerthi/Desktop/BMW.txt"); 
                while ((st = br.readLine()) != null)
                {
                    
                    String[] arrOfStr = st.split(" ", 4);
                    double price = Double.valueOf(arrOfStr[2]);
                    double profit = Double.valueOf(arrOfStr[3]);
                    cars[i] = new Car(arrOfStr[0],arrOfStr[1],price,profit);
                    i++;
                    if(arrOfStr[0] == "Honda")
                    {
                        FileWriter fw = new FileWriter(file1);
                        fw.write(st);
                    }
                     else if(arrOfStr[0] == "KIA")
                    {
                        FileWriter fw=new FileWriter(file2);
                        fw.write(st);
                    }
                    else if(arrOfStr[0] == "BMW")
                    {
                        FileWriter fw=new FileWriter(file3);
                        fw.write(st);
                    }
                }
                carShow(5544546,cars);
                
        }
        public static void carShow(double Enumber,Car cars[])
        {
                for(int i = 0;i < cars.length;i++)
                {
                        if(Enumber == Double.valueOf(cars[i].Engine_Number))
                        {
                                System.out.println(cars[i].Vehicle_Name);
                                System.out.println(cars[i].Engine_Number);
                                System.out.println(cars[i].Vehicle_Price);
                                System.out.println(cars[i].Profit);
                                System.out.println(cars[i].Total_Price);
                        }
                }
        }
                                
        
}

If you have any doubts please comment and please don't dislike.


Related Solutions

Problem Statement Design a class named Car that has the following fields: year: The year field...
Problem Statement Design a class named Car that has the following fields: year: The year field is an integer that holds the car’s year. make: the make field is a String that holds the make of the car. speed: the speed field is an integer that holds the car’s current speed. In addition, the class should have the following constructor and other methods: Constructor: the constructor should accept the car’s year >model and make as parameters. These values should be...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
Question 1 - Create a class named Student that has fields for an ID number, number...
Question 1 - Create a class named Student that has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. A Student also has a field for grade point average. Include a method to compute the grade point...
The Account class Create a class named Account, which has the following private properties:
in java The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber (), getBalance (), setBalan newBalance). There is no setNumber () once an account is created, its account number cannot change. Now implement these methods: void deposit (double amount) and void withdraw (double amount). For both these methods, if the amount is less than...
The Account class Create a class named Account , which has the following private properties:
 The Account class Create a class named Account , which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNunber(), getBalance(), setBalance (double newBalance) . There is no setNunber() - once an account is created, its account number cannot change. Now implement these methods: void deposit (double anount) and void withdraw(double anount). For both these methods, if the amount is less than zero,...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Car Class Write a class named Car that has the following member variables: • year. An...
Car Class Write a class named Car that has the following member variables: • year. An int that holds the car’s model year. • make. A string object that holds the make of the car. • speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. • Constructor. The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables....
Create a class named Student. Student has fields for an ID number, number of credit hours...
Create a class named Student. Student has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. Student also has a field for grade point average. Include a method to compute the grade point average field by dividing...
This code in java: Create a class named car. A car has color, model, company, registration...
This code in java: Create a class named car. A car has color, model, company, registration number. You can stear a car. A car can move forward. A car has a gear box. A typical gear decide weather car is moving forward or backward. A person owns a car. Kindly add some other functionalities like refuel
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT