JAVA CLASSES HELP import java.util.Scanner; class Bicycle { //YOUR CODE GOES HER
ID: 3912050 • Letter: J
Question
JAVA CLASSES HELP
import java.util.Scanner;
class Bicycle
{
//YOUR CODE GOES HERE
}
// driver class
public class main
{
public static void main(String args[])
{
//YOUR CODE GOES HERE
}
}
Explanation / Answer
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Bicycle[] bc = new Bicycle[3];
int g, s;
// taking input
for(int i=0; i<3; i++)
{
bc[i] = new Bicycle();
System.out.printf("Enter gear of bicycle%d: ",i+1);
g = sc.nextInt();
bc[i].setGear(g);
System.out.printf("Enter speed of bicycle%d: ",i+1);
s = sc.nextInt();
bc[i].setSpeed(s);
}
// printing output
System.out.println();
for(int i=0; i<3; i++)
{
bc[i].print();
}
}
}
class Bicycle
{
private int gear, speed;
Bicycle()
{
gear = 0;
speed = 0;
}
void setGear(int gear)
{
this.gear = gear;
}
int getGear()
{
return gear;
}
void setSpeed(int speed)
{
this.speed = speed;
}
int getSpeed()
{
return speed;
}
void print()
{
System.out.printf("Gear is %d and Speed is %d ",gear, speed);
}
}
/*sample output
Enter gear of bicycle1: 3
Enter speed of bicycle1: 4
Enter gear of bicycle2: 5
Enter speed of bicycle2: 6
Enter gear of bicycle3: 7
Enter speed of bicycle3: 8
Gear is 3 and Speed is 4
Gear is 5 and Speed is 6
Gear is 7 and Speed is 8
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.