Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Java Question 15 A train consists of a number of cars and Cindy puts the cars of a train in decreasing order of weight. Unfortunately,
Java
Question 15 A train consists of a number of cars and Cindy puts the cars of a train in decreasing order of weight. Unfortunately, ordering train cars is difficult. A person cannot easily lift a car and place it in the correct position. It is impractical, if not impossible, to insert a car into an existing train. So, a car may only 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 specific order. When a car arrives, Cindy can add it to the beginning or the end of the train, or simply not adding it. She likes to make the train as long as possible, but the cars of it must be sorted by weight, with the heaviest one at the front of the train. You can assume all the weights are different and we would like to find the longest train Cindy can make. (a) Write a class Longest Train with a private attribute weight, which is an array of integers representing the weights of the trains. The weight of car k is stored in weight[k-1]. The order of arrival is car 1, car 2, ..., car n, where n=weight.length. Also write the constructor with a single parameter weight which is assigned to the attribute. [2 marks] (b) Write a method findLongest () with the attribute weight as the parameter and returns an integer representing the longest train Cindy can make. You may write some other methods which are called by findLongest(). [13 marks]Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started