What I think you’re trying to achieve is that when you call your method “horn” with some parameter it has to either use “Beep!” or “Boop!”.
First of:
void horn(a,b)
Is not a valid function signature in Java, in a java function you always have to specify what type the input you’re providing is.
In your function you’d have to define that a and b are String like so:
void horn(String a, String b)
If you’d want your code to function in the way you wrote it right now you’d have to move your code a little bit and you’d end up with this:
public class Vehicle {
int maxSpeed;
int wheels;
String color;
double fuelCapacity;
void horn(String in) {
System.out.println(in);
}
void blink() {
System.out.println("I'm blinking!");
}
}
class MyClass {
public static void main(String[ ] args) {
String a = "Beep!";
String b = "Boop!";
Vehicle v1 = new Vehicle();
Vehicle v2 = new Vehicle();
v1.color = "red";
v2.horn(a);
v1.blink();
}
}
An other way to achieve the functionality you’re looking for: You could also just use a boolean.
void horn(boolean a) {
if (a)
{
System.out.println("Beep!");
}
else
{
System.out.println("Boop!");
}
}
Then in order to do what you wanted to do you’d have to call the method like this:
// Use either true or false.
v2.horn(true);
v2.horn(false);
0
solved Java – different parameters resulting to different outputs