Skip to content
Advertisement

Constructor in a for loop

I’ve a beginner at java programming and I’ve been tasked at creating a database. The database is a bike store with a super class Bike and sub classes mountain bike, road bike. I also have a class called bikeSystem that manages all the subclasses. So far, i’ve taken the approach of setting up a a constructor in the bike system class, and encapsulating with getter and setter methods. Then tried to create a for loop that sets the size of the database based on the attribute variable for bikesystem “bikes” that specifies the size of the database based on whatever value i change it to. Along with an array list that will create an array of size based on the size of the database.

However, this approach has not worked and i keep receiving errors along the lines of:
BikeSystem.java:18: error: constructor BikeSystem in class BikeSystem cannot be applied to given types; BikeSystem noOfBikes = new BikeSystem();

As well as a bunch of errors relating to non-static and static contexts. Any help or insight into this problem, or possible solutions is really much appreciated, thank you for your time.

JavaScript

Advertisement

Answer

I made a Bike class with a color propriety,a BykeSystem class where you can add lists of bikes,and the test class where you make the lists.I made it whithout a for loop.You can create then MountainBike class for example that extends Bike,add new propriety,like wheight,make a list of them in test class and add it to the BikeSystem list:

BikeSystem class :

JavaScript

Test class:

JavaScript

Bike class:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement