java programming exercises with solutions


Define a class named The message that contains an instance variable of type String named text that stores any textual content for the Message. Create a method named toString that returns the text field and also include a method to set this value.

Next, define a class for SMS that is derived from Message and includes instance variables for the recipientContactNo. Implement appropriate accessor and mutator methods. The body of the SMS message should be stored in the inherited variable text. Redefine the toString method to concatenate all text fields.

Similarly, define a class for Email that is derived from Message and includes an instance variable for the sender, receiver, and subject.  The textual contents of the file should be stored in the inherited variable text. Redefine the toString method to concatenate all text fields.

Create sample objects of type Email and SMS in your 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(Message  messageObject,  String  keyword) {
if (messageObject.toString().indexOf(keyword,0) >= 0)
return true;

return false;
}

Finally, include a method to encode the final message  “This is  Java” using an encoding scheme, according to which, each character should be replaced by the character that comes after it. For example, if the message contains character B orb, it should be replaced by C or c accordingly, while Z or z should be replaced with an A  or a. If the final message is “This is Java”, then the encoded message should be “UijtjtKbwb”.

Message.java

class  Message{

// instance variable text String text;
String text;

// constructor
public Message(String text) {
this.text = text;
}

// setter
public void setText(String text) {
this.text = text;
}

// toString method returns text
public String toString() {
return this.text;
}
}

SMS.java

// class SMS extending Message class
public class SMS extends Message {
// instance variable String recipientContactNo;
String recipientContactNo;

// constructor
public SMS(String text,String recipientContactNo) {
super(text);
this.recipientContactNo = recipientContactNo;
}

// getter method
public String getReceiptContactNo() {
return recipientContactNo;
}

// setter method
public void setReceiptContactNo(String receiptContactNo) {
this.recipientContactNo = receiptContactNo;
}

// toString method returns text concatenated with recipientContactNo
public String toString() {
return super.toString()+" "+this.recipientContactNo;
}
}


Email.java
//  email  class  extending  Message  class 
class Email extends Message {

// instance variables
String sender, reciever, subject;

// constructor
public Email(String sender, String reciever, String subject, String text) {
super(text);
this.sender = sender;
this.reciever = reciever;
this.subject = subject;
}

// toString() method returns text,sender,reciever,subject
public String toString() {
return super.toString() + " " + this.sender + " " + this.reciever + " " + this.subject;
}
}



Main.java
// main
class public class Main {

// containsKeywordmethod
public static boolean ContainsKeyword(Message messageObject, String keyword) {
if (messageObject.toString().indexOf(keyword, 0) >= 0) return true;

return false;
}

// encode method converts each character in a string to its successive character
public static void encode(Message messageObject) {
String msg = messageObject.text;
String encoded = "";

for (char c: msg.toCharArray()) {
if (!(c == ' '))
encoded += Character.toString((char)(((c - 'a' + 1) % 26) + 'a'));
}
System.out.println(encoded);
}

//main method
public static void main(String[] args) {

// sample email object
Email email = new Email("Me", "you", "Question", "Answered");
// sample SMS object
SMS sms = new SMS("Hey", "909099909");

// testing containsKeyword method
System.out.println("Testing ConatinsKeyword method..\n");
System.out.println(ContainsKeyword(email, "Me"));
System.out.println(ContainsKeyword(sms, "Hai"));

// testing encode method.
System.out.println("Testing encode method");

encode(new Message("This is Java"));
}
}