New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here


by: Taylor Kahl

Week3notes.pdf CSC 2310

Taylor Kahl
GPA 4.21

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

Week 3 of lecture material: finishing chapter 8, starting chapter 9. Also includes the solution to a problem our professor asked in class about writing a Class to calculate change owed in a transa...
Princliples of Computer Programming
Kebina Manandhar
Class Notes
25 ?




Popular in Princliples of Computer Programming

Popular in ComputerScienence

This 10 page Class Notes was uploaded by Taylor Kahl on Saturday January 30, 2016. The Class Notes belongs to CSC 2310 at Georgia State University taught by Kebina Manandhar in Winter 2016. Since its upload, it has received 78 views. For similar materials see Princliples of Computer Programming in ComputerScienence at Georgia State University.

Popular in ComputerScienence


Reviews for Week3notes.pdf


Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 01/30/16
From Monday, 1/25 lecture – this section wraps up chapter 8 and includes the code to a problem our professor presented in class Static keyword (I defined this last week, but I’ll go more in depth here)  So far, the classes we’re creating have used fields and methods WITHOUT the keyword “static”  Fields and methods can be static or non-static o Remember, a when a class has a non-static field/method, each object of that class has its own copy  Example: in the Point class, we had the fields int x and int y  Every Point object had its own x and y  Static fields and methods are part of the class – shared by all objects (each object does NOT have its own copy)  Static methods don’t have an implicit parameter – you can’t use the this keyword  Calling a static method from outside its class: o ClassName.MethodName()  Calling a non-static method from outside its class: o ObjectName.MethodName() o the object here is the implicit parameter  All objects within the class can access and modify static fields o Accessing a static field from inside the class: fieldName = value; o Accessing a static field from outside the class (only if it’s public): ClassName.fieldName = value;  When is the static keyword useful?  Let’s say we want to create a BankAccount class that gives each new account a unique ID public class BankAccount { private static int objectCount = 0; private int id; //a field for BankAccount objects public BankAccount() { //a constructor for new BankAccount objects objectCount++; //when the constructor is called, objectCount changes id = objectCount; } 1 bank account will have ID 1, 2 will have ID 2, and so on  (still in BankAccount class) a method to find how many BankAccount objects have been created: public static int getNumAccounts() { return objectCount; } Because the integer objectCount is static, every field and method can access or change it Task our professor gave us in class : Write a class that can find how much change is owed to you after you spend a certain amount of money at a store. Example: Total cost: $5.90. You pay: $20. Change owed: $14.10. I’m posting my Change class and then my client code, in that order. Of course, there are multiple ways to write this code – this is mine. I strongly recommend trying to write the code yourself. Then check my solution if you get really stuck. It took me a lot of trial and error, but that’s the only way I really learn this stuff! public class Change { private int dollars; //dollars and cents are the fields for Change objects private int cents; //private so they can’t be changed outside this class public Change(int x, int y) { //a constructor accepting 2 parameters for new Change objects dollars=x; cents=y; } public String toString() { //this rewrites the fields dollars and cents into a String String amount = "$"+dollars+"."; if ((cents/10)<1) { //in case cents only has 1 digit; we want it to have 2 digits amount+="0"+cents; //concatenating digits onto the String amount } else { amount+=cents; } return amount; } public int getDollars() { //dollars is private, so we can use this method to access it return dollars; } public int getCents() { return cents; } public void difference(Change amtPayed, Change changeOwed) { //calculates difference int diffDollars = (amtPayed.dollars - dollars); //in these 2 lines, dollars and cents refers to the fields int diffCents = (amtPayed.cents - cents); of amtOwed, which is passed as the implicit parameter in the client code when calling the method difference. if (diffCents<0) { //like how subtracting 90 cents from 00 cents gives a negative number diffDollars--; diffCents=Math.abs(diffCents); //absolute value } changeOwed.dollars=diffDollars; //assign the difference to the fields for changeOwed changeOwed.cents=diffCents; } } ***As it turns out, I didn’t actually need to use the getDollars() or getCents() methods, but methods like that are often useful so you can access private fields from outside the class they belong to public class ChangeClient { //to use the Change class public static void main(String[] args) { Change amtOwed = new Change(5, 90); //constructing objects and initializing their fields Change amtPayed = new Change(20, 0); Change changeOwed = new Change(0, 0); //set to 0 for now, fill it in later amtOwed.difference(amtPayed, changeOwed); //amtOwed is implicit parameter System.out.print("Change owed = "+changeOwed.toString()); //convert changeOwed to a String } } The output produces: Change owed = $14.90 From Wednesday, 1/27 lecture : Chapter 9: Inheritance and Interfaces Inheritance: Inheritance takes advantage of code reuse, allowing you to write code once and use it in multiple ways  Instance hierarchy – classes connected in is-a relationships that share common code  is-a relationship – 2 categories where one is a specialized version of another o Example: Every computer science major is a college student  Extended example: Employees at a law firm  “Employee” is the major category with the following subdivisions: o Lawyer o Marketer o Secretary  Legal secretary (subdivision of secretary category)  All employees have general rules and behaviors  All subdivisions have their own rules and behaviors that add to or change the employee code  Benefits of using inheritance: o You can specify a broad set of rules that apply to many related groups; you can change those rules once and the change will affect all other groups  We can think of “Employee” and all its subdivisions as related classes  These are the rules for employees: o work 40 hours/week o earn $40,000 a year o 2 weeks paid vacation  Here’s a sample Employee class: public class Employee { public int getHours() { return 40; } public double getSalary() { return 40000.0 } public int getVacationDays() { return 10; } }  Secretaries are employees, so they have all the characteristics of the employee class o with the added characteristic that they take dictation  We could write the Secretary class by rewriting everything from the Employee class and adding a takeDictation method… o But that would be redundant!! o Instead of rewriting the Employee class, the Secretary class can inherit it  Inheritance allows one class to inherit the states and behaviors of another o Superclass – parent class (original) o Subclass – child class (new)  A subclass may have only 1 superclass  But 1 superclass can be extended to many subclasses  Syntax for inheritance, using extend keyword: o public class [name] extends [superclassName] {  ex: Secretary class: public class Secretary extends Employee { public void takeDictation() { } } Secretary inherits all the methods from Employee, and we added a new one Overriding Methods:  What if one of the subclasses needs to change code from its inherited class?  Overriding – writing a new method to replace an inherited one o Example: Lawyers have 3 weeks of vacation, not 2 o We need to change the getVacationDays() method; just rewrite it w/ the same name public class Lawyer extends Employee { public int getVacationDays() { return 15; } } This method overrides getVacationDays() from Employee o Example: Legal Secretaries make $5000 more than other employees o LegalSecretary can be a subclass of Secretary public class LegalSecretary extends Secretary { public double getSalary() { return 45000.0 } } LegalSecretary inherits Secretary and overrides its getSalary() method Interacting with the Superclass:  What if everyone at the law firm gets a $10000 raise?  Then we’d have to change the getSalary() method in Employee AND in LegalSecretary  Instead, LegalSecretary can refer to the getSalary() method in Employee, even though we overrode it by writing a new one o This means that changing a method in Employee would change it in its subclasses too  super keyword – refers to a class’s superclass o this lets you call an overridden method o Syntax: super.methodName() o ex: In Employee: public double getSalary() { return 50000.0; } o In Secretary: public double getSalary() { return super.getSalary() + 5000; } Now LegalSecretary’s getSalary() method will refer to Employee’s getSalary() method and add 5000 Inheritance and Fields:  A subclass can’t refer to its superclass’s private fields o Add a public accessor method in the superclass ex: method to access a private field years: public int getYears() { return years; } Now the subclass can call this method to access years Inheritance and Constructors:  Constructors of superclasses are not inherited ex: a constructor in Employee that includes the # of years they’ve worked at the firm public Employee(int initialYears) { years = initialYears; } o But this constructor won’t be extended to subclasses – you have to write a new constructor for subclasses that calls the superclass’s constructor with the super keyword public Lawer(int years) { //parameter type must be the same as the superclass’s super(years); } o the super call has to be the first statement in the constructor ***Only have to use the super keyword when accessing overridden methods or constructors o multiple levels of hierarchy can exist – super only refers to the 1 level up The Object Class:  Ultimate superclass of all other java classes  Contains some of its own methods like: equals; toString  You can write a method that accepts an Object as a parameter, or return type ex: public static void newMethod(Object o) { The parameter can be any kind of object o but since it’s called as an Object, you can only use methods on it that are part of the Object class  the equals method: Part of the Objects class o == doesn’t work when comparing objects  it actually compares references to objects  ex: if p1 = (2,5) and p2 = (2,5), p1 == p2 returns false because they refer to different objects o classes have a default equals method that behaves like == operation  so it won’t work how you want it to o write a new one to replace the default ex: a method to compare the states of 2 points: public boolean equals(Point p2) { return x == p2.x && y == p2.y; }  Object variables: o any object can be stored as a variable of type Object  Object o1 = new Point(2,5);  Object o2 = “hello”; o objects that are classified as type Object can only use methods from the Object class  ex: for Object o2, .toString() will work but .length() will not o A method to compare objects: public boolean equals(Object name) { name can be any type of object But if any object can be passed, how do we know we’re comparing 2 of the same type? o Say we want to compare 2 Point objects; we can typecast a generic Object to be a Point Updated method: public boolean equals(Object name) { Point other = (Point) name; //changes name from an Object to a Point return x = other.x && y = other.y; }


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Steve Martinelli UC Los Angeles

"There's no way I would have passed my Organic Chemistry class this semester without the notes and study guides I got from StudySoup."

Janice Dongeun University of Washington

"I used the money I made selling my notes & study guides to pay for spring break in Olympia, Washington...which was Sweet!"

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."


"Their 'Elite Notetakers' are making over $1,200/month in sales by creating high quality content that helps their classmates in a time of need."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.