Question: Write an interface called Lock which has 2 methods which are called lock and unlock. The methods have no parameters and return nothing. A class

Write an interface called Lock which has 2 methods which are called lock and unlock. The methods have no parameters and return nothing.

A class X which implements the Lock interface behaves normally if it is unlocked. If locked, the toString method returns X is locked.. Either all of Xs objects are locked, or all unlocked. Initially, the class is unlocked. Calling the lock method with one object of type X, locks all the objects of type X. Similarly for unlock.

Modify the Address and Student classes from Chapter 7 so that they both implement the Lock interface.

Also, overload the Student constructor so that it can accept an integer course grade. Assume the grade is zero if none specified.

Your output must be exactly as shown below:

 Write an interface called Lock which has 2 methods which are

//******************************************************************** // Address.java Author: Lewis/Loftus // // Represents a street address. //********************************************************************

public class Address { private String streetAddress, city, state; private long zipCode;

//----------------------------------------------------------------- // Constructor: Sets up this address with the specified data. //----------------------------------------------------------------- public Address(String street, String town, String st, long zip) { streetAddress = street; city = town; state = st; zipCode = zip; }

//----------------------------------------------------------------- // Returns a description of this Address object. //----------------------------------------------------------------- public String toString() { String result;

result = streetAddress + " "; result += city + ", " + state + " " + zipCode;

return result; } }

//******************************************************************** // Student.java Author: Lewis/Loftus // // Represents a college student. //********************************************************************

public class Student implements Lock { private String firstName, lastName; private Address homeAddress, schoolAddress; private int finalGrade;

//----------------------------------------------------------------- // Constructor: Sets up this student with the specified values. //----------------------------------------------------------------- public Student(String first, String last, Address home, Address school, int grade) { firstName = first; lastName = last; homeAddress = home; schoolAddress = school; finalGrade = grade; } public Student(String first, String last, Address home, Address school) /o int grade { firstName = first; lastName = last; homeAddress = home; schoolAddress = school; finalGrade = 0; }

//----------------------------------------------------------------- // Returns a string description of this Student object. //----------------------------------------------------------------- public void lock() { homeAddress = "Address is locked"; schoolAddress = "Address is locked"; } public String toString() { String result; result = firstName + " " + lastName + " "; result += "Grade: "+finalGrade + " "; result += "Home Address: " + homeAddress + " "; result += "School Address: " + schoolAddress; return result; } public String unlock() { String result; result = firstName + " " + lastName + " "; result += "Grade: "+finalGrade + " "; result += "Home Address: " + homeAddress + " "; result += "School Address: " + schoolAddress; return result; } }

//******************************************************************** // StudentBody.java Author: Lewis/Loftus // // Demonstrates the use of an aggregate class, and an interface. //********************************************************************

public class StudentBody { //----------------------------------------------------------------- // Creates some Address and Student objects and prints them. //----------------------------------------------------------------- public static void main(String[] args) { Address school = new Address("800 Lancaster Ave.", "Villanova", "PA", 19085); Address jHome = new Address("21 Jump Street", "Blacksburg", "VA", 24551); Student john = new Student("John", "Smith", jHome, school); Student adam = new Student("Adam", "Smith", jHome, school, 80);

Address mHome = new Address("123 Main Street", "Euclid", "OH", 44132); Student marsha = new Student("Marsha", "Jones", mHome, school);

System.out.println(john); System.out.println(); System.out.println(adam); System.out.println(); System.out.println(marsha); school.lock(); System.out.println(); System.out.println(john); System.out.println(); System.out.println(adam); System.out.println(); System.out.println(marsha); john.lock(); System.out.println(); System.out.println(john); System.out.println(); System.out.println(adam); System.out.println(); System.out.println(marsha); adam.unlock(); System.out.println(); System.out.println(john); System.out.println(); System.out.println(adam); System.out.println(); System.out.println(marsha); } }

Java Vori s run Student Body John Saith Crade: 0 1 Jup Street Blacksburg. VA 24551 School Address 800 Lancaster Ave villanova, PA 1908.5 Adam Snith Grade: 80 Hone Address 1 Jump Street Blacksbur. VA 24551 School Address 800 Laneaster Are villanova, PA 19085 Har sha Jones Grade 0 Hone Address 123 Main Street Euclid, CHI44132 School Address 800 Laneaster Are villanova, PA 19035 John Snith Grade: 0 Hone ddress Address is locked School Address Addr@s% % locir@d. Adam Snith Grade: B0 School Address Marsha Jones Grade: 0 Hone Address Address is locked School Address Address 1s ocked Student is locked Student is locked Student is locked John Snith Grade: 0 Hone Address Address is locked School Address Address is locked Adam Snith Grade: B0 Hone Address Address is locked School Address Address is locked Harsha Jones Grade 0 Hone Address Address is locked School Address: Address is locked

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!