Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎notes‎ > ‎

2017-10-05 HW

posted Oct 5, 2017, 10:03 AM by Konstantinovich Samuel   [ updated Oct 5, 2017, 2:02 PM ]
1. Did you read the how to CS at Stuy?
2. What is the point of the homework in this class?

To make sure people improve their logic and looping skills over time, here is a passive homework assignment: 
Complete 1 problem per day (2 on a weekend) until you have 18 complete in each section.)
String-2 [21 total, you did 8]
Array-2 [32 total, you did 7]

This is separate from other assignments, so I suggest you do extra ones when you have little to no CS homework!

Array-3


What do we want to do with the homework classes?


UML:

Associations
There are several types of associations (relationships) between classes that we will discuss. 

For now, we can look at:

Aggregation:  The item contained inside does exist outside of the container.  Neither "owns" each other, they exist separately but are related in some way.
  A student has a collection of courses, and courses have a collection of students. 
  A course can have an instructor, instructors have one or more courses. 
  A company has a collection of employees, but they exist outside of the company.

Composition: (Ownership) The item contained inside doesn't exist outside of the container. 
  A house has rooms. You cannot have the rooms without the house. 
  A company has a collection of accounts, the accounts do not exist without the company.  

Sometimes it is not so clear what we want. Everything in computer science can depend on your goals or your priorities when modeling a situation.

Cars have an Engine.
CAN the Engine exist outside of the car? Sure, we can track them separately (Aggregation)
CAN we treat the Engine as being a dedicated part of the car that doesn't get moved separately? Sure, we can treat it as one unit (Composition)

In a computer model, it depends on your requirements. 
-If you need to track the Engine as an object separate from cars, then you can treat it like an Aggregation. (e.g. A car factory that deals with inventory might have a pile of engines)
-If you just own a bunch of cars, and never swap engines (most businesses don't) then you can treat it like a Composition. (e.g. A car dealer, or a car owner)


Triangle vs Point

Does the Triangle OWN the point?
Does the Point exist outside of the Triangle?

Aggregation:
What if we change the point? 
What if the point is Immutable?

Composition:
How do we prevent a point from being shared?


Static vs non-static methods:

Given initialized points in a main:

public class demo{ 

  /*other methods here*/

  public static void main(String[]args){
    Point P1 = new Point(1,2);
    Point P2 = new Point(2,2);

    //1. locally declared static method
    distanceBetween(P1,P2);
    //This is not a good way to deal with distance, need to write it in multiple places
    //You have to re-write this when used by several classses!
    
    //2. static method inside a class 
    Point.distanceBetween(P1,P2);
    //This is a better way to organize distance. 
    //A static method is put it in the related class (Point)
    //You do not have to re-write this when used by several classses!

    //3. nonstatic method in a class
    P1.distanceTo(P2);
    //Similar to 3, a little more convinient syntactically.
  }
}

Homework:
Get the 3 different distance functions working. You need a simple point class to do this.
Bring in a digital copy since you should compile to test it.

Comments