Java Programming Assignment: Creating a Message Class - Prof. Nelson Padua-Perez, Quizzes of Computer Science

Instructions for creating a java class named message with two private instance variables: text (string) and cost (double). The class includes a default constructor, a constructor with two parameters, a copy constructor, a tostring method, an equals method, and a static append method. The append method takes two message objects as parameters and returns a new message object with the concatenated text and the sum of costs.

Typology: Quizzes

Pre 2010

Uploaded on 02/13/2009

koofers-user-ka3
koofers-user-ka3 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC131 Spring 2005 Quiz #5, Duration 15 Minutes
First Name: Last Name: Student Id: cs131 _ _ _
Section time (circle one): 8AM 9AM 10AM 11AM 12PM 1PM
Problem Description
Write a complete Java class named Message. The class has two private instance variables named text (String)
and cost (double) which represent the message’s text and cost, respectively. All the methods of the class are non-
static except the method named append (described below). The methods you must implement are:
1. Default Constructor – Initializes the object with the value “NOMESSAGE” and 1.0.
2. Constructor – Initializes the object using two parameters that represent the text and cost of the object,
respectively.
3. Copy Constructor Creates a copy based on the parameter object. The text instance variable is
initialized with a duplicate of the parameter’s text.
4. toString – Returns a string generated by concatenating “Text: ”, text, “ Cost: ”, and cost.
5. equals – Boolean method that tests whether the current object has the same text as another Message,
which is given as the single parameter.
6. append – A static method that takes two Message object parameters and returns a new Message object.
The text of the new object is generated by concatenating the text of the first and second parameters. The
new cost is the sum of the objects’ costs.
Restrictions/Assumptions
1. You don’t need to provide comments.
2. You cannot add any other instance variables or methods.
BEGIN YOUR IMPLEMENTATION HERE (CONTINUE ON THE BACK IF NECESSARY)

Partial preview of the text

Download Java Programming Assignment: Creating a Message Class - Prof. Nelson Padua-Perez and more Quizzes Computer Science in PDF only on Docsity!

CMSC131 Spring 2005 Quiz #5, Duration 15 Minutes

First Name: Last Name: Student Id: cs131 _ _ _

Section time (circle one): 8 AM 9 AM 10 AM 11 AM 12 PM 1 PM

Problem Description

Write a complete Java class named Message. The class has two private instance variables named text (String) and cost (double) which represent the message’s text and cost , respectively. All the methods of the class are non- static except the method named append (described below). The methods you must implement are:

  1. Default Constructor – Initializes the object with the value “NOMESSAGE” and 1.0.
  2. Constructor – Initializes the object using two parameters that represent the text and cost of the object, respectively.
  3. Copy Constructor – Creates a copy based on the parameter object. The text instance variable is initialized with a duplicate of the parameter’s text.
  4. toString – Returns a string generated by concatenating “Text: ” , text , “ Cost: ” , and cost.
  5. equals – Boolean method that tests whether the current object has the same text as another Message, which is given as the single parameter.
  6. append – A static method that takes two Message object parameters and returns a new Message object. The text of the new object is generated by concatenating the text of the first and second parameters. The new cost is the sum of the objects’ costs.

Restrictions/Assumptions

  1. You don’t need to provide comments.
  2. You cannot add any other instance variables or methods.

BEGIN YOUR IMPLEMENTATION HERE (CONTINUE ON THE BACK IF NECESSARY)