Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

Example 1-1: Two common attributes of a person are the person's first name and last name. The typical operations on a person's name are to set the name and print the name. The following statements define a class with these properties.

public class Person
{

private String firstName; //store the first name
private String lastName; //store the last name

//Default constructor;
//Initialize firstName and lastName to empty string.
//Postcondition: firstName = ""; lastName = " ";
public Person ()
{
firstName = "";
lastName = "";

}

//Constructor with parameter
//Set firstName and lastName according to the parameters.
//Postcondition: firstName = first; lastName = last;
public Person (String first, String last)
{
setName (first, last);
}

//Method to output the first name and last name
//in the form firstName lastName
public String toString ()
{
return (firstName + " " + lastName);
}

//Method to set firstName and lastName according to
//the parameters
//Postcondition: firstName = first; lastName = last;
public void setName (String first, String last)
{
firstName = first;
lastName = last;
}
//Method to return the firstName
//Postcondition: the value of firstName is returned
public String getFirstName ()
{
return firstName;
}

//Method to return the lastName
//Postcondition: the value of lastName is returned
public String getLastName ()
{
return lastName;
}
}

The following program tests the class Person:

public class TestProgPerson
{
public static void main (String[ ] args)
{
Person name = new Person (); //Line 1

Person emp = new Person ("Donald", "Jackson"); //Line 2

System.out.println ("Line 3: name: " +name); //Line 3

name.setName ("Ashley", "Blair"); //Line 4
System.out.println ("Line 5: name: " +name); //Line 5

System.out.println ("Line 6: emp: " + emp); //Line 6

emp.setName ("Sandy", "Smith"); //Line 7
Sytem.out.println ("Line 8: emp: " + emp); //Line 8
}//end main
}

Question #1: Develop and execute a program for this exercise.

a) Example 1-1 above defined the class Person to store the name of a person. The methods that we included merely set the name and print the name of a person. Redefine the class Person so that, in addition to what the existing class does, you can:

i. Set the last name only.

ii. Set the first name only.

iii. Set the middle namde.

iv. Check whether a given last name is the same as the last name of this person.

v. Check whether a given first name is the same as the first name of this person.

vi. Check whether a given middle name is the same as the middle name of this person.

b) Add the method equals that returns true if two objects contain the same first, middle, and last name.

c) Add the method makeCopy that copies the instance variable of a Person object into another Person object.

d) Add the method getCopy that creates and returns the address of the object, which is a copy of another Person object.

e) Add the copy constructor.

f) Write the definitions of the methods of the class Person to implement the operations for this class.

g) Write a program that tests various operations of the class Person.

Java, Programming

  • Category:- Java
  • Reference No.:- M91612425
  • Price:- $20

Priced at Now at $20, Verified Solution

Have any Question?


Related Questions in Java

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Simple order processing systemquestion given the classes

Simple Order Processing System Question: Given the classes Ship (with getter and setter), Speedboat, and SpeedboatTest. Answer the following questions: Refine the whole application (all classes) and create Abstract class ...

Project requirementsfor the problem described in the next

Project requirements For the problem described in the next section, you must do the following: 1. include your student ID at the end of all filenames for all java code files. Three classes have been identified in section ...

Assignment - java program using array of objectsobjectives

Assignment - JAVA Program using array of objects Objectives - This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details - For this assignment, you are required to develop a Menu ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Object-oriented software development1 introduction 11

OBJECT-ORIENTED SOFTWARE DEVELOPMENT 1. Introduction 1.1 Assignment Requirement 1.2 Deliverables and Structure (what to submit) 1.3 Software Restrictions 1.4 How to score high... 1.5 Assumptions 2. System Requirements 2. ...

Solving 2nd degree equationsbull write the following java

Solving 2nd degree equations • Write the following Java methods • boolean real-sols(double a, double b, double c): it returns true if the 2nd degree equation ax2 + bx + c has real solutions • double solution1(double a, d ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As