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

Exam 1 Review

by: Taylor Kahl

Exam 1 Review 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

Comprehensive review of all lecture material so far
Princliples of Computer Programming
Kebina Manandhar
Study Guide
50 ?




Popular in Princliples of Computer Programming

Popular in ComputerScienence

This 8 page Study Guide was uploaded by Taylor Kahl on Thursday February 11, 2016. The Study Guide belongs to CSC 2310 at Georgia State University taught by Kebina Manandhar in Winter 2016. Since its upload, it has received 110 views. For similar materials see Princliples of Computer Programming in ComputerScienence at Georgia State University.

Similar to CSC 2310 at GSU


Reviews for Exam 1 Review


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: 02/11/16
Exam 1 Review From Chapter 8, Classes:  Class – a program or template for new types of objects  Object – an entity of a class that has fields and methods that belong to the object o Fields – values stored in an object o Methods – actions an object can perform  Classes aren’t complete programs – have to be used with client code o Client code is where you write the public static void main(String[] args) { method o create new objects/call methods from the client code My Point class will be in blue; the main method/client code will be in green  Creating a new class called Point to create point objects: (x-coordinate, y- coordinate) public class Point { private int x;//field private int y;//field public void move(int dx, int dy) { //a method to change the x/y fields x = x + dx; y = y + dy; } }  Now every new Point object will have its own x field, y field, and move() method  The fields are declared private to protect encapsulation o Encapsulation hides an object’s details from outside classes o Another word you might need to know: Encapsulation forces Abstraction – separating a class’s external behavior from its internal state to protect its data ***private fields are only visible inside their own class!  Notice that the move method does NOT contain the keyword static o fields and methods can be static or non-static o static – part of a class, not an object; each object does NOT have its own copy o All objects in the class can access or modify static fields and methods  a static field: private static int i;  Calling a static method: ClassName.methodName(); Writing client code to work with the point class: public class PonitMain { public static void main(String[] args) { Point p1 = new Point(); //create a new Point object named p1 o Objects’ fields are initialized to zero or null. p1 = (0,0) Auto-initializing new objects with constructors: o Constructor – a method that lets you assign values to an object’s fields as you create it  has no return type  called whenever a new Point object is made public Point(int initialX, int initialY) { x = initialX; y = initialY; } ***A common mistake is re-declaring x and y as “int x” and “int y” inside the constructor o Now when you create a new Point you have to pass 2 int parameters Point p2 = new Point(7,2); //p2 = (7,2) o A class can have multiple constructors for multiple ways to construct new objects  As long as each constructor takes a different # or type of parameter public Point() { //constructor 2 x = 0; y = 0; } Point p3 = new Point(5,-2); //(5,-2) Point p4 = new Point(); //(0,0) Calling a non-static method from your class in the main method: p1.move(3,8); //calls the move method on p1 o the general syntax is objectName.methodName(parameters); o the object is referred to as the implicit parameter – a method can refer to its fields  Shadowing – when 2 variables have the same name o normally illegal – allowed when 1 variable is a field in the class o this keyword lets you refer to the implicit parameter’s (the object’s) methods and fields  useful if you want to give 2 variables the same name public Point(int x, int y) { //another version of constructor 1 this.x = x; this.y = y; } x and y refer to the local variables in the constructor this.x and this.y refer to the object’s x and y fields  How to access private fields from outside the class: o Write accessor/mutator methods public int getX() { return x; } public int setX(int newX) { x=newX; } p1.getX(); //see the value of the x field p1.setX(5); //change x to 5  toString method: o classes have a default toString method to let you print output o But trying to print a Point object gives strange output – write your own toString() method public String toString() { return “(“ + x + “,” + y + “)”; //returns (x,y) } System.out.print(p3); //prints (5,-2) From Chapter 9, Inheritance and Interfaces:  Inheritance – allows one class to inherit the states and behaviors of another o useful if one class is a version of another – you can reuse code I’ll use the law firm analogy: everyone who works there is an employee with certain rules and behavior. But there are categories of employees such as lawyers and secretaries with their own rules and behavior  Lawyer and Secretary can inherit the Employee class  Superclass – parent class (original)  Subclass – inherits the superclass o A subclass may have only 1 superclass o But 1 superclass may be extended to many subclasses I’ll use blue for the subclass Lawyer and green for the superclass Employee public class Employee { private int years; //# of years at company public double getSalary() { return 40000.0; //general employees make $40000 }  Syntax for a Lawyer class which inherits Employee, using extend keyword: public class Lawyer extends Employee { o Lawyer inherits Employee’s method/s  Overriding methods: o Overriding – writing a new method to replace an inherited one  use the same header as the inherited method o super keyword refers to a class’s superclass  use it to call a superclass’s methods, even if they’re overridden  syntax: super.methodName() public double getSalary() { return super.getSalary() +5000; //takes the Employee method and adds 5000 } o this overrides the getSalary() method from Employee Inheritance and fields: o A subclass can’t refer to its superclass’s private fields o So add a public accessor method in the superclass (see ch. 8) Inheritance and constructors: o Subclasses don’t inherit constructors public Employee(int years) { this.years = years; } o If the superclass has a constructor, you have to write one in the subclass that takes the same parameters o In the subclass, call the superclass’s constructor using super:  let’s say that the Lawyer class also has a field for their college  we can add this field to the constructor too private String college; public Lawyer(int years, String college) { super(years); //calls Employee’s constructor = college; } o the super call has to be the first statement in the constructor The Object Class: o ultimate superclass of all other java classes; has methods like equals and toString o you can write a method that returns an Object or accepts it as a parameter o Object variables:  any object can be stored as a variable of type Object Object o1 = new Point(2,5); Object o2 = “hello”;  but they can only use methods from the Object class o The equals method (part of the objects class):  classes have a default equals method  but it compares references to objects, not the objects themselves if p1 = (2,5) and p2 = (2,5), p1.equals(p2) will return false because p1 is not the same variable as p2  you can write a new equals method to override the default o instanceof operator tests whether a variable refers to an object of a given type  String s = “carrot”;  s instanceof String – true  s instanceof Point – false  “hello” instanceof String – true Polymorphism – allows same code to be used with different types of objects o code will behave differently for different objects  An array of a superclass type can store any subtype as elements o Employee[] list = {new Lawyer(), new Secretary()};  A method accepting an object as a parameter can accept any subclass of that object  A variable of an object type can store any object of its subclasses o Employee frank = new Secretary(); o When we call methods on frank, it will be treated as a secretary  But the method has to exist in the Employee class!  Variables can be typecast as compatible objects Interfaces – a supertype that relates classes without code sharing  A list of abstract methods – have headers, but no body statements o A class that implements an interface must implement ALL of its methods and fill in the statements  Can’t create an object of interface type  Syntax: public interface name { public returnType methodName(); }  Implementing an interface: public class name implements interface { public returnType methodName() { statements; } }  Using polymorphism: o any class that implements an interface can be treated as the interface type From Chapter 10, ArrayLists:  ArrayLists are more flexible than arrays; you can add or remove elements  Stores a list of elements of a specified type, such as a list of Strings  Part of the java.util package – have to import it to use ArrayLists  Syntax for constructing a new ArrayList: ArrayList<type> name = new ArrayList<type>();  Adding elements to name: o name.add(value); adds value to the end of the array  Printing – ArrayLists already have a toString method so printing is simple; just use the variable name  Element indexes: o AstayLists use zero-based indexing, like String and Arrays o 1 element has index 0 o last element has index length – 1  Adding elements at specific indexes: o name.add(index, value);  Removing elements at specific indexes: o name.remove(index); ***When adding and removing elements, the ArrayList will change size and automatically shift the other elements as needed  Other ArrayList methods: clear() removes all elements get(index) gets value at that index size() returns current # of elements set(index, value) replaces element at index with the new value contains(value) returns true or false, whether the ArrayList contains that value indexOf(value) returns the index of the value’s first occurrence; -1 if it doesn’t find it  an ArrayList can be used as a parameter or a return type  An ArrayList cannot be of a primitive type; must be an object o can use primitive types using wrapper classes Primitive type Wrapper type int Integer double Double char Character boolean Boolean o so to make an ArrayList of a primitive type, you have to use its wrapper class  ArrayList<Integer> list = new ArrayList<Integer>();


Buy Material

Are you sure you want to buy this material for

50 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!"

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."


"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.