import testing.*; /* Answer to Question 1 part 1 abstract class Furniture { //To report the price of this Furniture abstract double price(); //To report the cost of shipping this Furniture abstract double shipping( boolean installation) ; //To report the total amount owed abstract double totalBill( boolean installation); } class Chair extends Furniture { boolean hasWheels; String covering; Chair( boolean hasWheels, String covering ) { this.hasWheels = hasWheels; this.covering = covering; } //new Chair(true, "leather").price() -> 105.95 //new Chair(false, "leather").price() -> 88.95 //new Chair(true, "tweed").price() -> 49.95 //To report the price of this Chair double price() { // ... this.hasWheels ... this.covering ... if (this.hasWheels && this.covering.equals("leather") ) return 105.95; else if (this.covering.equals("leather") ) return 88.95; else return 49.95; } //new Chair( true, "leather").shipping(false) -> 50 //new Chair( true, "leather").shipping(true) -> 100 //To report the cost of shipping this Chair double shipping( boolean installation ) { // ... this.hasWheels ... this.covering ... if (installation) return 100; else return 50; } //new Chair( true, "leather").totalBill(false) -> 155.95 //To report the total owed for this Chair double totalBill( boolean installation ) { // ... this.hasWheels ... this.covering ... return this.price() + this.shipping(installation); } } class Sofa extends Furniture { int numCushions; String covering; Sofa( int numCushions, String covering ) { this.numCushions = numCushions; this.covering = covering; } //new Sofa(2, "leather").price() -> 600 //new Sofa(3, "leather").price() -> 800 //new Sofa(3, "tweed").price() -> 700 //To report the price of this Sofa double price() { // ... this.numCushions ... this.covering ... return (200 * this.numCushions) + this.coverPrice(); } //new Sofa(2, "leather").coverPrice() -> 200 //new Sofa(5, "tweed").coverPrice() -> 100 double coverPrice() { // this.numCusions ... this.covering ... if (this.covering.equals("leather")) return 200; else return 100; } //new Sofa( 4, "leather").shipping(false) -> 50 //new Sofa( 4, "leather").shipping(true) -> 100 //To report the cost of shipping this sofa double shipping( boolean installation ) { // ... this.numCushions ... this.covering ... if (installation) return 100; else return 50; } //new Sofa( 3, "leather").totalBill(false) -> 850 //To report the total owed for this Sofa double totalBill( boolean installation ) { // ... this.hasWheels ... this.covering ... return this.price() + this.shipping(installation); } } */ /*Answer to Question1 part 2 The methods shipping and totalBill differ only in the template and examples, both have a covering field. */ //Answer to Question 1 part 3 follows abstract class Furniture { String covering; //To report the price of this Furniture abstract double price(); //new Chair( true, "leather").shipping(false) -> 50 //new Chair( true, "leather").shipping(true) -> 100 //To report the cost of shipping this Furniture double shipping( boolean installation ) { // ... this.covering ... if (installation) return 100; else return 50; } //new Sofa( 3, "leather").totalBill(true) -> 900 //new Chair( true, "leather").totalBill(false) -> 155.95 //To report the total owed for this Furniture double totalBill( boolean installation ) { // ... this.covering ... return this.price() + this.shipping(installation); } } class Chair extends Furniture { boolean hasWheels; Chair( boolean hasWheels, String covering ) { this.hasWheels = hasWheels; this.covering = covering; } //new Chair(true, "leather").price() -> 105.95 //new Chair(false, "leather").price() -> 88.95 //new Chair(true, "tweed").price() -> 49.95 //To report the price of this Chair double price() { // ... this.hasWheels ... this.covering ... if (this.hasWheels && this.covering.equals("leather") ) return 105.95; else if (this.covering.equals("leather") ) return 88.95; else return 49.95; } } class Sofa extends Furniture { int numCushions; Sofa( int numCushions, String covering ) { this.numCushions = numCushions; this.covering = covering; } //new Sofa(2, "leather").price() -> 600 //new Sofa(3, "leather").price() -> 800 //new Sofa(3, "tweed").price() -> 700 //To report the price of this Sofa double price() { // ... this.numCushions ... this.covering ... return (200 * this.numCushions) + this.coverPrice(); } //new Sofa(2, "leather").coverPrice() -> 200 //new Sofa(5, "tweed").coverPrice() -> 100 double coverPrice() { // this.numCusions ... this.covering ... if (this.covering.equals("leather")) return 200; else return 100; } } class FurnitureTests extends Test { Chair c1 = new Chair(true, "leather"); Chair c2 = new Chair(false, "leather"); Chair c3 = new Chair(true, "tweed"); Sofa s1 = new Sofa(2, "leather"); Sofa s2 = new Sofa(3, "leather"); Sofa s3 = new Sofa(3, "tweed"); TestResult testPriceChairOne() { return this.compareInexacts( c1.price(), 105.95 , .01); } TestResult testPriceChairTwo() { return this.compareInexacts( c2.price(), 88.95, .01); } TestResult testPriceChairThree() { return this.compareInexacts( c3.price(), 49.95, .01); } TestResult testPriceSofaOne() { return this.compareInexacts( s1.price(), 600, .01); } TestResult testPriceSofaTwo() { return this.compareInexacts( s2.price(), 800, .01); } TestResult testPriceSofaThree() { return this.compareInexacts( s3.price(), 700, .01); } TestResult testShippingOne() { return this.compareInexacts( s1.shipping(false), 50, .01); } TestResult testShippingTwo() { return this.compareInexacts( c2.shipping(true), 100, .01); } TestResult testTotalBillOne() { return this.compareInexacts( s1.totalBill(false), 650, .01); } } //Question 2 /* +----------------+ | BankAccount | +----------------+ | int idNum | | double balance | | double loans | +----------------+ */ class BankAccount { int idNum; double balance; double loans; BankAccount(int idNum, double balance, double loans) { this.idNum = idNum; this.balance = balance; this.loans = loans; } //To augment the balance of this Account by amount : Note modifies balance //new BankAccount(1,1.00,0.00).deposit(2.00) -> void // BankAccount(idNum = 1, balance=1.00,loans=0.0) => BankAccount(idNum=1,balance=3.0,loans=0.0) void deposit( double amount ) { // ... this.idNum ... this.balance ... this.loans ... this.balance = balance + amount; } //new BankAccount(1,1.00,0.00).mightPayLoan() => new BankAccount(1,1.0,0) //new BankAccount(2,10.0,5.55).mightPayLoan() -> new BankAccount(1,4.45,0) //To create a new BankAccount where the balance is used to offset the loan BankAccount mightPayLoan() { // ... this.idNum ... this.balance ... this.loans ... if (this.loans <= 0) return this; else if (this.balance >= this.loans) return new BankAccount(this.idNum, this.balance-this.loans, 0); else return new BankAccount(this.idNum, 0, this.loans- this.balance); } } class BankAccountTests extends Test { TestResult testMightPayLoanOne() { return this.compareObjects(new BankAccount(1,1.00,0.00).mightPayLoan(),new BankAccount(1,1.0,0)); } TestResult testMightPayLoanTwo() { return this.compareObjects(new BankAccount(2,10.0,5.55).mightPayLoan(),new BankAccount(1,4.45,0)); } } //Question 3 /* +--------------+ | Pixel | +--------------+ | double red | | double green | | double blue | +--------------+ */ class Pixel { double red; double green; double blue; Pixel(double red, double green, double blue) { this.red = red; this.green = green; this.blue = blue; } //new Pixel(.14,.4,.3).changeRed(.54) -> void //Pixel( red = .14, green =.4, blue =.3) => Pixel(red=.54,green=.4, blue =.3) //To change the red pixels' intensity to intensity void changeRed( double intensity ) { // ... this.red ... this.green ... this.blue this.red = intensity; } //Pixel p = new Pixel(1,0,0); //p.condModRed(1,.5) //p => Pixel( .5,0,0) //To conditionally change the value of this.red to the value of to void condModRed( double from, double to) { // ... this.red ... this.green ... this.blue ... from ... to if ((this.red - from) < 0.001) this.red = to; else this.red = from; } } abstract class Screen { //To modify all pixels on the screen with from red value to to abstract void modifyRed( double from, double to ); } class EmptyS extends Screen { //To modify all pixels on the empty screen with from red value to to void modifyRed( double from, double to ) {} } class LargerS extends Screen { Pixel p; Screen rest; LargerS ( Pixel p, Screen rest ) { this.p = p; this.rest = rest; } //To modify all pixels on the screen with from red value to to //Note: will potentially change p //Larger l = new Larger(new Pixel(0,0,0), new Empty()); //l.modifyRed(0,1.0) -> void //l => Larger( Pixel(1.0,0,0), Empty()) void modifyRed( double from, double to ) { // ... this.p.PixelMethod() ... this.rest.ScreenMethod() ... from ... to this.p.condModRed( from, to); this.rest.modifyRed(from, to); } } // Question 4 class StudentQueue { StudentLst queue = new EmptySL(); StudentQueue() { } //StudentQueue q = new StudentQueue(); // q.push(new Student("Sally",3.5)) // q.pop() -> new Student("Sally",3.5) //note: modifies this.queue, to be one element smaller // queue = new Larger(new Student("Sally",3.5),new EmptySL()) -> // queue = new EmptySL() // Removes and returns the top element -- should only be called on non-empty Student pop() { // ... this.queue.StudentLstMethod() ... Student top = this.queue.firstElt(); this.queue = this.queue.getNext(); return top; } //new StudentQueue().empty() == true; //Determines if this queue is empty or not boolean empty() { // ... this.queue.studentLstMethod() ... return this.queue.isEmpty(); } //new StudentQueue().push( new Student("Sally",3.5) ) -> void // StudentQueue( queue = new EmptySL() ) -> // StudentQueue(queue = new LargerSL(new Student("Sally",3.5),new EMptySL()) //Adds s to the queue, modifying this.queue void push( Student s ) { // ... this.queue.StudentLstMethod() ... this.queue = this.queue.add(s); } } abstract class StudentLst { //To return the first element of this StudentLst abstract Student firstElt(); //To return the next element of this StudentLst abstract StudentLst getNext(); //To determine if this studentLst is empty abstract boolean isEmpty(); //To add s to this StudentLst StudentLst add( Student s ) { // ... this ... s return new LargerSL( s, this ); } } class EmptySL extends StudentLst{ //Should not be called -- we'll talk about this later Student firstElt() { return new Student("Place holder",0); } //Should not be called -- we'll talk about this later too StudentLst getNext() { return this; } //new EmptySL().isEmpty() == true //To return whether or not this EmptySL is empty boolean isEmpty() { return true; } } class LargerSL extends StudentLst { Student first; StudentLst rest; LargerSL( Student first, StudentLst rest) { this.first = first; this.rest = rest; } //To return the first element of this LargerSL Student firstElt() { // ... this.first ... this.rest ... return this.first; } //To return the rest of this LargerSL StudentLst getNext() { // ... this.rest ... this.first ... return this.rest; } //To return whether this LargerSL is empty boolean isEmpty() { // ... this.rest ... this.first... return false; } } class Student { String name; double grades; Student( String name, double grades ) { this.name = name; this.grades = grades; } //new Student("sally",3.5).gpa -> 3.5 //To calculate the gpa of this student double gpa() { // this.name ... this.grades ... return this.grades; } } class Roster { StudentQueue roll; Roster() { this.roll = new StudentQueue(); } //Roster r = new Roster(); // r.enroll(new Student("Sally",3.5)) => void // r.roll.pop() => Student("Sally",3.5) //Adds a student to the role queue // Note modifies roll void enroll( Student s ) { //this.roll.StudentQueueMethod() ... this.roll.push( s ); } //r.totalGpa() -> 0 //r.enroll(new Student("Sally",3.5)) //r.enroll(new Student("Josh",2.5)) //r.totalGpa() -> 6.0 //Adds up the gpa of all students on the roll // Note: empties the roll double totalGpa() { // this.roll.StudentQueueMethod() ... if (this.roll.empty()) return 0; else return this.roll.pop().gpa() + totalGpa(); } //r.passing() -> true //r.enroll(new Student("Sally",3.5)) //r.enroll(new Student("Josh",0.5)) //r.passing() -> false //Determines if all students on the roll have gpa over 1 // Note: empties the roll boolean allPassing() { // ... this.roll.StudentQueueMethod() ... if (this.roll.empty()) return true; else return (this.roll.pop().gpa() > 1) && allPassing(); } } class RosterTest extends Test { Roster r = new Roster(); TestResult testEnroll() { r.enroll(new Student("Sally",3.5)); return this.compareObjects(r.roll.pop(), new Student("Sally",3.5)); } TestResult testTotalGpaMt() { return this.compareInexacts(r.totalGpa(), 0, 0.01); } TestResult testTotalGpa() { r.enroll(new Student("Sally",3.5)); r.enroll(new Student("Josh",2.5)); return this.compareInexacts(r.totalGpa(), 6.0, 0.01); } TestResult testAllPassingMt() { return this.compare(r.allPassing() ,true); } TestResult testAllPassing() { r.enroll(new Student("Sally",3.5)); r.enroll(new Student("Josh",0.5)); return this.compare(r.allPassing(), false); } } /* Question 5 To implement a priority queue, using the current representation, you could modify push so that instead of using add, as it currently does, it would use a sorting insert method on the list. This would also have to be added. This way, the first element is always going to be the smallest. */