Menu

Please Use Java Program Language Train Consists Number Cars Cindy Puts Cars Train Decreasi Q43785218

Please use JAVA program language

A train consists of a number of cars and Cindy puts the cars ofa train in decreasing order of weight. Unfortunately, orderingtrain cars is difficult. A person cannot easily lift a car andplace it in the correct position. It is impractical, if notimpossible, to insert a car into an existing train. So, a car mayonly be added to the beginning or the end of a train. The cars(with known weights) of a train arrive at a station in a specificorder. When a car arrives, Cindy can add it to the beginning or theend of the train, or simply not adding it. She likes to make thetrain as long as possible, but the cars of it must be sorted byweight, with the heaviest one at the front of the train. You canassume all the weights are different and we would like to find thelongest train Cindy can make.

(a) Write a class LongestTrain with a private attribute weight,which is an array of integers representing the weights of thetrains. The weight of car k is stored in weight[k-1]. The order ofarrival is car 1, car 2, …, car n, where n = weight.length. Alsowrite the constructor with a single parameter weight which isassigned to the attribute.

(b) Write a method findLongest() with the attribute weight asthe parameter and returns an integer representing the longest trainCindy can make. You may write some other methods which are calledby findLongest().

Expert Answer


Answer to Please use JAVA program language A train consists of a number of cars and Cindy puts the cars of a train in decreasing o…

OR