CS 2110 Obj. Oriented programming in Java PDF

Title CS 2110 Obj. Oriented programming in Java
Author Stephen Newcomb
Course Object-Oriented Programming And Data Structures
Institution Cornell University
Pages 8
File Size 89.2 KB
File Type PDF
Total Downloads 59
Total Views 159

Summary

Taken with David Gries...


Description

1/25/2018 Day 1 Intro ● ● ●

For specifications begin with /** and close with */ Comments begin /* and end */ Max integer in java is 2^31 and -2^31 ○ Wraps around once you get to max ■ I.e 2^31 +1 = -2^31 Primitive Types in Java ● Int: values ○ Operations +,-,= ● Double: decimals (floats in pythons) ● Char: code for stuff like @,$,# ● Boolean: true, false, not, etc Java is a strongly typed  language ● More disciplined ● Also more punishing ● Must declare the type of all variables ○ Ex: int x; ○ x = 10; ○ Or int x =10; ● Must have semicolon at the end of each line ● Casting is changing the type of a variable ● Can be automatically cast as it gets wider (but not narrower) ○ I.e will automatically cast a double to an int but not vice versa ● Can cast char ○ Ex (int) ‘V’ ■ 86 ○ (char) 86 ■ ‘V’ 1/30/18 ● ●

● ●

To create a new object of a class type ○ variable = new class() When a variable is set to an object it does not contain the object but rather a pointer to the object ○ J = new javax.swing.jframe() javax . swing . jframe package package class ■ Creates a window on screen ○ Call methods by using j followed by a period

● ●



● ●

■ I.e j.show Public is an access modifier, so anyone who has access has access to the class To create a subclass ○ public class C extends javax.swing.Jframe() ○ C inherits all methods from Jframe Writing methods ○ Under class write ■ public int method() Functions return something, procedures do not When writing a procedure write ○ public void because it has no return variable type

● ●

Null is the absence of a name or pointer Static means that you are only creating one copy of an element that is not stored in an object (global space?) ● { } (braces) act like indentation in python. Use to denote where classes, methods and statements begin and end 2/1/2018 ● Fields are class variables, can be public or private ● Fields are generally private and methods are generally public ● Getter and setter methods ○ Function similarly to python ○ Allows you to change and retrieve certain attributes without allowing the user to have total access ● 2/6/2018 ● ● ● ●





All classes,unless specifically extending another class, are subclasses of class object To use a class defined in another package must import the package Define a subclass by using extends ○ I.e apartment extends House Subclasses are different classes ○ Private fields and methods cannot be accessed outside of the class ■ Similar to _functions in python ○ Public fields and methods cannot be accessed ○ Protected fields can be accessed by subclasses This: points to the name of the current object ○ i.e this.floor = floor within the current object ○ Referencing a shadowed class field ○ Similar to self in Python Overriding Methods ○ Begin with @Override before function ( not required)



When you define a function of the same name as a parent class, when using the class it will go to the local method first ● When should you make a subclass? ○ A should extend B only if A “is a” B ■ An elephant is an animal so elephant extends animal ● Static Methods ○ Most methods are instance methods: every instance of a class has a copy of the method ○ Static Methods have only one copy of a method, does not have copies in each instance. ■ Only one overall method and not one in each object ○ Also useful in singleton pattern ■ Only one instance of something can exist ● Errors ○ Can either use assert statements ○ Or can throw an error ■ Ex. if(n x is on the right ○ Partition ○ Swap elements until you reach this ○ Partition and the recursively call QS ○ How long does it take? ■ nlog(n) to O(n2 ) ○ Takes O(log(n)) Space ○ Not stable Trees….

3/15/18

GUIs ●



Three Main Packages ○ AWT - abstract or awful windows toolkit, old ○ Swing - newer one building upon awt ○ JavaFX - brand new, complex JFrames ○ Has subclasses JLabel and Jbutton etc that can be connected to methods ○ Can place items in North,South,West,East Center



Ex  add(new JLabel("north"), BorderLayout.NORTH); 3/20/18

Listening for Events ●





Anonymous Functions ○ Easier way to write certain functions ○ Ex. Person p[] = New Person[10]; Code to create 10 people /* sort p by age Arrays.sort(p, (Person b, Person c) -> b.age – c.age); Can add a listener to a GUI interface public ButtonDemo1(String t) { super(t); add(westB, BLayout.WEST); add(eastB, BLayout, EAST); westB.setEnabled(false); eastB.setEnabled(true); eastB.addActionListener( e -> {boolean b= eastB.isEnabled(); eastB.setEnabled(!b); westB.setEnabled(b);} ● Can also create a listener stored in a local variable ○ Action listener x = … Can use input adapter to create a new method that returns the source of the event...


Similar Free PDFs