Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 2: A class that needs your help 11 points total; individual-only Consider the following class, which is intended to serve as a blueprint for

Problem 2: A class that needs your help

11 points total; individual-only Consider the following class, which is intended to serve as a blueprint for objects that encapsulate two pieces of data: a street number (which must be positive) and a street name (which must be no more than 30 characters long).

public class StreetAddress {

int num;

String name;

public boolean onEvenSide() {

return (this.num % 2 == 0);

}

}

1. (7 points) This class does not employ appropriate encapsulation. In section 2-1 of ps3_partI, revise the class to prevent direct access to the internals of a StreetAddress object while allowing indirect access through appropriate methods. Your revised version should include:

- whatever steps are needed to prevent direct access to the fields

- accessor methods that can be used to obtain the value of each field

- methods that can be used to change the value of each field. These methods should ensure that num is always greater than 0, and that name is no more than 30 characters long. Any attempt to assign an invalid value should produce an IllegalArgumentException.

- a constructor that initializes the fields of a newly created StreetAddress object to the values specified by the parameters of the constructor. Any attempt to specify an invalid value should produce an IllegalArgumentException. Take advantage of the error-checking code that is already present in the methods that you defined for changing the values of the fields.

No other methods are required.

2. (4 points) Now imagine that youre writing client code for your revised StreetAddress class i.e., code in a different class that uses StreetAddress objects. For each of the following tasks, write a single line of client code to accomplish the task:

a. Construct a StreetAddress object in which the number is 650 and the name is "Commonwealth Avenue" and assign it to a properly declared variable named ccds (short for Center for Computing and Data Sciences, the new home of the CS department!).

b. Your friend points out that the correct street number for the new building is 665, not 650. Change the value of the num field in the object that you created in part (a), giving it a value of 665. You should not create a new object; you should change the internals of the existing object.

Important: As a result of your changes from part 1, clients no longer have direct access to the fields in a StreetAddress object. As a result, you will need to make a method call to change the appropriate field.

c. Get the name component of the StreetAddress object that you created in part (a), and assign it to a properly declared variable named streetName. Here again, you will need to make an appropriate method call.

d. Now imagine that you are given a second StreetAddress object that has been created by someone else and has been assigned to the variable addr. Change the value of the num field in addr, giving it a value that is 1 more than its current value.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 19 21 2012 Proceedings Part 3 Lnai 7198

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284922, 978-3642284922

More Books

Students also viewed these Databases questions