Please finish this by using java.If u run StudentBody.java the output should be
ID: 3698732 • Letter: P
Question
Please finish this by using java.If u run StudentBody.java the output should be the same as the picture showing b
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 X’s 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.
Your output must be exactly as shown below:
//********************************************************************
// Address.java Author: Lewis/Loftus
//
// Represents a street address.
//********************************************************************
public class Address
{
private String streetAddress, city, state;
private long zipCode;
public Address (String street, String town, String st, long zip)
{
streetAddress = street;
city = town;
state = st;
zipCode = zip;
}
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
{
private int grade;
private String firstName, lastName;
private Address homeAddress, schoolAddress;
public Student (String first, String last, Address home,
Address school,int grade1)
{
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
grade = grade1;
}
public String toString()
{
String result;
result = firstName + " " + lastName + " ";
result += "Grade: "+grade;
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
{
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,0);
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,0);
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);
}
}
Explanation / Answer
here is your files : ---------->>>>>>>
Lock.java : --------->>>>>>>>>
public interface Lock{
public void lock();
public void unlock();
}
Address.java : ---------->>>>>>>>>>
public class Address implements Lock
{
private String streetAddress, city, state;
private long zipCode;
private static boolean lock_ = false;
public Address (String street, String town, String st, long zip)
{
streetAddress = street;
city = town;
state = st;
zipCode = zip;
}
public String toString()
{
String result;
if(Address.lock_){
result = "Address is lock";
}else{
result = streetAddress + " ";
result += city + ", " + state + " " + zipCode;
}
return result;
}
public void lock(){
Address.lock_ = true;
}
public void unlock(){
Address.lock_ = false;
}
}
Student.java : ----------->>>>>>>>>.
public class Student implements Lock
{
private int grade;
private String firstName, lastName;
private Address homeAddress, schoolAddress;
private static boolean lock_;
public Student (String first, String last, Address home,Address school,int grade1)
{
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
grade = grade1;
}
public String toString()
{
String result;
if(Student.lock_){
result = "Student is lock";
}else{
result = firstName + " " + lastName + " ";
result += "Grade: "+grade;
result += " Home Address: " + homeAddress + " ";
result += "School Address: " + schoolAddress;
}
return result;
}
public void lock(){
Student.lock_ = true;
}
public void unlock(){
Student.lock_ = false;
}
}
StudentBody.java : ---------->>>>>>>>>>>>>
public class StudentBody
{
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,0);
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,0);
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);
}
}