3 Replies - 306 Views - Last Post: 23 October 2013 - 02:03 AM Rate Topic: -----

#1 Yulia_Yukoma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-October 13

Help/advice with a client and class

Posted 22 October 2013 - 08:18 AM

Hello, I am stuck on a particular assignment on where we:

Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute: a String representing the telephone number in 10 digits (e.g. 5168761234) with no punctuation. Include a default constructor, an overloaded constructor, the accessor and mutator methods, and the methods equals() and toString(). Your toString() method should return the telephone number with punctuation (e.g. (516) 876-4257 ). Also include a method that returns the area code (first three digits). If there are less than three digits then your area code method should return “unknown area code”. In addition, write a client class to test all the methods in your class.

I am confuse how on what I should use as my variables other than the phone.
I know my private String should be the phonnumber but what should do my double or final int? I just need to know what variables I should maybe use and I should be able to do the rest.
I was going to use this as starting code for this assignment:

Class:
import java.text.DecimalFormat;

public class CellPhone
{
public final int MONTHS = 6; // default number of months
private String phoneNumber;
private double [] cellBills;

/** Default constructor
* creates cellBills with MONTHS elements
*/
public CellPhone( )
{
phoneNumber = ""; //best way to intiliaze the string
cellBills = new double [MONTHS]; //allocating storage
}

/** Constructor
* @param number cell phone number
* @param bills array of monthly bills
*/
public CellPhone( String number, double [] bills )
{
phoneNumber = number;

// instantiate array with same length as parameter
cellBills = new double [bills.length];

// copy parameter array to cellBills array
for ( int i = 0; i < cellBills.length; i++ )
{
cellBills[i] = bills[i];
}
}

/** Returns the phone number
* @return the phone number
*/
public String getPhoneNumber( )
{
return phoneNumber;
}

/** Returns an array of cell phone bills
* @return copy of cellBills array
*/
public double [] getCellBills( )
{
double [] temp = new double [cellBills.length];
for ( int i = 0; i < cellBills.length; i ++ )
{
temp[i] = cellBills[i];
}
return temp;
}

/** Calculates total of all cell phone bills
* @return total of all elements in cellBills array
*/
public double calcTotalBills( )
{
double total = 0.0; // initialize total to 0.0

for ( int i = 0; i < cellBills.length; i++ )
{
total += cellBills[i]; // add current element to total
}
return total;
}

/** Finds the maximum bill
* @return largest value in cellBills array
*/
public double findMaximumBill( )
{
double max = cellBills[0]; // assume first element is max

for ( int i = 1; i < cellBills.length; i++ )
{
if ( cellBills[i] > max )
max = cellBills[i]; // save new maximum
}
return max;
}

/** Returns printable version of CellPhone object
* @return phone number plus each month's bill
*/
public String toString( ) // C1
{
String returnValue = phoneNumber + "\n";
DecimalFormat money = new DecimalFormat( "$##0.00" );
for ( int i = 0; i < cellBills.length; i++ )
{
returnValue += money.format( cellBills[i] ) + "\t";
}
returnValue += "\n";

return returnValue;
}

/** Compares two CellPhone objects for equality
* @param c CellPhone object
* @return true if objects are equal; false, otherwise
*/
public boolean equals( Object c )
{
if ( !( c instanceof CellPhone ) )
return false;
else
{
CellPhone objCP = ( CellPhone ) c;
if ( !( phoneNumber.equals( objCP.phoneNumber ) ) )
return false;

if ( cellBills.length != objCP.cellBills.length )
return false; // arrays are not the same length

for ( int i = 0; i < cellBills.length; i++ )
{
if ( cellBills[i] != objCP.cellBills[i] )
return false;
}
return true;
}
}
}


Client:

import java.text.DecimalFormat;

public class CellPhoneClient
{
public static void main( String [] args )
{
double [] bills = new double[3]; // array of cell phone bills
bills[0] = 24.60; // assign values
bills[1] = 48.75;
bills[2] = 62.50;

// instantiate CellPhone object using default constructor
CellPhone c1 = new CellPhone( );

// instantiate two identical CellPhone objects (bills are array) this is the way to pass arrays in a parameter
CellPhone c2 = new CellPhone( "555-555-5555", bills );
CellPhone c3 = new CellPhone( "555-555-5555", bills );

// print data from c1 and c2
System.out.println( "c1 = " + c1.toString( ) );
System.out.println( "c2 = " + c2.toString( ) );

// find and print maximum bill
DecimalFormat money = new DecimalFormat( "$##0.00" );
System.out.println( "\nThe highest bill is "
+ money.format( c2.findMaximumBill( ) ) );

// find and print total of all bills
System.out.println( "\nThe total of all bills is "
+ money.format( c2.calcTotalBills( ) ) );

System.out.println( ); // print blank line
// call equals method
if ( c2.equals( c3 ) )
System.out.println( "c2 and c3 are equal" );
else
System.out.println( "c2 and c3 are not equal" );

// test encapsulation
// set new value in original array
bills[2] = 100.00;
// print c2 to show value in object not changed
System.out.println( "\nafter client changes original array\n"
+ "c2 = " + c2.toString( ) );

// test encapsulation further
// get array of cell bills and store in new array
double [] billsCopy = c2.getCellBills( );

billsCopy[1] = 50.00; // change value of one element
// print c2 to show value in object not changed
System.out.println( "\nafter client changes returned array\n"
+ "c2 = " + c2.toString( ) );
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Help/advice with a client and class

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2777
  • View blog
  • Posts: 11,758
  • Joined: 20-September 08

Re: Help/advice with a client and class

Posted 22 October 2013 - 11:26 AM

Quote

I am confuse how on what I should use as my variables other than the phone.


Quote

Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute
(my emphasis)

IOW, there is only one (instance) variable

This post has been edited by g00se: 22 October 2013 - 11:27 AM
Reason for edit:: Clarification

Was This Post Helpful? 1
  • +
  • -

#3 Yulia_Yukoma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-October 13

Re: Help/advice with a client and class

Posted 22 October 2013 - 08:56 PM

View Postg00se, on 22 October 2013 - 11:26 AM, said:

Quote

I am confuse how on what I should use as my variables other than the phone.


Quote

Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute
(my emphasis)

IOW, there is only one (instance) variable


Thank you for the reply, I just basically made the skeleton of the code. Going to st art figure out I am going to test for the phone number.




import java.text.DecimalFormat;

public class TelephoneNumber
{
  private String phoneNumber; // instance variables


  /** Default constructor
  *   creates cellBills with MONTHS elements
  */
  public TelephoneNumber( )
  {
     phoneNumber = "";  //best way to intiliaze the string
  }

  /** Constructor
  
  public TelephoneNumber( String phoneNumber )
  {
    phoneNumber = startNumber


  }


  // Accessor
  /** Returns the phone number
  *   @return the phone number
  */
  public String getPhoneNumber( )
  {
    return phoneNumber;
  }

	// Mutator Methods:
	// allows client to set model
	public void setModel( String newModel )
	{
		phoneNumber = newphoneNumber;
	}




 
   public string TelephoneNumerTest( )
   {

   }



   public String toString( ) // C1
   {
     String returnValue = phoneNumber + "\n";

   }


   public boolean equals( Object c )
   {
     if ( !( c instanceof TelephoneNumber ) )
         return false;
     else
     {
         TelephoneNumber objCP = ( TelephoneNumber ) c;
         if ( !( phoneNumber.equals( objCP.phoneNumber ) ) )
           return false;
     }
   }
 }

//- Add a member variable number of type String
//- Add a default constructor
//- Add an accessor
//- Write new tests in a TelephoneNumberTest class
//- Add an overloaded constructor
//- Add a mutator
//- Write additional tests in TelephoneNumberTest
//- Add equals method
//- Write additional tests in TelephoneNumberTest
//- Add toString method
//- Write additional tests in TelephoneNumberTest
//- Add getAreaCode method
//- Write additional tests in TelephoneNumberTest


Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2777
  • View blog
  • Posts: 11,758
  • Joined: 20-September 08

Re: Help/advice with a client and class

Posted 23 October 2013 - 02:03 AM

That's a start. In a class called TelephoneNumber, the instance variable only need be called number. The pattern of initialization should be

this.number = number;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1