1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

ITEC200 – Week03 Inheritance and Class Hierarchies.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Advanced Object-Oriented Programming Features
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
1 CSC/ECE 517 Fall 2010 Lec. 2 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Debugging 5.Testing.
03 Using Eclipse. 2 IDE Overview An IDE is an Interactive Development Environment Different IDEs meet different needs BlueJ and DrJava are designed as.
DT265-2 Object Oriented Software Development 2 Lecture 3 : Windows Programming Lecturer Pat Browne
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Neal Stublen Class Objectives  Develop an understanding of the.NET Framework  Gain proficiency using Visual Studio  Begin learning.
Writing Classes (Chapter 4)
1 v1.6 08/02/2006 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Refactoring 5.Debugging 6.Testing.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
TOOLS FOR DESIGN AND DEVELOPMENT ENVIRONMENTS. Case study - ECLIPSE Lecture notes 7.
CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.
Introduction to Eclipse CSC 216 Lecture 3 Ed Gehringer Using (with permission) slides developed by— Dwight Deugo Nesa Matic
Debugging. 2 © 2003, Espirity Inc. Module Road Map 1.Eclipse Debugging  Debug Perspective  Debug Session  Breakpoint  Debug Views  Breakpoint Types.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
EIE375 BlueJ: Getting Started Dr Lawrence Cheung.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937)
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Module 9. Dealing with Generalization Course: Refactoring.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java IDE Dwight Deugo Nesa Matic
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Module Road Map Refactoring Why Refactoring? Examples
University of Central Florida COP 3330 Object Oriented Programming
ATS Application Programming: Java Programming
Inheritance and Polymorphism
Eclipse Navigation & Usage.
University of Central Florida COP 3330 Object Oriented Programming
12 Data abstraction Packages and encapsulation
C# Programming: From Problem Analysis to Program Design
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Overview of Eclipse Lectures
Overview of Eclipse Lectures
Extending Classes.
Constructors, GUI’s(Using Swing) and ActionListner
Object-Oriented Programming
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Corresponds with Chapter 5
CSG2H3 Object Oriented Programming
Presentation transcript:

1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Debugging 5.Testing with JUnit 6.Version Control 7.Refactoring

2 CSC/ECE 517 Fall 2010 Lec. 3 Module Road Map 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Debugging 5.Testing with Junit 6.Version Control 7.Refactoring  Why Refactoring?  Examples  Common Refactorings

3 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » What is Refactoring? Refactoring is the process of changing a software system so that  the external behavior is not altered, but  the internal structure is improved. Refactoring ( is a “behavior-preserving transformation.”  Small changes per transformation   less likely to go wrong  System works after each change

4 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Organizing Java Code Eclipse comes with extensive support for organizing and refactoring Java code It is possible to:  Generate getters and setters for the fields  Organize missing import statements  Move fields, methods, classes  Rename methods, classes, packages

5 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Why Refactoring? Methods might no longer do (only) what their name suggests. Functionality that should be in two different classes might be in the same class. Functionality that should be in one class might be duplicated in two or more classes. Improve the design of existing code. Gain a better understanding of the code.

6 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Example Consider a method for computing the room charge for a hotel: public double getRoomCharge() { double roomCharge = 0.0;... code to compute room charge... return roomCharge; } What other factors might go into computing the room charge?

7 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Example Of course, to print out a bill for a customer, we also need to include incidentals and taxes … public double getRoomCharge() { double roomCharge = 0.0;... code to compute room charge... //now add the incidentals to roomCharge... code to add up incidentals... //now add the tax for the room to the charge...several lines of code to compute the tax... return roomCharge; } What’s inelegant about this method now?  3 sets of calculations in one function. Method does 3 things.  The name is not illustrative of what the method does.

8 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Example Better: Changing the name of the method (for example, calculateCustomerCharge). Does this fix the problem?  No, We also need to change the name at all call sites.  We need to update the documentation.  If this method overrides a method in another class, the other name may need to be changed too. Ditto if this method implements an interface. This is known as the Rename Method refactoring.

9 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Example Let’s refactor our getRoomCharge() method. public double calculateCustomerCharge() { double roomCharge = getRoomCharge(); double incidentals = getIncidentals(); double tax = getTax(roomCharge, incidentals); return roomCharge + incidentals + tax; } What have we done?  We defined additional methods to compute incidentals, tax, etc.  In order to do this, we added local variables for the quantities that are being calculated in the new methods.  Some pre-existing local variables ended up being parameters to the new method.  The returned value is different from what was returned in the pre- existing method.

10 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Common Refactorings Rename  Methods, Fields, Packages, Projects, Parameters, or Local Variables Encapsulate Field (generate getter and setter) Pull up a Field or Method (into superclass) Push down a Field or Method (into subclass) Extract Method, Local Variable, or Constant from an Expression Change Method Signature

11 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Renaming a Method Using Eclipse In a Java view showing methods (e.g., the Outline view) select the method to be renamed. From the view's pop-up menu, select Refactor » Rename, or select Refactor » Rename from the global menu bar or In a Java editor, select a reference to or the declaration of the method to be renamed. From the editor's pop-up menu, select Refactor » Rename, or select Refactor » Rename from the global menu bar. This pops up the Rename Method dialog box. Click Preview to preview the changes, or click OK to perform the refactoring.

12 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Encapsulating a Field The Encapsulate Field refactoring can be used to convert a public instance variable into a private instance variable with accessor functions. Example: Inelegant code— public PublicFieldClass{ public String name ; } public OtherClass{ public static void main(String[] args){ PublicFieldClass example = new PublicFieldClass(); example.name = "Joe"; System.out.println("My name is " + example.name); }

13 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Encapsulating a Field After refactoring, we have … public EncapsulatedFieldClass{ private String name; public String getName(){ return name; } public setName(String newName){ name = newName; } public OtherClass{ public static void main(String[] args){ EncapsulatedFieldClass example = new EncapsulatedFieldClass() example.setName("Joe") ; System.out.println("My name is " + example.getName()) ; }

14 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Encapsulating a Field Using Eclipse Select the field in one of the Java views (e.g., Outline, Package Explorer or Members view). From the field's pop-up menu, select Refactor » Encapsulate Field…, or from the menu bar, select Refactor » Encapsulate Field… Alternatively, in the Java editor, select the field. From the menu bar, select Refactor » Encapsulate Field…, or from the editor's pop-up menu, select Refactor » Encapsulate Field… This pops up the Encapsulate Field dialog. Type the names of the accessor routines in the Getter name and Setter name text fields. Click Preview to preview the changes or Click OK to perform refactoring.

15 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Pull Up Method Moves a field or method to a superclass of its declaring class. Suppose you have the same method—or nearly the same method—in two different classes in your system. It may be a good idea to centralize the behavior in a superclass. public class Employee extends Person { String getName() {... } public class Student extends Person { String getName() {... }

16 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Pull Up Method After the Pull up Method refactoring is applied … public class Person { String getName() {... }

17 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Pull Up Method Using Eclipse In a Java view (e.g., Outline, Package Explorer, Members), select the members that you want to pull up. From the menu bar, select Refactor » Pull Up or from the pop-up menu, select Refactor » Pull Up. This pops up the Pull up dialog. Select the methods to pull up and their new declaring class. Click Next. Select the methods to be removed in the subtypes after pull up and click Next to review the changes.

18 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Push Down Method Reverse of Pull up Method. Moves a set of methods and fields from a class to its subclasses. Can be used when some of the subclasses do not use a method defined in the superclass.

19 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Push Down Method Using Eclipse In a Java view (e.g., Outline, Package Explorer, Members), select the members that you want to push down. From the menu bar, select Refactor » Push Down or from the pop-up menu, select Refactor » Push Down. The Push Down dialog will open. Click Preview to preview the changes or click OK to perform the refactoring.

20 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Extracting a Local Variable An expression that occurs in more than one place is replaced with a local variable, whose value is calculated only once. If a program needs to use the same value in multiple places, it can be calculated only once and then used wherever needed. Advantages  Makes the code more efficient.  Makes the code more readable.  Creates a single point of maintenance for the logic of computing the expression.

21 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Extracting a Local Variable Using Eclipse In a Java editor, select the expression that you want to extract to a local variable. From the editor's pop-up menu, select Refactor » Extract Local Variable or from the menu bar, select Refactor » Extract Local Variable. This will open the Extract Local Variable dialog box. Type the name of the variable in the Variable name text field. Click Preview to preview the changes or click OK to perform the refactoring.

22 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Extracting a Method Creates a new method containing the statements or expression currently selected and replaces the selection with a reference to the new method. Advantages  Code readability  Minimize code duplication

23 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Extracting a Method Using Eclipse In an editor, select a set of statements or an expression from a method body. From the pop-up menu in the editor, select Refactor » Extract Method from the menu bar, select Refactor » Extract Method. This opens the Extract Method dialog box. Type the method name in the Method name text field. In the Access Modifier list, specify the method's visibility (public, default, protected, or private). Click Preview to preview the changes or click OK to perform the refactoring.

24 CSC/ECE 517 Fall 2010 Lec. 3 Refactoring » Change Method Signature Select the method in a Java view (e.g. Outline, Package Explorer, Members). From the menu bar, select Refactor » Change Method Signature or from the method's pop-up menu, select Refactor » Change Method Signature. This opens the Change Method Signature dialog box. Use the Access Modifier drop-down to control the method's visibility. Change the method's return type or name by editing the provided text fields. Select one or more parameters and use the Up and Down buttons to reorder the parameters (you can see a signature preview below the parameter list). Use the Add button to add a parameter; you can then edit its type, name and default value in the table. Switch to the Exceptions tab to add or remove thrown exceptions. Click Preview to preview the changes

25 CSC/ECE 517 Fall 2010 Lec. 3 Other Refactorings Supported by Eclipse Renaming  a package  a compilation unit  a type  a local variable  method parameters Extracting  a constant  an interface from a type Inlining  a local variable  a method  a constant  static members between types  an instance method to a component Converting  a local variable to a field  an anonymous inner class to a nested class  a nested type to a top level type Replacing  references to a type with references to one of its supertypes  a single reference to a type with a reference to one of its supertypes  an expression with a method parameter  constructor calls with factory method invocations

26 CSC/ECE 517 Fall 2010 Lec. 3 Exercise 3 Change the name of the class Debug that was created in Exercise 1 in the last lecture to RefactoredClass. Change the name of the method thirdMethod in the class RefactoredClass to refactoredMethod. Review the changes that will be made by Eclipse using the Preview option. Compile and run the project Debug.