a) Define a class named Customer that holds private fields for a customer ID num
ID: 3799646 • Letter: A
Question
a) Define a class named Customer that holds private fields for a customer ID number, last name, first name, and credit limit. Include four public functions that each set one of the four fields. Do not allow any credit limit over $10,000. Include a public function that displays a Customer’s data. Write a main() function in which you declare a Customer, set the Customer’s fields, and display the results. Save the file as Customer.java.
b) Write a main() function that declares an array of five Customer objects. Prompt the user for values for each Customer, and display all five Customer objects. Save the file as Customer2.java.
Explanation / Answer
Question a:
Customer.java
package a3;
public class Customer {
private int iD;
private String lastName;
private String firstName;
private int creditLimit;
public Customer(){
}
public int getiD() {
return iD;
}
public void setiD(int iD) {
this.iD = iD;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getCreditLimit() {
return creditLimit;
}
public void setCreditLimit(int creditLimit) {
if(creditLimit <= 10000){
this.creditLimit = creditLimit;
}
else{
System.out.println("Can not be added to credit limit more than 10000");
}
}
public static void main(String a[]){
Customer c = new Customer();
c.setFirstName("Suresh");
c.setiD(111);
c.setLastName("Murapaka");
c.setCreditLimit(5000);
System.out.println("******************************************");
System.out.println("First Name: "+c.getFirstName());
System.out.println("Last Name: "+c.getLastName());
System.out.println("ID: "+c.getiD());
System.out.println("Credit Limit: "+c.getCreditLimit());
}
}
Output:
******************************************
First Name: Suresh
Last Name: Murapaka
ID: 111
Credit Limit: 5000
Question b:
Customer2.java
import java.util.Scanner;
public class Customer2 {
private int iD;
private String lastName;
private String firstName;
private int creditLimit;
public Customer2(){
}
public int getiD() {
return iD;
}
public void setiD(int iD) {
this.iD = iD;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getCreditLimit() {
return creditLimit;
}
public void setCreditLimit(int creditLimit) {
if(creditLimit <= 10000){
this.creditLimit = creditLimit;
}
else{
System.out.println("Can not be added to credit limit more than 10000");
}
}
public static void main(String a[]){
Customer2 cust[] = new Customer2[5];
Scanner scan = new Scanner(System.in);
for(int i=0; i<cust.length; i++){
cust[i] = new Customer2();
System.out.println("Enter the first name: ");
String firstName = scan.next();
System.out.println("Enter the last name: ");
String lastName = scan.next();
System.out.println("Enter the ID: ");
int id = scan.nextInt();
System.out.println("Enter the credit limit: ");
int creditLimit = scan.nextInt();
cust[i].setFirstName(firstName);
cust[i].setiD(id);
cust[i].setLastName(lastName);
cust[i].setCreditLimit(creditLimit);
}
for(int i=0; i<cust.length; i++){
System.out.println("******************************************");
System.out.println("First Name: "+cust[i].getFirstName());
System.out.println("Last Name: "+cust[i].getLastName());
System.out.println("ID: "+cust[i].getiD());
System.out.println("Credit Limit: "+cust[i].getCreditLimit());
}
}
}
Output:
Enter the first name:
Suresh
Enter the last name:
Murapaka
Enter the ID:
111
Enter the credit limit:
1000
Enter the first name:
Sekhar
Enter the last name:
Murapaka
Enter the ID:
222
Enter the credit limit:
2000
Enter the first name:
Anshu
Enter the last name:
Murapaka
Enter the ID:
333
Enter the credit limit:
3000
Enter the first name:
Revathi1
Enter the last name:
Kella
Enter the ID:
4000
Enter the credit limit:
5000
Enter the first name:
AAAA
Enter the last name:
BBBB
Enter the ID:
5555
Enter the credit limit:
6000
******************************************
First Name: Suresh
Last Name: Murapaka
ID: 111
Credit Limit: 1000
******************************************
First Name: Sekhar
Last Name: Murapaka
ID: 222
Credit Limit: 2000
******************************************
First Name: Anshu
Last Name: Murapaka
ID: 333
Credit Limit: 3000
******************************************
First Name: Revathi1
Last Name: Kella
ID: 4000
Credit Limit: 5000
******************************************
First Name: AAAA
Last Name: BBBB
ID: 5555
Credit Limit: 6000
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.