



















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Detailed lecture notes covering topics such as java api, classes, software engineering, ethical issues in computer science, testing techniques, inheritance, design, gui, arraylist, vector, swing, jframe, japplet, coordinate system, graphics class methods, linear and binary search algorithms, and recursion for the cse 205 course.
Typology: Exams
1 / 59
This page cannot be seen from the preview
Don't miss anything!




















































CSE 205 – Lecture 2 – Software Engineering, Ethic, and Intro to Classes Outline o Java API o Introduction of Classes o Software Engineering o Social and Ethical Issues of Computer Science o Internet Citizenship API (Application Programmer Interfaces) o It contains the list of all pre-defined Java classes in the Java libraries o API contains packages, and each package contains classes Examples : java.lang package, java.util packing, etc. Java.lang package contains the String class, the System class, and so on o Notes Java.lang is always imported without an “import” statement. However to use a class in any other package, you need to import it It is usually best to import at the beginning of your code
import java.util.Scanner; Class Libraries o A class library is a collection of classes that we can use when developing programs o The Java standard class library is part of any Java development environment o Its classes are not part of the Java language per se, but we rely on them heavily o Various classes such as System, Scanner, String are part of the Java standard class library o Other class libraries can be obtained through third party vendors, or you can create them yourself Packages o The classes of the Java standard class library are organized into packages o Some of the packages in the standard class library are
Every time an object (instance) of a class is created, the object will have own data space A constructor is a special method that is used to set up a newly created object It has the same name as the class It does not return a value It has no return, type, not even void It often sets the initial values of instance variables The programmer does not have to define a constructor for a class. There is a default constructor for each class To instantiate an object of the class Customer (to allocate memory for the object) we can use the following statement: Customer customer1 = new Customer(); Note: you can have more than one definition of constructor by overloading Example public class Customer{ private int customerId; private double balance; public Customer{ //constructor{ customerId= 0; balance = 0.0; Encapsulation We can take one of two views of an object o Internal – the details of the variables and methods of the class that defines it o External – the services that an object provides and how the object intercts with the rest of the system
From the external view, an object is an encapsulated entity, providing a set of specific services An encapsulated object can be thought of as a black box – its inner workings are hidden from the client The client invokes the interface methods of the object, which manages the instance data
o Because instance data is private, a class usually provides services to access and modify data values o An accessor method returns the current value of a variable o A mutator method changes the value of a variable o The names of accessor and mutator methods take the form getX and setX, respectively, where X is the name of the value o They are sometimes called “getters” and “setters” Coin.java The isHeads method is an accessor The flip method is a mutator Instance Variables vs. Local Variables o Instance variables – variables declared at a class level. They are used by all methods in that class. o Local variables – variables declared in a method. They are used only in that method. The toString method o It is a method that takes no parameter and returns a String. o A returned string usually contains information on instance variables of its class. o Each class has a default toString method that contains its class object name and hash number. o When an object is used with System.out.print or println method, its toString method will be called. Method Overloading o The process of using the same method for multiple methods. o The signature -- the number, type, and order of the parameters o The signature of a method must be unique. If it is the same it will have issues reading the correct thing you want it to read o Example: We can have two definitions for the method “calc”:
“This” reference o The this reference refers to the instance variables of the object. o This approach eliminates the need to come up with a different yet equivalent name. Static Modifiers o it associates a variable or method with the class rather than an object. (there are static variables and static methods) it is invoked by the system without creating an object. o Static Methods – cannot reference instance variables. can reference static variables or local variables. o Static Variables – also called class variables o All objects created from the class share access to the static variable. o Changing the value of a static variable in one object changes it for all others. Data Scope o The scope of data is the area in a program in which that data can be used (referenced). o Data declared at the class level can be used by all methods in that class. o Data declared within a method can only be used in that method. o Data declared within a method is called local data. What is software engineering about? o An attempt to produce a repeatable process for the development and management of software projects. The quality of the software is a direct result of the process we follow to create it Software Development Models o A Software Development model is an organized approach to create quality software Testing Techniques o Generally, the goal of testing is to find errors o Often it is called defect testing
The Ten Commandments of Computer Ethics have been a highly effective code of ethics for the proper use of information technology . o 1. Thou shalt not use a computer to harm other people. o 2. Thou shalt not interfere with other people's computer work. o 3. Thou shalt not snoop around in other people's files. o 4. Thou shalt not use a computer to steal. o 5. Thou shalt not use a computer to bear false witness. o 6. Thou shalt not use or copy software for which you have not paid. o 7. Thou shalt not use other people's computer resources without authorization. o 8. Thou shalt not appropriate other people's intellectual output. o 9. Thou shalt think about the social consequences of the program you write. o 10. Thou shalt use a computer in ways that show consideration and respect.
CSE 205 – Lecture 4 – UML, Scanner, Split, Arrays UML Diagrams o UML stands for the Unified Modeling Language o UML diagrams show relationships among classes and objects o A UML class diagram consists of one or more classes, each with sections for the class name, attributes (data), and operations (methods) o Lines between classes represent associations o A solid arrow shows that one class uses the other (calls its methods) Class Diagrams o A class diagram consists of one or more classes, each with sections for the class name, Attributes (instance variables in Java), and methods, and describes relationship between classes. The String Class o A string is an object of the String class defined in java.lang package o Because strings are used so often, we don't have to use the new operator to create a String object String title = new String(" Java Software Solutions ") or title = "Java Software Solutions"; o This is special syntax that works only for strings o Each string literal (enclosed in double quotes) represents a String object o Methods from String class Once a String object has been created, neither its value nor its length can be changed Thus we say that an object of the String class is immutable However, several methods of the String class return new String objects that are modified versions of the original o Index of Character in a String It is occasionally helpful to refer to a particular character within a string This can be done by specifying the character's numeric index
o The values are delimited by braces and separated by commas Arrays as Parameters o An entire array can be passed as a parameter to a method o Like any other object, the reference to the array is passed, making the formal and actual parameters aliases of each other o Therefore, changing an array element within the method changes the original o An individual array element can be passed to a method as well, in which case the type of the formal parameter is the same as the element type Arrays of Objects o The elements of an array can be object references o The following declaration reserves space to store 5 references to String objects - String[] words = new String[5]; o It does NOT create the String objects themselves o Initially an array of objects holds null references o Each object stored in an array must be instantiated separately Arrays of Objects o The following declaration creates an array object called verbs and fills it with four String objects created using string literals Command-Line Arguments o The signature of the main method indicates that it takes an array of String objects as a parameter o These values come from command-line arguments that are provided when the interpreter is invoked Two Dimensional Arrays o A one-dimensional array stores a list of elements o A two-dimensional array can be thought of as a table of elements, with rows and columns
CSE 205 - Lecture #5 - Inheritance Inheritance: New classes can be created using inheritance o Basically, reusing the software, it is a way to support the reuse of a software o Example: Take three classes “Cat”, “Dog”, and “Raccoon” and each class needs integers “legs” and “tail” To make it simpler, we create an “Animal” class, and put the instance variables “legs” and “tails” into the “Animal class” so that the “Cat”, “Dog”, and “Raccoon” classes can inherit the integers from the animal class o Visibility modifiers (types of variables that can or cannot be accessed in certain areas) Public - inherited, can be accessed from anywhere (ex. public int legs) Private - not inherited, can be accessed only within its class (we use - for UML diagrams)(ex. private int legs) Protected - inherited, can be accessed by any class in the same package (we use # for UML class diagrams) (ex. protected int legs) Allows a child class to reference a variable or method directly in the child class (cat, dog, raccoon) from the parent class (Animal) Provides more encapsulation than public visibility o Encapsulation - providing a public interface while hiding implementation details (from book) “Constructors are not inherited from parent class* A child’s constructor is responsible for calling its parent’s constructor o This is referred to as “super” reference - child class calling the constructor from the parent class
If a child of an abstract class does not give a definition for every abstract method that it inherits from its parent, the child class is also considered abstract o Class Hierarchies - a child class of one parent can be the parent of another child, forming a class hierarchy There can be multiple levels of the hierarchy, i.e. more than two o Type conversion: Widening conversion - assigning a predecessor object to an ancestor reference In class work (a worksheet was handed out, one of the examples is very good to reference to in terms of how inheritance works): o Example of Inheritance code public class AClass { protected int x; protected int y; public AClass (int a, int b) { x = a; y = b; } public int addEm() { return x + y; } public void changeEm() { x++; y++; } public String toString() { return “ “ + x + “ “ + y;
The following is the BClass in which will have a third instance variable, and will demonstrate how to override and inherit from AClass above: public class BClass extends AClass { private int z; public BClass (int a, int b, int c) { super(a, b) z = c; } public int addEm() { return super.addEm() + z; } Public String toString() { return super.toString() + “ “ + z; } }
o It is best to find common characteristics of classes and group them higher in the class hierarchy to be used for multiple situations For example a child class truck and a child class car can have characteristics like tire and color in the parental class o Override methods as needed to change the function of the child class o Add new variables to children, but don’t redefine (shadow or someone copy) inherited variables o Allow each class to manage its own data; use the “super” reference to invoke the parent’s constructor to set up its data The super reference will maintain clear reading in the program as well o Even if there are no current uses for them, override general methods such as “toString” and “equals” with appropriate definitions o Use abstract classes to represent general concepts that child classes have in common o Use visibility modifiers carefully to provide needed access without encapsulation Encapsulation means combining fields together Restricting Inheritance o The “final” modifier can be used to restrict inheritance o If the “final” modifier is applied to a method, then that method cannot be overridden in any descendent classes o If the “final” modifier is applied to an entire class, then that class cannot be used to derive any child classes at all This means an abstract class cannot be declared as “final” Example of modifier that cannot be changed: “final int number” o All of these above are also key design decisions in inheritance design Interfaces o An interface describes a set of methods: No constructors
No instance variables o The interface must be implemented by some class Many java classes implement one or more interfaces Example no methods are implemented: public interface Animal{ public String sound (); } o to implement an interface, you must have all method specified exactly written in the interface o interfaces are used by Java in several ways to design GUI (graphical user interface) programs you must implement interfaces to make it work o They can also be used to specify class design They list all the methods that you must implement You add instance variables and constructors Polymorphism o A polymorphic reference is a reference variable that can refer to different types of objects at different points in time o Polymorphic references are resolved at run-time, not during compilation Example: animalList [0] = new Animal(); animalList[1] = new Mammal(); animalList[2] = new Dog(); for (int I = 0; I <= 2; i++){ animalList [i].move(); - } In this case, animalList[0].move refers to move() of Animal Class animalList[1].move refers to move() of Mammal Class This part is polymorphic