Starting from:
$30

$24

Collections using Java ArrayLists Solved

Create two new projects and name them Lab09a and Lab09b.

Question 1

In project Lab09a, write a program that generates first n integers of Fibonacci sequence where n is a positive integer given by the user. Your program should store the numbers in an ArrayList. Then, your program should display the index and the value of each element in the ArrayList. At the end, also display the count of even numbers and count of odd numbers in the list. Note that in the case the user enters an invalid value for n, print an appropriate error message as illustrated below. The Fibonacci numbers are defined as follows:
F​​= 0

0
F​​= 1

1
F​​= F​ ​+ F​

n    n-1    n-2

Sample Run 1:

Enter how many Fibonacci numbers will be generated: 6

*******************************************************************

Index: 0 Value: 0

Index: 1 Value: 1

Index: 2 Value: 1

Index: 3 Value: 2

Index: 4 Value: 3

Index: 5 Value: 5

*******************************************************************

Count of odd numbers in the list: 2

Count of even numbers in the list: 4

Sample Run 2:

Enter how many Fibonacci numbers will be generated: -8

*******************************************************************

No number is generated!

*******************************************************************

Sample Run 3:

Enter how many Fibonacci numbers will be generated: 1

*******************************************************************

Index: 0 Value: 0

*******************************************************************

Count of odd numbers in the list: 0

Count of even numbers in the list: 1
Question 2

In this question you are going to implement software for a car gallery. In project Lab09b, you will first write two classes: ​Car and ​CarGallery​. Then you will write an application class CarGalleryApp in which you will use ​Car and ​CarGallery ​classes. This application should implement a menu driven car gallery management system.

Car

    • Each car has a brand, model and a price.

    • Create 2 constructors for this class where

        ◦ Constructor 1: takes all properties as parameters.

        ◦ Constructor 2: takes an instance of Car class and sets its properties accordingly.

    • Write getter and setter methods for all properties. Also include a toString() method for Car class (See the sample run for the format).

CarGallery

Each gallery has a name and a list of cars. Create an ArrayList of Cars and do all of these operations on this list. For all these operations you can assume that the user enters a valid input.

    • Add Car. (See sample output)

    • Delete Car. (See sample output)

Create a constructor for this class where takes a name as its parameter and creates an empty list of cars. Write getter and setter methods for all properties. Also include a toString() method for CarGallery class.

CarGalleryApp

In this class you will write a menu driven car gallery management system, which will include a main method. Create an ArrayList of CarGalleries (let's call it galleryList) and do all of these operations on this list. For all these operations you may assume that the user enters a valid input and user will execute these operations until they select the Exit menu option:

    • Add Gallery. Request gallery name from the user. Create a carGallery object with the input you take from the user and add it to your ArrayList called galleryList. Then, display all galleries in the list and return to main menu.

    • Remove Gallery. Show the complete list of galleries to the user with their indices on the list (See sample output). Then, take the index of the gallery to be deleted from the user. Remove that gallery from the list and display the list once more. Finally, return to main menu. If there are no galleries in the list, display a message.

    • Add Car. Show the complete list of galleries to the user with their indices on the list (See sample output). Then, take the index of the gallery to be new car added.Request car brand, model and price from the user. Create a car object with the input you take from the user and add it to ArrayList of carGallery object. Then, display all cars in the list and return to main menu.

    • Sell Car. Show the complete list of galleries to the user with their indices on the list. (See sample output). Then, take the index of the gallery whose car will be sold from the user. Remove that car from the list and display the list once more. Finally, return to main menu. If there are no cars in the list, display a message.

    • Exit. Exits the program.
SAMPLE OUTPUT:

Welcome to the Car Gallery. Please enter your choice

(1)Add Gallery

(2)Remove Gallery

(3)Add Car

(4)Sell Car

(5)Exit

Choice: 1

*******************************************************************

Please enter gallery name: aslanlar

Current Status of    GalleryList

    0: aslanlar

*******************************************************************

Welcome to the Car Gallery. Please enter your choice

(1)Add Gallery

(2)Remove Gallery

(3)Add Car

(4)Sell Car

(5)Exit

Choice: 3

*******************************************************************

Current Status of    GalleryList

    0: aslanlar

Please enter gallery id: 0

Please enter car brand: Fiat

Please enter car model: Bravo

Please enter car price: 15000

Current Status of    aslanlar Gallery

    0: Fiat, Bravo, $15000

*******************************************************************

Welcome to the Car Gallery. Please enter your choice

(1)Add Gallery

(2)Remove Gallery

(3)Add Car

(4)Sell Car

(5)Exit

Choice: 3

*******************************************************************

Current Status of    GalleryList

    0: aslanlar

Please enter gallery id: 0

Please enter car brand: Fiat

Please enter car model: Doblo
Please enter car price: 17000

Current Status of    aslanlar Gallery


    0: Fiat, Bravo, $15000

    1: Fiat, Doblo, $17000

*******************************************************************

Welcome to the Car Gallery. Please enter your choice

(1)Add Gallery

(2)Remove Gallery

(3)Add Car

(4)Sell Car

(5)Exit

Choice: 4

*******************************************************************

Current Status of    GalleryList

    0: aslanlar

Please enter gallery id: 0

Current Status of    aslanlar Gallery

    0: Fiat, Bravo, $15000

    1: Fiat, Doblo, $17000

Please enter car id which is sold: 0

Current Status of    aslanlar Gallery

    0: Fiat, Doblo, $17000

*******************************************************************

Welcome to the Car Gallery. Please enter your choice

(1)Add Gallery

(2)Remove Gallery

(3)Add Car

(4)Sell Car

(5)Exit Choice: 2
*******************************************************************

Current Status of  GalleryList

    0: aslanlar

Please enter gallery id that will be removed: 0

Car Gallery is currently empty

*******************************************************************

Welcome to the Car Gallery. Please enter your choice

(1)Add Gallery

(2)Remove Gallery

(3)Add Car

(4)Sell Car

(5)Exit
Choice: 5

*******************************************************************

End of CarGallery

More products