//Solution set for Homework 2 //Tests omitted due to file corruption problems //Data Design Section //Question 1: A data design for customers and bank accounts /* +------------------+ +---------------------+ | Customer | +------>| Account | +------------------+ | +---------------------+ | int idNumber | | | double balance | | int phoneNumber | | | String creditRating | | Account custAcct |-----+ +---------------------+ +------------------+ */ class Customer { int idNumber; int phoneNumber; Account custAcct; Customer(int idNumber, int phoneNumber, Account custAcct) { this.idNumber = idNumber; this.phoneNumber = phoneNumber; this.custAcct = custAcct; } } class Account { double balance; String creditRating; Account(double balance, String creditRating) { this.balance = balance; this.creditRating = creditRating; } } //Question 2: A data design for Authors, Books, and Papers /* +----------------+ +--------------------------+ | Author | +-->| Book | +----------------+ | +--------------------------+ | String name | | | String title | | Book oneBook |---+ | int numChapters | | Paper onePaper |---+ | boolean hasIllustrations | +----------------+ | +--------------------------+ | +-----------------+ | | Paper |<-+ +-----------------+ | String location | | int year | +-----------------+ */ class Author { String name; Book oneBook; Paper onePaper; Author(String name, Book oneBook, Paper onePaper) { this.name = name; this.oneBook = oneBook; this.onePaper = onePaper; } } class Book { String title; int numChapters; boolean hasIllustrations; Book(String title, int numChapters, boolean hasIllustrations) { this.title = title; this.numChapters = numChapters; this.hasIllustrations = hasIllustrations; } } class Paper { String location; int year; Paper(String location, int year) { this.location = location; this.year = year; } } //Question 3: /* +---------------------------+ | abstract Automobile | +---------------------------+ +---------------------------+ | abstract int doors() | | abstract double capacity()| +---------------------------+ ^ | ----------------------------------- | | +------------------+ +-------------------+ | Car | | Truck | +------------------+ +-------------------+ | int numDoors | | boolean has2Doors | | double trunkVol | | int bedX | +------------------+ | int bedY | | int doors() | | int bedZ | | double capacity()| +-------------------+ +------------------+ | int doors() | | double capacity() | +-------------------+ */ abstract class Automobile { //To report on the number of doors in this Automobile abstract int doors(); //To report on the carrying capacity of this Automobile abstract double capacity(); } class Car extends Automobile { int numDoors; double trunkVol; Car(int numDoors, double trunkVol) { this.numDoors = numDoors; this.trunkVol = trunkVol; } //new Car(4,3.0).doors() == 4 //To report on the number of doors in this Car int doors() { // ... this.numDoors ... this.trunkVol ... return this.numDoors; } //new Car(4,3.0).capacity() == 3.0 //To report on the carrying capacity of this Car double capacity() { // ... this.numDoors ... this.trunkVol ... return this.trunkVol; } } class Truck extends Automobile { boolean has2Doors; int bedX; int bedY; int bedZ; Truck(boolean has2Doors, int bedX, int bedY, int bedZ) { this.has2Doors = has2Doors; this.bedX = bedX; this.bedY = bedY; this.bedZ = bedZ; } //new Truck( true, 1, 1, 1).doors() =- 2 //new Truck( false, 1,1,1).doors() == 4 //To report on the number of doors in this Truck int doors() { // this.has2Doors ... this.bedX ... this.bedY ... this.bedZ if (this.has2Doors) return 2; else return 4; } //Note: I don't expect you to have implemented this using 'if', since we haven't covered it yet //new Truck( true, 1, 2, 3).capacity() == 6 //To report on the carrying capacity of this Truck double capacity() { // this.has2Doors ... this.bedX ... this.bedY ... this.bedZ return this.bedX * this.bedY * this.bedZ; } } //Question 4: Zoo animals /* +-----------------------------------+ | abstract Animal | +-----------------------------------+ +-----------------------------------+ | abstract Animal feed( int weight) | | abstract boolean needBath() | | abstract boolean needMeds() | +-----------------------------------+ ^ | --------------------------------------------------------------- | | | +--------------------------+ +--------------------------+ +--------------------------+ | Shark | | Tiger | | Emu | +--------------------------+ +--------------------------+ +--------------------------+ | int weight | | boolean isHungry | | int weight | +--------------------------+ | boolean isDirty | | boolean isMedicated | | Animal feed( int weight) | +--------------------------+ +--------------------------+ | boolean needBath() | | Animal feed( int weight) | | Animal feed( int weight) | | boolean needMeds() | | boolean needBath() | | boolean needBath() | +--------------------------+ | boolean needMeds() | | boolean needMeds() | +--------------------------+ +--------------------------+ */ abstract class Animal { //To feed this Animal abstract Animal feed( int weight ); //To determine if this Animal needs a bath abstract boolean needBath(); //To determine if this Animal needs meds abstract boolean needMeds(); } class Shark extends Animal { int weight; Shark(int weight) { this.weight = weight; } //To feed this Shark // new Shark(3).feed(2) -> new Shark(5) Animal feed(int w) { // ... this.weight ... w return new Shark( this.weight + w); } //new Shark(3).needBath() == false //To determine if this Shark needs a bath boolean needBath() { // ... this.weight ... return false; } //new Shark(3).needMeds() == false //To determine if this Shark needs meds boolean needMeds() { //... this.weight ... return false; } } class Tiger extends Animal { boolean isHungry; boolean isDirty; Tiger(boolean isHungry, boolean isDirty) { this.isHungry = isHungry; this.isDirty = isDirty; } //new Tiger(true,true).feed(2) -> new Tiger(false,true) //To feed this Tiger Animal feed(int w) { //... this.isHungry ... this.isDirty ... return new Tiger( false, this.isDirty); } //new Tiger(true,true).needBath() == true //new Tiger(true,false).needBath() == false //To determine if this Tiger needs a bath boolean needBath() { // ... this.isHungry ... this.isDirty ... return this.isDirty; } //new Tiger(false,true).needMeds() == true //new Tiger(true,true).needMeds() == false //To determine if this Tiger needs meds boolean needMeds() { // ... this.isHungry ... this.isDirty ... return !this.isHungry; } } class Emu extends Animal { int weight; boolean isMedicated; Emu(int weight, boolean isMedicated) { this.weight = weight; this.isMedicated = isMedicated; } //new Emu(2,false).feed(2) => new Emu(4,false) //To feed this Emu Animal feed(int w) { /// ... this.weight ... this.isMedicated ... return new Emu(this.weight + w, this.isMedicated); } //new Emu(2,false).needBath() == true //To determine if this Emu needs a bath boolean needBath() { //... this.weight ... this.isMedicated ... return true; } //new Emu(2,false).needMeds() == false //new Emu(3,true).needMeds() == true //To determine if this Emu needs medicine boolean needMeds() { //... this.weight ... this.isMedicated ... return this.isMedicated; } } /////////////////////////////////////////////////// //Section 2 //Question 1 //a class Person { Date birthdate; String firstName; String lastName; boolean isMarried; Person( Date birthdate, String firstName, String lastName, boolean isMarried) { this.birthdate = birthdate; this.firstName = firstName; this.lastName = lastName; this.isMarried = isMarried; } //examples //new Person(new Date(12,14,1977), "James", "Redmond", true) //new Person(new Date(7,3,2004), "Lil' Ben", "Walter", false) //b //new Person(new Date(7,3,2004), "Lil' Ben", "Walter", false).youngerThan(new Date(7,3,2005)) == true //new Person(new Date(7,3,2004), "Lil' Ben", "Walter", false).youngerThan(new Date(1,1,2003)) -- false //To determine if this Person is older than the given Date boolean olderThan( Date givenDate) { // ... this.birthdate.MethodCall() ... this.firstName ... this.lastName ... this.isMarried ... return this.birthdate.comesBefore(givenDate); } //c //new Person(new Date(7,3,2004), "Lil' Ben", "Walter", false).youngerThan(new Person(new Date(12,14,1977), "James", "Redmond", true)) == true //new Person(new Date(12,14,1977), "James", "Redmond", true).youngerThan(new Person(new Date(7,3,2004), "Lil' Ben", "Walter", false)) == false //To determine if this Person is younger than the given Person boolean youngerThan( Person p) { // ... this.birthdate.MethodCall() ... this.firstName ... this.lastName ... this.isMarried ... p return p.olderThan(this.birthdate); } //d //new Person(new Date(7,3,2004), "Lil' Ben", "Walter", false).marries("Turner") -> new Person(new Date(7,3,2004), "Lil' Ben", "Turner", true) //To produce a married representation of this Person, with a new last name Person marries(String newName) { // ... this.birthdate.MethodCall() ... this.firstName ... this.lastName ... this.isMarried ... return new Person(this.birthdate,this.firstName,newName,true); } } //a continued class Date { int month; int day; int year; Date( int month, int day, int year ) { this.month = month; this.day = day; this.year = year; } //b continued //new Date(1,2,2004).comesBefore(1,3,2003) )== false //new Date(3,4,1999).comesBefore(12,3,1900)) == true // To determine if this Date happened before given Date boolean comesBefore( Date givenDate) { // this.month ... this.day ... this.year ... givenDate return givenDate.happenedAfter(this.year, this.month, this.day); } //new Date(1,2,2004).happenedAfter(2003,11,2) == true //new Date(3,5,1900).happenedAfter(1901,2,12) == false //To determine if this Date happened after the date represented by the given numbers boolean happenedAfter( int y, int m, int d) { // this.month ... this.year ... this.day ... y ... m ... d return (this.year > y) || (this.month > m) || (this.day > d); } //2a //new Date(1,1,1900).delay() -> new Date(1,1,1901) //Produces a Date one year in the future from this date, to delay events Date delay() { // this.month...this.day...this.year return new Date(this.month,this.day,this.year+1); } } //Question 2 /* +------------------+ +-------------+ | Concert | |->| Date | +------------------+ | +-------------+ | Date occursOn |--+ | int month | | int seats | | int day | | String preformer | | int year | | int tickets | +-------------+ | int guests | +------------------+ */ class Concert { Date occursOn; int seats; String preformer; //b int tickets; int guests; Concert( Date occursOn, int seats, String preformer, int tickets, int guests) { this.occursOn = occursOn; this.seats=seats; this.preformer = preformer; this.tickets = tickets; this.guests = guests; } //a //new Concert(new Date(1,1,2001),3,"Bjork",2,1).postpone() -> new Concert(new Date(1,1,2002),3,"Bjork",2,1) //Produces a new concert representation, occuring on a later date Concert postpone() { //this.occursOn.DateMethod()... this.seats ... this.preformer ... this.tickets ... this.guests return new Concert(this.occursOn.delay(),this.seats, this.preformer,this.tickets,this.guests); } //c //new Concert(new Date(1,1,2002),3,"Bjork",2,1).available() == 0 //To report how many seats are available for this concert int available() { // this.occursOn.DateMethod() ... this.seats ... this.preformer ... this.tickets ... this.guests return this.seats - (this.tickets + this.guests); } //d //new Concert(new Date(1,1,2002),4,"Bjork",2,1).buyTicket() -> new Concert(new Date(1,1,2002),4,"Bjork",3,1) //To produce a concert representation with one more ticket purchased Concert buyTicket() { // this.occursOn.DateMethod() ... this.seats ... this.preformer ... this.tickets ... this.guests return new Concert(this.occursOn,this.seats,this.preformer,this.tickets+1,this.guests); } } //Question 3 /* +---------------------+ +----------------------+ | Car2 | +->| Person2 | +---------------------+ _| +----------------------+ | Person2 driver |--+| | Location destination |--+ | Person2 passenger |---+ +----------------------+ | | Location currentLoc |---+ | +---------------------+ | +------------------+ | | +----------+<-------------+ | | Location |<------------------------+ +----------+ | String x | | String y | +----------+ */ class Car2 { Person2 driver; Person2 passenger; Location currentLoc; Car2(Person2 driver, Person2 passenger, Location currentLoc) { this.driver = driver; this.passenger = passenger; this.currentLoc = currentLoc; } //a //new Car2(new Person(new Location("55","Ellis")),new Person(new Location("54","Ellis"),new Location("55","Ellis").atLocation() == true //new Car2(new Person(new Location("55","Ellis")),new Person(new Location("54","Ellis"),new Location("56","Elis").atLocation() == fa;se //To determine if this Car has reached either Person's location boolean atLocation() { //driver.PersonMethod() ... passenger.PersonMethod() .. currentLoc.locationMethod() ... return this.driver.atLocation(this.currentLoc) || this.passenger.atLocation(this.currentLoc); } //b //new Car2(new Person(new Location("55","Ellis")),new Person(new Location("54","Ellis"),new Location("55","Ellis").goDriversWay() // -> new Car2(new Person(new Location("55","Ellis")),new Person(new Location("55","Ellis"),new Location("55",Ellis") //To produce a Car where passenger and driver are both going to driver's location Car2 goDriversWay() { //driver.PersonMethod() ... passenger.PersonMethod() .. currentLoc.locationMethod() ... return new Car2(this.driver,this.passenger.goYourWay(this.driver),this.currentLoc); } //c //new Car2(new Person(new Location("55","Ellis")),new Person(new Location("54","Ellis"),new Location("55","Ellis").close() == true //new Car2(new Person(new Location("55","Ellis")),new Person(new Location("54","Ellis"),new Location("4th","Temple").close() == false //To determine if the Car is close to either destination boolean close() { //driver.PersonMethod() ... passenger.PersonMethod() .. currentLoc.locationMethod() ... return this.driver.close(this.currentLoc) || this.passenger.close(this.currentLoc); } } class Person2 { Location destination; Person2( Location destination ) { this.destination = destination; } //new Person(new Location("55","Green")).atLocation(new Location("44","Vine")) == false //new Person(new Location("55","green")).atLocation(new Location("55","green")) == true //Determine if this Person is at their location boolean atLocation(Location l) { // this.destination.LocationMethod( ... ) ... return this.destination.sameAs(l); } //new Person2(new Location("4","3").goYourWay(new Person(new Location("F","A"))) -> new Person(new Location("F","A")) //Produce a person going p's way Person2 goYourWay( Person2 p) { // this.destination.LocationMethod() .... p return new Person2(p.destination); } //new Person2(new Location("r","3").close(new Location("4","3")) == true //new Person2(new Location("2","2").close(new Location("3","54")) == false //Determine if this Person is near their destination boolean close( Location l) { // this.destination.LocationMethod( ) ... return this.destination.close(l); } } class Location { String x; String y; Location(String x, String y) { this.x = x; this.y = y; } //new Location("x","y").sameAs(new Location("x","y")) == true //new Location("x","z").sameAs(new Location("z","x")) == false //To determine if this location is the same as location l boolean sameAs( Location l) { //...this.x ... this.y ... return l.sameX(this.x) && l.sameY(this.y); } //new Location("x","y").close(new Location("x","y")) == true //new Location("x","z").close(new Location("z","x")) == false //To determine if this location is on the same street as l boolean close( Location l) { //... this.x ... this.y ... return l.sameX(this.x) || l.sameY(this.y); } //new Location("x","y").sameX("x") == true // new Location("y","y").sameX("x") == false //To determine if ox is the same as this.x boolean sameX( String oX) { // ...this.x ... this.y ... return this.x.equals(oX); } //new Location("x","x").sameY("x") == true // new Location("y","y").sameY("x") == false boolean sameY( String oY) { // ... this.x ... this.y ... return this.y.equals(oY); } } //Question 4 abstract class UCPerson { //To determine if this UCPerson may use the Gym abstract boolean canAccessGym(); //To report how much money this UCPerson owes abstract int moneyOwed(); } class Staff extends UCPerson { boolean paidFee; int fines; Staff(boolean paidFee, int fines) { this.paidFee = paidFee; this.fines = fines; } //new Staff(true,0).canAccessGym() == true //new Staff(false,0).canAccessGym() == false //new Staff(true,3).canAccessGym() == false //To determine if this Staff may use the Gym boolean canAccessGym() { //this.paidFee ... this.fines ... return this.paidFee && this.fines == 0; } ////new Staff(true,3).moneyOwed() == 3 //To report how much money this Staff owes int moneyOwed() { // ... this.paidFee ... this.fines ... return this.fines; } } class Student extends UCPerson { int tuitionPaid; int hoursEnrolled; Student( int tuitionPaid, int hoursEnrolled) { this.tuitionPaid = tuitionPaid; this.hoursEnrolled = hoursEnrolled; } //new Student( 5000, 12).canAccessGym() == true //new Student(3000,2).canAccessGym() == false //To determine if this Student may use the gym boolean canAccessGym() { //this.tuitionPaid...this.hoursEnrolled... return (this.tuitionPaid>=5000) && (this.hoursEnrolled>=12); } //new Student(500,12).moneyOwed() == 4500 //To report how much money this student owes int moneyOwed() { //this.tuitionPaid ... this.hoursEnrolled ... return 5000 - this.tuitionPaid; } } //challenge /* +------------------------------+ | Gym | +------------------------------+ | int capacity | | int occupancy | +------------------------------+ | boolean canEnter(UCPerson p) | +------------------------------+ */ class Gym { int capacity; int occupancy; Gym(int capacity, int occupancy) { this.capacity = capacity; this.occupancy = occupancy; } //new Gym(500,400).canEnter(new Staff(true,0)) == true //new Gym(500,500).canEnter(new Student(500,12)) == false boolean canEnter(UCPerson p) { //...this.capacity ... this.occupancy ... p.UCPersonMethod() ... return this.capacity!=this.occupancy && p.canAccessGym(); } } //Question 5 /* +----------------------------------+ | abstract Canine | +----------------------------------+ +----------------------------------+ | abstract int weighs() | | abstract boolean isThreat() | | abstract Canine feed(int weight) | +----------------------------------+ ^ | --------------------- | | +--------------------+ +--------------------+ | Wolf | | Poodle | +--------------------+ +--------------------+ | int weight | | int weight | +--------------------+ | String size | | int weighs() | | boolean abused | | boolean isThreat() | +--------------------+ | Canine feed(int w) | | int weighs() | +--------------------+ | boolean isThreat() | | Canine feed(int w) | +--------------------+ */ abstract class Canine { //To report the weight of this canine abstract int weighs(); //To determine if this Canine is a threat abstract boolean isThreat(); //To feed the canine, increasing weighs() abstract Canine feed(int weight); } class Wolf extends Canine { int weight; Wolf(int weight) { this.weight = weight; } //new Wolf(10).weighs() == 10 //To report the weight of this Wolf int weighs() { //this.weight ... return this.weight; } //new Wolf(10).isThreat() == true //To determine if this Wolf is a threat boolean isThreat() { //this.weight ... return true; } //new Wolf(1).feed(10) -> new Wolf(11) //To produce a new Wolf, that weighs w more than this one Canine feed(int w) { return new Wolf(this.weight + w); } } class Poodle extends Canine { String size; int weight; boolean abused; Poodle(String size, int weight, boolean abused) { this.size = size; this.weight = weight; this.abused = abused; } //new Poodle("teacup",3,false).weighs() ==3 //To report how much this Poodle weighs int weighs() { // this.weight ... this.size ... this.abused return this.weight; } //new Poodle("teacup",3,false).isThreat() == false //new Poodle("large",3,true).isThreat() == true //new Poodle("large",4,false).isThreat() == false boolean isThreat() { //this.weight...this.size...this.abused... return this.abused && this.size.equals("large"); } //new Poodle("teacup",3,false).feed(1) -> new Poodle("teacup",4,false) //To produce a Poodle that weighs w more than this one Canine feed(int w) { //this.size...this.weight...this.abused return new Poodle(this.size,this.weight + w, this.abused); } }