please show me how to expand my code
hi
managed to create simple class and few methods but would like to improve it some easy way of course :) to let say have the code asking me if bikes are available for rent using methods ?
can you please show me how to do it in these code?
Code :
class Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
int price = 0;
void changeCadence(int newValue) {
cadence = newValue;
}
void changeGear(int newValue) {
gear = newValue;
}
void speedUp(int increment) {
speed = speed + increment;
}
void applyBrakes(int decrement) {
speed = speed - decrement;
}
void bickeprice(int newValue){
price = newValue;
}
void printStates() {
System.out.println("cadence:" +
cadence + " speed:" +
speed + " gear:" + gear + " it costs:" + price);
}
}
class BicycleDemo {
public static void main(String[] args) {
// Create two different
// Bicycle objects
Bicycle bike1 = new Bicycle();
Bicycle bike2 = new Bicycle();
// Invoke methods on
// those objects
bike1.changeCadence(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();
bike1.bickeprice(100);
bike2.changeCadence(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.bickeprice(150);
bike2.printStates();
}
}
thank you!
Re: please show me how to expand my code
Quote:
Originally Posted by
me.
hi
managed to create simple class and few methods but would like to improve it some easy way of course :) to let say have the code asking me if bikes are available for rent using methods ?
Your post shows the Bicycle class, but to see if Bicylces are available for rent you need another class that holds a collection of Bicycle objects, say called BikeShop or something like that. Then you give it Bicycle objects, perhaps held in an ArrayList<Bicycle> bikesForRent. Give BikeShop a method, say getNumberBikesForRent() and return the size of the ArrayList.
Quote:
can you please show me how to do it in these code?
Most here believe that this first attempt must be your responsibility. That's how you learn.
Also, you have two threads in this forum where you have not replied to replies given to you. You may wish to fix that and give them replies now. People appreciate that. Best of luck!
Re: please show me how to expand my code
hi curmudgeon,
can you have a look on that code I wrote in a mean time and let me know what I need to change to get it working , please
I will replay to previous threats now...
thanks
Quote:
class Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
int price = 0;
String name; // added string name for the name of bike there are two different...
void changeCadence(int newValue) {
cadence = newValue;
}
void changeGear(int newValue) {
gear = newValue;
}
void speedUp(int increment) {
speed = speed + increment;
}
void applyBrakes(int decrement) {
speed = speed - decrement;
}
void bickeprice(int newValue){
price = newValue;
}
void printStates() {
System.out.println("bike:" + name
"Cadence: " +
cadence + " speed:" +
speed + " gear:" + gear + " it costs:" + price);
}
int availability() { //made these for choose option
System.out.print("there are only two bikes at that moment:");
System.out.println("which one would you like to see?");
System.out.println ("Continue? (1/2): ");
char choice = scan.next().charAt(0);//scan for the char
if(choice == '1'){
bike1.printStates(); //starts the menu again if char == y
}
if (choice == '2'){
bike1.printStates();
}
else {
System.exit(0); //else exits
}
}
class BicycleDemo {
public static void main(String[] args) {
// Create two different
// Bicycle objects
Bicycle bike1 = new Bicycle();
Bicycle bike2 = new Bicycle();
// Invoke methods on
// those objects
bike1.name(giant); // these is the name for bike 1 but getting errors,
bike1.changeCadence(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();
bike1.bickeprice(100);
bike2.changeCadence(50);
bike2.name(xxx);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.bickeprice(150);
bike2.printStates();
}
}
Re: please show me how to expand my code
Quote:
Originally Posted by
curmudgeon
Your post shows the Bicycle class, but to see if Bicylces are available for rent you need another class that holds a collection of Bicycle objects, say called BikeShop or something like that. Then you give it Bicycle objects, perhaps held in an ArrayList<Bicycle> bikesForRent. Give BikeShop a method, say getNumberBikesForRent() and return the size of the ArrayList.
ok so I created new class BikeShop
Quote:
public class BikeShop {
public static void main(String[] args) {
String[][] names = {
{"giant ", "giantxxl" },
{"black", "blue"}
};
//
System.out.println(names[0][0] + names[1][0]);
//
System.out.println(names[0][2] + names[1][1]);
}
}
but still cant get it working?
Re: please show me how to expand my code
Quote:
Originally Posted by
me.
can you have a look on that code I wrote in a mean time and let me know what I need to change to get it working , please
I will replay to previous threats now...
I don't think that anyone wants threats on this site. Reply to threads, for sure, but leave threats alone.
Please use code tags when posting your code, like you did in your first post to this question. Please don't post your code in quote tags as the code becomes unreadable. Also, and again, don't put bike availability code within the Bicycle class. Think concrete for a moment -- would an individual bicycle know how many of its brethren are available for rental? No, of course not. So a Bicycle class shouldn't know either. Use a BikeShop class. You seem to have tried to create a BikeShop class, but all I see is a main method. This class should not have a main method or any static methods, but again it should hold an array or ArrayList of Bicycle objects. Keep at it.