CSCI 1103 Lab11: Review
- Due: 11:59pm Monday 11/27/2017
- Approximately 0.83% of total grade
- Submit to Canvas
- You may work with one partner on this lab but both partners must submit files and be physically present for Check-offs
- Lab exercises are open resource/open collaboration. You may freely discuss lab topics with other members of the class.
CODE DISTRIBUTION: lab11-code.zip
- Download the code distribution every lab
- See further setup instructions below
CHANGELOG:
1 Rationale
This lab does not introduce any additional new material and is intended mainly for review. Complete the previous lab on stacks if you have not done so already and submit it. Then proceed to the review problems to prepare for the upcoming exam.
There is no check-off for this lab. Instead, full credit will be
given for submitting answers to the questions given below in the file
QUESTIONS.txt
. The deadline for submission is by the next lab. You
do not need to complete all review problems to get full credit for the
lab.
Associated Reading
The following material from Eck will be covered on Exam 2 though it builds on material prior to that in the course.
Week | Dates | Lecture | Eck Book |
---|---|---|---|
7 | 10/16 | Methods / Functions | Ch 4 |
8 | 10/23 | Basic Objects / Classes | Ch 5 |
9 | 10/30 | Classes with Methods | Ch 5 |
10 | 11/06 | Object-oriented Classes | Ch 5 |
11 | 11/13 | Simple Data Structures | Ch 8.3.3, 7.4, 9.3 |
2 Download Lab Code and Setup
As in Lab01, download the code pack linked at the top of the page. Unzip this which will create a folder and create your files in that folder.
File | State | Notes |
---|---|---|
PARTNERS.txt | Edit | Fill in your name and the name of your partner in this text file |
QUESTIONS.txt | Edit | Write your answers to review problems in this file and submit |
Critter.java | Provided/Edit | Class for Review Problem 1 |
CritCompare.java | Provided/Edit | Class for Review Problem 1 |
3 Complete Lab10 on Stacks
If you have not completed work on the FixedStack
and GrowableStack
from lab10, do so before proceeding to review. Check-offs and
submission on this lab will be allowed through the end of the day.
Importantly, make sure you understand how to make the GrowableStack
appear to have an unlimited capacity by allocating larger arrays,
copying elements, and referring to the new array as elements are
push()
'd in into it. Be able to draw pictures of how this process
looks.
4 Review Problem 1: Critter Troubles
Consider the Critter
class included with the lab code and answer questions about it.
1: // Represent a creature of some kind 2: public class Critter{ 3: private String kind; 4: private String eats; 5: private double ferocity; 6: 7: public Critter(String kind, double ferocity){ 8: this.kind = kind; 9: ferocity = ferocity; 10: } 11: 12: public String getEats(){ 13: return this.eats; 14: } 15: 16: public void addToMenu(String kind){ 17: this.eats += kind + " "; 18: } 19: 20: public static void encounter(Critter a, Critter b){ 21: if(a.ferocity > b.ferocity){ 22: System.out.printf("%s would beat %s\n", 23: a.kind,b.kind); 24: } 25: else if(a.ferocity < b.ferocity){ 26: System.out.printf("%s would beat %s\n", 27: b.kind,a.kind); 28: } 29: else{ 30: System.out.printf("%s and %s grab falafel together\n", 31: b.kind,a.kind); 32: 33: } 34: } 35: }
4.1 Part A
Examine the constructor as it stands now. Draw a memory diagram of the following instance of the class (you do not need to include the picture in your submission but may have to draw one on an exam).
public void main(){ Critter hb = new Critter("Honey Badger",5.0); ... }
Does anything look wrong about the picture compared to how objects are typically initialized?
4.2 Part B
In an interactive session, the following code is run which produces somewhat unexpected results.
> Critter hb = new Critter("Honey Badger",5.0); > hb.addToMenu("snakes") > hb.getEats() "null snakes"
Explain why the word null
appears in the result of the getEats()
method. Relate this to a problem in with the constructor about and
describe fix in the constructor
4.3 Part C
An interactive session shows the following strange result.
> Critter ra = new Critter("Rabbit",1.0); > Critter fo = new Critter("Fox",3.0); > Critter hb = new Critter("Honey Badger",5.0); > Critter.encounter(fo, ra) Rabbit and Fox grab falafel together // Fox should beat Rabbit > Critter.encounter(hb, ra) Rabbit and Honey Badger grab falafel together // Honey Badger should beat Rabbit > Critter.encounter(hb, fo) Fox and Honey Badger grab falafel together // Honey Badger should beat Fox
The encounter()
method appears to be broken. Identify a problem with
the constructor which is causing this problems later with
encounter()
. Describe how to fix it.
4.4 Part D
Examine the encounter()
method. It makes use of private variables of
the Critter
class like kind
and ferocity
. Compiling the
Critter.java
file appears to work fine as in
> javac Critter.java > ls Critter.class // check for compiled class Critter.class // it is present
Compare to attempting to compile the separate class CritCompare.java
that has the identical method in it:
1: public class CritCompare{ 2: public static void encounter(Critter a, Critter b){ 3: if(a.ferocity > b.ferocity){ 4: System.out.printf("%s would beat %s\n", 5: a.kind,b.kind); 6: } 7: else if(a.ferocity < b.ferocity){ 8: System.out.printf("%s would beat %s\n", 9: b.kind,a.kind); 10: } 11: else{ 12: System.out.printf("%s and %s grab falafel together\n", 13: b.kind,a.kind); 14: 15: } 16: } 17: }
This leads to lots of errors:
> javac CritCompare.java CritCompare.java:3: error: .... ... 10 errors
Explain why compiling Critter.java
works but CritCompare
fails and
the meaning of the private
access modifier.
5 Review Problem 2: Portfolio Additions
5.1 Part A
Draw a memory diagram of the following interactive session of using
the Portfolio
class.
> Portfolio barren = new Portfolio("Barren", 3); > barren.deposit(200.00); > barren.addStock("MSFT",10.00,"Microsoft"); > barren.addStock("AAPL",50.00,"Apple."); > barren.buyShares("MSFT", 4); > barren.buyShares("APPL", 2); > barren.buyShares("GOOGL", 3);
5.2 Part B
Add a method to the Portfolio
class with the following prototype.
public class Portfolio{ // Search the array of stocks for the given symbol and remove that // stock from the array by sellig off all shares of it. If any // shares exist, add the sale profit to the portfolio cash. After // selling off all shares, eliminate the stock from the array by // shifting all remaining stocks back in the array and decreasing // the size. Return true if a stock is removed. If no stock with the // requested symbol exists, do not modify the portfolio and return // false. public boolean removeStock(String symbol){ // YOUR CODE HERE }
5.3 Part C
Most actual stock portfolios change in value because the price of
stocks is not fixed. Instead, stock prices fluctuate over time.
To model this, consider the addition of the following method to the
Portfolio
class.
public class Portfolio{ // Search the array of stocks for the given symbol and change its // price to the given newPrice. If a price is changed, return // true. If the requested symbol is not present, return false. public boolean updateStockPrice(String symbol, double newPrice){ // YOUR CODE HERE }
- Describe how one might implement this method in
Portfolio
perhaps giving code to do it. - You should encounter one major difficulty in your implementation of
the
updateStockPrice()
. Describe this and an associated change that would be required in theStock
class.
6 Getting Credit for this Lab
There is no need to demonstrate / check-off your lab with staff. Discuss review problems with them to improve your understanding.
Full credit for the lab will be given for submitting your answers in
QUESTIONS.txt
to canvas.
On Canvas:
- Click on the Assignments section
- Click on the appropriate link for this lab
- Scroll down to "Attach a File"
- Click "Browse My Computer"
- Select your
QUESTIONS.txt
file and press OK