Java Programming 1. Define a class named Payment that contains an instance varia
ID: 3724126 • Letter: J
Question
Java Programming
1. Define a class named Payment that contains an instance variable of type double that stores the amount of the payment and appropriate get and set methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment.
Next, define a class named CashPayment that is derived from Payment. This class should redefine the paymentDetails method to indicate that the payment is in cash. Include appropriate constructor(s).
Define a class named CreditCardPayment that is derived from Payment. This class should contain instance variables for the name on the card, expiration date, and credit card number. Include appropriate constructor(s). Finally, redefine the paymentDetails method to include all credit card information in the printout.
Define a class named PaymentTest class that contains a main method that creates at least two CashPayment and two CreditCardPayment objects with different values and calls paymentDetails for each.
2. Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value.
Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should be stored in the inherited variable text. Redefine the toString method to concatenate all text fields.
Similarly, define a class for File that is derived from Document and includes an instance variable for the pathname. The textual contents of the file should be stored in the inherited variable text. Redefine the toString method to concatenate all text fields.
Finally, define a class DocumentTest that creates several sample objects of type Email and File in the main method. Test your objects by passing them to the following subroutine that returns true if the object contains the specified keyword in the text property. public static boolean ContainsKeyword(Document docObject, String keyword) { if (docObject.toString().indexOf(keyword,0) >= 0) return true ; return false ; }
Explanation / Answer
Here is your solution;
Task 1:-
Payment.java
/**
*
*/
package com.test;
/**
* @author Lokesh Kumar
*
*/
public class Payment {
public double amount;
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public void paymentDetails(){
System.out.println(NumberWordConverter.getEnglishWord(this.amount));
}
}
CashPayment.java
/**
*
*/
package com.test;
/**
* @author Lokesh Kumar
*
*/
public class CashPayment extends Payment {
CashPayment(){}
CashPayment(double amount){
this.amount = amount;
}
public void paymentDetails() {
super.paymentDetails();
}
}
CreditCardPayment.java
/**
*
*/
package com.test;
/**
* @author Lokesh Kumar
*
*/
public class CreditCardPayment extends Payment {
private String nameOnCard;
private String exprationDate;
private String cardNumber;
public String getNameOnCard() {
return nameOnCard;
}
public void setNameOnCard(String nameOnCard) {
this.nameOnCard = nameOnCard;
}
public String getExprationDate() {
return exprationDate;
}
public void setExprationDate(String exprationDate) {
this.exprationDate = exprationDate;
}
public String getCardNumber() {
return cardNumber;
}
public void setCardNumber(String cardNumber) {
this.cardNumber = cardNumber;
}
CreditCardPayment(){}
/**
* @param nameOnCard
* @param exprationDate
* @param cardNumber
*/
public CreditCardPayment(String nameOnCard, String exprationDate, String cardNumber) {
super();
this.nameOnCard = nameOnCard;
this.exprationDate = exprationDate;
this.cardNumber = cardNumber;
}
public void paymentDetails() {
System.out.println(this.nameOnCard+" "+this.cardNumber+" "+this.exprationDate);
}
}
PaymentTest.java
/**
*
*/
package com.test;
/**
* @author Lokesh Kumar
*
*/
public class PaymentTest {
public static void main(String[] args) {
CashPayment cp = new CashPayment(100);
cp.paymentDetails();
CashPayment cp2 = new CashPayment(200.55);
cp2.paymentDetails();
CreditCardPayment cardPayment = new CreditCardPayment("Lokesh", "11/29", "XXXXXXXXXXXXXXX");
cardPayment.paymentDetails();
CreditCardPayment cardPayment2 = new CreditCardPayment("Phani", "12/29", "XXXXXXXXXXXXXXX");
cardPayment2.paymentDetails();
}
}
Task 2:-
Document.java
/**
*
*/
package com.test;
/**
* @author Lokesh Kumar
*
*/
public class Document {
public String text;
@Override
public String toString() {
return "Document [text=" + text + "]";
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
Email.java
/**
*
*/
package com.test;
/**
* @author Lokesh Kumar
*
*/
public class Email extends Document {
private String sender;
private String recipient;
private String titleOfEmail;
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getRecipient() {
return recipient;
}
public void setRecipient(String recipient) {
this.recipient = recipient;
}
public String getTitleOfEmail() {
return titleOfEmail;
}
public void setTitleOfEmail(String titleOfEmail) {
this.titleOfEmail = titleOfEmail;
}
@Override
public String toString() {
return "Email [sender=" + sender + ", recipient=" + recipient + ", titleOfEmail=" + titleOfEmail + ", text="
+ text + "]";
}
Email(){}
/**
* @param sender
* @param recipient
* @param titleOfEmail
*/
public Email(String sender, String recipient, String titleOfEmail,String text) {
super();
super.text = text;
this.sender = sender;
this.recipient = recipient;
this.titleOfEmail = titleOfEmail;
}
}
File.java
/**
*
*/
package com.test;
/**
* @author Lokesh Kumar
*
*/
public class File extends Document {
private String pathName;
@Override
public String toString() {
return "File [pathName=" + pathName + ", text=" + text + "]";
}
File() {
}
/**
* @param pathName
*/
public File(String pathName,String text) {
super();
super.text = text;
this.pathName = pathName;
}
}
DocumentTest.java
/**
*
*/
package com.test;
/**
* @author Lokesh Kumar
*
*/
public class DocumentTest {
public static void main(String[] args) {
Email email = new Email("ddd", "ddd", "test", "test");
File file = new File("text.txt", "test");
System.out.println(ContainsKeyword(email,"test"));
System.out.println(ContainsKeyword(file,"test"));
}
public static boolean ContainsKeyword(Document docObject, String keyword) {
if (docObject.toString().indexOf(keyword, 0) >= 0)
return true;
return false;
}
}
Number To word conversion:
NumberWordConverter.java
/**
*
*/
package com.test;
import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* @author Lokesh Kumar
*
*/
public class NumberWordConverter {
public static final String[] units = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
"Nineteen" };
public static final String[] tens = { "", // 0
"", // 1
"Twenty", // 2
"Thirty", // 3
"Forty", // 4
"Fifty", // 5
"Sixty", // 6
"Seventy", // 7
"Eighty", // 8
"Ninety" // 9
};
private static String convert(final int n) {
if (n < 0) {
return "Minus " + convert(-n);
}
if (n < 20) {
return units[n];
}
if (n < 100) {
return tens[n / 10] + ((n % 10 != 0) ? " " : "") + units[n % 10];
}
if (n < 1000) {
return units[n / 100] + " Hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100);
}
if (n < 100000) {
return convert(n / 1000) + " Thousand" + ((n % 10000 != 0) ? " " : "") + convert(n % 1000);
}
if (n < 10000000) {
return convert(n / 100000) + " Lakh" + ((n % 100000 != 0) ? " " : "") + convert(n % 100000);
}
return convert(n / 10000000) + " Crore" + ((n % 10000000 != 0) ? " " : "") + convert(n % 10000000);
}
private static String convertToPaisa(final int n) {
if (n < 0) {
return "minus " + convert(-n);
}
if (n < 20) {
return units[n];
}
if (n < 100) {
return tens[n / 10] + ((n % 10 != 0) ? " " : "") + units[n % 10];
}
if (n < 1000) {
return units[n / 100] + " hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100);
}
if (n < 1000000) {
return convert(n / 1000) + " thousand" + ((n % 1000 != 0) ? " " : "") + convert(n % 1000);
}
if (n < 1000000000) {
return convert(n / 1000000) + " million" + ((n % 1000000 != 0) ? " " : "") + convert(n % 1000000);
}
return convert(n / 1000000000) + " billion" + ((n % 1000000000 != 0) ? " " : "") + convert(n % 1000000000);
}
public static String getEnglishWord(final double m) {
BigDecimal bd = new BigDecimal((m - Math.floor(m)) * 100);
bd = bd.setScale(4, RoundingMode.HALF_DOWN);
int numberD = bd.intValue();
String string_temp = new Double(m).toString();
String string_form = string_temp.substring(0, string_temp.indexOf('.'));
int n = Integer.valueOf(string_form);
if(Integer.valueOf(numberD)>0){
return convert(n) + " and " + convertToPaisa(Integer.valueOf(numberD)) + " paisa";
} else {
return convert(n);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.