- Create a Java project. The name of the project must be lab10 . - Add three Jav
ID: 3778197 • Letter: #
Question
- Create a Java project. The name of the project must be lab10.
- Add three Java classes to your project, Test for the main class, furthermore Letter and POBox.
Exercises
For each of the classes below you find a UML diagram. With the help of the diagrams add the listed members to the class and use the attached description to implement the code.
1. Class Letter
Letter
- sender: String
- postage: double
Declare the fields
+ Letter(t: String, p: postage)
+ Letter(other: Letter)
+ toString( ) : String
+ equals(other: Letter): boolean
Initializer constructor
Copy constructor
Creates and returns a String message
about the field values
Compares two Letter objects for equality
which means the fields are equal
2. Class POBox
POBox
-width: double
-length: double
-letters: Letter [ ]
Declare all three fields
+getWidth( ): double
+ getLength( ):double
+ getLetters( ): Letter [ ]
+toString( ):String
+ equals(other: POBox): boolean
+POBox()
+PPOBox(w:double, len:double, lett:
Letter [ ])
+PObox(box: POBox)
Getters for the fields
Creates and returns a String message
about the field values
Compares two POBox objects for equality
(based upon the equality of the fields)
default constructor
initializer constructor
copy constructor
3. Class Test
This class contains the main method. As for the tasks of the main method you write code as follows:
- Instantiate a Letter array of length 5 (choose a name for the array, say myLetters) and instantiate Letter objects for the array entries. Use the initializer constructor for the individual Letter objects and an initializer list for the array. Choose names for the fields sender and postage values at will.
- Instantiate a POBox object named box; use the initializer constructor of the POBox class, for its parameters choose width and length at will, and use myLetters for the array parameter.
- Apply the copy constructor of the POBox class to create a deep copy of box, and save the copy in a reference boxCopy (local variable).
- Apply the = = operator to test the equality of the 0 index entries of the original array in the original box, and that of the copied array in boxCopy. Print the result to the console (see a sample output of Figure 1).
- Use the toString( ) method of the POBox class for both box and boxCopy, print the toString() return value output to the console (see a sample output of Figures 2 and 3).
- Use the equals( ) method of the POBox class to compare box and boxCopy for equality and print the output to the console (see sample output Figure 4).
v
v
v
v
*Sample output:
Check if the copy is deep! == should return false
Apply == to the 0 index letters: false
Figure 1
______________________________________________
The original box:
Box width: 12.0, box length: 20.0
sender: "Paul", postage: $0.49; sender: "Einstein", postage: $1.2; sender: "Sears", postage: $0.8; sender: "Laurie", postage: $0.49; sender: "State Farm", postage: $0.49;
Figure 2
________________________________________________
The copy of the box:
Box width: 12.0, box length: 20.0
sender: "Paul", postage: $0.49; sender: "Einstein", postage: $1.2; sender: "Sears", postage: $0.8; sender: "Laurie", postage: $0.49; sender: "State Farm", postage: $0.49;
Figure 3
_______________________________________________________
The box and its copy tested for equality: true
Figure 4
Letter
- sender: String
- postage: double
Declare the fields
+ Letter(t: String, p: postage)
+ Letter(other: Letter)
+ toString( ) : String
+ equals(other: Letter): boolean
Initializer constructor
Copy constructor
Creates and returns a String message
about the field values
Compares two Letter objects for equality
which means the fields are equal
Explanation / Answer
//import files
import java.io.*;
import java.lang.*;
import java.util.*;
//Letter class
class Letter
{
String sender;
double postage;
//constructor
public Letter(String t, double p)
{
sender=t;
postage=p;
}
public Letter(Letter other)
{
sender=other.sender;
postage=other.postage;
}
public String toString()
{
return "Sender:"+sender+" Postage:$"+postage;
}
public boolean equals(Letter other)
{
if(sender.equals(other.sender))
{
if(postage==other.postage)
return true;
return false;
}
return false;
}
}
//POBox class
class POBox
{
double width;
double length;
Letter[] letters;
public POBox()
{
width=0;
length=0;
letters=null;
}
public POBox(double w, double len, Letter[] lett)
{
width=w;
length=len;
letters=new Letter[lett.length];
for(int kk=0;kk<lett.length;kk++)
{
letters[kk]=lett[kk];
}
}
public POBox(POBox box)
{
width=box.width;
length=box.length;
letters=new Letter[box.letters.length];
for(int kk=0;kk<box.letters.length;kk++)
{
letters[kk]=new Letter(box.letters[kk]);
}
}
public double getWidth()
{
return width;
}
public double getLength()
{
return length;
}
public Letter[] getLetters()
{
return letters;
}
public String toString()
{
String tt="";
tt+="Box width:"+width+" , Box length:"+length+" ";
for(int kk=0;kk<letters.length;kk++)
{
tt+=letters[kk]+" ";
}
return tt;
}
public boolean equals(POBox other)
{
if(width==other.getWidth())
{
if(length==other.getLength())
{
for(int kk=0;kk<letters.length;kk++)
{
if(!letters[kk].equals(other.letters[kk]))
return false;
}
return true;
}
else
return false;
}
return false;
}
}
//Driver class
public class Test
{
public static void main(String[] args)
{
Letter[] myLetters=new Letter[5];
myLetters[0]=new Letter("Paul",0.49);
myLetters[1]=new Letter("Einstein",1.2);
myLetters[2]=new Letter("Sears",0.8);
myLetters[3]=new Letter("Laurie",0.49);
myLetters[4]=new Letter("State Farm",0.49);
POBox box=new POBox(12.0,20.0,myLetters);
POBox boxCopy=new POBox(box);
System.out.println("Check if the copy is deep! == should return false");
Letter[] m1=box.getLetters();
Letter[] m2=boxCopy.getLetters();
System.out.println("Apply == to the 0 index letters:"+(m1[0]==m2[0]));
System.out.println("The original box:");
System.out.println(box);
System.out.println("The copy of the box:");
System.out.println(boxCopy);
System.out.println("The box and its copy tested for equality:"+box.equals(boxCopy));
}
}
Sample output:
sh-4.3$ java -Xmx128M -Xms16M Test
Check if the copy is deep! == should return false
Apply == to the 0 index letters:false
The original box:
Box width:12.0 , Box length:20.0
Sender:Paul Postage:$0.49
Sender:Einstein Postage:$1.2
Sender:Sears Postage:$0.8
Sender:Laurie Postage:$0.49
Sender:State Farm Postage:$0.49
The copy of the box:
Box width:12.0 , Box length:20.0
Sender:Paul Postage:$0.49
Sender:Einstein Postage:$1.2
Sender:Sears Postage:$0.8
Sender:Laurie Postage:$0.49
Sender:State Farm Postage:$0.49
The box and its copy tested for equality:true
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.