Use the Bicycle.java file from Lab 4. public class Bicycle { // Instance field p
ID: 3692517 • Letter: U
Question
Use the Bicycle.java file from Lab 4.
public class Bicycle {
// Instance field
private String ownerName;
private int licenseNumber;
// Constructor
public Bicycle( String name, int license ) {
ownerName = name;
licenseNumber = license;
}
// Returns the name of this bicycle's owner
public String getOwnerName() {
return ownerName;
}
// Assigns the name of this bicycle's owner
public void setOwnerName( String name ) {
ownerName = name;
}
// Returns the license number of this bicycle
public int getLicenseNumber() {
return licenseNumber;
}
// Assigns the license number of this bicycle
public void setLicenseNumber( int license ) {
licenseNumber = license;
}
}
In the Bicycle class you must add a toString method that returns a String containing the owner’s name and license number, clearly labeled. The toString method should call the get methods.
public String toString()
Create a new BicycleTest program. In the program create an array of Bicycles size 4. Create 4 Bicycle objects and assign them to the array. Put your own name as one of the owners. Then use a for loop to walk through the array and output the owner’s name and license number of each Bicycle. Do this by calling the toString method using each array element.
Explanation / Answer
/**
* @author
*
*/
public class Bicycle {
// Instance field
private String ownerName;
private int licenseNumber;
// Constructor
public Bicycle(String name, int license) {
ownerName = name;
licenseNumber = license;
}
// Returns the name of this bicycle's owner
public String getOwnerName() {
return ownerName;
}
// Assigns the name of this bicycle's owner
public void setOwnerName(String name) {
ownerName = name;
}
// Returns the license number of this bicycle
public int getLicenseNumber() {
return licenseNumber;
}
// Assigns the license number of this bicycle
public void setLicenseNumber(int license) {
licenseNumber = license;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Owner Name=" + ownerName + ", License Number=" + licenseNumber;
}
}
/**
*
*/
/**
* @author
*
*/
public class BicycleTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// Bicycles size 4
Bicycle bicycles[] = new Bicycle[4];
// 4 Bicycle objects assigning to the array
bicycles[0] = new Bicycle("Srinivas Palli", 580);
bicycles[1] = new Bicycle("Rajesh", 580);
bicycles[2] = new Bicycle("Kishore", 580);
bicycles[3] = new Bicycle("Pavan", 580);
// use a for loop to walk through the array and output the owner’s
// name and license number of each Bicycle
for (Bicycle b : bicycles) {
System.out.println(b);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
OUTPUT:
Owner Name=Srinivas Palli, License Number=580
Owner Name=Rajesh, License Number=580
Owner Name=Kishore, License Number=580
Owner Name=Pavan, License Number=580
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.