Topics: .Understanding and accessing instance variables Object construction Enca
ID: 3931938 • Letter: T
Question
Topics: .Understanding and accessing instance variables Object construction Encapsulation Your programming assignments require individual work and effort to be of any benefit. Every student must wo independently on his or her assignments. This means that every student must ensure that neither a soft copy nor hard copy of their work gets into the hands of another student. Sharing your assignments with others in any way is NOT permitted. Violations of the University Academic Integrity policy will not be ignored. policy is found at http://www.asu.edu/studentlife/judicial/integrity, html The university academic integrity Use the following Coding Guidelines: .Give identifiers Give identifers semantic meaning and make them easy to read (examples num Students. gross Pay, etc) semantic meaning and make them easy to read (examples numStudents, grossPay, etc). Keep identifiers to a reasonably short length. .User upper case for constants. Use title cuse (first letter is upper case) for classes. Use lower case with uppercase word separators fa all other identifiers (variables, methods, objects). .Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated w ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent Use white space to make your program more readablec .Use comments after the ending brace of classes, methods, and blocks to identity to which block it belongs Assignments Documentation: At the beginning of each programming assignment you must have a comment block with the following information /AUTHOR: your name / FILENAME: title of the source file / SPECIFICATION: description of the program YOUR Lab Letter and Name of the IA for your Closed lab // FOR. CSE I 10-homework #-days and time ofyour classExplanation / Answer
package com.prt.test;
public class Customer {
private String firstName;
private String lastName;
private int idNumber;
private int noOfMatineeTickets;
private int noOfNormalTickets;
private double totalCost;
/**
* @param totalCost
* the totalCost to set
*/
public void setTotalCost(double totalCost) {
this.totalCost = totalCost;
}
/**
* @param firstName
* the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @param lastName
* the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @param idNumber
* the idNumber to set
*/
public void setIdNumber(int idNumber) {
this.idNumber = idNumber;
}
/**
* @param noOfMatineeTickets
* the noOfMatineeTickets to set
*/
public void setNoOfMatineeTickets(int noOfMatineeTickets) {
this.noOfMatineeTickets = noOfMatineeTickets;
}
/**
* @param noOfNormalTickets
* the noOfNormalTickets to set
*/
public void setNoOfNormalTickets(int noOfNormalTickets) {
this.noOfNormalTickets = noOfNormalTickets;
}
public Customer() {
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = idNumber;
this.noOfMatineeTickets = noOfMatineeTickets;
this.noOfNormalTickets = noOfNormalTickets;
this.totalCost = totalCost;
}
public Customer(String firstName, String lastName, int idNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = idNumber;
}
public Customer(String firstName, String lastName, int idNumber,
int noOfMatineeTickets, int noOfNormalTickets) {
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = idNumber;
this.noOfMatineeTickets = noOfMatineeTickets;
this.noOfNormalTickets = noOfNormalTickets;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @return the idNumber
*/
public int getIdNumber() {
return idNumber;
}
/**
* @return the noOfMatineeTickets
*/
public int getNoOfMatineeTickets() {
return noOfMatineeTickets;
}
/**
* @return the noOfNormalTickets
*/
public int getNoOfNormalTickets() {
return noOfNormalTickets;
}
/**
* @return the totalCost
*/
public double getTotalCost() {
return totalCost;
}
private static int getTickets() {
return 0;
}
private static int getNumCustomers() {
return 0;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + idNumber;
result = prime * result
+ ((lastName == null) ? 0 : lastName.hashCode());
result = prime * result + noOfMatineeTickets;
result = prime * result + noOfNormalTickets;
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Customer other = (Customer) obj;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (idNumber != other.idNumber)
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
return true;
}
public void computeTotalCost() {
int matineetickets = this.getNoOfMatineeTickets();
int normaltickets = this.getNoOfNormalTickets();
double totalcost = 5 * matineetickets + 7.5 * normaltickets;
System.out.println("totalcost $" + totalcost);
}
public Customer hasMore(Customer other) {
if (this.getNoOfMatineeTickets() == other.getNoOfMatineeTickets()
&& this.getNoOfNormalTickets() == other.getNoOfNormalTickets())
return this;
if ((this.getNoOfMatineeTickets() + this.getNoOfNormalTickets()) > (other
.getNoOfNormalTickets() + other.getNoOfMatineeTickets())) {
return this;
} else
return other;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "firstName=" + firstName + " " + "lastName=" + lastName + " "
+ "idNumber=" + idNumber + " " + "noOfMatineeTickets="
+ noOfMatineeTickets + " " + "noOfNormalTickets="
+ noOfNormalTickets;
}
}
package com.prt.test;
import java.util.Scanner;
public class Assignment6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Customer customer1 = new Customer();
System.out.println(".......customer's info.....");
System.out.println(customer1.toString());
// call the mutator methods
customer1.setFirstName("Grace");
customer1.setLastName("Hopper");
customer1.setNoOfNormalTickets(30);
customer1.setIdNumber(12345);
System.out.println(".......customer's info.....");
System.out.println(customer1);
// instantiate another customer using overloaded constructor
Customer customer2 = new Customer("Morgan", "smith", 555666777);
// print custmer's info
System.out.println(".......customer's info.....");
System.out.println(customer2);
customer2.setNoOfMatineeTickets(25);
// cusotmers information using keyboard
System.out.println("enter fname");
String fname = scanner.next();
System.out.println("enter lname");
String lname = scanner.next();
System.out.println("enter id");
int id = scanner.nextInt();
System.out.println("enter noofmatineetickets");
int matineeTickets = scanner.nextInt();
System.out.println("enter noofmatineetickets");
int normalTickets = scanner.nextInt();
// overloaded constructor
Customer customer3 = new Customer(fname, lname, id, matineeTickets,
normalTickets);
// customers info using get methods
System.out.println(".....customers info using get methods........");
System.out.println("firstname" + customer3.getFirstName());
System.out.println("lastname" + customer3.getLastName());
System.out.println("matinee tickets"
+ customer3.getNoOfMatineeTickets());
System.out.println("normaltickets" + customer3.getNoOfNormalTickets());
customer3.computeTotalCost();
// who has more tickets
System.out.println("who has more tickets");
System.out.println(customer1.hasMore(customer3));
System.out.println("first comparison");
if (customer2.equals(customer3))
System.out.println("customer2 and customer3 are same");
else
System.out.println("customer2 and customer3 are not same");
System.out.println("second comparison");
customer2 = customer3;
if (customer2.equals(customer3))
System.out.println("customer2 and customer3 are same");
else
System.out.println("customer2 and customer3 are not same");
}
}
output
.......customer's info.....
firstName=null
lastName=null
idNumber=0
noOfMatineeTickets=0
noOfNormalTickets=0
.......customer's info.....
firstName=Grace
lastName=Hopper
idNumber=12345
noOfMatineeTickets=0
noOfNormalTickets=30
.......customer's info.....
firstName=Morgan
lastName=smith
idNumber=555666777
noOfMatineeTickets=0
noOfNormalTickets=0
enter fname
john
enter lname
helen
enter id
12345
enter noofmatineetickets
25
enter noofmatineetickets
36
.....customers info using get methods........
firstnamejohn
lastnamehelen
matinee tickets25
normaltickets36
totalcost $395.0
who has more tickets
firstName=john
lastName=helen
idNumber=12345
noOfMatineeTickets=25
noOfNormalTickets=36
first comparison
customer2 and customer3 are not same
second comparison
customer2 and customer3 are same