Create a program BicycleStore that prompts and reads in theowner name and licens
ID: 3616999 • Letter: C
Question
Create a program BicycleStore that prompts and reads in theowner name and license number for 2 Bicycle objects. Output theinformation for both Bicycle objects using the get methods. Thenmodify the second Bicycle object so that it will be your Bicyclewith a new license number(use the set methods). Output theinformation for the second Bicycle again. Use the Scanner clas forinput.
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'sowner
public void setOwnerName( String name )
{
ownerName = name;
}
// Returns the license number of thisbicycle
public int getLicenseNumber( )
{
returnlicenseNumber;
}
// Assigns the license number of thisbicycle
public void setLicenseNumber( int license )
{
licenseNumber =license;
}
}
Explanation / Answer
import java.io.*; import java.util.*; import java.lang.*; 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 thisbicycle public int getLicenseNumber( ) { returnlicenseNumber; } // Assigns the license number of thisbicycle public void setLicenseNumber( int license ) { licenseNumber =license; } public static void main(String[] args) { try{ System.out.println("-------------------------------------------------------"); Scannerscanner = new Scanner (System.in); System.out.print("Enter the name: "); String s1= scanner.next (); System.out.print("Enter the License: "); int l1 =Integer.parseInt(scanner.next ()); Bicycle b1= new Bicycle(s1, l1); System.out.println("-------------------------------------------------------"); System.out.print("Enter the name: "); String s2= scanner.next (); System.out.print("Enter the License: "); int l2 =Integer.parseInt(scanner.next ()); Bicycle b2= new Bicycle(s2, l2); System.out.println("-------------------------------------------------------"); System.out.println("Outputting Bicycle 1 attributes"); System.out.println("Owner Name is: " +b1.getOwnerName()); System.out.println("License Number is: " +b1.getLicenseNumber()); System.out.println("-------------------------------------------------------"); System.out.println("Outputting Bicycle 2 attributes"); System.out.println("Owner Name is: " +b2.getOwnerName()); System.out.println("License Number is: " +b2.getLicenseNumber()); System.out.println("-------------------------------------------------------"); System.out.println("Setting Bicycle 2 attributes"); b2.setOwnerName("Harry Potter"); b2.setLicenseNumber(1111); System.out.println("-------------------------------------------------------"); System.out.println("Outputting Bicycle 2 attributes"); System.out.println("Owner Name is: " +b2.getOwnerName()); System.out.println("License Number is: " +b2.getLicenseNumber()); System.out.println("-------------------------------------------------------"); } catch(Exception e) { e.printStackTrace(); } }//end of main //System.out.println(s); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.