Category Archives: Computer Science

Walk-the-lake

Monsoon rains have created a lake in the middle of Santong Island.  Furthermore, that lake contains an island. Two Jeroos were separated by the rains.  One is on the main part of Santong Island, but the other is on the island in the middle of the lake.  The goal of this program is to have each Jeroo explore the shoreline of the lake.

  • Each Jeroo starts with one flower and the lake immediately to its right.
  • Each Jeroo starts by planting a flower then traveling, keeping the lake on its right, until it returns to the flower. The Jeroo then picks the flower.
  • (only plant 1 flower, at starting point…and make 1 lap until you are back at that flower)
  • Have the Jeroo on the main part of Santong Island walk the shore first.
  • then have the one on the lake’s island walk the shore

Download Walk the Lake  (This has several maps, choose any 1)

walk-the-lake1

 

 

Acknowledgement

 

This problem is adapted from one that was created originally by Erica Eddy of The University of Wisconsin – Parkside.

 

Or you can try walk the lake using loops using this map and Jeroo assignment

 

Bank Class

Bank Class

You have a lot of flexibility in creating this class, but here is what it should be able to do

Functionality that you *must* implement.

It’s up to you to decide how to implement the functionality below. You will only lose credit if your methods clearly make no sense, have bad variable names/method names.

  • add an Account  (of any type)
  • remove an account by
    • account id
    • owner (a Person object)

The functionality above ^^ can be implemented in any efficient way that makes sense, but it must be implemented. I am leaving that part up to you!


  • Some Specific Required Methods
    • public ArrayList<Person>  topAccountOwners(double cutoff)
      • this method returns an ArrayList of the owners whose accounts have at least cutoff  amount of balance
    • public void nightlyUpdate()
      • this runs through the accounts and if account is a SavingsAccount , then it calls that object’s recordDailyBalance()  method (You are going to have to use instanceOf  and then cast those  accounts to a  SavingsAccount )
    • public  Account topBalance()   ; returns the Account  object with the greatest balance
    • public  Account[] allWithinRange(double min, double max) returns an array of Accounts whose balances range from min  to max , inclusive
    • public  boolean transfer( double amount, int accountIdFrom , int accountIdTo) ;  this method transfers  money from the one account to the other. If the accountIdFrom  does not have enough $$, this method returns false.
    • public  void monthlyUpdate()
      • this runs through the accounts and if
      • account is a SavingsAccount , then it calls that object’s updateInterestPayment() method
    • ** extra credit ArrayList<Account> sortByAmount()  
      •   this method returns an arraylist of Accounts sorted by amount in the account

 

account_with_bank_no_labels

 

The Case for Abstract Classes and Methods

shape_abstract_class

 

Shape

download-bttn Shape Class

Constructor 

  • public Shape( int _col)

instance Variables

  • protected int color

Accesor Methods

  • public abstract double getArea(); //returns the area of the triangle
  • public int getColor()
  • public void render()  { System.out.println(“rendering shape” ) ; }

 

 


Now, let’s refactor our class relationships to represent our new Iheritance Tree

  1. refactor all subclasses to reflect new class hierarchy
  2. Update our new InteractiveMath class –InteractiveMath3

Circle Class

Create the following class

Circle

download-bttnCircle
Constructor 

  • public Circle( double _radius, int _col)

instance Variables

  • double radius
  • int color

Accesor Methods

  • public double getArea(); //returns the area of the triangle
  • public double getCircum() ;// returns the circumference

What should Circle Extend?

circle_should_extend_what

Answer!

Constructors And Inheritance

What relationships exist between super and sub classes?

 

  • How can we use superclasses to improve ourInteractiveMath2 code?
    • To be implemented in  computeNetArea() method
    • How can an element in the Polygon2 ‘ list that  is a RightTriangle2 call the getHypotenuse() method?
      • To be implemented in the new method void printDetails() which should , among other , things print the hypotenuse length for any RightTriangle2’s?
  • What is Inherited by subclasses?
    • Constructors?
    • instance variables
    • static variables?

 Next: What if we want to add a Circle Class

SuperClasses are Great II

How can we improve our current class structure?right_triangle_rectangle

 

 

 

 

 

 

 

We are going to create a second “Generation” of classes and will denote this change by appending a ‘2’ to the names of all classes in this “generation”

right_triangle_extends_polygon

 

Class: Polygon2 (superclass of Triangle2, Rectangle2)  Polygon2download-bttn

 

Constructor 

public Polygon2(int _col, double _width, double _height, int _sides)

instance Variables

  • private int color;
  • private int numSides
  • protected double width;
  • protected double height

Methods

  • public int getColor()
  • public double getWidth()
  • public double getHeight()

Class: Rectangle2 extends Polygon2  Rectangle2download-bttn

instance Variables

None needed (see superclass)

Accesor Methods //override each of the superclass

  • public double getArea(); //returns the area of the triangle

Class: RightTriangle2 extends Polygon2  RightTriangle2download-bttn

instance Variables

None needed (see superclass)

Accesor Methods //override each of the superclass

  • public double getArea(); //returns the area of the triangle
  • public double getHypotenuse()

InteractiveMath2   InteractiveMath2download-bttn

 

Next: What is inherited?

isLeapYear assignment

Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100; the centurial years that are exactly divisible by 400 are still leap years. For example, the year 1900 was not a leap year; the year 2000 was a leap year

 

 Call return value
 isLeapYear(100)  false
 isLeapYear(4)  true
 isLeapYear(104)  true
 isLeapYear(400)  true
 isLeapYear(800)  true
 isLeapYear(5)  false
 isLeapYear(803)  false

 

 

Seconds Since Birthdate

I . write bool isLeapYear( int year) : returns true or false depending on whether or not input is a leap year.

 

II. write a function called int secondsSince( int usersBYear, int usersBmonth, int usersBDay)  .

This function should return the number of seconds since that bithdate (year, month, day)

III.  In Main: use cout and cin to ask user for their bithdate  and then display how many seconds since that person’s bithdate

 

IV.  Determine how many seconds have occurred since 0/0/0 .

 

download-bttn Starting Code